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
drawElementImage(target, level, xoffset, yoffset, element)
drawElementImage(target, level, xoffset, yoffset, element, config)
Parameters
target-
A
GLenumspecifying 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
GLintspecifying the level of detail. Level 0 is the base image level and level n is the n-th mipmap reduction level. xoffset-
A
GLintspecifying the x offset within the texture image. yoffset-
A
GLintspecifying the y offset within the texture image. element-
A
drawableelement reference or anElementImageobject representing the DOM element to use in the texture. configOptional-
A object that can include the following properties:
sxOptional-
A
GLfloatrepresenting the horizontal coordinate of the top-left corner of the sourceelementarea to use in the texture. If omitted, defaults to0. syOptional-
A
GLfloatrepresenting the vertical coordinate of the top-left corner of the sourceelementarea to use in the texture. If omitted, defaults to0. swidthOptional-
A
GLfloatrepresenting the width of the sourceelementarea to use in the texture. If omitted, defaults to the element's width. sheightOptional-
A
GLfloatrepresenting the height of the sourceelementarea to use in the texture. If omitted, defaults to the element's height. widthOptional-
A
GLsizeirepresenting the width of the source when included in the texture, in texels. heightOptional-
A
GLsizeirepresenting 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.
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;
}