Skip to content

Instantly share code, notes, and snippets.

@ocrampete16
Created March 4, 2019 23:59
Show Gist options
  • Select an option

  • Save ocrampete16/702a2a5acd06da06ab0a39e93a2e4e4f to your computer and use it in GitHub Desktop.

Select an option

Save ocrampete16/702a2a5acd06da06ab0a39e93a2e4e4f to your computer and use it in GitHub Desktop.
The Model-View-Update Architecture for use with HTML5 Canvases
class CanvasMvu {
constructor(canvas, model, view, update) {
this.canvas = canvas;
this.model = model;
this.view = view;
this.update = update;
}
get callback() {
return () => {
this.clearCanvas();
this.view(this.context, this.model);
this.model = this.update(this.model);
window.requestAnimationFrame(this.callback);
};
}
get context() {
return this.canvas.getContext('2d');
}
clearCanvas() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment