生成するworkerを1つにする?
This commit is contained in:
parent
bd48a04fac
commit
6c78c5643c
|
@ -14,14 +14,15 @@
|
||||||
</TransitionGroup>
|
</TransitionGroup>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
<script lang="ts">
|
||||||
<script lang="ts" setup>
|
|
||||||
import { computed, onMounted, onUnmounted, shallowRef, useCssModule, watch } from 'vue';
|
|
||||||
import { defaultStore } from '@/store';
|
|
||||||
import DrawBlurhash from '@/workers/draw-blurhash?worker';
|
import DrawBlurhash from '@/workers/draw-blurhash?worker';
|
||||||
|
|
||||||
const worker = new DrawBlurhash();
|
const worker = new DrawBlurhash();
|
||||||
|
</script>
|
||||||
|
<script lang="ts" setup>
|
||||||
|
import { computed, onMounted, onUnmounted, shallowRef, useCssModule, watch } from 'vue';
|
||||||
|
import { v4 as uuid } from 'uuid';
|
||||||
|
import { defaultStore } from '@/store';
|
||||||
const $style = useCssModule();
|
const $style = useCssModule();
|
||||||
|
|
||||||
const props = withDefaults(defineProps<{
|
const props = withDefaults(defineProps<{
|
||||||
|
@ -57,6 +58,7 @@ const canvas = shallowRef<HTMLCanvasElement>();
|
||||||
let loaded = $ref(false);
|
let loaded = $ref(false);
|
||||||
let canvasWidth = $ref(props.width);
|
let canvasWidth = $ref(props.width);
|
||||||
let canvasHeight = $ref(props.height);
|
let canvasHeight = $ref(props.height);
|
||||||
|
let currentDrawId = $ref<string>();
|
||||||
const hide = computed(() => !loaded || props.forceBlurhash);
|
const hide = computed(() => !loaded || props.forceBlurhash);
|
||||||
|
|
||||||
function onLoad() {
|
function onLoad() {
|
||||||
|
@ -78,7 +80,9 @@ watch([() => props.width, () => props.height], () => {
|
||||||
|
|
||||||
function draw() {
|
function draw() {
|
||||||
if (props.hash == null) return;
|
if (props.hash == null) return;
|
||||||
|
currentDrawId = uuid();
|
||||||
worker.postMessage({
|
worker.postMessage({
|
||||||
|
id: currentDrawId,
|
||||||
hash: props.hash,
|
hash: props.hash,
|
||||||
width: canvasWidth,
|
width: canvasWidth,
|
||||||
height: canvasHeight,
|
height: canvasHeight,
|
||||||
|
@ -87,9 +91,10 @@ function draw() {
|
||||||
|
|
||||||
worker.addEventListener('message', event => {
|
worker.addEventListener('message', event => {
|
||||||
if (!canvas.value) return;
|
if (!canvas.value) return;
|
||||||
|
if (event.data.id !== currentDrawId) return;
|
||||||
const ctx = canvas.value.getContext('2d');
|
const ctx = canvas.value.getContext('2d');
|
||||||
if (!ctx) return;
|
if (!ctx) return;
|
||||||
const bitmap = event.data as ImageBitmap;
|
const bitmap = event.data.bitmap as ImageBitmap;
|
||||||
ctx.drawImage(bitmap, 0, 0, canvasWidth, canvasHeight);
|
ctx.drawImage(bitmap, 0, 0, canvasWidth, canvasHeight);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -100,10 +105,6 @@ watch(() => props.hash, () => {
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
draw();
|
draw();
|
||||||
});
|
});
|
||||||
|
|
||||||
onUnmounted(() => {
|
|
||||||
worker.terminate();
|
|
||||||
});
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss" module>
|
<style lang="scss" module>
|
||||||
|
|
|
@ -14,5 +14,5 @@ onmessage = async (event) => {
|
||||||
imageData.data.set(pixels);
|
imageData.data.set(pixels);
|
||||||
ctx.putImageData(imageData, 0, 0);
|
ctx.putImageData(imageData, 0, 0);
|
||||||
const bitmap = canvas.transferToImageBitmap();
|
const bitmap = canvas.transferToImageBitmap();
|
||||||
postMessage(bitmap);
|
postMessage({ bitmap, id: event.data.id });
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue