-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubtle.js
290 lines (227 loc) · 8.09 KB
/
subtle.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
(() => {
// Subtle particle
class Particle {
constructor(state) {
this.reinitialize(state);
}
// Initialize variables
reinitialize(state) {
// Opacity starts with 0
this.a = 0;
// Size starts with 0
this.r = 0;
// Random hue
this.c = Math.random() * 360;
// Random speed within [speed/2, speed]
this.s = (state.conf.speed + (Math.random() * state.conf.speed)) / 2.0;
// Random position
const calculatePosition = () => {
const getRandomIntInclusive = (min, max) => {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
};
const pad = state.conf.contain ? state.conf.size : 0;
this.x = getRandomIntInclusive(pad, state.width - pad * 2);
this.y = getRandomIntInclusive(pad, state.height - pad * 2);
};
calculatePosition();
// Random rotation in radians
this.rad = Math.random() * Math.PI * 2;
// Recalculate position while collisions with
// exclusion target are detected.
if (state.ex !== null) {
const cBounds = state.canvas.getBoundingClientRect();
const exBounds = state.ex.getBoundingClientRect();
while (subtle_intersects(
cBounds.left + this.x - (state.conf.size),
cBounds.left + this.x + (state.conf.size * 2),
cBounds.top + this.y - (state.conf.size),
cBounds.top + this.y + (state.conf.size * 2),
exBounds.left,
exBounds.right,
exBounds.top,
exBounds.bottom
)) calculatePosition();
}
}
// Update particle
update(state) {
// Increase size by speed
this.r += this.s;
// Increase hue by 1
this.c = (this.c + 1) % 360;
// r <= 25%
if (this.r < state.conf.size * 0.25) {
this.a = (this.r / (state.conf.size * 0.25));
}
// r >= 25% && r <= 75%
else if (this.r >= state.conf.size * 0.25 && this.r <= state.conf.size * 0.75) {
this.a = 1;
}
// r >= 75% && r <= 100%
else if (this.r >= state.conf.size * 0.75 && this.r <= state.conf.size) {
this.a = (state.conf.size - this.r) / (state.conf.size * 0.25);
}
// r > 100%
else {
this.reinitialize(state);
}
}
// Render particle
render(state) {
// Push state
state.context.save();
// Calculate lightness and saturation
const lit = state.conf.lightness * 100.0;
const sat = state.conf.saturation * 100.0;
// Set stroke style
state.context.strokeStyle = `hsla(${this.c},${sat}%,${lit}%,${this.a})`;
// Begin path
state.context.beginPath();
// Switch on modes
switch (state.conf.mode) {
// Circle
case Modes.circle: {
// Draw circle
state.context.arc(
this.x, // x
this.y, // y
this.r, // r
0, // sAngle
2 * Math.PI, // eAngle
);
break;
}
// Square
case Modes.square: {
// Test if rotation is randomized
if (state.conf.randomizeRotation) {
// Calculate center
const cx = this.x + this.r / 2;
const cy = this.y + this.r / 2;
// Translate to center
state.context.translate(cx, cy);
// Rotate canvas
state.context.rotate(this.rad);
}
// Draw rectangle
state.context.rect(
state.conf.randomizeRotation ? 0 : this.x, // x
state.conf.randomizeRotation ? 0 : this.y, // y
this.r, // w
this.r // h
);
break;
}
}
// Draw stroke
state.context.stroke();
// Pop state
state.context.restore();
}
}
// Setup
function subtle_setup(state) {
// Fitting function
const subtle_fit = () => {
const bounds = state.el.getBoundingClientRect();
state.canvas.style.left = `${bounds.left}px`;
state.canvas.style.top = `${bounds.top}px`;
state.canvas.style.width = `${bounds.width}px`;
state.canvas.style.height = `${bounds.height}px`;
state.canvas.width = bounds.width;
state.canvas.height = bounds.height;
state.width = bounds.width;
state.height = bounds.height;
}
// Fit canvas and refit on resize
subtle_fit() || window.addEventListener('resize', subtle_fit);
// Set time to current timestamp
state.time = Date.now();
// Create 2D context
state.context = state.canvas.getContext('2d');
// Spawn particles
state.particles = Array.from({length: state.conf.count}).map(_ => new Particle(state));
}
// Rectanglular intersection detection
function subtle_intersects(l1, r1, t1, b1, l2, r2, t2, b2) {
return r1 >= l2 && l1 <= r2 && b1 >= t2 && t1 <= b2;
}
// Update scene
function subtle_update(state) {
state.particles.forEach(p => p.update(state));
}
// Render scene
function subtle_render(state) {
// Clear canvas
state.context.clearRect(0, 0, state.width, state.height);
// Render all particles
state.particles.forEach(p => p.render(state));
}
// Present scene
function subtle_present(state) {
// Increase time
state.time += 0.1;
// Update scene
subtle_update(state);
// Render scene
subtle_render(state);
// Request next loop iteration
window.requestAnimationFrame(function() {
subtle_present(state);
});
}
// Render modes
const Modes = {
circle: 1,
square: 2,
};
// Default configuration
const DefaultConf = {
target: 'body',
exclude: null,
size: 32,
speed: 0.25,
count: 25,
lightness: 0.75,
saturation: 0.25,
randomizeRotation: false,
contain: false,
mode: Modes.square,
};
// Subtle object
const Subtle = {
mode: Modes,
};
/**
* Mounts the Subtle effect.
*/
Subtle.mount = function mount(conf) {
// Create new canvas
const canvas = document.createElement('canvas');
// Style appropriately
canvas.style.position = 'absolute';
canvas.style.zIndex = '-1';
// Merge configuration
const finalConf = Object.assign({}, DefaultConf, conf);
// Get target and exclusion elements
const targetEl = document.querySelector(finalConf.target || 'body');
const excludeEl = finalConf.exclude ? document.querySelector(finalConf.exclude) : null;
// Initialize state
const state = {
canvas: canvas,
conf: finalConf,
el: targetEl,
ex: excludeEl,
};
// Add canvas to target element
state.el.appendChild(canvas);
// Prepare rendering
subtle_setup(state);
// Start render loop
subtle_present(state);
};
// Export Subtle object
window.Subtle = Subtle;
})();