WebGLRenderingContext: texElementSubImage2D() method

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Want more browser support for this feature? Tell us why.

The texElementSubImage2D() method of the WebGLRenderingContext interface of the WebGL API uploads a drawable element's state recorded during the current rendering update (or an ElementImage) into a texture. Part of the HTML-in-Canvas API.

Syntax

js
drawElementImage(target, level, xoffset, yoffset, element)
drawElementImage(target, level, xoffset, yoffset, element, config)

Parameters

target

A GLenum specifying the binding point (target) of the active texture. Possible values:

  • gl.TEXTURE_2D: A two-dimensional texture.
  • gl.TEXTURE_CUBE_MAP_POSITIVE_X: Positive X face for a cube-mapped texture.
  • gl.TEXTURE_CUBE_MAP_NEGATIVE_X: Negative X face for a cube-mapped texture.
  • gl.TEXTURE_CUBE_MAP_POSITIVE_Y: Positive Y face for a cube-mapped texture.
  • gl.TEXTURE_CUBE_MAP_NEGATIVE_Y: Negative Y face for a cube-mapped texture.
  • gl.TEXTURE_CUBE_MAP_POSITIVE_Z: Positive Z face for a cube-mapped texture.
  • gl.TEXTURE_CUBE_MAP_NEGATIVE_Z: Negative Z face for a cube-mapped texture.
level

A GLint specifying the level of detail. Level 0 is the base image level and level n is the n-th mipmap reduction level.

xoffset

A GLint specifying the x offset within the texture image.

yoffset

A GLint specifying the y offset within the texture image.

element

A drawable element reference or an ElementImage object representing the DOM element to use in the texture.

config Optional

A object that can include the following properties:

sx Optional

A GLfloat representing the horizontal coordinate of the top-left corner of the source element area to use in the texture. If omitted, defaults to 0.

sy Optional

A GLfloat representing the vertical coordinate of the top-left corner of the source element area to use in the texture. If omitted, defaults to 0.

swidth Optional

A GLfloat representing the width of the source element area to use in the texture. If omitted, defaults to the element's width.

sheight Optional

A GLfloat representing the height of the source element area to use in the texture. If omitted, defaults to the element's height.

width Optional

A GLsizei representing the width of the source when included in the texture, in texels.

height Optional

A GLsizei representing the height of the source when included in the texture, in texels.

Return value

None (undefined).

Examples

Typical usage

Our WebGL example (source code) renders an HTML form as a texture on all six sides of a rotating 3D cube. The following snippet shows the code used to create and bind a texture, and define the updateElementTexture() function used later on to upload the latest snapshot of the source form element into the texture each time it is repainted.

js
const elementTexture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, elementTexture);
// No mipmaps: linear filtering with clamped edges (also required for
// non-power-of-two textures in WebGL 1).
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);

let texWidth = 0;
let texHeight = 0;

// Copies the latest snapshot of the element into the texture,
// (re)allocating the texture storage if the element's size has changed.
// Returns false if the element has no usable size yet.
function updateElementTexture() {
  // Size in canvas-grid pixels.
  const scale = canvas.width / canvas.clientWidth;
  const w = Math.ceil(source.offsetWidth * scale);
  const h = Math.ceil(source.offsetHeight * scale);
  if (!(w > 0 && h > 0)) return false;

  gl.bindTexture(gl.TEXTURE_2D, elementTexture);

  // texElementSubImage2D() doesn't allocate storage, so pre-allocate it
  // (only when the size changes).
  if (w !== texWidth || h !== texHeight) {
    gl.texImage2D(
      gl.TEXTURE_2D,
      0,
      gl.RGBA8,
      w,
      h,
      0,
      gl.RGBA,
      gl.UNSIGNED_BYTE,
      null,
    );
    texWidth = w;
    texHeight = h;
  }

  gl.texElementSubImage2D(gl.TEXTURE_2D, 0, 0, 0, source);
  return true;
}

Specifications

This feature does not appear to be defined in any specification.

Browser compatibility

See also