This commit is contained in:
tamaina 2023-05-07 10:35:31 +00:00
parent f2914a1bd0
commit 0be288907d
3 changed files with 31 additions and 48 deletions

View File

@ -54,20 +54,16 @@ function leaveHover(): void {
</script> </script>
<style lang="scss" module> <style lang="scss" module>
.transition_toggle_enterActive,
.transition_toggle_leaveActive { .transition_toggle_leaveActive {
transition: opacity .5s; transition: opacity .5s;
position: absolute;
top: 0;
left: 0;
} }
.transition_toggle_enterFrom,
.transition_toggle_leaveTo { .transition_toggle_leaveTo {
opacity: 0; opacity: 0;
} }
//.transition_toggle_enterTo,
//.transition_toggle_leaveFrom {
// opacity: 1;
//}
</style> </style>
<style lang="scss" scoped> <style lang="scss" scoped>

View File

@ -1,17 +1,17 @@
<template> <template>
<div :class="[$style.root, { [$style.cover]: cover }]" :title="title ?? ''"> <div :class="[$style.root, { [$style.cover]: cover }]" :title="title ?? ''">
<canvas ref="canvas" :class="$style.canvas" :width="canvasWidth" :height="canvasHeight" :title="title ?? undefined"/> <TransitionGroup
<Transition
:duration="defaultStore.state.animation && props.transition?.duration || undefined" :duration="defaultStore.state.animation && props.transition?.duration || undefined"
:enter-active-class="defaultStore.state.animation && props.transition?.enterActiveClass || undefined" :enter-active-class="defaultStore.state.animation && props.transition?.enterActiveClass || undefined"
:leave-active-class="defaultStore.state.animation && props.transition?.leaveActiveClass || undefined" :leave-active-class="defaultStore.state.animation && (props.transition?.leaveActiveClass ?? $style['transition_leaveActive']) || undefined"
:enter-from-class="defaultStore.state.animation && props.transition?.enterFromClass || undefined" :enter-from-class="defaultStore.state.animation && props.transition?.enterFromClass || undefined"
:leave-to-class="defaultStore.state.animation && props.transition?.leaveToClass || undefined" :leave-to-class="defaultStore.state.animation && props.transition?.leaveToClass || undefined"
:enter-to-class="defaultStore.state.animation && props.transition?.enterToClass || undefined" :enter-to-class="defaultStore.state.animation && props.transition?.enterToClass || undefined"
:leave-from-class="defaultStore.state.animation && props.transition?.leaveFromClass || undefined" :leave-from-class="defaultStore.state.animation && props.transition?.leaveFromClass || undefined"
> >
<img v-show="loaded && !forceBlurhash" :height="height" :width="width" :class="$style.img" :src="src ?? undefined" :title="title ?? undefined" :alt="alt ?? undefined" loading="eager" @load="onLoad"/> <canvas v-show="hide" key="canvas" ref="canvas" :class="$style.canvas" :width="canvasWidth" :height="canvasHeight" :title="title ?? undefined"/>
</Transition> <img v-show="!hide" key="img" :height="height" :width="width" :class="$style.img" :src="src ?? undefined" :title="title ?? undefined" :alt="alt ?? undefined" loading="eager" @load="onLoad"/>
</TransitionGroup>
</div> </div>
</template> </template>
@ -20,7 +20,7 @@ import { computed, onMounted, onUnmounted, shallowRef, useCssModule, watch } fro
import { defaultStore } from '@/store'; import { defaultStore } from '@/store';
import DrawBlurhash from '@/workers/draw-blurhash?worker'; import DrawBlurhash from '@/workers/draw-blurhash?worker';
let worker: Worker; const worker = new DrawBlurhash();
const $style = useCssModule(); const $style = useCssModule();
@ -54,17 +54,10 @@ const props = withDefaults(defineProps<{
}); });
const canvas = shallowRef<HTMLCanvasElement>(); const canvas = shallowRef<HTMLCanvasElement>();
const offscreen = computed(() => {
if (!canvas.value) return;
const _offscreen = canvas.value.transferControlToOffscreen();
worker.postMessage({
canvas: _offscreen,
}, [_offscreen]);
return _offscreen;
});
let loaded = $ref(false); let loaded = $ref(false);
let canvasWidth = $ref(props.width); let canvasWidth = $ref(props.width);
let canvasHeight = $ref(props.height); let canvasHeight = $ref(props.height);
const hide = computed(() => !loaded || props.forceBlurhash);
function onLoad() { function onLoad() {
loaded = true; loaded = true;
@ -84,7 +77,7 @@ watch([() => props.width, () => props.height], () => {
}); });
function draw() { function draw() {
if (props.hash == null || !offscreen.value) return; if (props.hash == null) return;
worker.postMessage({ worker.postMessage({
hash: props.hash, hash: props.hash,
width: canvasWidth, width: canvasWidth,
@ -92,22 +85,20 @@ function draw() {
}); });
} }
watch([() => props.hash, offscreen], () => { worker.addEventListener('message', event => {
if (!canvas.value) return;
const ctx = canvas.value.getContext('2d');
if (!ctx) return;
const bitmap = event.data as ImageBitmap;
ctx.drawImage(bitmap, 0, 0, canvasWidth, canvasHeight);
});
watch(() => props.hash, () => {
draw(); draw();
}); });
onMounted(() => { onMounted(() => {
worker = new DrawBlurhash();
if (props.forceBlurhash) {
draw(); draw();
} else {
// 100msblurhash
setTimeout(() => {
if (!loaded) {
draw();
}
}, 100);
}
}); });
onUnmounted(() => { onUnmounted(() => {
@ -116,6 +107,11 @@ onUnmounted(() => {
</script> </script>
<style lang="scss" module> <style lang="scss" module>
.transition_leaveActive {
position: absolute;
top: 0;
left: 0;
}
.root { .root {
position: relative; position: relative;
width: 100%; width: 100%;
@ -142,8 +138,5 @@ onUnmounted(() => {
.img { .img {
object-fit: contain; object-fit: contain;
position: absolute;
top: 0;
left: 0;
} }
</style> </style>

View File

@ -1,24 +1,18 @@
import { decode } from 'blurhash'; import { decode } from 'blurhash';
let canvas: OffscreenCanvas | null = null; onmessage = async (event) => {
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');
}
if (!('hash' in event.data && typeof event.data.hash === 'string')) { if (!('hash' in event.data && typeof event.data.hash === 'string')) {
return; return;
} }
const width = event.data.width ?? 64; const width = event.data.width ?? 64;
const height = event.data.height ?? 64; const height = event.data.height ?? 64;
const canvas = new OffscreenCanvas(width, height);
const ctx = canvas.getContext!('2d'); const ctx = canvas.getContext!('2d');
if (!ctx) return; if (!ctx) return;
const imageData = ctx.createImageData(width, height); const imageData = ctx.createImageData(width, height);
const pixels = decode(event.data.hash, width, height); const pixels = decode(event.data.hash, width, height);
imageData.data.set(pixels); imageData.data.set(pixels);
ctx!.putImageData(imageData, 0, 0); ctx.putImageData(imageData, 0, 0);
const bitmap = canvas.transferToImageBitmap();
postMessage(bitmap, [bitmap]);
}; };