-
Notifications
You must be signed in to change notification settings - Fork 0
/
spiralStar.js
94 lines (80 loc) · 2.45 KB
/
spiralStar.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const { lerp } = require("canvas-sketch-util/math");
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const margin = 50;
let width = innerWidth - margin;
let height = innerHeight - margin;
ctx.canvas.width = width;
ctx.canvas.height = height;
window.addEventListener("resize", () => {
width = innerWidth - margin;
height = innerHeight - margin;
ctx.canvas.width = width;
ctx.canvas.height = height;
});
function Particle(size, step, radius, position) {
this.size = size;
this.radius = radius;
this.step = step;
this.position = position;
}
Particle.prototype.update = function updateParticle() {
this.position += this.step;
this.draw();
};
Particle.prototype.draw = function drawParticle() {
ctx.beginPath();
ctx.fillStyle = "#fff";
const x = Math.cos(this.position) * this.radius + width / 2;
const y = Math.sin(this.position) * this.radius + height / 2;
drawStar(x, y, 6, this.size, this.size);
// ctx.arc(x, y, this.size, 0, Math.PI * 2, false);
// ctx.fill();
};
const particles = [];
function init() {
ctx.fillStyle = "#000";
ctx.fillRect(0, 0, width, height);
for (let index = 1; index < 100; index++) {
const size = index * 0.1;
const step = Math.random() * 0.002 + 0.002;
// const radius = lerp(0, width, index / (200 - 2));
const radius = Math.random() * width;
const position = Math.random() * Math.PI * 2;
const particle = new Particle(size, step, radius, position);
particle.draw();
particles.push(particle);
}
}
function animate() {
requestAnimationFrame(animate);
ctx.fillStyle = "rgba(0,0,0,1)";
ctx.fillRect(0, 0, width, height);
particles.forEach((particle) => {
particle.update();
});
}
function drawStar(posX, posY, spikes, innerRadius, outerRadius) {
ctx.beginPath();
ctx.strokeStyle = "#fff";
let x = posX;
let y = posY;
let rotation = (Math.PI / 2) * 3;
const step = Math.PI / spikes;
ctx.moveTo(x, y - outerRadius);
for (let i = 0; i < spikes; i++) {
x = posX + Math.cos(rotation) * outerRadius;
y = posY + Math.sin(rotation) * outerRadius;
ctx.lineTo(x, y);
rotation += step;
x = posX + Math.cos(rotation) * innerRadius;
y = posY + Math.sin(rotation) * innerRadius;
ctx.lineTo(x, y);
rotation += step;
}
ctx.lineTo(posX, posY - outerRadius);
ctx.closePath();
ctx.stroke();
}
init();
animate();