HTMLCanvasElement: contextrestored event

فهرست فصل‌ها دانلود آرشیوناشر

MDN Web Docs · نسخهٔ df622a2a1c1e · CC BY-SA 2.5 و مجوز جداگانهٔ ناشر برای نمونه‌کد

{{APIRef("Canvas API")}}

The **contextrestored** event of the Canvas API is fired if the user agent restores the backing storage for a [CanvasRenderingContext2D](/en-US/docs/Web/API/CanvasRenderingContext2D).

You can redraw, re-retrieve resources, and reinitialize the state of your context after receiving this event.

Syntax

Use the event name in {{domxref("EventTarget/addEventListener", "addEventListener()")}}:

js-nolint
addEventListener("contextrestored", (event) => { })

Alternatively, assign to the event handler property of the interface:

js-nolint
oncontextrestored = (event) => { }

Event type

A generic {{domxref("Event")}}.

Example

The code fragment below detects the context restored event.

js
const canvas = document.getElementById("canvas");

canvas.addEventListener("contextrestored", (e) => {
  console.log(e);
  // call to redrawCanvas() or similar
});

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also

  • [HTMLCanvasElement: contextlost event](/en-US/docs/Web/API/HTMLCanvasElement/contextlost_event)
  • [CanvasRenderingContext2D.isContextLost()](/en-US/docs/Web/API/CanvasRenderingContext2D/isContextLost)
  • [OffscreenCanvas: contextlost event](/en-US/docs/Web/API/OffscreenCanvas/contextlost_event)