zco*_*rts 5 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'))
});
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
不相信这是最好的方式,但这是我第一次使用Raphael所以我只是通过查看Raphael源代码做了所有这些...
在我们的应用程序中,我们有一个线条工具。我们在纸上画一条有 2 个可移动端点的线。
我们应用程序中的所有形状都有一个关联的 VisualModel,其中包含其中的所有几何数据。这些 VisualModel 还兼作 Actor。任何 Actor 都可以订阅任何其他 Actor,并且当发生更改时,所有感兴趣的各方都会做出响应。
这样的系统允许通过重绘函数来更改线条的路径,只要两个连接的对象修改其 X/Y 坐标,就会调用该重绘函数。
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
});
}
Run Code Online (Sandbox Code Playgroud)
我们创建了“可移动”mixin。这个 mixin 可以让你为你的形状添加可移动性。此 mixin 将更新 x/y 坐标,进而触发您的线路类将拾取的“更改”事件。
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);
}
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
这方面的表现非常棒。您可以访问 eventbrite.com 查看实际情况,创建活动,启用预留座位(第 2 步),添加新地图,单击左侧的“对象”并在纸上画一条线。
| 归档时间: |
|
| 查看次数: |
3517 次 |
| 最近记录: |