-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworker.js
More file actions
112 lines (112 loc) · 3.39 KB
/
Copy pathworker.js
File metadata and controls
112 lines (112 loc) · 3.39 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
const context = {};
const distance = (fx, fy, sx, sy) => { // compute distance between pixels
return Math.sqrt(Math.pow(sy - fy, 2) + Math.pow(sx - fx, 2));
}
const withinRadius = (px, py, shorePixel) => { // compute if shorePixel is within the radius of any of the toDo pixels
if (distance(px, py, shorePixel[0], shorePixel[1]) <= context.radius) {
return true;
}
for (let i = 0; i < context.shore.length; i++) {
if (context.shore[i][0] == shorePixel[0] && context.shore[i][1] == shorePixel[1]) {
continue;
}
if (distance(px, py, context.shore[i][0], context.shore[i][1]) <= context.radius) {
return true;
}
}
return false;
}
const getPixel = (x, y) => {
const start = (y * context.width + x) * 4;
return context.pixels.slice(start, start + 4);
}
const putPixel = (x, y) => {
const start = (y * context.width + x) * 4;
context.pixels[start] = context.pixel[0];
context.pixels[start + 1] = context.pixel[1];
context.pixels[start + 2] = context.pixel[2];
context.pixels[start + 3] = context.pixel[3];
}
const isBlank = (x, y) => {
const pixel = getPixel(x, y);
if (Math.abs(context.blank[0] - pixel[0]) <= (context.threshold[0] || context.threshold) &&
Math.abs(context.blank[1] - pixel[1]) <= (context.threshold[1] || context.threshold) &&
Math.abs(context.blank[2] - pixel[2]) <= (context.threshold[2] || context.threshold) &&
Math.abs(context.blank[3] - pixel[3]) <= (context.threshold[3] || context.threshold)) {
return true;
}
return false;
}
const doNeighbor = (px, py, withinImage, shorePixel) => {
const label = `${px}|${py}`;
if (withinImage && isBlank(px, py)) {
if (withinRadius(px, py, shorePixel)) {
putPixel(px, py);
context.filled.push([px, py]);
context.toDoNext.push([px, py]);
}
else {
context.edge.push([px, py]);
}
}
}
const parseNeighbors = (x, y, shorePixel) => {
let px = x;
let py = y - 1;
doNeighbor(px, py, py >= 0, shorePixel);
px = x + 1;
py = y;
doNeighbor(px, py, px <= context.width, shorePixel);
px = x;
py = y + 1;
doNeighbor(px, py, py <= context.height, shorePixel);
px = x - 1;
py = y;
doNeighbor(px, py, px >= 0, shorePixel);
}
const doShorePixel = (shorePixel) => {
for (let i = 0; i < context.toDo.length; i++) {
parseNeighbors(context.toDo[i][0], context.toDo[i][1], shorePixel);
}
context.toDo = context.toDoNext; // new shore line
context.toDoNext = [];
}
const parseShore = () => {
for (let i = 0; i < context.shore.length; i++) {
context.toDo = [context.shore[i]];
while (context.toDo.length) {
doShorePixel(context.shore[i]);
}
}
}
const init = (input) => {
const keys = Object.keys(input);
for (let i = 0; i < keys.length; i++) {
context[keys[i]] = input[keys[i]];
}
}
onmessage = (message) => {
switch (message.data.type) {
case 'init':
init(message.data.input);
postMessage({status: 'initDone'});
break;
case 'work':
context.toDoNext = [];
context.edge = [];
context.filled = [];
init(message.data.input);
parseShore();
postMessage({
status: 'done',
output: {
workerIndex: context.workerIndex, // worker index
frameIndex: context.frameIndex,
worked: context.shore.length,
filled: context.filled, // pixels to be filled in main thread
nextShore: context.edge
}
});
break;
}
}