Skip to content

Instantly share code, notes, and snippets.

@changhuixu
Last active July 13, 2018 21:25
Show Gist options
  • Save changhuixu/d651bf094defcba73e5624ddcb769dc4 to your computer and use it in GitHub Desktop.
Save changhuixu/d651bf094defcba73e5624ddcb769dc4 to your computer and use it in GitHub Desktop.
export class CanvasAnimation {
private readonly context: CanvasRenderingContext2D;
constructor(private readonly canvas: HTMLCanvasElement) {
this.context = this.canvas.getContext('2d');
window.requestAnimationFrame(() => this.draw());
}
draw() {
// do stuff
window.requestAnimationFrame(() => this.draw());
}
}
// Below is the way to call animation
const canvas = <HTMLCanvasElement>document.getElementById('canvasId');
new CanvasAnimation(canvas);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment