Firefox Tomorrow

web api interface

WebGLRenderingContext

View on MDN ↗

Available in workers

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

To get an access to a WebGL context for 2D and/or 3D graphics rendering, call getContext() on a <canvas> element, supplying “webgl” as the argument:

const canvas = document.getElementById("myCanvas");
const 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.

If you require a WebGL 2.0 context, see WebGL2RenderingContext; this supplies access to an implementation of OpenGL ES 3.0 graphics.

Constants

See the WebGL constants page.

The WebGL context

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

  • canvas
  • drawingBufferWidth
    • : The read-only width of the current drawing buffer. Should match the width of the canvas element associated with this context.
  • drawingBufferHeight
    • : The read-only height of the current drawing buffer. Should match the height of the canvas element associated with this context.
  • getContextAttributes()
    • : Returns a WebGLContextAttributes object that contains the actual context parameters. Might return null, if the context is lost.
  • isContextLost()
    • : Returns true if the context is lost, otherwise returns false.
  • makeXRCompatible()
    • : Ensures the context is compatible with the user’s XR hardware, re-creating the context if necessary with a new configuration to do so. This can be used to start an application using standard 2D presentation, then transition to using a VR or AR mode later.

Viewing and clipping

State information

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

Buffers

Framebuffers

Renderbuffers

Textures

Programs and shaders

Uniforms and attributes

Drawing buffers

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

Color spaces

Working with extensions

These methods manage WebGL extensions:

Specifications

SpecificationsStandards references are available on the canonical MDN page.

Browser compatibility

Browser compatibilityCompatibility data is available on the canonical MDN page.

See also