HTMLCanvasElement: contextlost イベント
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Experimental: これは実験的な機能です。
本番で使用する前にブラウザー互換性一覧表をチェックしてください。
contextlost
はキャンバス API のイベントで、 CanvasRenderingContext2D
コンテキストに関連付けられたバックストレージが失われたことをユーザーエージェントが検出すると、発行されます。
コンテキストは、ドライバーのクラッシュやアプリケーションのメモリー不足など、いくつかの理由で失われることがあります。
既定では、ユーザーエージェントはコンテキストの復元を試み、contextrestored
イベントを発行します。
ユーザーコードは、イベント処理中に Event.preventDefault()
を呼び出すことにより、コンテキストが復元されるのを防ぐことができます。
構文
このイベント名を addEventListener()
などのメソッドで使用したり、イベントハンドラープロパティを設定したりしてください。
addEventListener("contextlost", (event) => {});
oncontextlost = (event) => {};
イベント型
一般的な Event
です。
例
次のコードは、contextlost
イベントを検出するものです。
const canvas = document.getElementById("canvas");
canvas.addEventListener("contextlost", (event) => {
console.log(event);
});
コンテキストが復元されないようにするためのコードは次のようになります。
const canvas = document.getElementById("canvas");
canvas.addEventListener("contextlost", (event) => {
event.preventDefault();
});
仕様書
Specification |
---|
HTML # event-contextlost |
ブラウザーの互換性
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
contextlost event |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- No support
- No support