milsymbol icon indicating copy to clipboard operation
milsymbol copied to clipboard

Support for offscreen canvas

Open jjwtay opened this issue 10 months ago • 3 comments

Currently line 15 and 39 of ascanvas.js calls:

document.createElement('canvas')

While this works in the main thread this makes this library unusable in a webworker utilizing an offscreen canvas. Testing this locally within one of our projects I added:

const makeCanvas = (width = 100, height = width, useOffscreenCanvas = true) => {
  if (typeof OffscreenCanvas === undefined || !useOffscreenCanvas) {
    const canvas = document.createElement("canvas");
    canvas.width = width;
    canvas.height = height;
    return canvas;
  }

  return new OffscreenCanvas(width, height);
};

This allowed us to proceed and render within a webworker. As far as I know the main ramification against this would be any calls to canvas.toDataURL() would need to be wrapped in a way to check if it's an offscreen canvas first with an alternative if it is an offscreen canvas. I don't know if this is something you would want in your library or not so wanted to bring this to your attention for discussion.

jjwtay avatar Apr 03 '24 17:04 jjwtay