RaphaelJS似乎缺乏形态等级

时间:2011-05-22 18:36:30

标签: javascript svg raphael

拉斐尔似乎缺乏形态等级?

我无法创建一个较小的圆圈“附加”到一个较大的圆圈,并且知道它会在较大的圆圈上缩放/翻译。

类似地,如果我将元素放入集合中,则拖动处理程序将单独连接到形状,并且在其回调中我没有处理集合。

我忽略了什么吗?

2 个答案:

答案 0 :(得分:2)

Raphael的当前行为不会为“集合”创建任何真实元素。

答案 1 :(得分:1)

如果要在集合上启用Drag'nDrop,可以使用以下代码:

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);