Created
March 4, 2019 23:59
-
-
Save ocrampete16/702a2a5acd06da06ab0a39e93a2e4e4f to your computer and use it in GitHub Desktop.
The Model-View-Update Architecture for use with HTML5 Canvases
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
| 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