简体   繁体   English

在 web-worker 中将 ImageBitMap/Image 绘制到 OffScreenCanvas

[英]Drawing ImageBitMap/Image to OffScreenCanvas in a web-worker

I have a vue.js application where I set up vue-workers/web-workers to perform some image processing(scaling/cropping) in the background after a user uploads a couple images.我有一个 vue.js 应用程序,我在其中设置了 vue-workers/web-workers 以在用户上传几张图像后在后台执行一些图像处理(缩放/裁剪)。

I decided to go with ImageBitMpas & OffScreenCanvas since Canvas and Image Object is not supported in web workers. I decided to go with ImageBitMpas & OffScreenCanvas since Canvas and Image Object is not supported in web workers. I can create an ImageBitMap from my image file using CreateImageBitMap() method.我可以使用 CreateImageBitMap() 方法从我的图像文件创建一个 ImageBitMap。 It successfully returns a promise and then resolves to an ImageBitMap.它成功返回 promise,然后解析为 ImageBitMap。 I can print it to my console and see the ImageBitMap gets created.我可以将它打印到我的控制台并查看 ImageBitMap 已创建。 I added a picture of the ImageBitmap printed to the console below.我添加了一张打印到下面控制台的 ImageBitmap 图片。

ImageBitmap is created and error is thrown ImageBitmap 已创建并引发错误

var loadingImage = new Image();
loadingImage.onload = () => {

    console.log(createImageBitmap(loadingImage)); // I can see the ImageBitmap logged to console
    const canvas = new OffscreenCanvas(100,100);
    const ctx = canvas.getContext('2d');

    Promise.all([
        createImageBitmaps(loadingImage);
    ]).then(function(result){
        console.log("printing promise resolved");
        console.log(result);
        ctx.drawImage(result,0,0,200,200);  /// <-- this is where error is thrown
    })  

    // other code here .... 

};
loadingImage.src = base64Src; 

Question: Where I am having trouble is when I pass this ImageBitMap to the OffScreenCanvas.draw() method I get an error saying "failed to execute drawImage on OffscreenCanvasRenderer..." You can see the exact error in the console after the ImageBitMap is printed in the picture below.问题:我遇到问题的地方是当我将此 ImageBitMap 传递给 OffScreenCanvas.draw() 方法时,我收到一条错误消息“无法在 OffscreenCanvasRenderer 上执行 drawImage ...”您可以在 ImageBitMap 之后在控制台中看到确切的错误打印在下面的图片中。

Has anyone successfully drawn an image to an OffScreenCanvas in a vue-worker/web-worker?有没有人成功地将图像绘制到 vue-worker/web-worker 中的 OffScreenCanvas? Any Example of working code would be greatly appreciated!任何工作代码示例将不胜感激!

Also from my research, it seems like createImageBitMap() is only supported in Chrome??同样根据我的研究,似乎只有 Chrome 支持 createImageBitMap() ? Is there another way instead of using ImageBitMap to draw to OffScreenCanvas that will work on both chrome and Safari???除了使用 ImageBitMap 绘制到 OffScreenCanvas 之外,还有其他方法可以在 chrome 和 Safari 上工作吗?

您是否尝试过使用ImageBitmapRenderingContext

canvas.getContext('bitmaprenderer').transferFromImageBitmap(bitmap);

In the example the type of result is an array , because of Promise.all but that's not supported as image source.在示例中,结果的类型是array ,因为Promise.all但不支持作为图像源。 Removing Promise.all and directly using the resolved result solves the problem.删除 Promise.all 并直接使用解决的结果可以解决问题。

var loadingImage = new Image();
loadingImage.onload = () => {

    console.log(createImageBitmap(loadingImage)); // I can see the ImageBitmap logged to console
    const canvas = new OffscreenCanvas(100,100);
    const ctx = canvas.getContext('2d');
    
    createImageBitmap(loadingImage).then(function(result){
        console.log("printing promise resolved");
        console.log(result);
        ctx.drawImage(result,0,0,200,200);  /// <-- this is where error is thrown
    })  

    // other code here .... 

};
loadingImage.src = "https://i.stack.imgur.com/ioGgw.png"; 

This prints, no error (in Chrome):这打印,没有错误(在 Chrome 中):

[object Promise] { ... }
"printing promise resolved"
[object ImageBitmap] {
  close: function close() { [native code] },
  height: 402,
  width: 812
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM