2020-07-18 18:24:07 +03:00
|
|
|
<template>
|
2023-04-15 15:35:19 +03:00
|
|
|
<div :class="[$style.root, { [$style.cover]: cover }]" :title="title ?? ''">
|
|
|
|
<canvas v-if="!loaded || forceBlurhash" ref="canvas" :class="$style.canvas" :width="width" :height="height" :title="title ?? ''"/>
|
|
|
|
<img v-if="src && !forceBlurhash" v-show="loaded" :class="$style.img" :src="src" :title="title ?? ''" :alt="alt ?? ''" @load="onLoad"/>
|
2020-07-18 18:24:07 +03:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2022-01-15 23:59:35 +02:00
|
|
|
<script lang="ts" setup>
|
2023-04-15 15:35:19 +03:00
|
|
|
import { onMounted, watch } from 'vue';
|
2020-07-18 18:24:07 +03:00
|
|
|
import { decode } from 'blurhash';
|
|
|
|
|
2022-01-15 23:59:35 +02:00
|
|
|
const props = withDefaults(defineProps<{
|
|
|
|
src?: string | null;
|
2022-07-05 16:25:34 +03:00
|
|
|
hash?: string;
|
2023-04-15 09:29:57 +03:00
|
|
|
alt?: string | null;
|
2022-01-15 23:59:35 +02:00
|
|
|
title?: string | null;
|
2023-04-15 15:35:19 +03:00
|
|
|
height?: number;
|
|
|
|
width?: number;
|
2022-01-15 23:59:35 +02:00
|
|
|
cover?: boolean;
|
2023-04-15 09:29:57 +03:00
|
|
|
forceBlurhash?: boolean;
|
2022-01-15 23:59:35 +02:00
|
|
|
}>(), {
|
|
|
|
src: null,
|
|
|
|
alt: '',
|
|
|
|
title: null,
|
2023-04-15 15:35:19 +03:00
|
|
|
height: 64,
|
|
|
|
width: 64,
|
2022-01-15 23:59:35 +02:00
|
|
|
cover: true,
|
2023-04-15 09:29:57 +03:00
|
|
|
forceBlurhash: false,
|
2022-01-15 23:59:35 +02:00
|
|
|
});
|
2020-07-18 18:24:07 +03:00
|
|
|
|
2023-01-03 03:12:37 +02:00
|
|
|
const canvas = $shallowRef<HTMLCanvasElement>();
|
2022-01-15 23:59:35 +02:00
|
|
|
let loaded = $ref(false);
|
2023-04-15 15:35:19 +03:00
|
|
|
let width = $ref(props.width);
|
|
|
|
let height = $ref(props.height);
|
|
|
|
|
|
|
|
function onLoad() {
|
|
|
|
loaded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
watch([() => props.width, () => props.height], () => {
|
|
|
|
const ratio = props.width / props.height;
|
|
|
|
if (ratio > 1) {
|
|
|
|
width = Math.round(64 * ratio);
|
|
|
|
height = 64;
|
|
|
|
} else {
|
|
|
|
width = 64;
|
|
|
|
height = Math.round(64 / ratio);
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
immediate: true,
|
|
|
|
});
|
2020-07-18 18:24:07 +03:00
|
|
|
|
2022-01-15 23:59:35 +02:00
|
|
|
function draw() {
|
|
|
|
if (props.hash == null) return;
|
2023-04-15 15:35:19 +03:00
|
|
|
const pixels = decode(props.hash, width, height);
|
2022-01-15 23:59:35 +02:00
|
|
|
const ctx = canvas.getContext('2d');
|
2023-04-15 15:35:19 +03:00
|
|
|
const imageData = ctx!.createImageData(width, height);
|
2022-01-15 23:59:35 +02:00
|
|
|
imageData.data.set(pixels);
|
|
|
|
ctx!.putImageData(imageData, 0, 0);
|
|
|
|
}
|
2020-07-18 18:24:07 +03:00
|
|
|
|
2023-04-15 15:35:19 +03:00
|
|
|
watch(() => props.hash, () => {
|
|
|
|
draw();
|
|
|
|
});
|
2020-07-18 18:24:07 +03:00
|
|
|
|
2022-01-15 23:59:35 +02:00
|
|
|
onMounted(() => {
|
|
|
|
draw();
|
2020-07-18 18:24:07 +03:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2023-01-14 13:31:48 +02:00
|
|
|
<style lang="scss" module>
|
|
|
|
.root {
|
2021-04-16 06:06:54 +03:00
|
|
|
position: relative;
|
2020-07-18 18:24:07 +03:00
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
&.cover {
|
2023-04-15 15:35:19 +03:00
|
|
|
> .canvas,
|
2023-01-14 13:31:48 +02:00
|
|
|
> .img {
|
2020-10-17 14:12:00 +03:00
|
|
|
object-fit: cover;
|
|
|
|
}
|
|
|
|
}
|
2020-07-18 18:24:07 +03:00
|
|
|
}
|
2023-01-14 13:31:48 +02:00
|
|
|
|
|
|
|
.canvas,
|
|
|
|
.img {
|
|
|
|
display: block;
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
}
|
|
|
|
|
|
|
|
.canvas {
|
2023-04-15 15:35:19 +03:00
|
|
|
object-fit: contain;
|
2023-01-14 13:31:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
.img {
|
|
|
|
object-fit: contain;
|
|
|
|
}
|
2020-07-18 18:24:07 +03:00
|
|
|
</style>
|