-
Notifications
You must be signed in to change notification settings - Fork 4
/
fastgif.html
170 lines (149 loc) · 4.61 KB
/
fastgif.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
<!DOCTYPE html>
<script src="https://cdn.rawgit.com/matt-way/gifuct-js/de68974e/dist/gifuct-js.min.js"></script>
<script>
window.gifuctLib = window.GIF;
</script>
<script src="https://cdn.rawgit.com/friendlyanon/gif-engine-js/9c59e055/gif-engine-js.js"></script>
<script>
window.gifEngineLib = GIF; // not on window, defined via 'const'
</script>
<script src="https://cdn.rawgit.com/buzzfeed/libgif-js/0654e7f2/libgif.js"></script>
<script>
function globalError(msg) {
var el = document.createElement('h1');
document.body.appendChild(el);
el.style.color = 'red';
el.textContent = msg;
throw new Error();
}
</script>
<script nomodule>
globalErr('Your browser doesn\'t have modules!')
</script>
<script type="module">
import * as fastgif from './fastgif.js';
fastgif.setDebug();
const opts = ['elves', 'hibiscus', 'large', 'rudolph', 'santa'];
const choice = opts[~~(Math.random() * opts.length)];
const src = `testdata/${choice}.gif`;
if (!window.WebAssembly) {
globalErr('Your browser doesn\'t have WebAssembly!')
}
function fetchToBuffer(url) {
return window.fetch(url).then((response) => response.arrayBuffer());
}
async function render(all, label) {
const canvas = document.createElement('canvas');
if (all.length === 0) {
throw new Error('can\'t play image with no frames');
}
canvas.width = all[0].imageData.width;
canvas.height = all[0].imageData.height;
const heading = document.createElement('h1');
heading.textContent = label;
document.body.appendChild(heading);
document.body.appendChild(canvas);
const ctx = canvas.getContext('2d');
let frame = 0;
while (true) {
ctx.putImageData(all[frame].imageData, 0, 0);
await new Promise((resolve) => window.setTimeout(resolve, all[frame].delay));
if (++frame === all.length) {
frame = 0;
}
}
}
async function run() {
const buf = await fetchToBuffer(src);
sizer.textContent = `gif size: ${buf.byteLength} bytes`;
const times = {};
const log = async (method, fn) => {
const start = performance.now();
const out = await fn();
const duration = performance.now() - start;
times[method] = duration;
return out;
};
// gifengine
const gifEngineFrames = await log('gif-engine-js', async () => {
console.time('gifEngine-new');
const gifEngine = await gifEngineLib(buf);
console.timeEnd('gifEngine-new');
console.time('gifEngine-frames');
const promiseFrames = gifEngine.frames.map((_, i) => gifEngine.toImageData(i));
const out = (await Promise.all(promiseFrames)).map((raw, i) => {
return {
imageData: raw[0],
delay: gifEngine.frames[i].graphicExtension.delay,
};
});
console.timeEnd('gifEngine-frames');
return out;
});
// wasm
const wasmFrames = await log('wasm', async () => {
const wasmDecoder = new fastgif.Decoder();
return await wasmDecoder.decode(buf);
});
sizer.textContent += ` (${wasmFrames.length} frames)`;
// gifuct
const gifuctFrames = await log('gifuct', async () => {
console.time('gifuct-new')
const gifuct = new gifuctLib(buf);
console.timeEnd('gifuct-new')
console.time('gifuct-frames');
const gifuctRaw = gifuct.decompressFrames(true);
const out = gifuctRaw.map((raw) => {
return {
imageData: new ImageData(raw.patch, raw.dims.width, raw.dims.height),
delay: raw.delay,
};
});
console.timeEnd('gifuct-frames');
return out;
});
// SuperGif
await log('supergif', async () => {
const supergif = new SuperGif({gif: supergifHolder, draw_while_loading: false});
console.time('supergif');
await new Promise((resolve) => {
supergif.load_url(src, resolve);
});
const heading = document.createElement('h1');
heading.textContent = 'supergif';
const supergifCanvas = supergif.get_canvas();
supergifCanvas.parentNode.insertBefore(heading, supergifCanvas);
console.timeEnd('supergif');
});
// log stats at top
stats.textContent = '';
for (const key in times) {
const li = document.createElement('li');
const v = new Number(times[key]).toFixed(2);
li.textContent = `${key}: ${v}ms`;
li.style.order = Math.round(times[key] * 100);
stats.appendChild(li);
}
// render all
return Promise.race([
render(wasmFrames, 'wasm'),
render(gifEngineFrames, 'gif-engine-js'),
render(gifuctFrames, 'gifuct'),
]);
}
run().catch((err) => console.error(err));
</script>
<style>
#stats {
display: flex;
flex-flow: column;
list-style: none;
margin: 0;
}
#sizer {
margin-bottom: 1em;
}
</style>
<div id="sizer"></div>
<ul id="stats">Working...</ul>
<span id="supergifHolder"></span>