admin管理员组文章数量:1401469
Webworkers can fetch regular images through XMLHttpRequest
, right? How can the workers then draw these images to OffscreenCanvas? Probably want to use XMLHttp.responseType = 'blob'
?
Another way would be to set the src
of an image element and then transfer the element to worker, but my workers always reject such images.
Thanks
Webworkers can fetch regular images through XMLHttpRequest
, right? How can the workers then draw these images to OffscreenCanvas? Probably want to use XMLHttp.responseType = 'blob'
?
Another way would be to set the src
of an image element and then transfer the element to worker, but my workers always reject such images.
Thanks
Share Improve this question asked Jun 12, 2019 at 0:42 ituyituy 2414 silver badges11 bronze badges1 Answer
Reset to default 7The ImageBitmap API is here for this purpose (among others).
Note: this demo will currently run only on Chrome...
const offcanvas = canvas.transferControlToOffscreen();
const worker = new Worker(getWorkerURL());
worker.onmessage = e => console.log(e.data);
worker.postMessage(offcanvas, [offcanvas]);
function getWorkerURL() {
return URL.createObjectURL(
new Blob([
worker_script.textContent
])
);
}
<canvas id="canvas" height="450"></canvas>
<script id="worker_script" type="ws">
onmessage = async (evt) => {
try {
const canvas = evt.data;
const ctx = canvas.getContext('2d');
if(!ctx) {
postMessage('unsupported browser');
return;
}
const imgblob = await fetch('https://upload.wikimedia/wikipedia/mons/5/55/John_William_Waterhouse_A_Mermaid.jpg')
.then(r => r.blob());
const img = await createImageBitmap(imgblob);
ctx.drawImage(img, 0,0, canvas.width, canvas.height);
}
catch(e) {
postMessage('unsupported browser');
throw e;
}
};
</script>
本文标签: javascriptWebworker OffscreenCanvas draw regular imageStack Overflow
版权声明:本文标题:javascript - Webworker OffscreenCanvas draw regular image - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744237086a2596605.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论