-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
144 lines (113 loc) · 4.22 KB
/
index.js
File metadata and controls
144 lines (113 loc) · 4.22 KB
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
const addEventListeners = (callbacks) => {
const events = [
{ type: 'mouseup', handler: callbacks.onMouseUp },
{ type: 'mousedown', handler: callbacks.onMouseDown },
{ type: 'mousemove', handler: callbacks.onMouseMove },
{ type: 'touchstart', handler: callbacks.onTouchStart },
{ type: 'touchend', handler: callbacks.onTouchEnd },
{ type: 'touchmove', handler: callbacks.onTouchMove },
{ type: 'keyup', handler: callbacks.onKeyUp },
];
events.forEach(event => document.addEventListener(event.type, event.handler, false));
};
const constructBuffer = (size) => new Array(size).fill(1);
const getWindowInnerWidth = () => window.innerWidth - 19;
const getWindowInnerHeight = () => window.innerHeight - 19;
const getWidth = () => Math.floor(reduce(getWindowInnerWidth()));
const getHeight = () => Math.floor(reduce(getWindowInnerHeight()));
const getBufferSize = () => getWidth() * getHeight();
const reduce = (value) => value / 2;
const getNormalizedCode = (code) => (code + 1) % 255;
document.oncontextmenu = () => false;
const initializeSpirals = () => {
const width = getWidth();
const height = getHeight();
const initialBuffer = constructBuffer(getBufferSize());
const finalBuffer = constructBuffer(getBufferSize());
const canvas = document.getElementById('canvas');
canvas.width = width;
canvas.height = height;
canvas.style.width = `${getWindowInnerWidth()}px`;
canvas.style.height = `${getWindowInnerHeight()}px`;
const context = canvas.getContext('2d');
context.fillStyle = 'white';
context.fillRect(0, 0, getWindowInnerWidth(), getWindowInnerHeight());
return { width, height, context, initialBuffer, finalBuffer };
};
const loadSpirals = (layerX, layerY, initialBuffer, width) => {
initialBuffer[Math.floor(layerX) + Math.floor(layerY) * width] = 255;
return initialBuffer;
};
const drawSpirals = (state) => {
const { context, width, height, configuration, mouseClicked, initialBuffer, finalBuffer } = state;
if (mouseClicked) {
state.initialBuffer = loadSpirals(configuration.layerX, configuration.layerY, initialBuffer, width);
}
const pixelData = context.getImageData(0, 0, width, height);
for (let i = width; i < width * height - 1; i++) {
const code = ((initialBuffer[i - 1] + initialBuffer[i + 1] + initialBuffer[i - width] + initialBuffer[i + width]) >> 1) - finalBuffer[i];
finalBuffer[i] = code - (code >> 8);
pixelData.data[i * 4] = 255 - getNormalizedCode(finalBuffer[i]);
}
state.initialBuffer = finalBuffer;
state.finalBuffer = initialBuffer;
context.putImageData(pixelData, 0, 0);
return state;
};
const onKeyUp = (state) => {
state.initialBuffer = loadSpirals(Math.random() * state.width, Math.random() * state.height, state.initialBuffer, state.width);
return state;
};
const onMouseUp = (state) => {
state.mouseClicked = false;
return state;
};
const onMouseDown = (mouseEvent, state) => {
state.mouseClicked = true;
state.configuration = {
layerX: reduce(mouseEvent.layerX),
layerY: reduce(mouseEvent.layerY),
};
return state;
};
const onMouseMove = (mouseEvent, state) => {
if (state.mouseClicked) {
state.configuration = {
layerX: reduce(mouseEvent.layerX),
layerY: reduce(mouseEvent.layerY),
};
}
return state;
};
const onTouchEnd = (state) => {
state.mouseClicked = false;
return state;
};
const onTouchStart = (touchEvent, state) => {
state.mouseClicked = true;
state.configuration = {
layerX: reduce(touchEvent.touches[0].clientX),
layerY: reduce(touchEvent.touches[0].clientY),
};
return state;
};
const onTouchMove = (touchEvent, state) => {
if (state.mouseClicked) {
state.configuration = {
layerX: reduce(touchEvent.touches[0].clientX),
layerY: reduce(touchEvent.touches[0].clientY),
};
}
return state;
};
let state = initializeSpirals();
addEventListeners({
onMouseUp: () => state = onMouseUp(state),
onMouseDown: (e) => state = onMouseDown(e, state),
onMouseMove: (e) => state = onMouseMove(e, state),
onTouchStart: (e) => state = onTouchStart(e, state),
onTouchEnd: () => state = onTouchEnd(state),
onTouchMove: (e) => state = onTouchMove(e, state),
onKeyUp: () => state = onKeyUp(state)
});
setInterval(() => state = drawSpirals(state), 30);