Skip to content

Instantly share code, notes, and snippets.

@vyuvalv
Created November 22, 2020 15:43
Show Gist options
  • Save vyuvalv/6827093a055c5482c8db87cfedd6dad7 to your computer and use it in GitHub Desktop.
Save vyuvalv/6827093a055c5482c8db87cfedd6dad7 to your computer and use it in GitHub Desktop.
canvas animation
// 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