PeculiarVentures/GammaCV

Enable gammaCV to work in webworkers

euan-smith opened this issue · 1 comments

gammaCV can work fine in webworkers, however because a webgl canvas is generated using document.createElement, a fairly ugly hack is needed to get it to work.

self.document={
  createElement(tagName) {
    if (tagName === 'canvas') return new OffscreenCanvas(1,1);
    throw new Error('cannot create a '+tagName)
  }
}

(initialising with a size of 1,1 was a guess which seems to work)
The use of webworkers can enable much more complex image processing pipelines to still effectively run in the browser (especially on older machines) without blocking the UI thread and impacting user experience.

This could be solved easily in a few different ways, depending on your preference:

  1. A configuration option for the consumer of the library to provide a function which returns a new canvas.
  2. Auto-detection of the context and behaving differently depending on if in a webworker or the main browser thread.
  3. Using OffscreenCanvas by default and only falling back to using document.createElement if that is not available.

Happy to notify, that a fix for this released in a v0.5.0. No additional action required from the developer, GammaCV will fallback to OffscreenCanvas when DOM is not available.