2020-07-19 00:24:07 +09:00
|
|
|
<template>
|
2023-04-15 21:35:19 +09:00
|
|
|
<div :class="[$style.root, { [$style.cover]: cover }]" :title="title ?? ''">
|
2023-04-29 22:57:46 +09:00
|
|
|
<img v-if="!loaded && src && !forceBlurhash" :class="$style.loader" :src="src" @load="onLoad"/>
|
|
|
|
<Transition
|
|
|
|
mode="in-out"
|
|
|
|
:enter-active-class="defaultStore.state.animation && (props.transition?.enterActiveClass ?? $style['transition_toggle_enterActive']) || undefined"
|
|
|
|
:leave-active-class="defaultStore.state.animation && (props.transition?.leaveActiveClass ?? $style['transition_toggle_leaveActive']) || undefined"
|
|
|
|
:enter-from-class="defaultStore.state.animation && props.transition?.enterFromClass || undefined"
|
|
|
|
:leave-to-class="defaultStore.state.animation && props.transition?.leaveToClass || undefined"
|
2023-04-29 01:39:04 +09:00
|
|
|
:enter-to-class="defaultStore.state.animation && (props.transition?.enterToClass ?? $style['transition_toggle_enterTo']) || undefined"
|
|
|
|
:leave-from-class="defaultStore.state.animation && (props.transition?.leaveFromClass ?? $style['transition_toggle_leaveFrom']) || undefined"
|
2023-04-29 22:57:46 +09:00
|
|
|
>
|
2023-04-29 01:39:04 +09:00
|
|
|
<canvas v-if="!loaded || forceBlurhash" ref="canvas" :class="$style.canvas" :width="width" :height="height" :title="title ?? undefined"/>
|
2023-04-29 22:57:46 +09:00
|
|
|
<img v-else :class="$style.img" :src="src ?? undefined" :title="title ?? undefined" :alt="alt ?? undefined"/>
|
|
|
|
</Transition>
|
2020-07-19 00:24:07 +09:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2022-01-16 06:59:35 +09:00
|
|
|
<script lang="ts" setup>
|
2023-04-29 22:57:46 +09:00
|
|
|
import { onMounted, shallowRef, useCssModule, watch } from 'vue';
|
2020-07-19 00:24:07 +09:00
|
|
|
import { decode } from 'blurhash';
|
2023-04-29 22:57:46 +09:00
|
|
|
import { defaultStore } from '@/store';
|
|
|
|
|
|
|
|
const $style = useCssModule();
|
2020-07-19 00:24:07 +09:00
|
|
|
|
2022-01-16 06:59:35 +09:00
|
|
|
const props = withDefaults(defineProps<{
|
2023-04-29 22:57:46 +09:00
|
|
|
transition?: {
|
|
|
|
enterActiveClass?: string;
|
|
|
|
leaveActiveClass?: string;
|
|
|
|
enterFromClass?: string;
|
|
|
|
leaveToClass?: string;
|
|
|
|
enterToClass?: string;
|
|
|
|
leaveFromClass?: string;
|
|
|
|
} | null;
|
2022-01-16 06:59:35 +09:00
|
|
|
src?: string | null;
|
2022-07-05 22:25:34 +09:00
|
|
|
hash?: string;
|
2023-04-15 15:29:57 +09:00
|
|
|
alt?: string | null;
|
2022-01-16 06:59:35 +09:00
|
|
|
title?: string | null;
|
2023-04-15 21:35:19 +09:00
|
|
|
height?: number;
|
|
|
|
width?: number;
|
2022-01-16 06:59:35 +09:00
|
|
|
cover?: boolean;
|
2023-04-15 15:29:57 +09:00
|
|
|
forceBlurhash?: boolean;
|
2022-01-16 06:59:35 +09:00
|
|
|
}>(), {
|
2023-04-29 22:57:46 +09:00
|
|
|
transition: null,
|
2022-01-16 06:59:35 +09:00
|
|
|
src: null,
|
|
|
|
alt: '',
|
|
|
|
title: null,
|
2023-04-15 21:35:19 +09:00
|
|
|
height: 64,
|
|
|
|
width: 64,
|
2022-01-16 06:59:35 +09:00
|
|
|
cover: true,
|
2023-04-15 15:29:57 +09:00
|
|
|
forceBlurhash: false,
|
2022-01-16 06:59:35 +09:00
|
|
|
});
|
2020-07-19 00:24:07 +09:00
|
|
|
|
2023-04-29 22:57:46 +09:00
|
|
|
const canvas = shallowRef<HTMLCanvasElement>();
|
2022-01-16 06:59:35 +09:00
|
|
|
let loaded = $ref(false);
|
2023-04-15 21:35:19 +09: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-19 00:24:07 +09:00
|
|
|
|
2022-01-16 06:59:35 +09:00
|
|
|
function draw() {
|
2023-04-29 22:57:46 +09:00
|
|
|
if (props.hash == null || !canvas.value) return;
|
2023-04-15 21:35:19 +09:00
|
|
|
const pixels = decode(props.hash, width, height);
|
2023-04-29 22:57:46 +09:00
|
|
|
const ctx = canvas.value.getContext('2d');
|
2023-04-15 21:35:19 +09:00
|
|
|
const imageData = ctx!.createImageData(width, height);
|
2022-01-16 06:59:35 +09:00
|
|
|
imageData.data.set(pixels);
|
|
|
|
ctx!.putImageData(imageData, 0, 0);
|
|
|
|
}
|
2020-07-19 00:24:07 +09:00
|
|
|
|
2023-04-29 22:57:46 +09:00
|
|
|
watch([() => props.hash, canvas], () => {
|
2023-04-15 21:35:19 +09:00
|
|
|
draw();
|
|
|
|
});
|
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>
|
|
|
|
|
2023-01-14 20:31:48 +09:00
|
|
|
<style lang="scss" module>
|
2023-04-29 22:57:46 +09:00
|
|
|
.transition_toggle_enterActive,
|
|
|
|
.transition_toggle_leaveActive {
|
|
|
|
position: absolute;
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
|
|
|
}
|
|
|
|
|
2023-04-29 01:39:04 +09:00
|
|
|
.transition_toggle_enterTo,
|
|
|
|
.transition_toggle_leaveFrom {
|
|
|
|
opacity: 0;
|
|
|
|
}
|
|
|
|
|
2023-04-29 22:57:46 +09:00
|
|
|
.loader {
|
|
|
|
position: absolute;
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
|
|
|
width: 0;
|
|
|
|
height: 0;
|
|
|
|
}
|
|
|
|
|
2023-01-14 20:31:48 +09:00
|
|
|
.root {
|
2021-04-16 12:06:54 +09:00
|
|
|
position: relative;
|
2020-07-19 00:24:07 +09:00
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
|
2020-10-17 20:12:00 +09:00
|
|
|
&.cover {
|
2023-04-15 21:35:19 +09:00
|
|
|
> .canvas,
|
2023-01-14 20:31:48 +09:00
|
|
|
> .img {
|
2020-10-17 20:12:00 +09:00
|
|
|
object-fit: cover;
|
|
|
|
}
|
|
|
|
}
|
2020-07-19 00:24:07 +09:00
|
|
|
}
|
2023-01-14 20:31:48 +09:00
|
|
|
|
|
|
|
.canvas,
|
|
|
|
.img {
|
|
|
|
display: block;
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
}
|
|
|
|
|
|
|
|
.canvas {
|
2023-04-15 21:35:19 +09:00
|
|
|
object-fit: contain;
|
2023-01-14 20:31:48 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
.img {
|
|
|
|
object-fit: contain;
|
|
|
|
}
|
2020-07-19 00:24:07 +09:00
|
|
|
</style>
|