Files
jellyfin-vue/packages/frontend/src/components/Layout/Images/Blurhash/BlurhashCanvas.vue
T

58 lines
1.4 KiB
Vue
Raw Normal View History

2020-09-20 15:02:14 +02:00
<template>
2024-02-22 14:56:06 +01:00
<canvas
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>
<script setup lang="ts">
import { transfer } from 'comlink';
import { shallowRef, watch, useTemplateRef } from 'vue';
2025-04-30 00:36:49 +02:00
import { blurhashDrawer } from '#/plugins/workers';
import { BLURHASH_DEFAULT_HEIGHT, BLURHASH_DEFAULT_WIDTH, BLURHASH_DEFAULT_PUNCH, isDocumentVisible } from '#/store';
/**
* 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.
*/
2025-04-30 00:36:49 +02:00
const { hash,
width = BLURHASH_DEFAULT_WIDTH,
height = BLURHASH_DEFAULT_HEIGHT,
punch = BLURHASH_DEFAULT_PUNCH
} = defineProps<{
hash: string;
width?: number;
height?: number;
punch?: number;
}>();
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;
try {
2025-04-30 00:36:49 +02:00
const offscreen = canvasRef.value.transferControlToOffscreen();
await blurhashDrawer.draw(transfer(
{ canvas: offscreen,
hash: hash,
width: width,
2025-04-30 00:36:49 +02:00
height: height,
punch: punch
}, [offscreen]));
} catch {
2024-02-22 14:56:06 +01:00
error.value = true;
}
2020-09-20 15:02:14 +02:00
}
});
</script>