-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas2d-renderer.js
37 lines (32 loc) · 1.16 KB
/
canvas2d-renderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* ---------------------------------------------------------------------------
** This software is in the public domain, furnished "as is", without technical
** support, and with no warranty, express or implied, as to its usefulness for
** any purpose.
**
** -------------------------------------------------------------------------*/
export class Canvas2DRenderer {
ctx = null;
constructor(canvas) {
this.ctx = canvas.getContext("2d");
}
draw(frame) {
requestAnimationFrame(() => {
this.ctx.canvas.width = frame.displayWidth;
this.ctx.canvas.height = frame.displayHeight;
this.ctx.drawImage(frame, 0, 0, frame.displayWidth, frame.displayHeight);
frame.close();
});
}
drawText(text) {
this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
this.ctx.font = "16px Arial";
this.ctx.textAlign = 'center';
this.ctx.textBaseline = 'middle';
this.ctx.fillStyle = 'black';
this.ctx.fillText(text, this.ctx.canvas.width/2, this.ctx.canvas.height/2);
}
clear() {
this.ctx.fillStyle = 'white';
this.ctx.fillRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
}
};