Created
November 22, 2020 15:43
-
-
Save vyuvalv/6827093a055c5482c8db87cfedd6dad7 to your computer and use it in GitHub Desktop.
canvas animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// We must initialize the animate method in constructor | |
constructor() { | |
super(); | |
this.animate = this.animate.bind(this); | |
} | |
onPlay() { | |
// Run animation | |
this.animate(); | |
} | |
lastRenderTime = 0; | |
speed = 200; // milliseconds | |
animate(timestamp = 0) { | |
// current LWC supported methods | |
const requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame; | |
this.interval = requestAnimationFrame(this.animate); | |
// interval speed control | |
const delta = (timestamp - this.lastRenderTime); | |
if (delta < this.speed) { | |
return; | |
} | |
this.lastRenderTime = timestamp; | |
// Do Something | |
this.nextGeneration(); | |
} | |
onStop() { | |
window.cancelAnimationFrame(this.interval); | |
this.interval = false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment