2020-07-18 18:24:07 +03:00
|
|
|
<template>
|
2020-10-17 14:12:00 +03:00
|
|
|
<div class="xubzgfgb" :class="{ cover }" :title="title">
|
2021-11-19 12:36:12 +02:00
|
|
|
<canvas v-if="!loaded" ref="canvas" :width="size" :height="size" :title="title"/>
|
2020-07-18 18:24:07 +03:00
|
|
|
<img v-if="src" :src="src" :title="title" :alt="alt" @load="onLoad"/>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2022-01-15 23:59:35 +02:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import { onMounted } 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;
|
2022-01-15 23:59:35 +02:00
|
|
|
alt?: string;
|
|
|
|
title?: string | null;
|
|
|
|
size?: number;
|
|
|
|
cover?: boolean;
|
|
|
|
}>(), {
|
|
|
|
src: null,
|
|
|
|
alt: '',
|
|
|
|
title: null,
|
|
|
|
size: 64,
|
|
|
|
cover: true,
|
|
|
|
});
|
2020-07-18 18:24:07 +03:00
|
|
|
|
2022-01-15 23:59:35 +02:00
|
|
|
const canvas = $ref<HTMLCanvasElement>();
|
|
|
|
let loaded = $ref(false);
|
2020-07-18 18:24:07 +03:00
|
|
|
|
2022-01-15 23:59:35 +02: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 18:24:07 +03:00
|
|
|
|
2022-01-15 23:59:35 +02:00
|
|
|
function onLoad() {
|
|
|
|
loaded = true;
|
|
|
|
}
|
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>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
.xubzgfgb {
|
2021-04-16 06:06:54 +03:00
|
|
|
position: relative;
|
2020-07-18 18:24:07 +03:00
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
|
|
|
|
> canvas,
|
|
|
|
> img {
|
2020-10-17 14:12:00 +03:00
|
|
|
display: block;
|
2020-07-18 18:24:07 +03:00
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
2020-10-17 14:12:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
> canvas {
|
2021-04-16 06:06:54 +03:00
|
|
|
position: absolute;
|
2020-07-18 18:24:07 +03:00
|
|
|
object-fit: cover;
|
|
|
|
}
|
2020-10-17 14:12:00 +03:00
|
|
|
|
|
|
|
> img {
|
|
|
|
object-fit: contain;
|
|
|
|
}
|
|
|
|
|
|
|
|
&.cover {
|
|
|
|
> img {
|
|
|
|
object-fit: cover;
|
|
|
|
}
|
|
|
|
}
|
2020-07-18 18:24:07 +03:00
|
|
|
}
|
|
|
|
</style>
|