如何停止requestAnimationFrame方法启动的动画 如何停止一个requestAnimationFrame递归/...

\u5982\u4f55\u505c\u6b62requestAnimationFrame\u65b9\u6cd5\u542f\u52a8\u7684\u52a8\u753b

\u3000\u3000HTML5/CSS3\u65f6\u4ee3\uff0c\u6211\u4eec\u8981\u5728web\u91cc\u505a\u52a8\u753b\u9009\u62e9\u5176\u5b9e\u5df2\u7ecf\u5f88\u591a\u4e86:
(1) \u4f60\u53ef\u4ee5\u7528CSS3\u7684animattion+keyframes;
(2) \u4f60\u4e5f\u53ef\u4ee5\u7528css3\u7684transition;

(3) \u4f60\u8fd8\u53ef\u4ee5\u7528\u901a\u8fc7\u5728canvas\u4e0a\u4f5c\u56fe\u6765\u5b9e\u73b0\u52a8\u753b\uff0c\u4e5f\u53ef\u4ee5\u501f\u52a9jQuery\u52a8\u753b\u76f8\u5173\u7684API\u65b9\u4fbf\u5730\u5b9e\u73b0;

(4) \u5f53\u7136\u6700\u539f\u59cb\u7684\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528window.setTimout()\u6216\u8005window.setInterval()\u901a\u8fc7\u4e0d\u65ad\u66f4\u65b0\u5143\u7d20\u7684\u72b6\u6001\u4f4d\u7f6e\u7b49\u6765\u5b9e\u73b0\u52a8\u753b\uff0c\u524d\u63d0\u662f\u753b\u9762\u7684\u66f4\u65b0\u9891\u7387\u8981\u8fbe\u5230\u6bcf\u79d260\u6b21\u624d\u80fd\u8ba9\u8089\u773c\u770b\u5230\u6d41\u7545\u7684\u52a8\u753b\u6548\u679c\u3002
\u3000\u3000\u73b0\u5728\u53c8\u591a\u4e86\u4e00\u79cd\u5b9e\u73b0\u52a8\u753b\u7684\u65b9\u6848\uff0c\u90a3\u5c31\u662f\u8fd8\u5728\u8349\u6848\u5f53\u4e2d\u7684 window.requestAnimationFrame()\u65b9\u6cd5\u3002
\u3000\u3000

\u3000\u3000\u521d\u8bc6requestAnimationFrame

\u6765\u770bMDN\u4e0a\u5bf9\u5176\u7ed9\u51fa\u7684\u8be0\u91ca\uff1a
The window.requestAnimationFrame() method tells the browser that you wish to perform an animation and requests that the browser call a specified function to update an animation before the next repaint. The method takes as an argument a callback to be invoked before the repaint.
\u3000\u3000window.requestAnimationFrame()\u5c06\u544a\u77e5\u6d4f\u89c8\u5668\u4f60\u9a6c\u4e0a\u8981\u5f00\u59cb\u52a8\u753b\u6548\u679c\u4e86\uff0c\u540e\u8005\u9700\u8981\u5728\u4e0b\u6b21\u52a8\u753b\u524d\u8c03\u7528\u76f8\u5e94\u65b9\u6cd5\u6765\u66f4\u65b0\u753b\u9762\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5c31\u662f\u4f20\u9012\u7ed9window.requestAnimationFrame()\u7684\u56de\u8c03\u51fd\u6570\u3002
\u3000\u3000\u4e5f\u53ef\u8fd9\u4e2a\u65b9\u6cd5\u539f\u7406\u5176\u5b9e\u4e5f\u5c31\u8ddfsetTimeout/setInterval\u5dee\u4e0d\u591a\uff0c\u901a\u8fc7\u9012\u5f52\u8c03\u7528\u540c\u4e00\u65b9\u6cd5\u6765\u4e0d\u65ad\u66f4\u65b0\u753b\u9762\u4ee5\u8fbe\u5230\u52a8\u8d77\u6765\u7684\u6548\u679c\uff0c\u4f46\u5b83\u4f18\u4e8esetTimeout/setInterval\u7684\u5730\u65b9\u5728\u4e8e\u5b83\u662f\u7531\u6d4f\u89c8\u5668\u4e13\u95e8\u4e3a\u52a8\u753b\u63d0\u4f9b\u7684API\uff0c\u5728\u8fd0\u884c\u65f6\u6d4f\u89c8\u5668\u4f1a\u81ea\u52a8\u4f18\u5316\u65b9\u6cd5\u7684\u8c03\u7528\uff0c\u5e76\u4e14\u5982\u679c\u9875\u9762\u4e0d\u662f\u6fc0\u6d3b\u72b6\u6001\u4e0b\u7684\u8bdd\uff0c\u52a8\u753b\u4f1a\u81ea\u52a8\u6682\u505c\uff0c\u6709\u6548\u8282\u7701\u4e86CPU\u5f00\u9500\u3002

\u57fa\u672c\u8bed\u6cd5
(1)\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7window\u6765\u8c03\u7528\uff0c\u63a5\u6536\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u56de\u8c03;
(2) \u8fd4\u56de\u4e00\u4e2aID\u503c\uff0c\u901a\u8fc7\u628a\u8fd9\u4e2aID\u503c\u4f20\u7ed9window.cancelAnimationFrame()\u53ef\u4ee5\u53d6\u6d88\u8be5\u6b21\u52a8\u753b\u3002
requestAnimationFrame(callback)//callback\u4e3a\u56de\u8c03\u51fd\u6570
\u3000\u3000\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50
\u6a21\u62df\u4e00\u4e2a\u8fdb\u5ea6\u6761\u52a8\u753b\uff0c\u521d\u59cbdiv\u5bbd\u5ea6\u4e3a1px,\u5728step\u51fd\u6570\u4e2d\u5c06\u8fdb\u5ea6\u52a01\u7136\u540e\u518d\u66f4\u65b0\u5230div\u5bbd\u5ea6\u4e0a\uff0c\u5728\u8fdb\u5ea6\u8fbe\u5230100\u4e4b\u524d\uff0c\u4e00\u76f4\u91cd\u590d\u8fd9\u4e00\u8fc7\u7a0b\u3002
\u3000\u3000\u4e3a\u4e86\u6f14\u793a\u65b9\u4fbf\u52a0\u4e86\u4e00\u4e2a\u8fd0\u884c\u6309\u94ae\uff08\u70b9\u51fb\u89c2\u770b\u6548\u679c\uff09
html>


\u52a8\u753b


0%




\u3000\u3000window.requestAnimationFrame =
window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame
|| window.msRequestAnimationFrame;
var start = null;
var ele = document.getElementById("test");
var progress = 0;
var stop=null;
function step(timestamp) {
if(progress>=100) progress=0;
progress += 1;
ele.style.width = progress + "%";
ele.innerHTML=progress + "%";
if (progress < 100) {
stop=requestAnimationFrame(step);
}
}
document.getElementById("run").addEventListener("click", function() {
ele.style.width = "1px";
step();
}, false);
document.getElementById("stop").addEventListener("click", function() {
window.cancelAnimationFrame(stop);//\u53ef\u4ee5\u53d6\u6d88\u8be5\u6b21\u52a8\u753b\u3002
}, false);



\u3000\u3000

1. \u542f\u52a8/\u505c\u6b62\u7684\u65b9\u6cd5\u4e4b\u4e00\u662f\u8fd9\u6837\u7684var requestId;

function loop() {
...
// do stuff
...
requestId = window.requestAnimationFrame(loop, canvas);
}

function start() {
if (!requestId) {
loop();
}
}

function stop() {
if (requestId) {
window.cancelRequestAnimationFrame(requestId);
requestId = undefined;
}
}


\u5f53\u7136\uff0c\u4f60\u9700\u8981\u4e00\u4e2apolyfill\u662f\u8de8\u5e73\u53f0

2. \u6211\u5efa\u8bae\u6709\u4e00\u4e2a\u770b\u7684polyfill gibhub\u9875\u9762\u3002\u6709\u5173\u4e8e\u8fd9\u662f\u600e\u4e48

3. \u6240\u4ee5\uff0c\u505a\u66f4\u591a\u7684\u6d4b\u8bd5\u540e\uff0c\u6211\u53d1\u73b0\u4e86\u5b83\uff0c\u7684\u786e\uff0c\u6211\u662f\u63d0\u51fa\u4e86\u4e00\u4e2a\u95ee\u9898\uff0c\u800c\u4e0d\u662f\u52a8\u753b\u505c\u6b62\uff08\u8fd9\u6bd5\u7adf\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u9012\u5f52\uff09\u7684\u5176\u4ed6\u4ee3\u7801\u3002\u8fd9\u4e2a\u95ee\u9898\u662f\u5728\u52a8\u6001\u6dfb\u52a0\u548c\u5220\u9664\u6e32\u67d3\u7684\u4ece\u9875\u9762\u3002\u4e4b\u540e\uff0c\u6211\u5df2\u7ecf\u505c\u6b62\u8fd9\u6837\u505a\uff0c\u5bf9\u4e8e\u786e\u5b9e\u6ca1\u6709\u7406\u7531\u8fd9\u6837\u505a\uff0c\u4ee5\u53ca\u5305\u62ec\u5b83\u66fe\u7ecf\u6240\u5728\u7684\u521d\u59cb\u5316\u53d1\u751f\u7684\u4e00\u5207\uff0c\u4e00\u5207\u5f00\u59cb\u5de5\u4f5c\u7684\u7f5a\u6b3e\u3002

HTML5/CSS3时代,我们要在web里做动画选择其实已经很多了:
(1) 你可以用CSS3的animattion+keyframes;
(2) 你也可以用css3的transition;

(3) 你还可以用通过在canvas上作图来实现动画,也可以借助jQuery动画相关的API方便地实现;

(4) 当然最原始的你还可以使用window.setTimout()或者window.setInterval()通过不断更新元素的状态位置等来实现动画,前提是画面的更新频率要达到每秒60次才能让肉眼看到流畅的动画效果。
现在又多了一种实现动画的方案,那就是还在草案当中的 window.requestAnimationFrame()方法。

初识requestAnimationFrame

来看MDN上对其给出的诠释:
The window.requestAnimationFrame() method tells the browser that you wish to perform an animation and requests that the browser call a specified function to update an animation before the next repaint. The method takes as an argument a callback to be invoked before the repaint.
window.requestAnimationFrame()将告知浏览器你马上要开始动画效果了,后者需要在下次动画前调用相应方法来更新画面。这个方法就是传递给window.requestAnimationFrame()的回调函数。
也可这个方法原理其实也就跟setTimeout/setInterval差不多,通过递归调用同一方法来不断更新画面以达到动起来的效果,但它优于setTimeout/setInterval的地方在于它是由浏览器专门为动画提供的API,在运行时浏览器会自动优化方法的调用,并且如果页面不是激活状态下的话,动画会自动暂停,有效节省了CPU开销。

基本语法
(1)可以直接调用,也可以通过window来调用,接收一个函数作为回调;
(2) 返回一个ID值,通过把这个ID值传给window.cancelAnimationFrame()可以取消该次动画。
requestAnimationFrame(callback)//callback为回调函数
一个简单的例子
模拟一个进度条动画,初始div宽度为1px,在step函数中将进度加1然后再更新到div宽度上,在进度达到100之前,一直重复这一过程。
为了演示方便加了一个运行按钮(点击观看效果)
html>
<head>
<meta charset="gbk">
<title>动画</title>
</head>
<body>
<div id="test" style="width:1px;height:17px;background:#0f0;">0%</div>
<input type="button" value="Run" id="run"/>
<input type="button" value="停止" id="stop"/>
</body>
<script>
window.requestAnimationFrame =
window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame
|| window.msRequestAnimationFrame;
var start = null;
var ele = document.getElementById("test");
var progress = 0;
var stop=null;
function step(timestamp) {
if(progress>=100) progress=0;
progress += 1;
ele.style.width = progress + "%";
ele.innerHTML=progress + "%";
if (progress < 100) {
stop=requestAnimationFrame(step);
}
}
document.getElementById("run").addEventListener("click", function() {
ele.style.width = "1px";
step();
}, false);
document.getElementById("stop").addEventListener("click", function() {
window.cancelAnimationFrame(stop);//可以取消该次动画。
}, false);

</script>
</html>

浏览器支持情况
既然还是草案状态下引入的一个功能,在使用全我们就需要关心一下各浏览器对它的支持情况了。就目前来说,主流现代浏览器都对它提供了支持,请看下图:

Polyfill
Polyfill就是垫片,按发明这个词的人的原话来说,它就是一段这样的代码,让浏览器原生地支持我们期望使用的一些API。
就比如这里的requestAnimationFrame,在看到了上面的浏览器支持情况后,你就知道了比上面列出的浏览器版本老的就不支持该方法,但为了让代码能够有更好的浏览器兼容性在老机器上也能运行不报错,我们可以写一些代码让浏览器在不支持requestAnimationFrame的情况下使用window.setTimeout(),这是一种回退(fallback)到过去的方法。
这样一来,就可以通俗一点的理解polyfill了,它就是备胎。
下面是由Paul Irish及其他贡献者放在GitHub Gist上的代码片段,用于在浏览器不支持requestAnimationFrame情况下的回退,回退到使用setTmeout的情况。当然,如果你确定代码是工作在现代浏览器中,下面的代码是不必的。
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
// requestAnimationFrame polyfill by Erik M ller. fixes from Paul Irish and Tino Zijdel
// MIT license
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] || window[vendors[x] +
'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame) window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() {
callback(currTime + timeToCall);
}, timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame) window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}());

上面代码作用有二,一是把各浏览器前缀进行统一,二是在浏览器没有requestAnimationFrame方法时将其指向setTimeout方法。
提到备胎代码呢,这里多说一句,在CSS代码中,我们也经常使用这种回退的技巧,即对同一条CSS规则,编写多条以不同浏览器前缀开头代码,或者编写一条备用样式。
下面是一个CSS中的备胎代码的例子:
div {
background: rgb(0, 0, 0); /* fallback */
background: rgba(0, 0, 0, 0.5);
}
代码中设置div背景为黑色带50%的透明度,但IE9-的浏览器是不支持rbga格式的颜色的,所以浏览器会回退到上一条CSS规则应用rgb颜色。
Reference:
1. article about rAF from css tricks: http://css-tricks.com/using-requestanimationframe/
2. article about rAF from Paul Irish:http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
3. what is polyfill http://remysharp.com/2010/10/08/what-is-a-polyfill/

  • 濡備綍浣跨敤requestanimationframe瀵规祻瑙堝櫒鎬ц兘杩涜璋冧紭
    绛旓細澶嶅埗浠g爜 浠g爜濡備笅: // shim layer with setTimeout fallback window.requestAnimFrame = (function(){ return window.requestAnimationFrame window.webkitRequestAnimationFrame window.mozRequestAnimationFrame window.oRequestAnimationFrame window.msRequestAnimationFrame function(/* function */ callback, /* DO...
  • HTML5鐢╟anvas鎬庝箞瀹炵幇鍔ㄧ敾鏁堟灉
    绛旓細window.requestAnimFrame = (function(callback) { return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(callback) { window.setTimeout(callback, 1000 / 60...
  • 2023骞磋法骞寸绂忎唬鐮
    绛旓細3銆亀indow.requestAnimFrame =(function(){ return window.requestAnimationFrame window.webkitRequestAnimationFrame window.mozRequestAnimationFrame function( callback ) { window.setTimeout( callback锛1000 /60 );};})();// now we will setup our basic variables for the demo var canvas ...
  • 鐢佃剳椤甸潰鐨勪粈涔堢壒鏁?
    绛旓細}window.requestAnimFrame = (function () {return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function (fn) { window.setTimeout(fn, 1000 / 60) };})();window.onload = function () {ctx = document.getElementById('canvas').getConte...
  • 本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网