L'interface WebGLRenderingContext fournit le contexte de rendu OpenGL ES 2.0 pour le dessin d'une suface dans un élément HTML <canvas>.

Pour obtenir une instance de cette interface, appelez getContext() sur un élément <canvas>, en spécifiant "webgl" en paramètre :

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

Une fois que avez le contexte de rendu WebGL, vous pouvez dessiner à l'intérieur.

Le tutoriel WebGL contient plus d'informations, d'exemples, et de ressources sur comment débuter en 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) This is an experimental API that should not be used in production code.

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

WebGLRenderingContext.scissor() (en-US)
Defines the scissor box.
Sets the viewport.

State information

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.
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.
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.
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.


Binds a WebGLBuffer object to a given target.
Updates buffer data.
WebGLRenderingContext.bufferSubData() (en-US)
Updates buffer data starting at a passed offset.
Creates a WebGLBuffer object.
Deletes a WebGLBuffer object.
WebGLRenderingContext.getBufferParameter() (en-US)
Returns information about the buffer.
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.


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.
Creates a WebGLTexture object.
WebGLRenderingContext.deleteTexture() (en-US)
Deletes a WebGLTexture object.
Generates a set of mipmaps for a WebGLTexture object.
Returns information about the texture.
WebGLRenderingContext.isTexture() (en-US)
Returns a Boolean indicating if the passed WebGLTexture is valid.
Specifies a 2D texture image.
WebGLRenderingContext.texSubImage2D() (en-US)
Updates a sub-rectangle of the current WebGLTexture.
Sets texture parameters.
Sets texture parameters.

Programs and shaders

Attaches a WebGLShader to a WebGLProgram.
WebGLRenderingContext.bindAttribLocation() (en-US)
Binds a generic vertex index to a named attribute variable.
Compiles a WebGLShader.
Creates a WebGLProgram.
Creates a WebGLShader.
WebGLRenderingContext.deleteProgram() (en-US)
Deletes a WebGLProgram.
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.
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.
Sets the source code in a WebGLShader.
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.
Enables a vertex attribute array at a given position.
WebGLRenderingContext.getActiveAttrib() (en-US)
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.
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.
Specifies a value for a uniform variable.
Specifies a matrix value for a uniform variable.
WebGLRenderingContext.vertexAttrib[1234]f[v]() (en-US)
Specifies a value for a generic vertex attribute.
Specifies the data formats and locations of vertex attributes in a vertex attributes array.

Drawing buffers

Clears specified buffers to preset values.
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")}}

<p>[ Here would go the result of WebGL feature detection ]</p>
<button>Press here to detect WebGLRenderingContext</button>
body {
  text-align : center;
button {
  display : block;
  font-size : inherit;
  margin : auto;
  padding : 0.6em;
// Run everything inside window load event handler, to make sure
// DOM is fully loaded and styled before trying to manipulate it.
window.addEventListener("load", function() {
  var paragraph = document.querySelector("p"),
    button = document.querySelector("button");
  // Adding click event handler to button.
  button.addEventListener("click", detectWebGLContext, false);
  function detectWebGLContext () {
    // Create canvas element. The canvas is not added to the
    // document itself, so it is never displayed in the
    // browser window.
    var canvas = document.createElement("canvas");
    // Get WebGLRenderingContext from canvas element.
    var gl = canvas.getContext("webgl")
      || canvas.getContext("experimental-webgl");
    // Report the result.
    if (gl && gl instanceof WebGLRenderingContext) {
      paragraph.textContent =
        "Congratulations! Your browser supports WebGL.";
    } else {
      paragraph.textContent = "Failed to get WebGL context. "
        + "Your browser or device may not support WebGL.";
}, false);

The source code of this example is also available on GitHub.

Effect of canvas size on rendering with WebGL

With scissor() and clear() we can demonstrate how the WebGL drawing buffer is affected by the size of the canvas.

The size of the first canvas is set to the styled Element size, determined by CSS. This is done by assigning the width and height properties of the canvas to the values of the clientWidth and clientHeight properties, respectively.

In contrast, no such assignment is done for the second canvas. The internal width and height properties of the canvas remain at default values, which are different than the actual size of the canvas Element in the browser window.

The effect is clearly visible when using scissor() and clear() to draw a square in the center of the canvas, by specifying its position and size in pixels. In the first canvas, we get the desired result. In the second, the square has the wrong shape, size, and position.

<p>Compare the two canvases.</p>
<canvas>Your browser does not seem to support
    HTML5 canvas.</canvas>
<canvas>Your browser does not seem to support
    HTML5 canvas.</canvas>
body {
  text-align : center;
canvas {
  display : inline-block;
  width : 120px;
  height : 80px;
  margin : auto;
  padding : 0;
  border : none;
  background-color : black;
window.addEventListener("load", function() {
  "use strict"
  var firstCanvas = document.getElementsByTagName("canvas")[0],
    secondCanvas = document.getElementsByTagName("canvas")[1];
  firstCanvas.width = firstCanvas.clientWidth;
  firstCanvas.height = firstCanvas.clientHeight;
  [firstCanvas, secondCanvas].forEach(function(canvas) {
    var gl = canvas.getContext("webgl")
      || canvas.getContext("experimental-webgl");
    if (!gl) {
      document.querySelector("p").innerHTML =
        "Failed to get WebGL context. "
        + "Your browser or device may not support WebGL.";
    gl.viewport(0, 0,
      gl.drawingBufferWidth, gl.drawingBufferHeight);
    gl.scissor(30, 10, 60, 60);
    gl.clearColor(1.0, 1.0, 0.0, 1.0);
}, false);

The source code of this example is also available on GitHub.


Specification Status Comment
WebGL 1.0
La définition de 'WebGLRenderingContext' dans cette spécification.
Recommendation Initial definition

Browser compatibility

No compatibility data found for api/WebGLRenderingContext.
Check for problems with this page or contribute missing data to mdn/browser-compat-data.

See also