The WebGLRenderingContext.readPixels() method of the WebGL API reads a block of pixels from a specified rectangle of the current color framebuffer into an ArrayBufferView object.
// WebGL1: void gl.readPixels(x, y, width, height, format, type, pixels); // WebGL2: void gl.readPixels(x, y, width, height, format, type, GLintptr offset); void gl.readPixels(x, y, width, height, format, type, ArrayBufferView pixels, GLuint dstOffset);
GLint specifying the first horizontal pixel that is read from the lower left corner of a rectangular block of pixels.GLint specifying the first vertical pixel that is read from the lower left corner of a rectangular block of pixels.GLsizei specifying the width of the rectangle.GLsizei specifying the height of the rectangle.GLenum specifying the format of the pixel data. Possible values: gl.ALPHA: Discards the red, green and blue components and reads the alpha component.gl.RGB: Discards the alpha components and reads the red, green and blue components.gl.RGBA: Red, green, blue and alpha components are read from the color buffer.GLenum specifying the data type of the pixel data. Possible values: gl.UNSIGNED_BYTEgl.UNSIGNED_SHORT_5_6_5gl.UNSIGNED_SHORT_4_4_4_4gl.UNSIGNED_SHORT_5_5_5_1gl.FLOATArrayBufferView object to read data into. The array type must match the type of the type parameter. Uint8Array for gl.UNSIGNED_BYTE.Uint16Array for gl.UNSIGNED_SHORT_5_6_5, gl.UNSIGNED_SHORT_4_4_4_4, or gl.UNSIGNED_SHORT_5_5_5_1.Float32Array for gl.FLOAT.dstOffset Optional
None.
gl.INVALID_ENUM error is thrown if format or type is not an accepted value.gl.INVALID_OPERATION error is thrown if type is gl.UNSIGNED_SHORT_5_6_5 and format is not gl.RGB.type is gl.UNSIGNED_SHORT_4_4_4_4 and format is not gl.RGBA.type does not match the typed array type of pixels.gl.INVALID_FRAMEBUFFER_OPERATION error is thrown if the currently bound framebuffer is not framebuffer complete.var canvas = document.getElementById('canvas');
var gl = canvas.getContext('webgl');
var pixels = new Uint8Array(gl.drawingBufferWidth * gl.drawingBufferHeight * 4);
gl.readPixels(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
console.log(pixels); // Uint8Array
| Specification | Status | Comment |
|---|---|---|
| WebGL 1.0 The definition of 'readPixels' in that specification. | Recommendation | Initial definition. |
| OpenGL ES 2.0 The definition of 'glReadPixels' in that specification. | Standard | Man page of the OpenGL API. |
| Desktop | ||||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
| Basic support | 9 | 12 | 4 | 11 | 12 | 5.1 |
WebGL2 |
56 | No | 51 | No | 43 | No |
| Mobile | |||||||
|---|---|---|---|---|---|---|---|
| Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
| Basic support | Yes | 25 | Yes | Yes | 12 | 8.1 | Yes |
WebGL2 |
58 | 58 | No | 51 | 43 | No | 7.0 |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/readPixels