From 0be288907da7e0dc5946ac20706f2ad02d88f25b Mon Sep 17 00:00:00 2001 From: tamaina Date: Sun, 7 May 2023 10:35:31 +0000 Subject: [PATCH] wip --- .../src/components/MkGalleryPostPreview.vue | 10 ++-- .../src/components/MkImgWithBlurhash.vue | 53 ++++++++----------- .../frontend/src/workers/draw-blurhash.ts | 16 ++---- 3 files changed, 31 insertions(+), 48 deletions(-) diff --git a/packages/frontend/src/components/MkGalleryPostPreview.vue b/packages/frontend/src/components/MkGalleryPostPreview.vue index d694dcce35..8be9dd25a2 100644 --- a/packages/frontend/src/components/MkGalleryPostPreview.vue +++ b/packages/frontend/src/components/MkGalleryPostPreview.vue @@ -54,20 +54,16 @@ function leaveHover(): void { diff --git a/packages/frontend/src/workers/draw-blurhash.ts b/packages/frontend/src/workers/draw-blurhash.ts index cbf8d39d72..e85303fed4 100644 --- a/packages/frontend/src/workers/draw-blurhash.ts +++ b/packages/frontend/src/workers/draw-blurhash.ts @@ -1,24 +1,18 @@ import { decode } from 'blurhash'; -let canvas: OffscreenCanvas | null = null; - -onmessage = (event) => { - if ('canvas' in event.data) { - canvas = event.data.canvas; - } - if (!canvas) { - console.error('Cannot draw blurhash without canvas', canvas, event.data); - throw new Error('Cannot draw blurhash without canvas'); - } +onmessage = async (event) => { if (!('hash' in event.data && typeof event.data.hash === 'string')) { return; } const width = event.data.width ?? 64; const height = event.data.height ?? 64; + const canvas = new OffscreenCanvas(width, height); 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); + ctx.putImageData(imageData, 0, 0); + const bitmap = canvas.transferToImageBitmap(); + postMessage(bitmap, [bitmap]); };