-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathencode_web_workers.html
292 lines (242 loc) · 8.7 KB
/
encode_web_workers.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>gif-encoder</title>
<style>
body {
font-family: sans-serif;
padding: 20px;
}
a {
color: tomato;
}
</style>
</head>
<body>
<p>
A fast GIF encoder in JavaScript, see
<a href="https://github.com/mattdesl/gifenc">mattdesl/gifenc</a>.
</p>
<p>
This demo uses Worker Modules so it may only work in Chrome. This could be
resolved with a bundler like Parcel or Webpack.
</p>
<div id="progress"></div>
<canvas></canvas>
<script type="module">
import { GIFEncoder, quantize, applyPalette } from "/src/index.js";
const workerUrl = "/test/worker.js";
encode();
function download(buf, filename, type) {
const blob = buf instanceof Blob ? buf : new Blob([buf], { type });
const url = URL.createObjectURL(blob);
const anchor = document.createElement("a");
anchor.href = url;
anchor.download = filename;
anchor.click();
}
async function encode() {
const canvas = document.querySelector("canvas");
const context = canvas.getContext("2d");
const width = 1024;
const height = 1024;
canvas.width = width;
canvas.height = height;
canvas.style.width = "250px";
canvas.style.height = "auto";
const fps = 30;
const duration = 5;
const totalFrames = Math.ceil(duration * fps);
const fpsInterval = 1 / fps;
const delay = fpsInterval * 1000;
const render = sketch();
const frames = new Array(totalFrames).fill(0).map((_, i) => i);
console.time("encode");
// Setup an encoder that we will write frames into
const encoder = await createWorkerEncoder({
totalFrames,
fps,
width,
height,
progress(v) {
const el = document.querySelector("#progress");
el.textContent = `Progress: ${Math.round(v * 100)}%`;
},
});
// We use for 'of' to loop with async await
for (let i of frames) {
// a t value 0..1 to animate the frame
const playhead = i / totalFrames;
// Render to 2D context
render({ context, width, height, playhead });
// Get RGBA data from canvas
const data = context.getImageData(0, 0, width, height).data;
// Send data to worker
encoder.addFrame(data, i);
// Wait a tick so that we don't lock up browser
await new Promise((resolve) => setTimeout(resolve, 0));
}
// Now the main thread is available again for UI/rendering work...
console.log("Waiting for workers...");
// Get the resulting buffer
const buffer = await encoder.finish();
console.timeEnd("encode");
download(buffer, "animation.gif", { type: "image/gif" });
}
async function createWorkerEncoder(opts = {}) {
const {
progress = () => {},
totalFrames,
width,
height,
fps = 30,
maxColors = 256,
workerCount = 4,
format = "rgb444",
} = opts;
if (!totalFrames) throw new Error("Must specify totalFrames > 0");
if (!width || !height)
throw new Error("Must specify { width, height }");
const workers = new Array(workerCount).fill(null).map(() => {
// Worker modules are Chrome-only at the time of writing
return new Worker(workerUrl, { type: "module" });
});
const frames = new Array(totalFrames).fill(null);
const fpsInterval = 1 / fps;
const delay = fpsInterval * 1000;
let remaining = totalFrames;
let workerIndex = 0;
// First we send encoder options to each worker
workers.forEach((w) => {
w.postMessage({
event: "init",
repeat: 0,
delay,
maxColors,
width,
height,
});
});
// Then we wait for all workers to be ready
await Promise.all(workers.map((w) => waitForReady(w)));
const gif = GIFEncoder({ auto: false });
return {
addFrame(data, frame) {
// We cycle through all workers uniformly
const worker = workers[workerIndex++ % workers.length];
// Send data to worker
worker.postMessage([data, frame], [data.buffer]);
frames[frame] = waitForFrame(worker, frame).then((ev) => {
const [data, frame] = ev.data;
nextProgress();
return data;
});
},
async finish() {
// Once all chunks are ready
const chunks = await Promise.all(frames);
// Write the header first
gif.writeHeader();
// Now we can write each chunk
for (let i = 0; i < chunks.length; i++) {
gif.stream.writeBytesView(chunks[i]);
}
// Finish the GIF
gif.finish();
// Close workers
workers.forEach((w) => w.terminate());
workers.length = 0;
// Return bytes
return gif.bytesView();
},
};
function nextProgress() {
remaining--;
const p = (totalFrames - remaining) / totalFrames;
progress(p);
}
function waitForReady(worker) {
return new Promise((resolve) => {
const handler = (ev) => {
if (ev.data === "ready") {
worker.removeEventListener("message", handler);
resolve(ev);
}
};
worker.addEventListener("message", handler, { passive: true });
});
}
function waitForFrame(worker, frame) {
return new Promise((resolve) => {
const handler = (ev) => {
if (ev.data[1] === frame) {
worker.removeEventListener("message", handler);
resolve(ev);
}
};
worker.addEventListener("message", handler, { passive: true });
});
}
}
// This could be replaced with your own generative artwork...
function sketch() {
return ({ context, width, height, playhead }) => {
context.clearRect(0, 0, width, height);
context.fillStyle = "white";
context.fillRect(0, 0, width, height);
const gridSize = 7;
const padding = width * 0.2;
const tileSize = (width - padding * 2) / gridSize;
for (let x = 0; x < gridSize; x++) {
for (let y = 0; y < gridSize; y++) {
// get a 0..1 UV coordinate
const u = gridSize <= 1 ? 0.5 : x / (gridSize - 1);
const v = gridSize <= 1 ? 0.5 : y / (gridSize - 1);
// scale to dimensions with a border padding
const tx = lerp(padding, width - padding, u);
const ty = lerp(padding, height - padding, v);
// here we get a 't' value between 0..1 that
// shifts subtly across the UV coordinates
const offset = u * 0.2 + v * 0.1;
const t = (playhead + offset) % 1;
// now we get a value that varies from 0..1 and back
let mod = Math.sin(t * Math.PI);
// we make it 'ease' a bit more dramatically with exponential
mod = Math.pow(mod, 3);
// now choose a length, thickness and initial rotation
const length = tileSize * 0.65;
const thickness = tileSize * 0.1;
const initialRotation = Math.PI / 2;
// And rotate each line a bit by our modifier
const rotation = initialRotation + mod * Math.PI;
context.fillStyle = `hsl(0, ${v * 100}%, 50%)`;
// Now render...
draw(context, tx, ty, length, thickness, rotation);
}
}
};
function lerp(min, max, t) {
return min * (1 - t) + max * t;
}
function draw(context, x, y, length, thickness, rotation) {
context.save();
// Rotate in place
context.translate(x, y);
context.rotate(rotation);
context.translate(-x, -y);
// Draw the line
context.fillRect(
x - length / 2,
y - thickness / 2,
length,
thickness
);
context.restore();
}
}
</script>
</body>
</html>