This commit is contained in:
2021-03-10 15:26:55 +01:00
parent db706e08f5
commit 2a94207c73
8 changed files with 138 additions and 20 deletions

View File

@@ -1,3 +1,5 @@
import { bufToImageUrl } from ".";
const worker = new Worker("build/ai-worker.js");
let i = 0;
@@ -6,14 +8,33 @@ let cb = {};
worker.addEventListener("message", ev => {
if (ev.data.i in cb) {
cb[ev.data.i](ev.data.res);
cb[ev.data.i](ev.data.result);
delete cb[ev.data.i]
delete ev.data.i;
}
})
const analyze = (img: Image) => new Promise((res, rej) => {
i++;
const _i = i;
worker.postMessage({ i: _i, pixels: img.data, width: img.width, height: img.height });
const canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
const cx = canvas.getContext("2d");
const image = document.createElement("img");
image.onload = () => {
cx.drawImage(image, 0, 0);
const pixels = cx.getImageData(0, 0, image.width, image.height).data
worker.postMessage({ i: _i, pixels, width: img.width, height: img.height });
}
image.src = bufToImageUrl(img.data, img.type);
cb[_i] = res;
});

View File

@@ -3,6 +3,5 @@ const urlCreator = window.URL || window.webkitURL;
export default (buff: ArrayBuffer, mimeType: string) => {
const blob = new Blob([buff], { type: mimeType });
const imageUrl = urlCreator.createObjectURL(blob);
return imageUrl;
}