javascript - Canvas requestAnimationFrame pause -


how pause canvas animation made requestanimationframe ? start animation this:

code:

window.requestanimframe = (function() {     return  window.requestanimationframe       ||             window.webkitrequestanimationframe ||             window.mozrequestanimationframe    ||             function(callback) {                 window.settimeout(callback, 1000 / 60);             }; })();  function start() {     update();     requestanimframe(start); }  start(); 

now want add pause option after keydown. there simple way it?

what create variable stores state of animation: paused or unpaused. change state every time click button. should work:

var ispaused = false;  window.requestanimframe = (function() {     return  window.requestanimationframe       ||             window.webkitrequestanimationframe ||             window.mozrequestanimationframe    ||             function(callback) {                 window.settimeout(callback, 1000 / 60);             }; })();  function start() {     if (ispaused) {         update();     }      requestanimframe(start); }  window.onkeydown = function() {     ispaused = !ispaused; // flips pause state };  start(); 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -