This commit is contained in:
tamaina 2023-05-17 07:49:55 +00:00
parent 0801e79361
commit 05b729ef91

View file

@ -114,20 +114,6 @@ watch([() => props.width, () => props.height, root], () => {
immediate: true, immediate: true,
}); });
workerPromise.then(worker => {
if (!worker) return;
worker.postMessage({
id: viewId,
hash: props.hash,
});
worker.addEventListener('message', event => {
if (event.data.id !== viewId) return;
drawImage(event.data.bitmap as ImageBitmap);
});
});
function drawImage(bitmap: CanvasImageSource) { function drawImage(bitmap: CanvasImageSource) {
// canvasmountedTmp // canvasmountedTmp
if (!canvas.value) { if (!canvas.value) {
@ -165,6 +151,19 @@ async function draw() {
} }
} }
function workerOnMessage(event: MessageEvent) {
if (event.data.id !== viewId) return;
drawImage(event.data.bitmap as ImageBitmap);
}
workerPromise.then(worker => {
if (worker) {
worker.addEventListener('message', workerOnMessage);
}
draw();
});
watch(() => props.src, () => { watch(() => props.src, () => {
waitForDecode(); waitForDecode();
}); });
@ -181,7 +180,11 @@ onMounted(() => {
}); });
onUnmounted(() => { onUnmounted(() => {
workerPromise.then(worker => worker?.postMessage!({ id: viewId, delete: true })); workerPromise.then(worker => {
if (worker) {
worker.removeEventListener('message', workerOnMessage);
}
});
}); });
</script> </script>