-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworker.js
36 lines (34 loc) · 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
importScripts( "filters.js");
const filters = {
"none": none,
"grayscale": grayscale,
"brighten": brighten,
"swap": swap
}
self.onmessage = (msg) => {
const data = msg.data;
console.log( "worker.js › Received message", data);
switch( data.op) {
case "sip":
case "crunch":
case "listen": {
self.postMessage( { ...data, result: "ack" });
break;
}
case "filter": {
const { canvas, filter } = data.args;
console.assert( typeof filters[ filter] == "function",
`worker.js › No filter available by the name ${filter}`);
const imageData = data.imageDataByRef;
filters[ filter]( imageData);
self.postMessage(
{ op: data.op, args: data.args, result: imageData },
// This extra-argument specifies the Transferable object
// that will be passed out of the worker with zero-copy
[ imageData.data.buffer ]);
break;
}
default:
throw new Error( `Zapit! Received unknown command ${data.op}`);
}
};