OffscreenCanvas: contextlost イベント
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
contextlost
は OffscreenCanvas
インターフェイスのイベントで、ブラウザーが OffscreenCanvasRenderingContext2D
コンテキストが失われたことを検出すると発行されます。 コンテキストは、関連付けられた GPU ドライバのクラッシュ、アプリケーションのメモリー不足、といった具合に、いくつかの理由で失われることがあります。
既定では、ユーザーエージェントはコンテキストの復元を試み、その後 contextrestored
イベントを発行します。 ユーザーコードは、イベント処理中に Event.preventDefault()
を呼び出すことで、コンテキストの復元を防ぐことができます。
構文
このイベント名を addEventListener()
などのメソッドで使用するか、イベントハンドラープロパティを設定するかしてください。
js
addEventListener("contextlost", (event) => { }) oncontextlost = (event) => { }
イベント型
汎用的な Event
です。
例
下記のコードは、contextlost
イベントを検出します。
js
const canvas = new OffscreenCanvas(256, 256); const gl = offscreen.getContext("2d"); // 描画などを行う canvas.addEventListener("contextlost", (event) => { console.log(event); });
コンテキストが復元されないようにするには、イベントハンドラーのコードを次のように変更します。
js
canvas.addEventListener("contextlost", (event) => { event.preventDefault(); });
仕様書
Specification |
---|
HTML> # event-contextlost> |
HTML> # handler-offscreencanvas-oncontextlost> |
ブラウザーの互換性
Loading…