Snap.svg animation stop() 不会停止动画
Snap.svg animation stop() doesn't stop animation
我正在尝试沿着路径制作简单的动画,该路径在元素到达窗口边界时停止。下面是一些示例代码:
var s = Snap("#bouncy");
var ball = s.circle(100, 100, 10);
var mypath = getLinearPath({x: 300, y: 300}, {x: 300, y: -1000});
var windowBBox = s.getBBox();
function animateAlongPath(path, element, start, dur) {
var len = Snap.path.getTotalLength(path);
console.log(element);
ball.current_anim = Snap.animate(start, len, function (value) {
var movePoint = Snap.path.getPointAtLength(path, value);
var ballBounds = element.node.getBoundingClientRect();
if (Snap.path.isPointInsideBBox(windowBBox, movePoint.x, movePoint.y)) {
console.log("moving to ", movePoint);
var t = new Snap.Matrix();
t.translate(movePoint.x, movePoint.y);
element.transform(t);
} else {
console.log("stopping");
this.stop();
element.stop();
ball.current_anim.stop();
}
}, dur);
};
function getLinearPath(start, end) {
var p;
p = s.path("M " + start.x + " " + start.y + " L " + end.x + " " + end.y);
return p
};
animateAlongPath(mypath, ball, 0, 1000);
我只是想在球到达窗框顶部时停止动画。但是,当调用 stop() 时(在动画句柄或元素上),我继续收到回调,直到浏览器冻结。
如何取消动画并防止将来回调?
我阅读了Snap.svg的源代码,我发现anim.stop调用自己的回调函数。因此,在动画回调时调用 stop 方法会进行无限循环。
为了解决这个问题,你可以定义临时变量来阻止这样的无限循环。
var s = Snap("#bouncy");
var ball = s.circle(100, 100, 10);
var mypath = getLinearPath({x: 300, y: 300}, {x: 300, y: -1000});
var windowBBox = s.getBBox();
function animateAlongPath(path, element, start, dur) {
var len = Snap.path.getTotalLength(path);
console.log(element);
ball.current_anim = Snap.animate(start, len, function (value) {
var movePoint = Snap.path.getPointAtLength(path, value);
var ballBounds = element.node.getBoundingClientRect();
if (Snap.path.isPointInsideBBox(windowBBox, movePoint.x, movePoint.y)) {
console.log("moving to ", movePoint);
var t = new Snap.Matrix();
t.translate(movePoint.x, movePoint.y);
element.transform(t);
} else {
console.log("stopping");
//this.stop();
//element.stop();
//NOTE: ball.current_anim.stop calls ball.current_anim
var anim = ball.current_anim;
delete ball.current_anim;
if(anim){anim.stop();}
}
}, dur);
};
function getLinearPath(start, end) {
var p;
p = s.path("M " + start.x + " " + start.y + " L " + end.x + " " + end.y);
return p
};
animateAlongPath(mypath, ball, 0, 1000);
相关文章:
- 在 HTML 页面中嵌入动画 SVG,暂停(不启动动画)
- 按顺序显示动画 SVG 元素
- 范围设置后启动svg动画-svg
- 如何通过javascript添加动画svg
- 如何从线的末端绘制动画(SVG)
- Javascript-变形/动画svg路径数据没有SMIL或库
- 动画SVG视图框更改
- 动画SVG Path元素的背景图像
- GSAP:在动画SVG时保留以前的转换
- 在视口中动画SVG元素
- 在IE11中使用CSS动画SVG路径
- 动画SVG背景图案
- 沿着SVG路径动画SVG渐变
- jQuery数据表和动画SVG处理时
- 使用js/jQuery动画SVG路径
- 动画SVG属性,如填充颜色没有第三方库
- 在if-else条件下动画SVG
- 使用jQuery动画SVG的颜色和比例悬停
- 悬停时捕捉SVG动画SVG/离开时重置SVG
- 动画SVG从一个值到另一个值再到原始值