second day
This commit is contained in:
8
view/src/helpers/BuffToImg.ts
Normal file
8
view/src/helpers/BuffToImg.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
|
||||
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;
|
||||
}
|
18
view/src/helpers/CountPixels.ts
Normal file
18
view/src/helpers/CountPixels.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
const worker = new Worker("worker.js");
|
||||
|
||||
let i = 0;
|
||||
|
||||
let cb = {};
|
||||
|
||||
worker.addEventListener("message", ev => {
|
||||
if (ev.data.i in cb) {
|
||||
cb[ev.data.i](ev.data.res);
|
||||
}
|
||||
})
|
||||
|
||||
export default (arr: ArrayBuffer) => new Promise((res, rej) => {
|
||||
i++;
|
||||
const _i = i;
|
||||
worker.postMessage({ i: _i, arr });
|
||||
cb[_i] = res;
|
||||
});
|
25
view/src/helpers/FileToImage.ts
Normal file
25
view/src/helpers/FileToImage.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import bufToImageUrl from "./BuffToImg";
|
||||
|
||||
export default (f: File): Promise<Image> => new Promise(async (res, rej) => {
|
||||
|
||||
const arr = await f.arrayBuffer()
|
||||
|
||||
const img = document.createElement("img");
|
||||
img.src = bufToImageUrl(arr, f.type);
|
||||
|
||||
img.onload = async () => {
|
||||
res({
|
||||
id: 0,
|
||||
width: img.width,
|
||||
height: img.height,
|
||||
name: f.name,
|
||||
filename: f.name,
|
||||
type: f.type,
|
||||
colors: [],
|
||||
overlayData: new ArrayBuffer(0),
|
||||
lastModified: f.lastModified,
|
||||
data: arr
|
||||
});
|
||||
};
|
||||
|
||||
});
|
3
view/src/helpers/index.ts
Normal file
3
view/src/helpers/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export { default as bufToImageUrl } from "./BuffToImg";
|
||||
export { default as fileToImage } from "./FileToImage";
|
||||
export { default as countPixels } from "./CountPixels";
|
Reference in New Issue
Block a user