CanvasRenderingContext2D: isContextLost() method

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

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

{{APIRef("Canvas API")}}

The **CanvasRenderingContext2D.isContextLost()** method of the Canvas 2D API returns true if the rendering context is lost (and has not yet been reset). This might occur due to driver crashes, running out of memory, and so on.

If the user agent detects that the canvas backing storage is lost it will fire the [contextlost event](/en-US/docs/Web/API/HTMLCanvasElement/contextlost_event) at the associated [HTMLCanvasElement](/en-US/docs/Web/API/HTMLCanvasElement). If this event is not canceled it will attempt to reset the backing storage to the default state (this is equivalent to calling {{domxref("CanvasRenderingContext2D.reset()")}}). On success it will fire the [contextrestored event](/en-US/docs/Web/API/HTMLCanvasElement/contextrestored_event), indicating that the context is ready to reinitialize and redraw.

Syntax

js-nolint
isContextLost()

Parameters

None.

Return value

true if the rendering context was lost; false otherwise.

Examples

js
const ctx = canvas.getContext("2d");

if (ctx.isContextLost()) {
  console.log("Context is lost");
}

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also

  • The interface defining this method: {{domxref("CanvasRenderingContext2D")}}
  • [HTMLCanvasElement: contextlost event](/en-US/docs/Web/API/HTMLCanvasElement/contextlost_event)
  • [HTMLCanvasElement: contextrestored event](/en-US/docs/Web/API/HTMLCanvasElement/contextrestored_event)