This commit is contained in:
20
packages/frontend/src/workers/draw-blurhash.ts
Normal file
20
packages/frontend/src/workers/draw-blurhash.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { decode } from 'blurhash';
|
||||
|
||||
let canvas: OffscreenCanvas | null = null;
|
||||
|
||||
onmessage = (event) => {
|
||||
if ('canvas' in event.data) {
|
||||
canvas = event.data.canvas;
|
||||
}
|
||||
if (!(canvas && 'hash' in event.data && typeof event.data.hash === 'string')) {
|
||||
return;
|
||||
}
|
||||
const width = event.data.width ?? 64;
|
||||
const height = event.data.height ?? 64;
|
||||
const ctx = canvas.getContext!('2d');
|
||||
if (!ctx) return;
|
||||
const imageData = ctx.createImageData(width, height);
|
||||
const pixels = decode(event.data.hash, width, height);
|
||||
imageData.data.set(pixels);
|
||||
ctx!.putImageData(imageData, 0, 0);
|
||||
};
|
Reference in New Issue
Block a user