2020-07-18 17:24:07 +02:00
|
|
|
|
<template>
|
2023-05-19 02:44:06 +02:00
|
|
|
|
<div ref="root" :class="[$style.root, { [$style.cover]: cover }]" :title="title ?? ''">
|
|
|
|
|
<TransitionGroup
|
|
|
|
|
:duration="defaultStore.state.animation && props.transition?.duration || undefined"
|
2023-05-19 06:58:09 +02:00
|
|
|
|
:enterActiveClass="defaultStore.state.animation && props.transition?.enterActiveClass || undefined"
|
|
|
|
|
:leaveActiveClass="defaultStore.state.animation && (props.transition?.leaveActiveClass ?? $style['transition_leaveActive']) || undefined"
|
|
|
|
|
:enterFromClass="defaultStore.state.animation && props.transition?.enterFromClass || undefined"
|
|
|
|
|
:leaveToClass="defaultStore.state.animation && props.transition?.leaveToClass || undefined"
|
|
|
|
|
:enterToClass="defaultStore.state.animation && props.transition?.enterToClass || undefined"
|
|
|
|
|
:leaveFromClass="defaultStore.state.animation && props.transition?.leaveFromClass || undefined"
|
2023-04-29 15:57:46 +02:00
|
|
|
|
>
|
2023-05-19 02:44:06 +02:00
|
|
|
|
<canvas v-show="hide" key="canvas" ref="canvas" :class="$style.canvas" :width="canvasWidth" :height="canvasHeight" :title="title ?? undefined"/>
|
|
|
|
|
<img v-show="!hide" key="img" ref="img" :height="imgHeight" :width="imgWidth" :class="$style.img" :src="src ?? undefined" :title="title ?? undefined" :alt="alt ?? undefined" loading="eager" decoding="async"/>
|
|
|
|
|
</TransitionGroup>
|
2020-07-18 17:24:07 +02:00
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
2023-05-19 02:44:06 +02:00
|
|
|
|
<script lang="ts">
|
2023-05-19 06:58:09 +02:00
|
|
|
|
import { $ref } from 'vue/macros';
|
2023-05-19 02:44:06 +02:00
|
|
|
|
import DrawBlurhash from '@/workers/draw-blurhash?worker';
|
|
|
|
|
import TestWebGL2 from '@/workers/test-webgl2?worker';
|
|
|
|
|
import { WorkerMultiDispatch } from '@/scripts/worker-multi-dispatch';
|
|
|
|
|
import { extractAvgColorFromBlurhash } from '@/scripts/extract-avg-color-from-blurhash';
|
|
|
|
|
|
|
|
|
|
const workerPromise = new Promise<WorkerMultiDispatch | null>(resolve => {
|
|
|
|
|
const testWorker = new TestWebGL2();
|
|
|
|
|
testWorker.addEventListener('message', event => {
|
|
|
|
|
if (event.data.result) {
|
|
|
|
|
const workers = new WorkerMultiDispatch(
|
|
|
|
|
() => new DrawBlurhash(),
|
|
|
|
|
Math.min(navigator.hardwareConcurrency - 1, 4),
|
|
|
|
|
);
|
|
|
|
|
resolve(workers);
|
|
|
|
|
if (_DEV_) console.log('WebGL2 in worker is supported!');
|
|
|
|
|
} else {
|
|
|
|
|
resolve(null);
|
|
|
|
|
if (_DEV_) console.log('WebGL2 in worker is not supported...');
|
|
|
|
|
}
|
|
|
|
|
testWorker.terminate();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
</script>
|
|
|
|
|
|
2022-01-15 22:59:35 +01:00
|
|
|
|
<script lang="ts" setup>
|
2023-05-19 02:44:06 +02:00
|
|
|
|
import { computed, nextTick, onMounted, onUnmounted, shallowRef, useCssModule, watch } from 'vue';
|
|
|
|
|
import { v4 as uuid } from 'uuid';
|
|
|
|
|
import { render } from 'buraha';
|
2023-04-29 15:57:46 +02:00
|
|
|
|
import { defaultStore } from '@/store';
|
|
|
|
|
const $style = useCssModule();
|
2020-07-18 17:24:07 +02:00
|
|
|
|
|
2022-01-15 22:59:35 +01:00
|
|
|
|
const props = withDefaults(defineProps<{
|
2023-04-29 15:57:46 +02:00
|
|
|
|
transition?: {
|
2023-05-19 02:44:06 +02:00
|
|
|
|
duration?: number | { enter: number; leave: number; };
|
2023-04-29 15:57:46 +02:00
|
|
|
|
enterActiveClass?: string;
|
|
|
|
|
leaveActiveClass?: string;
|
|
|
|
|
enterFromClass?: string;
|
|
|
|
|
leaveToClass?: string;
|
|
|
|
|
enterToClass?: string;
|
|
|
|
|
leaveFromClass?: string;
|
|
|
|
|
} | null;
|
2022-01-15 22:59:35 +01:00
|
|
|
|
src?: string | null;
|
2022-07-05 15:25:34 +02:00
|
|
|
|
hash?: string;
|
2023-04-15 08:29:57 +02:00
|
|
|
|
alt?: string | null;
|
2022-01-15 22:59:35 +01:00
|
|
|
|
title?: string | null;
|
2023-04-15 14:35:19 +02:00
|
|
|
|
height?: number;
|
|
|
|
|
width?: number;
|
2022-01-15 22:59:35 +01:00
|
|
|
|
cover?: boolean;
|
2023-04-15 08:29:57 +02:00
|
|
|
|
forceBlurhash?: boolean;
|
2022-01-15 22:59:35 +01:00
|
|
|
|
}>(), {
|
2023-04-29 15:57:46 +02:00
|
|
|
|
transition: null,
|
2022-01-15 22:59:35 +01:00
|
|
|
|
src: null,
|
|
|
|
|
alt: '',
|
|
|
|
|
title: null,
|
2023-04-15 14:35:19 +02:00
|
|
|
|
height: 64,
|
|
|
|
|
width: 64,
|
2022-01-15 22:59:35 +01:00
|
|
|
|
cover: true,
|
2023-04-15 08:29:57 +02:00
|
|
|
|
forceBlurhash: false,
|
2022-01-15 22:59:35 +01:00
|
|
|
|
});
|
2020-07-18 17:24:07 +02:00
|
|
|
|
|
2023-05-19 02:44:06 +02:00
|
|
|
|
const viewId = uuid();
|
2023-04-29 15:57:46 +02:00
|
|
|
|
const canvas = shallowRef<HTMLCanvasElement>();
|
2023-05-19 02:44:06 +02:00
|
|
|
|
const root = shallowRef<HTMLDivElement>();
|
|
|
|
|
const img = shallowRef<HTMLImageElement>();
|
2022-01-15 22:59:35 +01:00
|
|
|
|
let loaded = $ref(false);
|
2023-05-19 02:44:06 +02:00
|
|
|
|
let canvasWidth = $ref(64);
|
|
|
|
|
let canvasHeight = $ref(64);
|
|
|
|
|
let imgWidth = $ref(props.width);
|
|
|
|
|
let imgHeight = $ref(props.height);
|
|
|
|
|
let bitmapTmp = $ref<CanvasImageSource | undefined>();
|
|
|
|
|
const hide = computed(() => !loaded || props.forceBlurhash);
|
|
|
|
|
|
|
|
|
|
function waitForDecode() {
|
|
|
|
|
if (props.src != null && props.src !== '') {
|
|
|
|
|
nextTick()
|
|
|
|
|
.then(() => img.value?.decode())
|
|
|
|
|
.then(() => {
|
|
|
|
|
loaded = true;
|
|
|
|
|
}, error => {
|
|
|
|
|
console.error('Error occured during decoding image', img.value, error);
|
|
|
|
|
throw Error(error);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
loaded = false;
|
|
|
|
|
}
|
2023-04-15 14:35:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 02:44:06 +02:00
|
|
|
|
watch([() => props.width, () => props.height, root], () => {
|
2023-04-15 14:35:19 +02:00
|
|
|
|
const ratio = props.width / props.height;
|
|
|
|
|
if (ratio > 1) {
|
2023-05-19 02:44:06 +02:00
|
|
|
|
canvasWidth = Math.round(64 * ratio);
|
|
|
|
|
canvasHeight = 64;
|
2023-04-15 14:35:19 +02:00
|
|
|
|
} else {
|
2023-05-19 02:44:06 +02:00
|
|
|
|
canvasWidth = 64;
|
|
|
|
|
canvasHeight = Math.round(64 / ratio);
|
2023-04-15 14:35:19 +02:00
|
|
|
|
}
|
2023-05-19 02:44:06 +02:00
|
|
|
|
|
|
|
|
|
const clientWidth = root.value?.clientWidth ?? 300;
|
|
|
|
|
imgWidth = clientWidth;
|
|
|
|
|
imgHeight = Math.round(clientWidth / ratio);
|
2023-04-15 14:35:19 +02:00
|
|
|
|
}, {
|
|
|
|
|
immediate: true,
|
|
|
|
|
});
|
2020-07-18 17:24:07 +02:00
|
|
|
|
|
2023-05-19 02:44:06 +02:00
|
|
|
|
function drawImage(bitmap: CanvasImageSource) {
|
|
|
|
|
// canvasがない(mountedされていない)場合はTmpに保存しておく
|
|
|
|
|
if (!canvas.value) {
|
|
|
|
|
bitmapTmp = bitmap;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// canvasがあれば描画する
|
|
|
|
|
bitmapTmp = undefined;
|
|
|
|
|
const ctx = canvas.value.getContext('2d');
|
|
|
|
|
if (!ctx) return;
|
|
|
|
|
ctx.drawImage(bitmap, 0, 0, canvasWidth, canvasHeight);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function draw() {
|
|
|
|
|
if (!canvas.value || props.hash == null) return;
|
|
|
|
|
|
2023-04-29 15:57:46 +02:00
|
|
|
|
const ctx = canvas.value.getContext('2d');
|
2023-05-19 02:44:06 +02:00
|
|
|
|
if (!ctx) return;
|
|
|
|
|
|
|
|
|
|
// avgColorでお茶をにごす
|
|
|
|
|
ctx.beginPath();
|
|
|
|
|
ctx.fillStyle = extractAvgColorFromBlurhash(props.hash) ?? '#888';
|
|
|
|
|
ctx.fillRect(0, 0, canvasWidth, canvasHeight);
|
|
|
|
|
|
|
|
|
|
const workers = await workerPromise;
|
|
|
|
|
if (workers) {
|
|
|
|
|
workers.postMessage(
|
|
|
|
|
{
|
|
|
|
|
id: viewId,
|
|
|
|
|
hash: props.hash,
|
|
|
|
|
width: canvasWidth,
|
|
|
|
|
height: canvasHeight,
|
|
|
|
|
},
|
|
|
|
|
undefined,
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
const work = document.createElement('canvas');
|
|
|
|
|
work.width = canvasWidth;
|
|
|
|
|
work.height = canvasHeight;
|
|
|
|
|
render(props.hash, work);
|
|
|
|
|
ctx.drawImage(work, 0, 0, canvasWidth, canvasHeight);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error occured during drawing blurhash', error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function workerOnMessage(event: MessageEvent) {
|
|
|
|
|
if (event.data.id !== viewId) return;
|
|
|
|
|
drawImage(event.data.bitmap as ImageBitmap);
|
2022-01-15 22:59:35 +01:00
|
|
|
|
}
|
2020-07-18 17:24:07 +02:00
|
|
|
|
|
2023-05-19 02:44:06 +02:00
|
|
|
|
workerPromise.then(worker => {
|
|
|
|
|
if (worker) {
|
|
|
|
|
worker.addListener(workerOnMessage);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-15 14:35:19 +02:00
|
|
|
|
draw();
|
|
|
|
|
});
|
2020-07-18 17:24:07 +02:00
|
|
|
|
|
2023-05-19 02:44:06 +02:00
|
|
|
|
watch(() => props.src, () => {
|
|
|
|
|
waitForDecode();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
watch(() => props.hash, () => {
|
2022-01-15 22:59:35 +01:00
|
|
|
|
draw();
|
2020-07-18 17:24:07 +02:00
|
|
|
|
});
|
|
|
|
|
|
2023-05-19 02:44:06 +02:00
|
|
|
|
onMounted(() => {
|
|
|
|
|
// drawImageがmountedより先に呼ばれている場合はここで描画する
|
|
|
|
|
if (bitmapTmp) {
|
|
|
|
|
drawImage(bitmapTmp);
|
|
|
|
|
}
|
|
|
|
|
waitForDecode();
|
|
|
|
|
});
|
2023-04-29 15:57:46 +02:00
|
|
|
|
|
2023-05-19 02:44:06 +02:00
|
|
|
|
onUnmounted(() => {
|
|
|
|
|
workerPromise.then(worker => {
|
|
|
|
|
worker?.removeListener(workerOnMessage);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
</script>
|
2023-04-28 18:39:04 +02:00
|
|
|
|
|
2023-05-19 02:44:06 +02:00
|
|
|
|
<style lang="scss" module>
|
|
|
|
|
.transition_leaveActive {
|
2023-04-29 15:57:46 +02:00
|
|
|
|
position: absolute;
|
|
|
|
|
top: 0;
|
|
|
|
|
left: 0;
|
|
|
|
|
}
|
2023-01-14 12:31:48 +01:00
|
|
|
|
.root {
|
2021-04-16 05:06:54 +02:00
|
|
|
|
position: relative;
|
2020-07-18 17:24:07 +02:00
|
|
|
|
width: 100%;
|
|
|
|
|
height: 100%;
|
|
|
|
|
|
2020-10-17 13:12:00 +02:00
|
|
|
|
&.cover {
|
2023-04-15 14:35:19 +02:00
|
|
|
|
> .canvas,
|
2023-01-14 12:31:48 +01:00
|
|
|
|
> .img {
|
2020-10-17 13:12:00 +02:00
|
|
|
|
object-fit: cover;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-18 17:24:07 +02:00
|
|
|
|
}
|
2023-01-14 12:31:48 +01:00
|
|
|
|
|
|
|
|
|
.canvas,
|
|
|
|
|
.img {
|
|
|
|
|
display: block;
|
|
|
|
|
width: 100%;
|
|
|
|
|
height: 100%;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.canvas {
|
2023-04-15 14:35:19 +02:00
|
|
|
|
object-fit: contain;
|
2023-01-14 12:31:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.img {
|
|
|
|
|
object-fit: contain;
|
|
|
|
|
}
|
2020-07-18 17:24:07 +02:00
|
|
|
|
</style>
|