简体   繁体   中英

SVG Image not loaded in web-worker

I'm trying to run this typescript code in a web-worker:

   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);

}

But throws "The source image could not be decoded." with "InvalidStateError" 在此处输入图片说明

I've tried this code also:

   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;}

But the problem now is that the new Image() object is not defined in a web-worker as it doesn't have access to the DOM. This last method however works in a non web-worker scenario, But the createImageBitmap doesn't work anywhere.

Anyone knows how to build and image from an SVG in a web-worker or any workaround for this situation?

Thanks

Seeing as their is no implementation of the spec yet,

A possible workaround for now would be to generate an image from your svg string in the main thread then post the resulting image bit map back to your worker.

So in your main thread code

// 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)});
});

In your worker

self.addEventListener('message', (ev) => {
  // Do whatever you need with the image
  const svgImage = ev.data.svg;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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