karl/view/public/worker.js
2021-03-10 01:20:22 +01:00

29 lines
575 B
JavaScript

self.addEventListener('message', function (e) {
const { data: { i, arr: pixels } } = e;
let store = {};
let total = pixels.length / 4;
const threshold = 200;
for (let i = 0; i < total; i++) {
const r = pixels[i * 4 + 0] > threshold ? 1 : 0;
const g = pixels[i * 4 + 1] > threshold ? 1 : 0;
const b = pixels[i * 4 + 2] > threshold ? 1 : 0;
const id = r + "-" + g + "-" + b;
store[id] = store[id] + 1 || 1;
}
Object.keys(store).forEach(k => {
store[k] = store[k] / total;
});
self.postMessage({ res: store, i });
}, false);