-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.js
More file actions
183 lines (158 loc) · 6.09 KB
/
decoder.js
File metadata and controls
183 lines (158 loc) · 6.09 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
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
import { createCanvasRecorder } from "./recorder.js";
import options from "./options.js";
import { lerp } from "./utils.js";
const canvas = document.getElementById('mainCanvas');
const ctx = canvas.getContext('2d', { willReadFrequently: true });
const previewCanvas = document.getElementById('previewCanvas');
const previewCtx = previewCanvas.getContext('2d', { willReadFrequently: true });
let pause = false;
window.addEventListener('keydown', ({key}) => {
if(key === ' ') {
pause = !pause;
pause ? video.pause() : video.play();
} else if(key === 's') {
const url = canvas.toDataURL();
const a = document.createElement('a');
a.href = url;
a.download = 'screenshot';
a.click();
}
})
const video = document.createElement('video');
video.src = 'static.webm';
video.muted = true;
video.play();
const record = false;
const recordingFrameRate = 30;
const recorder = createCanvasRecorder(canvas);
video.addEventListener('loadeddata', () => {
const { videoWidth: width, videoHeight: height } = video;
canvas.width = previewCanvas.width = width;
canvas.height = previewCanvas.height = height;
pixelArrayLen = width * height * 4;
oldData = prevFrame = new Uint8ClampedArray(pixelArrayLen).fill(255);
if(record) recorder.start(1000 / recordingFrameRate);
decode();
})
video.addEventListener('ended', () => {
if(record) recorder.stop();
});
const { movingColor, staticColor } = options;
const preview = true;
let oldData, prevFrame, pixelArrayLen;
function decode() {
const { width, height } = canvas;
const { fillThreshold: threshold, colorChange } = options;
if(preview) previewCtx.drawImage(video, 0, 0);
ctx.drawImage(video, 0, 0);
const newImageData = ctx.getImageData(0, 0, width, height);
const { data: newData } = newImageData;
const data = new Uint8ClampedArray(pixelArrayLen).fill(255);
const setMovingPixel = (index) => {
data[index] = movingColor[0];
data[index+1] = movingColor[1];
data[index+2] = movingColor[2];
}
const setStaticPixel = (index) => {
data[index] = staticColor[0];
data[index+1] = staticColor[1];
data[index+2] = staticColor[2];
}
let movingPixels = 0;
let staticPixels = 0;
for(let i = 0; i < data.length; i+=4) {
if(newData[i] !== oldData[i]) {
setMovingPixel(i);
movingPixels++;
} else {
setStaticPixel(i)
staticPixels++;
}
}
// const fillArray = new Uint8ClampedArray(pixelArrayLen).fill(255);
// Fill with threshold
function fill({color, setColorFunction}) {
const checkColor = (index) => {
return ( data[index] === color[0] &&
data[index+1] === color[1] &&
data[index+2] === color[2] )
}
// This array is for testing
// const fillDataArray = (index) => {
// fillArray[index] = color[0];
// fillArray[index+1] = color[1];
// fillArray[index+2] = color[2];
// }
const fill = (i, j) => {
for(let x = i-1; x < i+2; x++) {
if(x < 0 || x >= width) continue;
for(let y = j-1; y < j+2; y++) {
if(y < 0 || y >= height) continue;
const index = (x + y * width) * 4;
setColorFunction(index);
// fillDataArray(index);
}
}
}
// Horizontal Fill
for(let j = 0; j < height; j++) {
let min;
const checked = new Set();
for(let i = 0; i < width; i++) {
const index = (i + j * width) * 4;
if(checked.has(index)) continue;
if(checkColor(index)) {
if(typeof(min) !== 'number') {
min = i;
} else if(i - min < threshold) {
for(let x = min + 1; x < i; x++) {
const index = (x + j * width) * 4;
checked.add(index);
setColorFunction(index);
fill(x, j);
}
min = null;
}
}
}
}
// Vertical Fill
for(let i = 0; i < width; i++) {
let min;
const checked = new Set();
for(let j = 0; j < height; j++) {
const index = (i + j * width) * 4;
if(checked.has(index)) continue;
if(checkColor(index)) {
if(typeof(min) !== 'number') {
min = j;
} else if(j - min < threshold) {
for(let x = min + 1; x < j; x++) {
const index = (i + x * width) * 4;
checked.add(index);
setColorFunction(index);
fill(i, x);
}
min = null;
}
}
}
}
}
if(threshold > 0) {
fill( movingPixels < staticPixels ?
{ color: movingColor, setColorFunction: setMovingPixel } :
{ color: staticColor, setColorFunction: setStaticPixel });
}
oldData = ctx.getImageData(0, 0, width, height).data;
const convertionRate = colorChange;
const convertedData = new Uint8ClampedArray(pixelArrayLen).fill(255);
for(let i = 0; i < data.length; i+=4) {
convertedData[i] = lerp(prevFrame[i], data[i], convertionRate);
convertedData[i+1] = lerp(prevFrame[i+1], data[i+1], convertionRate);
convertedData[i+2] = lerp(prevFrame[i+2], data[i+2], convertionRate);
}
prevFrame = convertedData;
ctx.putImageData(new ImageData(convertedData, width, height), 0, 0);
video.requestVideoFrameCallback(decode);
}