Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. HTMLCanvasElement
  4. contextlost

HTMLCanvasElement: contextlost event

Limited availability

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

Thecontextlost event of theCanvas API is fired if the user agent detects that the backing storage associated with aCanvasRenderingContext2D context is lost.Contexts can be lost for several reasons like driver crashes or the application runs out of memory, etc.

By default the user agent will attempt to restore the context and then fire thecontextrestored event.User code can prevent the context from being restored by callingEvent.preventDefault() during event handling.

Syntax

Use the event name in methods likeaddEventListener(), or set an event handler property.

js
addEventListener("contextlost", (event) => { })oncontextlost = (event) => { }

Event type

A genericEvent.

Example

The code fragment below detects thecontextlost event.

js
const canvas = document.getElementById("canvas");canvas.addEventListener("contextlost", (event) => {  console.log(event);});

To prevent the context from being restored the code might instead look like this:

js
const canvas = document.getElementById("canvas");canvas.addEventListener("contextlost", (event) => {  event.preventDefault();});

Specifications

Specification
HTML
# event-contextlost

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp