| 1 | // RenderLoop to handle drawing frames onto the canvas |
| 2 | class RenderLoop { |
| 3 | constructor(pipeline, canvas) { |
| 4 | this.timer = null; |
| 5 | this.pipeline = pipeline; |
| 6 | this.canvas = canvas; |
| 7 | this.context = canvas.getContext("2d"); |
| 8 | } |
| 9 | |
| 10 | start() { |
| 11 | this.stop(); |
| 12 | this.next(); |
| 13 | } |
| 14 | |
| 15 | stop() { |
| 16 | cancelAnimationFrame(this.timer); |
| 17 | } |
| 18 | |
| 19 | next() { |
| 20 | this.timer = requestAnimationFrame(() => this.run()); |
| 21 | } |
| 22 | |
| 23 | run() { |
| 24 | const frame = this.pipeline.consume(); |
| 25 | if (frame) { |
| 26 | this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); |
| 27 | this.context.drawImage(frame.image, 0, 0, this.canvas.width, this.canvas.height); |
| 28 | } |
| 29 | this.next(); |
| 30 | } |
| 31 | } |
nothing calls this directly
no outgoing calls
no test coverage detected