WebGLRenderingContext.readPixels()
方法在
WebGL API
reads a block of pixels from a specified rectangle of the current color framebuffer into an
ArrayBufferView
对象。
// 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.
WebGL2 adds
GLenum
specifying the data type of the pixel data. Possible values:
gl.UNSIGNED_BYTE
gl.UNSIGNED_SHORT_5_6_5
gl.UNSIGNED_SHORT_4_4_4_4
gl.UNSIGNED_SHORT_5_5_5_1
gl.FLOAT
WebGL2 adds
gl.BYTE
gl.UNSIGNED_INT_2_10_10_10_REV
gl.HALF_FLOAT
gl.SHORT
gl.UNSIGNED_SHORT
gl.INT
gl.UNSIGNED_INT
ArrayBufferView
object to read data into. The array type must match the type of the
type
参数。
Uint8Array
for
gl.UNSIGNED_BYTE
.
Uint16Array
for
gl.UNSIGNED_SHORT_5_6_5
,
gl.UNSIGNED_SHORT_4_4_4_4
,或
gl.UNSIGNED_SHORT_5_5_5_1
.
Float32Array
for
gl.FLOAT
.
dstOffset
可选
Offset. Defaults to 0.
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
不是
gl.RGB
.
type
is
gl.UNSIGNED_SHORT_4_4_4_4
and
format
不是
gl.RGB
A.
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
| 规范 | 状态 | 注释 |
|---|---|---|
|
WebGL 1.0
The definition of 'readPixels' in that specification. |
推荐 | 初始定义。 |
|
OpenGL ES 2.0
The definition of 'glReadPixels' in that specification. |
标准 | Man page of the OpenGL API. |
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
更新 GitHub 上的兼容性数据| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
readPixels
|
Chrome 9 | Edge 12 | Firefox 4 | IE 11 | Opera 12 | Safari 5.1 | WebView Android Yes | Chrome Android 25 | Firefox Android Yes | Opera Android 12 | Safari iOS 8 | Samsung Internet Android 1.5 |
WebGL2
|
Chrome 56 | Edge 79 | Firefox 51 | IE No | Opera 43 | Safari No | WebView Android 58 | Chrome Android 58 | Firefox Android 51 | Opera Android 43 | Safari iOS No | Samsung Internet Android 7.0 |
完整支持
不支持
WebGLRenderingContext
activeTexture()
attachShader()
bindAttribLocation()
bindBuffer()
bindFramebuffer()
bindRenderbuffer()
bindTexture()
blendColor()
blendEquation()
blendEquationSeparate()
blendFunc()
blendFuncSeparate()
bufferData()
bufferSubData()
checkFramebufferStatus()
clear()
clearColor()
clearDepth()
clearStencil()
colorMask()
commit()
compileShader()
compressedTexImage[23]D()
compressedTexSubImage2D()
copyTexImage2D()
copyTexSubImage2D()
createBuffer()
createFramebuffer()
createProgram()
createRenderbuffer()
createShader()
createTexture()
cullFace()
deleteBuffer()
deleteFramebuffer()
deleteProgram()
deleteRenderbuffer()
deleteShader()
deleteTexture()
depthFunc()
depthMask()
depthRange()
detachShader()
disable()
disableVertexAttribArray()
drawArrays()
drawElements()
enable()
enableVertexAttribArray()
finish()
flush()
framebufferRenderbuffer()
framebufferTexture2D()
frontFace()
generateMipmap()
getActiveAttrib()
getActiveUniform()
getAttachedShaders()
getAttribLocation()
getBufferParameter()
getContextAttributes()
getError()
getExtension()
getFramebufferAttachmentParameter()
getParameter()
getProgramInfoLog()
getProgramParameter()
getRenderbufferParameter()
getShaderInfoLog()
getShaderParameter()
getShaderPrecisionFormat()
getShaderSource()
getSupportedExtensions()
getTexParameter()
getUniform()
getUniformLocation()
getVertexAttrib()
getVertexAttribOffset()
hint()
isBuffer()
isContextLost()
isEnabled()
isFramebuffer()
isProgram()
isRenderbuffer()
isShader()
isTexture()
lineWidth()
linkProgram()
pixelStorei()
polygonOffset()
readPixels()
renderbufferStorage()
sampleCoverage()
scissor()
shaderSource()
stencilFunc()
stencilFuncSeparate()
stencilMask()
stencilMaskSeparate()
stencilOp()
stencilOpSeparate()
texImage2D()
texParameter[fi]()
texSubImage2D()
uniform[1234][fi][v]()
uniformMatrix[234]fv()
useProgram()
validateProgram()
vertexAttrib[1234]f[v]()
vertexAttribPointer()
viewport()
ANGLE_instanced_arrays
EXT_blend_minmax
EXT_color_buffer_half_float
EXT_disjoint_timer_query
EXT_frag_depth
EXT_sRGB
EXT_shader_texture_lod
EXT_texture_filter_anisotropic
OES_element_index_uint
OES_standard_derivatives
OES_texture_float
OES_texture_float_linear
OES_texture_half_float
OES_texture_half_float_linear
OES_vertex_array_object
WEBGL_color_buffer_float
WEBGL_compressed_texture_atc
WEBGL_compressed_texture_etc1
WEBGL_compressed_texture_pvrtc
WEBGL_compressed_texture_s3tc
WEBGL_compressed_texture_s3tc_srgb
WEBGL_debug_renderer_info
WEBGL_debug_shaders
WEBGL_depth_texture
WEBGL_draw_buffers
WEBGL_lose_context
WebGL2RenderingContext
WebGLActiveInfo
WebGLBuffer
WebGLContextEvent
WebGLFramebuffer
WebGLObject
WebGLProgram
WebGLQuery
WebGLRenderbuffer
WebGLSampler
WebGLShader
WebGLShaderPrecisionFormat
WebGLSync
WebGLTexture
WebGLTransformFeedback
WebGLUniformLocation
WebGLVertexArrayObject