2020-09-20 15:02:14 +02:00
|
|
|
<template>
|
2024-02-22 14:56:06 +01:00
|
|
|
<canvas
|
2024-12-03 13:30:00 +01:00
|
|
|
v-if="isDocumentVisible && !error"
|
2024-02-22 14:56:06 +01:00
|
|
|
ref="canvas"
|
|
|
|
|
v-bind="$attrs"
|
|
|
|
|
:key="`canvas-${hash}`"
|
|
|
|
|
:width="width"
|
|
|
|
|
:height="height" />
|
|
|
|
|
<slot v-else />
|
2020-09-20 15:02:14 +02:00
|
|
|
</template>
|
|
|
|
|
|
2022-11-29 22:03:26 +01:00
|
|
|
<script setup lang="ts">
|
2024-07-11 22:57:03 +02:00
|
|
|
import { transfer } from 'comlink';
|
2024-09-04 18:15:42 +02:00
|
|
|
import { shallowRef, watch, useTemplateRef } from 'vue';
|
2025-04-30 00:36:49 +02:00
|
|
|
import { blurhashDrawer } from '#/plugins/workers';
|
2024-12-29 18:31:07 +00:00
|
|
|
import { BLURHASH_DEFAULT_HEIGHT, BLURHASH_DEFAULT_WIDTH, BLURHASH_DEFAULT_PUNCH, isDocumentVisible } from '#/store';
|
2024-12-03 13:30:00 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Browsers stop canvases when the page is out of view (for example, minimised or in a background tab).
|
|
|
|
|
* We unmount the canvas with `isDocumentVisible` to free resources and ensure the correct appearance when the
|
|
|
|
|
* page is restored.
|
|
|
|
|
*/
|
2024-07-11 22:57:03 +02:00
|
|
|
|
2025-04-30 00:36:49 +02:00
|
|
|
const { hash,
|
|
|
|
|
width = BLURHASH_DEFAULT_WIDTH,
|
|
|
|
|
height = BLURHASH_DEFAULT_HEIGHT,
|
|
|
|
|
punch = BLURHASH_DEFAULT_PUNCH
|
|
|
|
|
} = defineProps<{
|
2024-09-04 20:14:58 +02:00
|
|
|
hash: string;
|
|
|
|
|
width?: number;
|
|
|
|
|
height?: number;
|
|
|
|
|
punch?: number;
|
|
|
|
|
}>();
|
2021-01-11 13:22:14 +01:00
|
|
|
|
2024-02-22 14:56:06 +01:00
|
|
|
const error = shallowRef(false);
|
2025-04-30 00:36:49 +02:00
|
|
|
const canvasRef = useTemplateRef('canvas');
|
|
|
|
|
|
|
|
|
|
watch(canvasRef, async () => {
|
|
|
|
|
if (canvasRef.value) {
|
|
|
|
|
error.value = false;
|
2022-12-02 13:46:26 +01:00
|
|
|
|
2022-11-29 22:03:26 +01:00
|
|
|
try {
|
2025-04-30 00:36:49 +02:00
|
|
|
const offscreen = canvasRef.value.transferControlToOffscreen();
|
|
|
|
|
|
|
|
|
|
await blurhashDrawer.draw(transfer(
|
|
|
|
|
{ canvas: offscreen,
|
|
|
|
|
hash: hash,
|
2024-09-04 20:14:58 +02:00
|
|
|
width: width,
|
2025-04-30 00:36:49 +02:00
|
|
|
height: height,
|
|
|
|
|
punch: punch
|
|
|
|
|
}, [offscreen]));
|
2022-11-29 22:03:26 +01:00
|
|
|
} catch {
|
2024-02-22 14:56:06 +01:00
|
|
|
error.value = true;
|
2022-11-29 22:03:26 +01:00
|
|
|
}
|
2020-09-20 15:02:14 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
</script>
|