开发者

RaphaelJS seems to lack shape hierarchies

Raphael seems to lack shape hierarchies?

I can't create a smaller circle "attached" to a larger circle, and know that it will be scaled/translated when the larger one is.开发者_如何学Python

Similarly, if i put elements into a set, the drag handler connects to shapes individually and in its callbacks i have no handle on the set.

Am i overlooking something?


This the current behaviour as Raphael does not create any real element for a "set".


If you want to enable Drag'nDrop on a set, you can use the following code :

Raphael.el.set_drag = function (aSet) {
    // Enable drag'n drop in a Set
    var startAll = function () {
        // storing original coordinates
        for (var i in this.set.items) {
            var comp = this.set.items[i];
            try {
                comp.attr({opacity: 0.3});
            } catch (ex) {;}
            if (comp.type == "path") {
                comp.ox = comp.getBBox().x;
                comp.oy = comp.getBBox().y;
            }
            else {
                comp.ox = comp.attrs.cx || comp.attrs.x;
                comp.oy = comp.attrs.cy || comp.attrs.y;
            }
        }
    },

    moveAll = function (dx, dy) {
        for (var i in this.set.items) {
            var comp = this.set.items[i];
            if (comp.attrs.cx)             // ellipse
                comp.attr({cx: comp.ox + dx, cy: comp.oy + dy});
            else if (comp.attrs.x)
                comp.attr({x: comp.ox + dx, y: comp.oy + dy});
            else            // path
                comp.translate(comp.ox - comp.getBBox().x + dx, comp.oy - comp.getBBox().y + dy);
        }
    },

    upAll = function () {
        for (var i in this.set.items) {
            var comp = this.set.items[i];
            if (comp.attrs.cx)             // ellipse
                comp.attr({cx: comp.ox, cy: comp.oy + dy});
            else if (comp.attrs.x)
                comp.attr({x: comp.ox, y: comp.oy + dy});
            else            // path
                comp.translate(comp.ox , comp.oy - comp.getBBox().y + dy);
            this.set.items[i].attr({opacity: 1});
        }
    };

    this.set = aSet; //create a "set" property on the element
    this.drag(moveAll,startAll,upAll);
    return this;    
}


// Create your elements
var first_element = paper.rect(10,10,100,50).attr({fill:'#f00'});
var second_element = paper.rect(30,300,100,50).attr({fill:'#0f0'});

// Add elements to your set
var myset = paper.set();
myset.push(first_element);
myset.push(second_element);

// Add handler on the elements
first_element.set_drag(myset);
second_element.set_drag(book_set);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜