The WebGLRenderingContext interface provides the OpenGL ES 2.0 rendering context for the drawing surface of an HTML <canvas> element.

To get an object of this interface, call getContext() (en-US) on a <canvas> element, supplying "webgl" as the argument:

var canvas = document.getElementById('myCanvas');
var gl = canvas.getContext('webgl');

Once you have the WebGL rendering context for a canvas, you can render within it.

The WebGL tutorial has more information, examples, and resources on how to get started with WebGL.


See the WebGL constants page.

The WebGL context

The following properties and methods provide general information and functionality to deal with the WebGL context:

A read-only back-reference to the HTMLCanvasElement. Might be null if it is not associated with a <canvas> element.
WebGLRenderingContext.commit() (en-US) Experimental

Pushes frames back to the original HTMLCanvasElement, if the context is not directly fixed to a specific canvas.

WebGLRenderingContext.drawingBufferWidth (en-US)
The read-only width of the current drawing buffer. Should match the width of the canvas element associated with this context.
WebGLRenderingContext.drawingBufferHeight (en-US)
The read-only height of the current drawing buffer. Should match the height of the canvas element associated with this context.
WebGLRenderingContext.getContextAttributes() (en-US)
Returns a WebGLContextAttributes object that contains the actual context parameters. Might return null, if the context is lost.
WebGLRenderingContext.isContextLost() (en-US)
Returns true if the context is lost, otherwise returns false.

Viewing and clipping

State information

WebGLRenderingContext.activeTexture() (en-US)
Selects the active texture unit.
WebGLRenderingContext.blendColor() (en-US)
Sets the source and destination blending factors.
WebGLRenderingContext.blendEquation() (en-US)
Sets both the RGB blend equation and alpha blend equation to a single equation.
WebGLRenderingContext.blendEquationSeparate() (en-US)
Sets the RGB blend equation and alpha blend equation separately.
WebGLRenderingContext.blendFunc() (en-US)
Defines which function is used for blending pixel arithmetic.
WebGLRenderingContext.blendFuncSeparate() (en-US)
Defines which function is used for blending pixel arithmetic for RGB and alpha components separately.
WebGLRenderingContext.clearColor() (en-US)
Specifies the color values used when clearing color buffers.
WebGLRenderingContext.clearDepth() (en-US)
Specifies the depth value used when clearing the depth buffer.
WebGLRenderingContext.clearStencil() (en-US)
Specifies the stencil value used when clearing the stencil buffer.
WebGLRenderingContext.colorMask() (en-US)
Sets which color components to enable or to disable when drawing or rendering to a WebGLFramebuffer (en-US).
WebGLRenderingContext.cullFace() (en-US)
Specifies whether or not front- and/or back-facing polygons can be culled.
WebGLRenderingContext.depthFunc() (en-US)
Specifies a function that compares incoming pixel depth to the current depth buffer value.
WebGLRenderingContext.depthMask() (en-US)
Sets whether writing into the depth buffer is enabled or disabled.
WebGLRenderingContext.depthRange() (en-US)
Specifies the depth range mapping from normalized device coordinates to window or viewport coordinates.
WebGLRenderingContext.disable() (en-US)
Disables specific WebGL capabilities for this context.
WebGLRenderingContext.enable() (en-US)
Enables specific WebGL capabilities for this context.
WebGLRenderingContext.frontFace() (en-US)
Specifies whether polygons are front- or back-facing by setting a winding orientation.
WebGLRenderingContext.getParameter() (en-US)
Returns a value for the passed parameter name.
WebGLRenderingContext.getError() (en-US)
Returns error information.
WebGLRenderingContext.hint() (en-US)
Specifies hints for certain behaviors. The interpretation of these hints depend on the implementation.
WebGLRenderingContext.isEnabled() (en-US)
Tests whether a specific WebGL capability is enabled or not for this context.
WebGLRenderingContext.lineWidth() (en-US)
Sets the line width of rasterized lines.
WebGLRenderingContext.pixelStorei() (en-US)
Specifies the pixel storage modes
WebGLRenderingContext.polygonOffset() (en-US)
Specifies the scale factors and units to calculate depth values.
WebGLRenderingContext.sampleCoverage() (en-US)
Specifies multi-sample coverage parameters for anti-aliasing effects.
WebGLRenderingContext.stencilFunc() (en-US)
Sets the both front and back function and reference value for stencil testing.
WebGLRenderingContext.stencilFuncSeparate() (en-US)
Sets the front and/or back function and reference value for stencil testing.
WebGLRenderingContext.stencilMask() (en-US)
Controls enabling and disabling of both the front and back writing of individual bits in the stencil planes.
WebGLRenderingContext.stencilMaskSeparate() (en-US)
Controls enabling and disabling of front and/or back writing of individual bits in the stencil planes.
WebGLRenderingContext.stencilOp() (en-US)
Sets both the front and back-facing stencil test actions.
WebGLRenderingContext.stencilOpSeparate() (en-US)
Sets the front and/or back-facing stencil test actions.


WebGLRenderingContext.bindBuffer() (en-US)
Binds a WebGLBuffer object to a given target.
WebGLRenderingContext.bufferData() (en-US)
Updates buffer data.
WebGLRenderingContext.bufferSubData() (en-US)
Updates buffer data starting at a passed offset.
WebGLRenderingContext.createBuffer() (en-US)
Creates a WebGLBuffer object.
WebGLRenderingContext.deleteBuffer() (en-US)
Deletes a WebGLBuffer object.
WebGLRenderingContext.getBufferParameter() (en-US)
Returns information about the buffer.
WebGLRenderingContext.isBuffer() (en-US)
Returns a Boolean indicating if the passed buffer is valid.


WebGLRenderingContext.bindFramebuffer() (en-US)
Binds a WebGLFrameBuffer object to a given target.
WebGLRenderingContext.checkFramebufferStatus() (en-US)
Returns the status of the framebuffer.
WebGLRenderingContext.createFramebuffer() (en-US)
Creates a WebGLFrameBuffer object.
WebGLRenderingContext.deleteFramebuffer() (en-US)
Deletes a WebGLFrameBuffer object.
WebGLRenderingContext.framebufferRenderbuffer() (en-US)
Attaches a WebGLRenderingBuffer object to a WebGLFrameBuffer object.
WebGLRenderingContext.framebufferTexture2D() (en-US)
Attaches a textures image to a WebGLFrameBuffer object.
WebGLRenderingContext.getFramebufferAttachmentParameter() (en-US)
Returns information about the framebuffer.
WebGLRenderingContext.isFramebuffer() (en-US)
Returns a Boolean indicating if the passed WebGLFrameBuffer object is valid.
WebGLRenderingContext.readPixels() (en-US)
Reads a block of pixels from the WebGLFrameBuffer.


WebGLRenderingContext.bindRenderbuffer() (en-US)
Binds a WebGLRenderBuffer object to a given target.
WebGLRenderingContext.createRenderbuffer() (en-US)
Creates a WebGLRenderBuffer object.
WebGLRenderingContext.deleteRenderbuffer() (en-US)
Deletes a WebGLRenderBuffer object.
WebGLRenderingContext.getRenderbufferParameter() (en-US)
Returns information about the renderbuffer.
WebGLRenderingContext.isRenderbuffer() (en-US)
Returns a Boolean indicating if the passed WebGLRenderingBuffer is valid.
WebGLRenderingContext.renderbufferStorage() (en-US)
Creates a renderbuffer data store.


WebGLRenderingContext.bindTexture() (en-US)
Binds a WebGLTexture object to a given target.
WebGLRenderingContext.compressedTexImage2D() (en-US)
Specifies a 2D texture image in a compressed format.
WebGLRenderingContext.compressedTexSubImage2D() (en-US)
Specifies a 2D texture sub-image in a compressed format.
WebGLRenderingContext.copyTexImage2D() (en-US)
Copies a 2D texture image.
WebGLRenderingContext.copyTexSubImage2D() (en-US)
Copies a 2D texture sub-image.
WebGLRenderingContext.createTexture() (en-US)
Creates a WebGLTexture object.
WebGLRenderingContext.deleteTexture() (en-US)
Deletes a WebGLTexture object.
WebGLRenderingContext.generateMipmap() (en-US)
Generates a set of mipmaps for a WebGLTexture object.
WebGLRenderingContext.getTexParameter() (en-US)
Returns information about the texture.
WebGLRenderingContext.isTexture() (en-US)
Returns a Boolean indicating if the passed WebGLTexture is valid.
WebGLRenderingContext.texImage2D() (en-US)
Specifies a 2D texture image.
WebGLRenderingContext.texSubImage2D() (en-US)
Updates a sub-rectangle of the current WebGLTexture.
WebGLRenderingContext.texParameterf() (en-US)
Sets texture parameters.
WebGLRenderingContext.texParameteri() (en-US)
Sets texture parameters.

Programs and shaders

WebGLRenderingContext.attachShader() (en-US)
Attaches a WebGLShader to a WebGLProgram.
WebGLRenderingContext.bindAttribLocation() (en-US)
Binds a generic vertex index to a named attribute variable.
WebGLRenderingContext.compileShader() (en-US)
Compiles a WebGLShader.
WebGLRenderingContext.createProgram() (en-US)
Creates a WebGLProgram.
WebGLRenderingContext.createShader() (en-US)
Creates a WebGLShader.
WebGLRenderingContext.deleteProgram() (en-US)
Deletes a WebGLProgram.
WebGLRenderingContext.deleteShader() (en-US)
Deletes a WebGLShader.
WebGLRenderingContext.detachShader() (en-US)
Detaches a WebGLShader.
WebGLRenderingContext.getAttachedShaders() (en-US)
Returns a list of WebGLShader objects attached to a WebGLProgram.
WebGLRenderingContext.getProgramParameter() (en-US)
Returns information about the program.
WebGLRenderingContext.getProgramInfoLog() (en-US)
Returns the information log for a WebGLProgram object.
WebGLRenderingContext.getShaderParameter() (en-US)
Returns information about the shader.
WebGLRenderingContext.getShaderPrecisionFormat() (en-US)
Returns a WebGLShaderPrecisionFormat object describing the precision for the numeric format of the shader.
WebGLRenderingContext.getShaderInfoLog() (en-US)
Returns the information log for a WebGLShader object.
WebGLRenderingContext.getShaderSource() (en-US)
Returns the source code of a WebGLShader as a string.
WebGLRenderingContext.isProgram() (en-US)
Returns a Boolean indicating if the passed WebGLProgram is valid.
WebGLRenderingContext.isShader() (en-US)
Returns a Boolean indicating if the passed WebGLShader is valid.
WebGLRenderingContext.linkProgram() (en-US)
Links the passed WebGLProgram object.
WebGLRenderingContext.shaderSource() (en-US)
Sets the source code in a WebGLShader.
WebGLRenderingContext.useProgram() (en-US)
Uses the specified WebGLProgram as part the current rendering state.
WebGLRenderingContext.validateProgram() (en-US)
Validates a WebGLProgram.

Uniforms and attributes

WebGLRenderingContext.disableVertexAttribArray() (en-US)
Disables a vertex attribute array at a given position.
WebGLRenderingContext.enableVertexAttribArray() (en-US)
Enables a vertex attribute array at a given position.
Returns information about an active attribute variable.
WebGLRenderingContext.getActiveUniform() (en-US)
Returns information about an active uniform variable.
Returns the location of an attribute variable.
WebGLRenderingContext.getUniform() (en-US)
Returns the value of a uniform variable at a given location.
WebGLRenderingContext.getUniformLocation() (en-US)
Returns the location of a uniform variable.
WebGLRenderingContext.getVertexAttrib() (en-US)
Returns information about a vertex attribute at a given position.
WebGLRenderingContext.getVertexAttribOffset() (en-US)
Returns the address of a given vertex attribute.
WebGLRenderingContext.uniform[1234][fi][v]() (en-US)
Specifies a value for a uniform variable.
WebGLRenderingContext.uniformMatrix[234]fv() (en-US)
Specifies a matrix value for a uniform variable.
WebGLRenderingContext.vertexAttrib[1234]f[v]() (en-US)
Specifies a value for a generic vertex attribute.
WebGLRenderingContext.vertexAttribPointer() (en-US)
Specifies the data formats and locations of vertex attributes in a vertex attributes array.

Drawing buffers

WebGLRenderingContext.clear() (en-US)
Clears specified buffers to preset values.
WebGLRenderingContext.drawArrays() (en-US)
Renders primitives from array data.
WebGLRenderingContext.drawElements() (en-US)
Renders primitives from element array data.
WebGLRenderingContext.finish() (en-US)
Blocks execution until all previously called commands are finished.
WebGLRenderingContext.flush() (en-US)
Empties different buffer commands, causing all commands to be executed as quickly as possible.

Working with extensions

These methods manage WebGL extensions:

WebGLRenderingContext.getSupportedExtensions() (en-US)
Returns an Array of DOMString elements with all the supported WebGL extensions.
WebGLRenderingContext.getExtension() (en-US)
Returns an extension object.


WebGL context feature detection

{{page("/en-US/Learn/WebGL/By_example/Detect_WebGL", "summary")}}

{{page("/en-US/Learn/WebGL/By_example/Detect_WebGL", "detect-webgl-source")}}

Effect of canvas size on rendering with WebGL

{{page("/en-US/Learn/WebGL/By_example/Canvas_size_and_WebGL", "canvas-size-and-webgl-intro")}}

{{page("/en-US/Learn/WebGL/By_example/Canvas_size_and_WebGL", "canvas-size-and-webgl-source")}}


Specification Status Comment
WebGL 1.0
Die Definition von 'WebGLRenderingContext' in dieser Spezifikation.
Empfehlung Initial definition

Browser compatibility

Wir konvertieren die Kompatibilitätsdaten in ein maschinenlesbares JSON Format. Diese Kompatibilitätstabelle liegt noch im alten Format vor, denn die darin enthaltenen Daten wurden noch nicht konvertiert. Finde heraus wie du helfen kannst! (en-US)

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Basic support 9 20 4.0 (2.0) 11 12 5.1
Available in workers Nicht unterstützt Nicht unterstützt 44 (44) [1] Nicht unterstützt Nicht unterstützt Nicht unterstützt
Feature Android Chrome for Android Edge Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile
Basic support ? 25 (Ja) 4.0 (2.0) 1.0 ? 12 8.0
Available in workers Nicht unterstützt Nicht unterstützt Nicht unterstützt 44.0 (44) [1] Nicht unterstützt Nicht unterstützt Nicht unterstützt Nicht unterstützt

[1] This feature is behind a feature preference setting. In about:config, set gfx.offscreencanvas.enabled to true.

See also