2020-07-19 00:24:07 +09:00
|
|
|
<template>
|
2023-05-28 12:24:38 +09:00
|
|
|
<canvas
|
|
|
|
v-if="!loaded"
|
|
|
|
ref="canvas"
|
|
|
|
:width="size"
|
|
|
|
:height="size"
|
|
|
|
:title="title"
|
|
|
|
/>
|
|
|
|
<img
|
|
|
|
v-if="src"
|
|
|
|
:src="src"
|
|
|
|
:title="title"
|
|
|
|
:type="type"
|
|
|
|
:alt="alt"
|
|
|
|
:class="{ cover }"
|
|
|
|
:style="{ 'object-fit': cover ? 'cover' : null }"
|
|
|
|
loading="lazy"
|
|
|
|
@load="onLoad"
|
|
|
|
/>
|
2020-07-19 00:24:07 +09:00
|
|
|
</template>
|
|
|
|
|
2022-01-16 06:59:35 +09:00
|
|
|
<script lang="ts" setup>
|
2023-04-08 09:01:42 +09:00
|
|
|
import { onMounted } from "vue";
|
|
|
|
import { decode } from "blurhash";
|
2020-07-19 00:24:07 +09:00
|
|
|
|
2023-04-08 09:01:42 +09:00
|
|
|
const props = withDefaults(
|
|
|
|
defineProps<{
|
|
|
|
src?: string | null;
|
|
|
|
hash?: string;
|
|
|
|
alt?: string;
|
|
|
|
type?: string | null;
|
|
|
|
title?: string | null;
|
|
|
|
size?: number;
|
|
|
|
cover?: boolean;
|
|
|
|
}>(),
|
|
|
|
{
|
|
|
|
src: null,
|
|
|
|
type: null,
|
|
|
|
alt: "",
|
|
|
|
title: null,
|
|
|
|
size: 64,
|
|
|
|
cover: true,
|
|
|
|
}
|
|
|
|
);
|
2020-07-19 00:24:07 +09:00
|
|
|
|
2022-01-16 06:59:35 +09:00
|
|
|
const canvas = $ref<HTMLCanvasElement>();
|
|
|
|
let loaded = $ref(false);
|
2020-07-19 00:24:07 +09:00
|
|
|
|
2022-01-16 06:59:35 +09:00
|
|
|
function draw() {
|
|
|
|
if (props.hash == null) return;
|
|
|
|
const pixels = decode(props.hash, props.size, props.size);
|
2023-04-08 09:01:42 +09:00
|
|
|
const ctx = canvas.getContext("2d");
|
2022-01-16 06:59:35 +09:00
|
|
|
const imageData = ctx!.createImageData(props.size, props.size);
|
|
|
|
imageData.data.set(pixels);
|
|
|
|
ctx!.putImageData(imageData, 0, 0);
|
|
|
|
}
|
2020-07-19 00:24:07 +09:00
|
|
|
|
2022-01-16 06:59:35 +09:00
|
|
|
function onLoad() {
|
|
|
|
loaded = true;
|
|
|
|
}
|
2020-07-19 00:24:07 +09:00
|
|
|
|
2022-01-16 06:59:35 +09:00
|
|
|
onMounted(() => {
|
|
|
|
draw();
|
2020-07-19 00:24:07 +09:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
2023-05-28 12:24:38 +09:00
|
|
|
canvas,
|
|
|
|
img {
|
|
|
|
display: block;
|
2020-07-19 00:24:07 +09:00
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
2023-05-28 12:24:38 +09:00
|
|
|
}
|
2020-07-19 00:24:07 +09:00
|
|
|
|
2023-05-28 12:24:38 +09:00
|
|
|
canvas {
|
|
|
|
position: absolute;
|
|
|
|
inset: 0;
|
|
|
|
object-fit: cover;
|
|
|
|
}
|
2020-10-17 20:12:00 +09:00
|
|
|
|
2023-05-28 12:24:38 +09:00
|
|
|
img {
|
|
|
|
object-fit: contain;
|
2020-07-19 00:24:07 +09:00
|
|
|
}
|
|
|
|
</style>
|