2023-07-27 05:31:52 +00:00
|
|
|
|
<!--
|
2024-02-13 15:59:27 +00:00
|
|
|
|
SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 05:31:52 +00:00
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
-->
|
|
|
|
|
|
2020-07-18 15:24:07 +00:00
|
|
|
|
<template>
|
2023-06-01 14:27:58 +00:00
|
|
|
|
<div ref="root" :class="['chromatic-ignore', $style.root, { [$style.cover]: cover }]" :title="title ?? ''">
|
2023-05-19 00:44:06 +00:00
|
|
|
|
<TransitionGroup
|
|
|
|
|
:duration="defaultStore.state.animation && props.transition?.duration || undefined"
|
2023-05-19 04:58:09 +00:00
|
|
|
|
:enterActiveClass="defaultStore.state.animation && props.transition?.enterActiveClass || undefined"
|
2023-06-01 08:19:46 +00:00
|
|
|
|
:leaveActiveClass="defaultStore.state.animation && (props.transition?.leaveActiveClass ?? $style.transition_leaveActive) || undefined"
|
2023-05-19 04:58:09 +00:00
|
|
|
|
: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 13:57:46 +00:00
|
|
|
|
>
|
2024-07-12 07:25:44 +00:00
|
|
|
|
<canvas v-show="hide" key="canvas" ref="canvas" :class="$style.canvas" :width="canvasWidth" :height="canvasHeight" :title="title ?? undefined" tabindex="-1"/>
|
|
|
|
|
<img v-show="!hide" key="img" ref="img" :height="imgHeight ?? undefined" :width="imgWidth ?? undefined" :class="$style.img" :src="src ?? undefined" :title="title ?? undefined" :alt="alt ?? undefined" loading="eager" decoding="async" tabindex="-1"/>
|
2023-05-19 00:44:06 +00:00
|
|
|
|
</TransitionGroup>
|
2020-07-18 15:24:07 +00:00
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
2023-05-19 00:44:06 +00:00
|
|
|
|
<script lang="ts">
|
|
|
|
|
import DrawBlurhash from '@/workers/draw-blurhash?worker';
|
|
|
|
|
import TestWebGL2 from '@/workers/test-webgl2?worker';
|
2023-09-19 07:37:43 +00:00
|
|
|
|
import { WorkerMultiDispatch } from '@/scripts/worker-multi-dispatch.js';
|
|
|
|
|
import { extractAvgColorFromBlurhash } from '@/scripts/extract-avg-color-from-blurhash.js';
|
2023-05-19 00:44:06 +00:00
|
|
|
|
|
2023-07-02 04:46:49 +00:00
|
|
|
|
const canvasPromise = new Promise<WorkerMultiDispatch | HTMLCanvasElement>(resolve => {
|
2023-06-01 08:50:26 +00:00
|
|
|
|
// テスト環境で Web Worker インスタンスは作成できない
|
|
|
|
|
if (import.meta.env.MODE === 'test') {
|
2023-07-02 04:46:49 +00:00
|
|
|
|
const canvas = document.createElement('canvas');
|
|
|
|
|
canvas.width = 64;
|
|
|
|
|
canvas.height = 64;
|
|
|
|
|
resolve(canvas);
|
2023-06-01 08:50:26 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-05-19 00:44:06 +00:00
|
|
|
|
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 {
|
2023-07-02 04:46:49 +00:00
|
|
|
|
const canvas = document.createElement('canvas');
|
|
|
|
|
canvas.width = 64;
|
|
|
|
|
canvas.height = 64;
|
|
|
|
|
resolve(canvas);
|
2023-05-19 00:44:06 +00:00
|
|
|
|
if (_DEV_) console.log('WebGL2 in worker is not supported...');
|
|
|
|
|
}
|
|
|
|
|
testWorker.terminate();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
</script>
|
|
|
|
|
|
2022-01-15 21:59:35 +00:00
|
|
|
|
<script lang="ts" setup>
|
2023-12-07 05:42:09 +00:00
|
|
|
|
import { computed, nextTick, onMounted, onUnmounted, shallowRef, watch, ref } from 'vue';
|
2023-05-19 00:44:06 +00:00
|
|
|
|
import { v4 as uuid } from 'uuid';
|
|
|
|
|
import { render } from 'buraha';
|
2023-09-19 07:37:43 +00:00
|
|
|
|
import { defaultStore } from '@/store.js';
|
2020-07-18 15:24:07 +00:00
|
|
|
|
|
2022-01-15 21:59:35 +00:00
|
|
|
|
const props = withDefaults(defineProps<{
|
2023-04-29 13:57:46 +00:00
|
|
|
|
transition?: {
|
2023-05-19 00:44:06 +00:00
|
|
|
|
duration?: number | { enter: number; leave: number; };
|
2023-04-29 13:57:46 +00:00
|
|
|
|
enterActiveClass?: string;
|
|
|
|
|
leaveActiveClass?: string;
|
|
|
|
|
enterFromClass?: string;
|
|
|
|
|
leaveToClass?: string;
|
|
|
|
|
enterToClass?: string;
|
|
|
|
|
leaveFromClass?: string;
|
|
|
|
|
} | null;
|
2022-01-15 21:59:35 +00:00
|
|
|
|
src?: string | null;
|
2024-01-30 10:53:53 +00:00
|
|
|
|
hash?: string | null;
|
2023-04-15 06:29:57 +00:00
|
|
|
|
alt?: string | null;
|
2022-01-15 21:59:35 +00:00
|
|
|
|
title?: string | null;
|
2023-04-15 12:35:19 +00:00
|
|
|
|
height?: number;
|
|
|
|
|
width?: number;
|
2022-01-15 21:59:35 +00:00
|
|
|
|
cover?: boolean;
|
2023-04-15 06:29:57 +00:00
|
|
|
|
forceBlurhash?: boolean;
|
2023-07-02 04:46:49 +00:00
|
|
|
|
onlyAvgColor?: boolean; // 軽量化のためにBlurhashを使わずに平均色だけを描画
|
2022-01-15 21:59:35 +00:00
|
|
|
|
}>(), {
|
2023-04-29 13:57:46 +00:00
|
|
|
|
transition: null,
|
2022-01-15 21:59:35 +00:00
|
|
|
|
src: null,
|
|
|
|
|
alt: '',
|
|
|
|
|
title: null,
|
2023-04-15 12:35:19 +00:00
|
|
|
|
height: 64,
|
|
|
|
|
width: 64,
|
2022-01-15 21:59:35 +00:00
|
|
|
|
cover: true,
|
2023-04-15 06:29:57 +00:00
|
|
|
|
forceBlurhash: false,
|
2023-07-02 04:46:49 +00:00
|
|
|
|
onlyAvgColor: false,
|
2022-01-15 21:59:35 +00:00
|
|
|
|
});
|
2020-07-18 15:24:07 +00:00
|
|
|
|
|
2023-05-19 00:44:06 +00:00
|
|
|
|
const viewId = uuid();
|
2023-04-29 13:57:46 +00:00
|
|
|
|
const canvas = shallowRef<HTMLCanvasElement>();
|
2023-05-19 00:44:06 +00:00
|
|
|
|
const root = shallowRef<HTMLDivElement>();
|
|
|
|
|
const img = shallowRef<HTMLImageElement>();
|
2023-12-07 05:42:09 +00:00
|
|
|
|
const loaded = ref(false);
|
|
|
|
|
const canvasWidth = ref(64);
|
|
|
|
|
const canvasHeight = ref(64);
|
|
|
|
|
const imgWidth = ref(props.width);
|
|
|
|
|
const imgHeight = ref(props.height);
|
|
|
|
|
const bitmapTmp = ref<CanvasImageSource | undefined>();
|
|
|
|
|
const hide = computed(() => !loaded.value || props.forceBlurhash);
|
2023-05-19 00:44:06 +00:00
|
|
|
|
|
|
|
|
|
function waitForDecode() {
|
|
|
|
|
if (props.src != null && props.src !== '') {
|
|
|
|
|
nextTick()
|
|
|
|
|
.then(() => img.value?.decode())
|
|
|
|
|
.then(() => {
|
2023-12-07 05:42:09 +00:00
|
|
|
|
loaded.value = true;
|
2023-05-19 00:44:06 +00:00
|
|
|
|
}, error => {
|
2023-07-24 06:05:37 +00:00
|
|
|
|
console.log('Error occurred during decoding image', img.value, error);
|
2023-05-19 00:44:06 +00:00
|
|
|
|
});
|
|
|
|
|
} else {
|
2023-12-07 05:42:09 +00:00
|
|
|
|
loaded.value = false;
|
2023-05-19 00:44:06 +00:00
|
|
|
|
}
|
2023-04-15 12:35:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 00:44:06 +00:00
|
|
|
|
watch([() => props.width, () => props.height, root], () => {
|
2023-04-15 12:35:19 +00:00
|
|
|
|
const ratio = props.width / props.height;
|
|
|
|
|
if (ratio > 1) {
|
2023-12-07 05:42:09 +00:00
|
|
|
|
canvasWidth.value = Math.round(64 * ratio);
|
|
|
|
|
canvasHeight.value = 64;
|
2023-04-15 12:35:19 +00:00
|
|
|
|
} else {
|
2023-12-07 05:42:09 +00:00
|
|
|
|
canvasWidth.value = 64;
|
|
|
|
|
canvasHeight.value = Math.round(64 / ratio);
|
2023-04-15 12:35:19 +00:00
|
|
|
|
}
|
2023-05-19 00:44:06 +00:00
|
|
|
|
|
|
|
|
|
const clientWidth = root.value?.clientWidth ?? 300;
|
2023-12-07 05:42:09 +00:00
|
|
|
|
imgWidth.value = clientWidth;
|
|
|
|
|
imgHeight.value = Math.round(clientWidth / ratio);
|
2023-04-15 12:35:19 +00:00
|
|
|
|
}, {
|
|
|
|
|
immediate: true,
|
|
|
|
|
});
|
2020-07-18 15:24:07 +00:00
|
|
|
|
|
2023-05-19 00:44:06 +00:00
|
|
|
|
function drawImage(bitmap: CanvasImageSource) {
|
|
|
|
|
// canvasがない(mountedされていない)場合はTmpに保存しておく
|
|
|
|
|
if (!canvas.value) {
|
2023-12-07 05:42:09 +00:00
|
|
|
|
bitmapTmp.value = bitmap;
|
2023-05-19 00:44:06 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// canvasがあれば描画する
|
2023-12-07 05:42:09 +00:00
|
|
|
|
bitmapTmp.value = undefined;
|
2023-05-19 00:44:06 +00:00
|
|
|
|
const ctx = canvas.value.getContext('2d');
|
|
|
|
|
if (!ctx) return;
|
2023-12-07 05:42:09 +00:00
|
|
|
|
ctx.drawImage(bitmap, 0, 0, canvasWidth.value, canvasHeight.value);
|
2023-05-19 00:44:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-02 04:46:49 +00:00
|
|
|
|
function drawAvg() {
|
2024-07-19 01:11:44 +00:00
|
|
|
|
if (!canvas.value) return;
|
|
|
|
|
|
|
|
|
|
const color = (props.hash != null && extractAvgColorFromBlurhash(props.hash)) || '#888';
|
2023-05-19 00:44:06 +00:00
|
|
|
|
|
2023-04-29 13:57:46 +00:00
|
|
|
|
const ctx = canvas.value.getContext('2d');
|
2023-05-19 00:44:06 +00:00
|
|
|
|
if (!ctx) return;
|
|
|
|
|
|
|
|
|
|
// avgColorでお茶をにごす
|
|
|
|
|
ctx.beginPath();
|
2024-07-19 01:11:44 +00:00
|
|
|
|
ctx.fillStyle = color;
|
2023-12-07 05:42:09 +00:00
|
|
|
|
ctx.fillRect(0, 0, canvasWidth.value, canvasHeight.value);
|
2023-07-02 04:46:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function draw() {
|
2024-07-19 01:11:44 +00:00
|
|
|
|
if (import.meta.env.MODE === 'test' && props.hash == null) return;
|
2023-05-19 00:44:06 +00:00
|
|
|
|
|
2023-07-02 04:46:49 +00:00
|
|
|
|
drawAvg();
|
|
|
|
|
|
2024-07-19 01:11:44 +00:00
|
|
|
|
if (props.hash == null) return;
|
|
|
|
|
|
2023-07-02 04:46:49 +00:00
|
|
|
|
if (props.onlyAvgColor) return;
|
|
|
|
|
|
|
|
|
|
const work = await canvasPromise;
|
|
|
|
|
if (work instanceof WorkerMultiDispatch) {
|
|
|
|
|
work.postMessage(
|
2023-05-19 00:44:06 +00:00
|
|
|
|
{
|
|
|
|
|
id: viewId,
|
|
|
|
|
hash: props.hash,
|
|
|
|
|
},
|
|
|
|
|
undefined,
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
render(props.hash, work);
|
2023-07-02 04:46:49 +00:00
|
|
|
|
drawImage(work);
|
2023-05-19 00:44:06 +00:00
|
|
|
|
} catch (error) {
|
2023-07-06 00:19:10 +00:00
|
|
|
|
console.error('Error occurred during drawing blurhash', error);
|
2023-05-19 00:44:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function workerOnMessage(event: MessageEvent) {
|
|
|
|
|
if (event.data.id !== viewId) return;
|
|
|
|
|
drawImage(event.data.bitmap as ImageBitmap);
|
2022-01-15 21:59:35 +00:00
|
|
|
|
}
|
2020-07-18 15:24:07 +00:00
|
|
|
|
|
2023-07-02 04:46:49 +00:00
|
|
|
|
canvasPromise.then(work => {
|
|
|
|
|
if (work instanceof WorkerMultiDispatch) {
|
|
|
|
|
work.addListener(workerOnMessage);
|
2023-05-19 00:44:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-15 12:35:19 +00:00
|
|
|
|
draw();
|
|
|
|
|
});
|
2020-07-18 15:24:07 +00:00
|
|
|
|
|
2023-05-19 00:44:06 +00:00
|
|
|
|
watch(() => props.src, () => {
|
|
|
|
|
waitForDecode();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
watch(() => props.hash, () => {
|
2022-01-15 21:59:35 +00:00
|
|
|
|
draw();
|
2020-07-18 15:24:07 +00:00
|
|
|
|
});
|
|
|
|
|
|
2023-05-19 00:44:06 +00:00
|
|
|
|
onMounted(() => {
|
|
|
|
|
// drawImageがmountedより先に呼ばれている場合はここで描画する
|
2023-12-07 05:42:09 +00:00
|
|
|
|
if (bitmapTmp.value) {
|
|
|
|
|
drawImage(bitmapTmp.value);
|
2023-05-19 00:44:06 +00:00
|
|
|
|
}
|
|
|
|
|
waitForDecode();
|
|
|
|
|
});
|
2023-04-29 13:57:46 +00:00
|
|
|
|
|
2023-05-19 00:44:06 +00:00
|
|
|
|
onUnmounted(() => {
|
2023-07-02 04:46:49 +00:00
|
|
|
|
canvasPromise.then(work => {
|
|
|
|
|
if (work instanceof WorkerMultiDispatch) {
|
|
|
|
|
work.removeListener(workerOnMessage);
|
|
|
|
|
}
|
2023-05-19 00:44:06 +00:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
</script>
|
2023-04-28 16:39:04 +00:00
|
|
|
|
|
2023-05-19 00:44:06 +00:00
|
|
|
|
<style lang="scss" module>
|
|
|
|
|
.transition_leaveActive {
|
2023-04-29 13:57:46 +00:00
|
|
|
|
position: absolute;
|
|
|
|
|
top: 0;
|
|
|
|
|
left: 0;
|
|
|
|
|
}
|
2023-01-14 11:31:48 +00:00
|
|
|
|
.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-04-15 12:35:19 +00:00
|
|
|
|
> .canvas,
|
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 {
|
2023-04-15 12:35:19 +00:00
|
|
|
|
object-fit: contain;
|
2023-01-14 11:31:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.img {
|
|
|
|
|
object-fit: contain;
|
|
|
|
|
}
|
2020-07-18 15:24:07 +00:00
|
|
|
|
</style>
|