0

I currently use the following:

const canvasOneBuffer = new Uint8Array(canvasOneImageData.data.buffer);
// within an x/y loop:
const newPixel1Data0 = canvasOneBuffer[4 * (x + y * canvasOne.width) + 0];
const newPixel1Data1 = canvasOneBuffer[4 * (x + y * canvasOne.width) + 1];
const newPixel1Data2 = canvasOneBuffer[4 * (x + y * canvasOne.width) + 2];
const newPixel1Data3 = canvasOneBuffer[4 * (x + y * canvasOne.width) + 3];
const pixel1Data = new Uint8ClampedArray(4);
pixel1Data[0] = newPixel1Data0;
pixel1Data[1] = newPixel1Data1;
pixel1Data[2] = newPixel1Data2;
pixel1Data[3] = newPixel1Data3;

This seems a bit obtuse though. Is there any way I can go about creating the new Uint8ClampedArray(4); with a single read from the buffer array?

1 Answer 1

1

you mean like

const pixel1Data = canvasOneBuffer.slice(0, 4)
.map(canvasItem => {
  // do what ever with canvasItem
});
Sign up to request clarification or add additional context in comments.

1 Comment

this is plain ES6 if you need to use ES5 replace canvasItem => { // do what ever with canvasItem } with function(canvasItem) { // do whatever here}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.