Skip to content

Commit 7240ce6

Browse files
committed
Using offscreen canvas to extract the pixel data of webgl canvas using CanvasRenderingContext2D.
1 parent 27fb4ac commit 7240ce6

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

src/image/filters.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,12 @@ Filters._toPixels = function(canvas) {
4040
.getContext('2d')
4141
.getImageData(0, 0, canvas.width, canvas.height).data;
4242
} else if (canvas.getContext('webgl')) {
43-
const gl = canvas.getContext('webgl');
44-
const len = gl.drawingBufferWidth * gl.drawingBufferHeight * 4;
45-
const data = new Uint8Array(len);
46-
gl.readPixels(
47-
0,
48-
0,
49-
canvas.width,
50-
canvas.height,
51-
gl.RGBA,
52-
gl.UNSIGNED_BYTE,
53-
data
54-
);
55-
return data;
43+
const offCanvas = document.createElement('canvas');
44+
offCanvas.width = canvas.width;
45+
offCanvas.height = canvas.height;
46+
const gl = offCanvas.getContext('2d');
47+
gl.drawImage(canvas, 0, 0);
48+
return gl.getImageData(0, 0, offCanvas.width, offCanvas.height).data;
5649
}
5750
}
5851
};

0 commit comments

Comments
 (0)