-
Notifications
You must be signed in to change notification settings - Fork 0
/
Canvas.js
64 lines (42 loc) · 1.46 KB
/
Canvas.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Canvas {
constructor(width, height) {
this.canvas = document.createElement("canvas");
this.canvas.width = this.width = width;
this.canvas.height = this.height = height;
this.draw = this.canvas.getContext("2d");
document.body.appendChild(this.canvas);
this.canvas.style.position = "absolute";
this.canvas.style.margin = "auto";
this.canvas.style.top = "0px";
this.canvas.style.left = "0px";
this.canvas.style.bottom = "0px";
this.canvas.style.right = "0px";
// this.canvas.style.border = "5px solid black"; // fancy, but not necessary
// this.canvas.style.backgroundColor = "white"; // optional
// this.canvas.style.cursor = "none"; // also optional
}
rect(x, y, width, height, colour) {
this.draw.fillStyle = colour;
this.draw.fillRect(x, y, width, height);
}
circle(x, y, radius, colour) {
this.draw.beginPath();
this.draw.fillStyle = colour;
this.draw.arc(x, y, radius, 0, 2 * Math.PI);
this.draw.fill();
this.draw.closePath();
}
text(text, x, y, colour, size, font, extras) {
if(extras != undefined) {
this.draw.font = extras + " " + size + "px " + font;
} else {
this.draw.font = size + "px " + font;
}
this.draw.fillStyle = colour;
this.draw.textAlign = "center";
this.draw.fillText(text, x, y + (size / 2));
}
clear_screen() {
this.draw.clearRect(0, 0, this.width, this.height);
}
}