-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
50 lines (43 loc) · 1.05 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
window.addEventListener('message', function (event) {
switch (event.data.type) {
case 'videobrew.init':
setup();
break;
case 'videobrew.tick':
tick(event.data.frame);
break;
case 'videobrew.setup':
break; // renderer side only
}
});
const squares = document.querySelectorAll('div');
const colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'];
let initialHues = [90, 180, 270, 360];
let hues = [];
const width = 360;
const height = 640;
const framerate = 30;
const frameCount = 2 * framerate; // seconds
function messageRenderer(type, data) {
parent.postMessage({ ...data, type }, '*');
}
function setup() {
messageRenderer('videobrew.setup', {
width: width,
height: height,
framerate: framerate,
frameCount: frameCount,
});
}
function tick(frame) {
if(frame === 0) {
hues = [...initialHues];
}
move();
}
function move() {
hues = hues.map(hue => (hue + 1) % 360);
squares.forEach((square, i) => {
square.style.backgroundColor = `hsl(${hues[i]}, 100%, 50%)`;
});
}