【发布时间】:2013-05-15 02:24:06
【问题描述】:
我正在制作一个小动画,其中用户拖动一个圆圈,圆圈返回到起点。我想出了一种让圆圈返回起点的方法。唯一的问题是它会在返回之前撞到框架的一侧。是否可以直接返回(沿着在形状和起点之间绘制的线的路径)。
另一个问题是我的 setInterval 不想停止。如果您再次尝试拉动它,它会在您释放鼠标之前将其拉回。每次之后它似乎也加快了速度。我曾尝试使用带有计时器的 while 循环,但结果并不理想。这是可以修复的吗?
var paper = Raphael(0, 0, 320, 200);
//var path = paper.path("M10 10L40 40").attr({stoke:'#000000'});
//var pathArray = path.attr("path");
var circle = paper.circle(50, 50, 20);
var newX;
var newY;
circle.attr("fill", "#f00");
circle.attr("stroke", "#fff");
var start = function () {
this.attr({cx: 50, cy: 50});
this.cx = this.attr("cx"),
this.cy = this.attr("cy");
},
move = function (dx, dy) {
var X = this.cx + dx,
Y = this.cy + dy;
this.attr({cx: X, cy: Y});
},
up = function () {
setInterval(function () {
if(circle.attr('cx') > 50){
circle.attr({cx : (circle.attr('cx') - 1)});
} else if (circle.attr('cx') < 50){
circle.attr({cx : (circle.attr('cx') + 1)});
}
if(circle.attr('cy') > 50){
circle.attr({cy : (circle.attr('cy') - 1)});
} else if (circle.attr('cy') < 50){
circle.attr({cy : (circle.attr('cy') + 1)});
}
path.attr({path: pathArray});
},2);
};
circle.drag(移动,开始,向上);
这是 Jfiddle:http://jsfiddle.net/Uznp2/
非常感谢:D
【问题讨论】:
-
使用 setInterval 为某些东西设置动画是一种糟糕的方法。在 Raphael 中,我们可以利用 Element.animate() 方法,还有很多额外的优势。
标签: javascript loops animation raphael transform