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