-
Notifications
You must be signed in to change notification settings - Fork 0
/
swirl.js
54 lines (49 loc) · 1.52 KB
/
swirl.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
// From
// https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Basic_animations
function generateParticles(amount, width, height) {
let particlesArray = [];
for (let i = 0; i < amount; i++) {
particlesArray[i] =
new Particle(width / 2, height / 2, 4, generateColor(), 1);
}
return particlesArray;
}
function generateColor() {
let hexSet = "0123456789ABCDEF";
let finalHexString = "#";
for (let i = 0; i < 6; i++) {
finalHexString += hexSet[Math.ceil(Math.random() * 15)];
}
return finalHexString;
}
function Particle(x, y, particleTrailWidth, strokeColor, rotateSpeed) {
this.width = x * 2;
this.height = y * 2;
this.x = x;
this.y = y;
this.particleTrailWidth = particleTrailWidth;
this.strokeColor = strokeColor;
this.theta = Math.random() * Math.PI * 2;
this.rotateSpeed = rotateSpeed;
this.t = Math.random() * 150;
this.rotate = context => {
const ls = {
x : this.x,
y : this.y,
};
this.theta += this.rotateSpeed;
this.x = (this.width / 2) + Math.cos(this.theta) * this.t;
this.y = (this.height / 2) + Math.sin(this.theta) * this.t;
context.beginPath();
context.lineWidth = this.particleTrailWidth;
context.strokeStyle = this.strokeColor;
context.moveTo(ls.x, ls.y);
context.lineTo(this.x, this.y);
context.stroke();
};
}
function anim(canvas, context, particles) {
context.fillStyle = "rgba(0,0,0)";
context.fillRect(0, 0, canvas.width, canvas.height);
particles.forEach((particle) => particle.rotate(context));
}