Raphael JS - 维护两个对象之间的路径

时间:2012-03-31 13:13:55

标签: javascript animation path raphael

我为Raphael JS创建了一个插件。它基本上允许你打电话

paper.connect(OBJ1,OBJ2,颜色)

在两个对象之间绘制一条线,并在对象设置动画时保持线条。 这是我到目前为止所提出的。它有效,但它不是很高效,任何关于我还能做些什么来达到同样的事情的建议。

Raphael.fn.connect = function(obj1, obj2, colour) {
    // list of paths each object has
    obj1.connections = []
    obj2.connections = []
    // get the bounding box of each object
    var box1 = obj1.getBBox()
    var box2 = obj2.getBBox()
    // create a line/path from object 1 to object 2
    var p = this.path("M" + (box1.x + box1.width / 2) + ","
            + (box1.y + box1.height / 2) + "L" + (box2.x + box2.width / 2)
            + "," + (box2.y + box2.height / 2))
    // adjust attributes of the path
    p.attr({
        stroke : colour,
        "stroke-linecap" : "round",
        "stroke-opacity" : Math.max(obj1.attr('opacity'), obj2.attr('opacity'))
    })
    // set the start and end element for this path
    p.startElement = obj1;
    p.endElement = obj2;
    // add the path to each of the object
    obj1.connections.push(p)
    obj2.connections.push(p)
    // mark each object as being connected
    obj1.connected = true;
    obj2.connected = true;
    // listen for the Raphael frame event
    eve.on("raphael.anim.frame.*", function(obj) {
        // if the object the frame event is fired on is connected
        if (this.connected) {
            // for each connection on this object
            for ( var c in this.connections) {
                var path = this.connections[c]; // temp path
                var b1 = path.startElement.getBBox(); // get the current
                                                        // location of start
                                                        // element
                var b2 = path.endElement.getBBox();// get the current location
                                                    // of end element
                // move the path to the new locations
                path.attr({
                    path : "M " + (b1.x + b1.width / 2) + " "
                            + (b1.y + b1.height / 2) + "L "
                            + (b2.x + b2.width / 2) + " "
                            + (b2.y + b2.height / 2),
                    opacity : Math.max(path.startElement.attr('opacity'),
                            path.endElement.attr('opacity'))
                });
            }
        }
    });
}

不相信这是最好的方式,但这是我第一次使用Raphael所以我只是通过查看Raphael源代码做了所有这些...

1 个答案:

答案 0 :(得分:1)

在我们的应用中,我们有一个线条工具。我们在纸上放了一条带有2个可移动终点的线。

我们的应用程序中的所有形状都有一个关联的VisualModel,其中包含其中的所有几何数据。这些VisualModels也兼作Actors。任何演员都可以订阅任何其他演员,当发生变化时,所有感兴趣的人都会回复。

这样的系统允许线路的路径通过重绘功能进行更改,只要2个连接的对象修改其X / Y坐标,就会调用该功能。

connecting_line.js(重绘)

redraw: function() {
    var x1 = this.shapeView1.visualModel.get('x'),
        y1 = this.shapeView1.visualModel.get('y'),
        x2 = this.shapeView2.visualModel.get('x'),
        y2 = this.shapeView2.visualModel.get('y'),
        pathData;

    pathData = 'M' + x1 + ',' + y1 + 'L' + x2 + ',' + y2;

    this.line.attr({
        path: pathData,
        fill: '#000000',
        stroke: LineConstants.COLOR,
        'stroke-width': LineConstants.THICKNESS
    });
}

我们创造了"可移动"混入。这个mixin允许您为形状添加可移动性。这个mixin将更新x / y坐标,然后触发“更改”。你的线路课程会选择的事件。

movable.js

handleDraggging: function(delta) {
    this.shape.move(delta);
}

move: function(delta) {
    //... compute movement based on delta
    this.visualModel.set('x', xPosition);
    this.visualModel.set('y', yPosition);
}

connecting_line.js

initialize: function(shapeView1, shapeView2) {
    // ...
    this.shapeView1 = shapeView1;
    this.shapeView2 = shapeView2;

    this.listenTo(shapeView1.visualModel, 'change:x change:y', this.redraw);
    this.listenTo(shapeView2.visualModel, 'change:x change:y', this.redraw);
}

表现非常好。您可以通过访问eventbrite.com,创建活动,启用预留座位(步骤2),添加新地图,点击'对象'来查看它的实际效果。在左边,在纸上画一条线。