SB2055
SB2055

Reputation: 12862

How to quickly read a subarray out of a source array

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?

Upvotes: 0

Views: 56

Answers (1)

Dayan Moreno Leon
Dayan Moreno Leon

Reputation: 5435

you mean like

const pixel1Data = canvasOneBuffer.slice(0, 4)
.map(canvasItem => {
  // do what ever with canvasItem
});

Upvotes: 1

Related Questions