-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
375 lines (317 loc) · 9.73 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
*********************************
VARIABLES
*********************************
*/
const breakpoint = 750 // mobile breakpoint
const canvas = document.querySelector('canvas') // grab important DOM elements
const ctx = canvas.getContext('2d')
const splashimage = document.querySelector('.wrapper')
const main = document.querySelector('main')
const colorway = {
warm: {
light: '#FFA500',
medium: '#FF8C00',
dark: '#FF4500',
complement: '#ffc800',
background: '#ffc800',
solidRing: '#FFA500',
accent: '#FFFFC0'
},
cool: {
light: '#00DFFF',
medium: '#008CFF',
dark: '#0000CD',
complement: '#ffc800',
background: '#00AFFF',
solidRing: '#ffc800',
accent: '#FFFFC0'
}
}
let mode = 'warm' // some defaults on loading
let WIDTH = canvas.width
let HEIGHT = canvas.height
let audioctx, source, song, analyser, freqs, meanFreq // audio context globals
let isRunning = false // binaries
let isMobile, canvasStarted
let sunAngle, radius, radians, rays, // animation math variables
rayPositionIncrement, angle,
dashIntervals, rayHeightFactor, rValue, bValue, gValue
/*
*********************************
HELPER FUNCTIONS
*********************************
*/
function setMainColours () { // COLOURS SPLASH PAGE ACCORDING TO MODE
main.style.backgroundColor = colorway[mode].background
const rings = Array.from(main.children[0].children)
rings[0].style.borderColor = colorway[mode].medium
rings.slice(1, 3).forEach(ring => {
ring.style.borderTopColor = colorway[mode].solidRing
ring.style.borderLeftColor = colorway[mode].solidRing
})
rings[3].style.borderTopColor = colorway[mode].dark
rings[3].style.borderLeftColor = colorway[mode].dark
rings[4].style.borderTopColor = colorway[mode].dark
rings[5].style.borderTopColor = colorway[mode].medium
rings[6].style.borderTopColor = colorway[mode].background
rings[7].children[1].children[0].style.fill = colorway[mode].dark
rings[8].children[1].children[0].style.fill = colorway[mode].medium
rings[9].children[1].children[0].style.fill = colorway[mode].dark
rings[10].children[1].children[0].style.fill = colorway[mode].accent
}
function animateCenterRing () { // animates the splash page ring
if (!sunAngle) sunAngle = 0
main.children[0].children[10].style.transform = `scale(0.54) rotate(${sunAngle - 264}deg)`
main.children[0].children[6].style.transform = `rotate(${sunAngle + 110}deg)`
main.children[0].children[5].style.transform = `rotate(${sunAngle + 55}deg)`
sunAngle -= 0.1
if (!canvasStarted) requestAnimationFrame(animateCenterRing)
else sunAngle = 0
}
function drawStaticRings (ctx, radius) { // draws all the static rings on canvas
ctx.setLineDash([])
ring(ctx, radius - 20, 4, colorway[mode].complement)
ring(ctx, radius - 30, 2, colorway[mode].light)
ring(ctx, radius - 40, 4, colorway[mode].medium)
ring(ctx, radius - 55, 4, colorway[mode].medium)
ring(ctx, radius - 85, 1, colorway[mode].dark)
ring(ctx, radius - 90, 1.5, colorway[mode].accent)
if (!isMobile) {
ring(ctx, radius - 110, 10, colorway[mode].light)
ring(ctx, radius - 130, 3, colorway[mode].medium)
} else {
ring(ctx, radius - 100, 10, colorway[mode].light)
}
}
function ring (ctx, radius, weight, stroke) { // draws a ring to canvas
ctx.beginPath()
ctx.strokeStyle = stroke
ctx.lineWidth = weight
ctx.arc(WIDTH / 2, HEIGHT / 2, radius, 0, 2 * Math.PI)
ctx.stroke()
ctx.closePath()
}
function paintBackground () { // calculcates canvas background colour
if (meanFreq) {
ctx.fillStyle = `rgb(${rValue}, ${gValue - meanFreq / 1.4}, ${bValue})`
} else {
ctx.fillStyle = colorway[mode].background
}
ctx.fillRect(0, 0, WIDTH, HEIGHT)
}
function setAnimationValues () { // SETS INITIAL ANIMATION VALUES
freqs = new Uint8Array(analyser.frequencyBinCount)
dashIntervals = [5, 5, 10, 15, 25, 20, 35, 10, 50]
angle = 0
rayPositionIncrement = 1
gValue = 200
isRunning = true
}
function center (el, halfsize) { // centers an element
el.style.top = `${(HEIGHT / 2) - (halfsize)}px`
el.style.left = `${(WIDTH / 2) - (halfsize)}px`
}
function average (array) { // finds mean of array
const sum = array.reduce((sum, value) => {
return sum + value
})
return sum / array.length
}
/*
*********************************
EVENT HANDLERS
*********************************
*/
function keyHandler (e) { // controls colouring
if (e.keyCode === 32) { // spacebar
mode = mode === 'warm' ? 'cool' : 'warm'
setMainColours()
}
if (e.keyCode === 38) { // up arrow
gValue = gValue > 254 ? 255 : gValue += 10
}
if (e.keyCode === 40) { // down arrow
gValue = gValue < 1 ? 0 : gValue -= 10
}
if (e.keyCode === 27 && canvasStarted) { // escape
destroy(0)
}
}
function dragHandler (e) { // when files dragged across
e.preventDefault()
e.stopPropagation()
}
function clickHandler () { // toggles pause and play
if (audioctx.state === 'running') {
isRunning = false
audioctx.suspend()
} else if (audioctx.state === 'suspended') {
isRunning = true
requestAnimationFrame(draw)
audioctx.resume()
}
}
/*
*********************************
ON LOAD
*********************************
*/
setMainColours(main)
resizeCanvas()
animateCenterRing()
window.addEventListener('drop', init)
window.addEventListener('click', init)
window.addEventListener('touchstart', init)
window.addEventListener('dragover', dragHandler, false)
window.addEventListener('resize', resizeCanvas)
window.addEventListener('keydown', keyHandler)
/*
*********************************
MAIN FUNCTIONS
*********************************
*/
function init (e) {
window.removeEventListener('click', init)
e.preventDefault()
canvasStarted = true
// RESETS ADUIO CONTEXT AND ANIMATIONS
isRunning = false
if (audioctx && audioctx.state !== 'closed') {
audioctx.close()
}
// LOAD CANVAS AND AUDIOCONTEXT
main.style.opacity = 0
canvas.style.opacity = 1
audioctx = new (
window.AudioContext || window.webkitAudioContext
)()
window.addEventListener('click', clickHandler)
window.addEventListener('touchstart', clickHandler)
// DECIDES TYPE OF AUDIO NODE TO CREATE
if (e.type === 'click' || e.type === 'touchstart') {
loadAudioFile(e)
} else {
loadAudioBuffer(e)
}
}
function loadAudioFile (e) { // PLAY PRE-LOADED SAMPLE
song = new Audio()
song.src = 'media/sample.mp3'
source = audioctx.createMediaElementSource(song)
analyser = audioctx.createAnalyser()
analyser.connect(audioctx.destination)
source.connect(analyser)
song.play()
setAnimationValues()
draw()
}
function loadAudioBuffer (e) { // DECODE AUDIO BUFFER
const file = e.dataTransfer.files[0]
const reader = new FileReader()
reader.addEventListener('load', e => {
const data = e.target.result
audioctx.decodeAudioData(data, buffer => {
source = audioctx.createBufferSource()
source.buffer = buffer
analyser = audioctx.createAnalyser()
analyser.connect(audioctx.destination)
source.connect(analyser)
source.start(0)
if (audioctx.state === 'suspended') audioctx.resume() // bugfix for safari
setAnimationValues()
draw()
})
})
reader.readAsArrayBuffer(file)
}
function draw () {
if (audioctx.state === 'closed') return // prevents paint after destroy
if (source) {
source.addEventListener('ended', destroy.bind(source, 1500))
song.addEventListener('ended', destroy.bind(song, 1500))
}
paintBackground()
drawStaticRings(ctx, radius)
// CENTER MOVING RING
angle += 0.001
ctx.beginPath()
ctx.setLineDash([5, 5])
ctx.strokeStyle = colorway[mode].complement
ctx.lineWidth = 7
ctx.arc(
WIDTH / 2,
HEIGHT / 2,
radius - 70,
angle,
2 * Math.PI + angle
)
ctx.stroke()
ctx.closePath()
ctx.setLineDash([])
// RAYS
ctx.lineWidth = 4
rayPositionIncrement -= 0.05
ctx.setLineDash(dashIntervals)
analyser.getByteFrequencyData(freqs)
const meanArray = []
rValue = mode === 'warm' ? 255 : 0
bValue = mode === 'warm' ? 0 : 255
for (let i = 0; i < rays; i++) {
meanArray.push(freqs[i])
const rayHeight = freqs[i] * rayHeightFactor
const barColour = `rgb(${rValue}, ${255 - freqs[i]}, ${bValue})`
const x = (WIDTH / 2) +
Math.cos(radians * (i + rayPositionIncrement)) * radius
const xEnd = (WIDTH / 2) +
Math.cos(radians * (i + rayPositionIncrement)) * (radius + rayHeight)
const y = (HEIGHT / 2) +
Math.sin(radians * (i + rayPositionIncrement)) * radius
const yEnd = (HEIGHT / 2) +
Math.sin(radians * (i + rayPositionIncrement)) * (radius + rayHeight)
ctx.strokeStyle = barColour
ctx.beginPath()
ctx.moveTo(x, y)
ctx.lineTo(xEnd, yEnd)
ctx.stroke()
ctx.closePath()
}
// GET MEAN FREQUENCY AMPLITUDE
meanFreq = average(meanArray)
// CONTINUE LOOP
if (isRunning) requestAnimationFrame(draw)
}
function resizeCanvas () {
canvas.width = window.innerWidth
WIDTH = canvas.width
canvas.height = window.innerHeight
HEIGHT = canvas.height
paintBackground()
if (!isRunning) {
center(splashimage, 150)
}
isMobile = WIDTH < breakpoint
rays = isMobile ? 100 : 200
radians = (Math.PI * 2) / rays
radius = isMobile ? 100 : 150
rayHeightFactor = isMobile ? 0.4 : 1
if (canvasStarted && !isRunning) draw()
}
// needed a timeout since ended event always triggers too early
function destroy (timeout = 1500, e) {
setTimeout(function () {
if (audioctx && audioctx.state !== 'closed') {
audioctx.close()
}
isRunning = false
paintBackground()
center(splashimage, 150)
canvasStarted = false
animateCenterRing()
canvas.style.opacity = 0
main.style.opacity = 1
window.addEventListener('click', init)
}, timeout)
window.removeEventListener('click', clickHandler)
window.removeEventListener('touchstart', clickHandler)
}