PositionSensorVRDevice: getState() method
Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
Non-standard: This feature is not standardized. We do not recommend using non-standard features in production, as they have limited browser support, and may change or be removed. However, they can be a suitable alternative in specific cases where no standard option exists.
Phương thức getState() của giao diện PositionSensorVRDevice trả về trạng thái hiện tại của cảm biến vị trí cho frame hiện tại (ví dụ, trong callback window.requestAnimationFrame hiện tại) hoặc cho frame trước, được chứa trong đối tượng VRPose. Đây là phương thức bạn thường muốn sử dụng thay vì PositionSensorVRDevice.getImmediateState.
Cú pháp
getState()
Tham số
Không có.
Giá trị trả về
Một đối tượng VRPose.
Ví dụ
Ví dụ sau đây sử dụng WebVR API để cập nhật view của một cảnh CanvasRenderingContext2D đơn giản trên mỗi frame của vòng lặp requestAnimationFrame.
function setView() {
const posState = gPositionSensor.getState();
if (posState.hasPosition) {
posPara.textContent = `Position: x${roundToTwo(
posState.position.x,
)} y${roundToTwo(posState.position.y)} z${roundToTwo(posState.position.z)}`;
xPos = -posState.position.x * WIDTH * 2;
yPos = posState.position.y * HEIGHT * 2;
zPos = -posState.position.z > 0.01 ? -posState.position.z : 0.01;
}
if (posState.hasOrientation) {
orientPara.textContent = `Orientation: x${roundToTwo(
posState.orientation.x,
)} y${roundToTwo(posState.orientation.y)} z${roundToTwo(
posState.orientation.z,
)}`;
xOrient = posState.orientation.x * WIDTH;
yOrient = -posState.orientation.y * HEIGHT * 2;
zOrient = posState.orientation.z * 180;
}
}