-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas.js
75 lines (75 loc) · 2.04 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
65
66
67
68
69
70
71
72
73
74
75
class Canvas{
constructor(width = window.innerWidth, height = window.innerHeight){
this.canvas = document.createElement("canvas");
this.ctx = this.canvas.getContext("2d");
this.canvas.width = width;
this.canvas.height = height;
this.width = width;
this.height = height;
document.body.appendChild(this.canvas);
}
draw(obj){
this.ctx.beginPath();
if (obj.type == "circle"){
this.ctx.arc(obj.x, obj.y, obj.radius, 0, Math.PI*2);
this.ctx.fillStyle = obj.color;
this.ctx.fill();
}
else if (obj.type == "rect"){
this.ctx.rect(obj.x, obj.y, obj.width, obj.height);
this.ctx.fillStyle = obj.color;
this.ctx.fill();
}
else if (obj.type == "text"){
this.ctx.fillStyle = obj.color;
this.ctx.font = obj.font + ' ' + obj.fontSize + 'px';
this.ctx.fillText(obj.text,obj.x,obj.y);
}
this.ctx.closePath();
}
clear(){
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
}
class Circle{
constructor(x,y,radius,color){
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
this.type = "circle";
}
move(offsetX,offsetY){
this.x += offsetX;
this.y += offsetY;
}
}
class Text{
constructor(text, x , y , color ,font = "Arial", fontSize = 10){
this.type = "text";
this.text = text;
this.x = x;
this.y = y;
this.color = color;
this.font = font;
this.fontSize = fontSize;
}
move(offsetX,offsetY){
this.x += offsetX;
this.y += offsetY;
}
}
class Rect{
constructor(x, y, width, height, color){
this.type = "rect";
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = color;
}
move(offsetX,offsetY){
this.x += offsetX;
this.y += offsetY;
}
}