-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflower.js
197 lines (134 loc) · 3.82 KB
/
flower.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// The amount of circles around each "center"
// For the flower of life this is 6, but other values can give interesting shapes too
const nodes = 6;
// The distance between the circles
// If set to `null`, will be automatically determined based on screen size
let rads = null;
// How many levels deep do we keep creating "circles around centers"
// If set to `null`, will be automatically determined based on screen size
let maxLevel = null;
// The lower this number, the bigger the circles become when you move down with your mouse
const mouseYDivider = 2;
let moved = false;
let rings = [];
let grow = 3;
let colorShiftDirection = 1;
function setup() {
colorMode(HSB);
document.addEventListener('dblclick', function() {
fullscreen(!fullscreen());
});
let largestScreenDimention;
if (windowWidth > windowHeight) {
largestScreenDimention = windowWidth;
} else {
largestScreenDimention = windowHeight;
}
if (!rads) {
let dividingFactor;
if (largestScreenDimention > 1000) {
dividingFactor = 5;
} else {
dividingFactor = 3;
}
rads = Math.round(largestScreenDimention / dividingFactor);
}
if (!maxLevel) {
maxLevel = Math.round(largestScreenDimention / rads) - 1;
if (maxLevel > 7) maxLevel = 7;
}
const canvas = createCanvas(windowWidth, windowHeight);
recreateRings();
flipGrowDirection();
colorShiftFlipper();
moveMouseAutomatically();
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
recreateRings();
}
function draw() {
background(0, 0, 0, 0.08);
for(let i = 0; i < rings.length; i++){
rings[i].draw();
}
}
let intensity = 128
function mouseWheel(event) {
rads -= event.delta
if (rads < 0) {
rads = 0
} else if (rads > 500) {
rads = 500
}
recreateRings()
}
function recreateRings() {
const startx = width / 2;
const starty = height / 2;
const addedCoords = []
rings = []
rings.push(new Circle(startx, starty, rads));
function makeRings(x, y, level) {
level = level | 0;
for(let i = 0; i < nodes; i++) {
let newX = x + rads / 2 * cos(2 * PI * i / nodes);
let newY = y + rads / 2 * sin(2 * PI * i / nodes);
let coords = newX + ',' + newY;
if (addedCoords.indexOf(coords) == -1) {
rings.push(new Circle(newX, newY, rads));
addedCoords.push(coords)
}
if (level < maxLevel) {
makeRings(newX, newY, level + 1)
}
}
}
makeRings(startx, starty)
}
class Circle {
constructor(x, y, r) {
this.x = x;
this.y = y;
this.r = r;
}
draw() {
let color = 0;
moved = true;
color = map(mouseX, 0, width, 0, 255);
stroke(color, 150, intensity);
noFill();
ellipse(this.x, this.y, mouseY / mouseYDivider, mouseY / mouseYDivider);
moved = false;
}
}
function flipGrowDirection() {
grow = Math.random() < .5;
setTimeout(flipGrowDirection, random(400, 1200));
}
function colorShiftFlipper() {
colorShiftDirection = random([-1, 1]);
setTimeout(colorShiftFlipper, random(400, 1200));
}
function moveMouseAutomatically() {
if (grow) {
mouseY += 2
} else {
mouseY -= 2
}
mouseX += colorShiftDirection * width / 64;
if (mouseX < 0 || mouseX > width) {
if (mouseX < 0) {
mouseX = 0;
} else if (mouseX > width) {
mouseX = width;
}
colorShiftDirection = !colorShiftDirection;
}
if (mouseY < 0) {
mouseY = 0;
} else if (mouseY > height) {
mouseY = height;
}
setTimeout(moveMouseAutomatically, 50)
}