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
Post a Comment