首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未在web-worker中加载SVG图像

未在web-worker中加载SVG图像
EN

Stack Overflow用户
提问于 2019-09-18 22:51:40
回答 1查看 508关注 0票数 5

我正在尝试在web-worker中运行此typescript代码:

代码语言:javascript
复制
   async BuildImage(): Promise<void> {

    let data1 = `<svg xmlns='http://www.w3.org/2000/svg' width='50' height='50'>
                    <foreignObject width='100%' height='100%' style="background:blue">
                      <div xmlns='http://www.w3.org/1999/xhtml' style='font-size:12px'>                        
                        <ellipse cx="23" cy="23" rx="25" ry="25" style="fill:yellow;stroke:purple;stroke-width:2" />
                      </div>
                    </foreignObject>
                  </svg>`;

    let svg = new Blob([data1], { type: "image/svg+xml;charset=utf-8" });
    var image = await createImageBitmap(svg);

}

但是抛出了带有"InvalidStateError""The source image could not be decoded."

我也尝试过这个代码:

代码语言:javascript
复制
   async BuildImage(): Promise<void> {

    let data1 = `<svg xmlns='http://www.w3.org/2000/svg' width='50' height='50'>
                    <foreignObject width='100%' height='100%' style="background:blue">
                      <div xmlns='http://www.w3.org/1999/xhtml' style='font-size:12px'>                        
                        <ellipse cx="23" cy="23" rx="25" ry="25" style="fill:yellow;stroke:purple;stroke-width:2" />
                      </div>
                    </foreignObject>
                  </svg>`;

    let svg = new Blob([data1], { type: "image/svg+xml;charset=utf-8" });
    let url = URL.createObjectURL(svg);

    var loadImageAsync = new Promise<HTMLImageElement>(resolve => {

        let img = new Image();
        img.onload = () => resolve(img);
        img.onerror = () => resolve(img);

        img.src = url;
    });

    this.image = await loadImageAsync;}

但现在的问题是,new Image()对象没有在web-worker中定义,因为它不能访问DOM。然而,最后一种方法在非web-worker场景中有效,但createImageBitmap在任何地方都不起作用。

有谁知道如何在web-worker或任何解决方案中从SVG构建和镜像这种情况?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2020-04-15 13:40:59

由于他们还没有实现该规范,

目前可能的解决方法是在主线程中从svg字符串生成一个图像,然后将生成的图像位图发送回您的worker。

所以在你的主线程代码中

代码语言:javascript
复制
// Loads the SVG image asynchronously
function loadSVGAsync(svgString) 
 return new Promise(resolve => {
  const img = new Image();
  img.onload = function () {
    resolve(this);
  };
  img.src = 'data:image/svg+xml;charset=utf8,' + encodeURIComponent(svgString);
 });
}

const worker = new Worker('...');

worker.addEventListener('message', async ev => {
 const YourSvgString = '...';

 const img = await loadSVGAsync(YourSvgString);

 // Pass over the image Bit map to your worker
 worker.postMessage({svg: await createImageBitMap(img)});
});

在您的worker中

代码语言:javascript
复制
self.addEventListener('message', (ev) => {
  // Do whatever you need with the image
  const svgImage = ev.data.svg;
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57995608

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档