-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathworker.js
45 lines (38 loc) · 1.1 KB
/
worker.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
import { GIFEncoder, quantize, applyPalette } from "/src/index.js";
let options;
self.addEventListener("message", (ev) => {
const detail = ev.data;
if (detail.event === "init") {
// Upon init, save our options
options = { ...detail };
// And send back a ready event
self.postMessage("ready");
} else {
// Get the options for the encoder
const {
format = "rgb444",
width,
height,
maxColors = 256,
repeat = 0,
delay = 0,
} = options;
// Get the data + index for this frame
const [data, frame] = detail;
// Get palette from this frame
const palette = quantize(data, maxColors, { format });
// Now get an indexed bitmap image
const index = applyPalette(data, palette, format);
// Encode into a single GIF frame chunk
const gif = GIFEncoder({ auto: false });
gif.writeFrame(index, width, height, {
first: frame === 0,
repeat: 0,
delay,
palette,
});
// Send the result back ot main thread
const output = gif.bytesView();
self.postMessage([output, frame], [output.buffer]);
}
});