HTML oncontextrestored Event Attribute
Description
The oncontextrestored HTML event attribute is an event handler that triggers when a WebGL rendering context that was previously lost has been successfully restored. It is part of the WebGL API and is primarily used when working with <canvas> elements for 3D graphics rendering.
Key Points
Related Event:
- The actual event is called
webglcontextrestored. - It occurs after a
webglcontextlostevent, which happens when the WebGL context is lost (e.g., due to graphics driver reset, browser resource management, or heavy GPU usage).
- The actual event is called
Purpose:
- Allows developers to restore any resources (like textures, buffers, shaders) that were deleted when the context was lost.
- Without handling this event, your WebGL graphics may fail to render properly after a context loss.
Event Target:
- Typically attached to a
<canvas>element that uses a WebGL rendering context.
- Typically attached to a
Event Object:
The event handler receives a
WebGLContextEventobject with properties such as:type- the type of the event, which will be"webglcontextrestored".target- the<canvas>element associated with the context.
Difference from
oncontextlost:oncontextlostfires when the WebGL context is lost.oncontextrestoredfires when the context has been restored and the canvas can resume rendering.
Usage Example
HTML
<canvas id="myCanvas" width="400" height="300"
onwebglcontextrestored="handleContextRestored()">
</canvas>
JavaScript
const canvas = document.getElementById('myCanvas');
const gl = canvas.getContext('webgl');
canvas.addEventListener('webglcontextlost', function(event) {
event.preventDefault(); // Prevent default behavior to allow restoration
console.log('WebGL context lost!');
}, false);
canvas.addEventListener('webglcontextrestored', function(event) {
console.log('WebGL context restored!');
// Reinitialize shaders, buffers, textures, etc.
initWebGLResources(gl);
}, false);
function initWebGLResources(gl) {
// Example: recreate buffers and textures
console.log('Reinitializing WebGL resources...');
}
Best Practices
- Always handle
oncontextrestoredif your application uses WebGL to ensure a smooth user experience. - Reinitialize all GPU resources that were lost during the context loss.
- Prevent default context loss behavior using
event.preventDefault()insideoncontextlost.
Syntax
<canvas oncontextrestored="script()"></canvas>
Values
- scriptThe name of the script to use when the event has been triggered.
Example
Browser Support
The following information will show you the current browser support for the HTML oncontextrestored event attribute. Hover over a browser icon to see the version that first introduced support for this HTML event attribute.
This event attribute is supported in some modern browsers, but not all.
Desktop
Tablets & Mobile
-
Last updated by CSSPortal on: 27th December 2025
Sponsors
Copy Paste List
Copy and paste or download lists in your preferred format, including plain text, PDF, or HTML.
Unicode Characters & Emojis
Search all Unicode characters and emojis plus other tools.
Check Shortened URLs
Expand shortened URLs to view the destination link.
Advertise Here
Advertise your company and products here!
