Skip to content

checking for pixel array reassignment inside updatePixels #5127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/core/p5.Renderer2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,45 @@ p5.Renderer2D.prototype.updatePixels = function(x, y, w, h) {
pixelsState.imageData;
}

let pixels = pixelsState.pixels;
if (this._pInst instanceof p5 && this._pInst._isGlobal === true) {
pixels = globalThis.pixels;
}

if (pixels !== pixelsState.imageData.data) {
let imgData;
if (pixels instanceof Uint8ClampedArray) {
if (pixels.length === pixelsState.imageData.data.length) {
imgData = new ImageData(
pixels,
pixelsState.imageData.width,
pixelsState.imageData.height
);
} else {
let imgArr = new Uint8ClampedArray(pixelsState.imageData.data.length);
imgArr.set(pixels);
imgData = new ImageData(
imgArr,
pixelsState.imageData.width,
pixelsState.imageData.height
);
}
} else {
if (Array.isArray(pixels)) {
let imgArr = new Uint8ClampedArray(pixelsState.imageData.data.length);
imgArr.set(pixels);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the original implementation for this is simpler but if you just want to keep things consistent then I guess this should be fine as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I thought it'll be fine this way too, if we are doing the same above. :D
As for unit tests, I checked and I think no unit tests exist for updatePixels(), so I guess I will write some.

imgData = new ImageData(
imgArr,
pixelsState.imageData.width,
pixelsState.imageData.height
);
}
}

pixelsState._setProperty('imageData', imgData);
pixelsState._setProperty('pixels', imgData.data);
}

this.drawingContext.putImageData(pixelsState.imageData, x, y, 0, 0, w, h);
};

Expand Down