DOM

Element.requestPointerLock() メソッド

 編集:2022.09.23 by T.A.

[Edge]。実験的。→

実験的: これは実験的な機能です。本番で使用する前にブラウザー互換性一覧表をチェックしてください。

elementポインターをロックすることをブラウザーに要求します。

リクエストが成功したか失敗したかを追跡するには、pointerlockchange および pointerlockerror イベントを Document レベルで待ち受けする必要があります。

構文
element.requestPointerLock()
element Element
戻り値

HTML:

<DIV id=IdTest style="background-color:#ddd; width:100px">[DIV要素]</DIV>

JavaScript: 以下を組み込む。

function JobStart(eve){     //ポインターが押された時のイベント処理:
  IdTest.onpointermove=Job; //ポインターが動いた時の処理を割り当てる。
  IdTest.requestPointerLock();
  IdTest.setPointerCapture(eve.pointerId); //ポインター キャプチャーの設定?
}
function JobEnd(eve){        //ポインターが放された時のイベント処理:
  IdTest.onpointermove=null; //ポインターが動いた時の処理を外す。
  IdTest.releasePointerCapture(eve.pointerId); //ポインター キャプチャーの解放。
}
function Job(eve){  //ポインターが動いた時のイベント処理:
  IdTest.style.transform="translate("+(eve.clientX-80)+"px"; //要素を移動させる。
}
function JobErr(eve){
  alert(eve.message);
}
IdTest.onpointerdown=JobStart; //ポインターが押された時のイベント処理の割り当て。
IdTest.onpointerup=JobEnd;     //ポインターが放された時のイベント処理の割り当て。
IdTest.onpointerlockerror=JobErr;

setPointerCapture()pointermovepointerdownpointerup


テスト