-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfigures.js
More file actions
97 lines (84 loc) · 2.18 KB
/
figures.js
File metadata and controls
97 lines (84 loc) · 2.18 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class Rectangle{
constructor(x, y, height, width, color) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = color
}
draw(ctx) {
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height)
}
}
class Circle{
constructor(x, y, radius, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.startAngle = 0;
this.endAngle = 2 * Math.PI;
this.color = color;
}
draw(ctx) {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, this.startAngle, this.endAngle)
ctx.fillStyle = this.color;
ctx.fill();
ctx.closePath();
}
}
class Square {
constructor(x, y, size, color) {
this.x = x;
this.y = y;
this.width = size;
this.height = size;
this.color = color
}
draw(ctx) {
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height)
}
}
class Triangle {
constructor(x, y, size, color) {
this.x = x;
this.y = y;
this.size = size;
this.color = color
}
draw(ctx) {
ctx.beginPath();
ctx.moveTo(this.x, this.y + this.size);
ctx.lineTo(this.x + this.size, this.y + this.size );
ctx.lineTo(this.x + this.size/2, this.y);
ctx.closePath();
ctx.fillStyle = this.color;
ctx.fill();
}
}
class RandomShape {
constructor(x, y, color) {
this.x = x;
this.y = y;
this.color = color
}
draw(ctx) {
const nbPoints = Math.floor(Math.random() * 10) ;
const points = [];
for (let i = 0; i < nbPoints; i++) {
const randomX = Math.random() * 100 - 50;
const randomY = Math.random() * 100 - 50;
points.push({ x: this.x + randomX, y: this.y + randomY });
}
ctx.beginPath();
ctx.moveTo(points[0].x, points[0].y);
for (let i = 1; i < points.length; i++) {
ctx.lineTo(points[i].x, points[i].y);
}
ctx.closePath();
ctx.fillStyle = this.color;
ctx.fill();
}
}