-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
107 lines (96 loc) · 2.25 KB
/
script.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
95
96
97
98
99
100
101
102
103
104
105
106
107
let size = 400;
let squareSize = size * 0.02;
let step = size * 0.01;
let squares = [];
let hue = 0;
class Square {
constructor(x, y, size, hue) {
this.x = x;
this.y = y;
this.size = size;
this.centre = {
x: this.x + squareSize * 0.5,
y: this.y + squareSize * 0.5
};
this.hue = hue;
}
update() {
// calculate angle from center of circle
const angle = atan2(this.centre.y - Circle.y, this.centre.x - Circle.x);
// extend the line to the edge of the circle
const x = Circle.x + Circle.radius * cos(angle);
const y = Circle.y + Circle.radius * sin(angle);
// get the distance from the point on the line
const distance = abs(dist(this.centre.x, this.centre.y, x, y));
// if distance is smaller than radius * 0.7, make square larger
// otherwise, square size is half
let newSize = squareSize * 0.5;
if (distance < Circle.radius * 0.7)
newSize = map(
distance,
0,
Circle.radius * 0.7,
squareSize,
squareSize * 0.5
);
this.size = newSize;
}
draw() {
stroke(hue, 100, 40, 1);
fill(hue, 100, 40, 1);
square(this.x, this.y, this.size);
}
}
function createSquares() {
for (let x = step; x < size; x += squareSize + step) {
for (let y = step; y < size; y += squareSize + step) {
const square = new Square(x, y, squareSize, 0);
squares.push(square);
}
}
}
const Circle = {
x: size / 2,
y: size / 2,
radius: 0,
acceleration: 5
};
function setup() {
colorMode(HSL);
const c = createCanvas(size, size);
c.parent("canvas");
resize();
}
function draw() {
clear();
Circle.radius += Circle.acceleration;
Circle.acceleration += 0.02;
hue += 2;
if (hue > 360) hue = 0;
if (Circle.radius > size * 1.5) {
Circle.radius = 0;
Circle.acceleration = 5;
}
for (let i = 0; i < squares.length; i++) {
squares[i].update();
squares[i].draw();
}
}
function resize() {
squares = [];
if (windowWidth < 400) {
size = windowWidth * 0.8;
} else {
size = 400;
}
squareSize = size * 0.02;
step = size * 0.01;
Circle.x = size / 2;
Circle.y = size / 2;
resizeCanvas(size, size);
createSquares();
}
function windowResized() {
resize();
}
requestAnimationFrame(() => {});