-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawBody.js
43 lines (36 loc) · 1.12 KB
/
drawBody.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
class drawBody {
constructor(ctx, trailLength, radius) {
this.ctx = ctx;
this.trailLength = trailLength;
this.radius = radius;
this.positions = [];
}
storePosition(x, y) {
this.positions.push({x, y});
if (this.positions.length > this.trailLength) this.positions.shift();
}
draw(x, y) {
this.storePosition(x, y);
for (let i = 0; i < this.positions.length; i++) {
let transparency;
let circleScaleFactor;
const scaleFactor = ( i + 1 ) / this.positions.length;
circleScaleFactor = scaleFactor;
if (scaleFactor != 1) {
transparency = scaleFactor / 2;
} else {
transparency = scaleFactor;
}
this.ctx.beginPath();
this.arc(
this.positions[i].x,
this.positions[i].y,
circleScaleFactor * this.radius,
0,
2 * Math.PI
);
this.ctx.fillStyle = `rgb(0, 12, 153, ${transparency})`;
this.ctx.fill();
}
}
}