Skip to content

Commit 9b62f2e

Browse files
committed
Polyfill ImageData in Node.js environments
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.
1 parent 86f943c commit 9b62f2e

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/display/canvas.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ class CanvasExtraState {
586586
}
587587

588588
function putBinaryImageData(ctx, imgData) {
589-
if (typeof ImageData !== "undefined" && imgData instanceof ImageData) {
589+
if (imgData instanceof ImageData) {
590590
ctx.putImageData(imgData, 0, 0);
591591
return;
592592
}

src/display/node_utils.js

+7
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ if (
5151
warn("Cannot polyfill `DOMMatrix`, rendering may be broken.");
5252
}
5353
}
54+
if (!globalThis.ImageData) {
55+
if (canvas?.ImageData) {
56+
globalThis.ImageData = canvas.ImageData;
57+
} else {
58+
warn("Cannot polyfill `ImageData`, rendering may be broken.");
59+
}
60+
}
5461
if (!globalThis.Path2D) {
5562
if (canvas?.Path2D) {
5663
globalThis.Path2D = canvas.Path2D;

0 commit comments

Comments
 (0)