Skip to content

Web worker example is incorrect #261

@pastelmind

Description

@pastelmind

The web worker example added in 25ee647 (see #13) is incorrect. The new Image() constructor is not available in a web worker context:

Uncaught ReferenceError: Image is not defined

The web worker test didn't catch this because it never called createCanvasFromData() at all.

const createCanvas = (width, height) => {
  return new OffscreenCanvas(width, height);
};

const createCanvasFromData = (data) => {
  // This should fail, but didn't because the function is never called
  // for the test PSD file
  const image = new Image();
  image.src = 'data:image/jpeg;base64,' + agPsd.byteArrayToBase64(data);
  const canvas = new OffscreenCanvas(image.width, image.height);
  canvas.getContext('2d').drawImage(image, 0, 0);
  return canvas;
};

agPsd.initializeCanvas(createCanvas, createCanvasFromData);

The typical way to draw an image file to a OffscreenCanvas is to convert it to an ImageBitmap and draw it to an OffscreenCanvas. However, this is an async operation:

const imageBitmap = await createImageBitmap(blob);
const canvas = new OffscreenCanvas(imageBitmap.width, imageBitmap.height);
canvas.getContext('2d').drawImage(imageBitmap, 0, 0);

The current implementation requires createCanvasFromData() to be synchronous, which makes it incompatible with web workers.

ag-psd/src/imageResources.ts

Lines 1268 to 1270 in f3662b0

} else if (data.byteLength) {
target.thumbnail = createCanvasFromData(data);
}

Perhaps web workers could set useRawThumbnail: true and decode the thumbnail ArrayBuffers in the UI thread. I haven't tested this, though.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions