Skip to content

Commit

Permalink
Polyfill ImageData in Node.js environments
Browse files Browse the repository at this point in the history
Given that `ImageData` has been supported for many years in all browsers, see [MDN](https://developer.mozilla.org/en-US/docs/Web/API/ImageData#browser_compatibility), we have a `typeof` check that's only necessary in Node.js environments.
Since the `@napi-rs/canvas` package provides that functionality, we can thus add an `ImageData` polyfill which allows us to ever so slightly simplify the code.
  • Loading branch information
Snuffleupagus committed Nov 9, 2024
1 parent aaea218 commit c85ba9a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/display/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ class CanvasExtraState {
}

function putBinaryImageData(ctx, imgData) {
if (typeof ImageData !== "undefined" && imgData instanceof ImageData) {
if (imgData instanceof ImageData) {
ctx.putImageData(imgData, 0, 0);
return;
}
Expand Down
7 changes: 7 additions & 0 deletions src/display/node_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ if (
warn("Cannot polyfill `DOMMatrix`, rendering may be broken.");
}
}
if (!globalThis.ImageData) {
if (canvas?.ImageData) {
globalThis.ImageData = canvas.ImageData;
} else {
warn("Cannot polyfill `ImageData`, rendering may be broken.");
}
}
if (!globalThis.Path2D) {
if (canvas?.Path2D) {
globalThis.Path2D = canvas.Path2D;
Expand Down

0 comments on commit c85ba9a

Please sign in to comment.