RelativeOrientationSensor
Khả dụng hạn chế
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
Giao diện RelativeOrientationSensor của Sensor APIs mô tả hướng vật lý của thiết bị mà không cần tính đến hệ tọa độ tham chiếu của Trái Đất.
Để sử dụng cảm biến này, người dùng phải cấp quyền cho các cảm biến thiết bị 'accelerometer' và 'gyroscope' thông qua Permissions API. Ngoài ra, tính năng này có thể bị chặn bởi Chính sách Quyền được đặt trên máy chủ của bạn.
Hàm khởi tạo
RelativeOrientationSensor()-
Tạo một đối tượng
RelativeOrientationSensormới.
Thuộc tính phiên bản
Không có thuộc tính cụ thể; kế thừa các thuộc tính từ tổ tiên OrientationSensor và Sensor.
Phương thức phiên bản
Không có phương thức cụ thể; kế thừa các phương thức từ tổ tiên OrientationSensor và Sensor.
Sự kiện
Không có sự kiện cụ thể; kế thừa các sự kiện từ tổ tiên, Sensor.
Ví dụ
>Ví dụ cơ bản
Ví dụ sau, dựa trên demo Orientation Phone của Intel, khởi tạo một RelativeOrientationSensor với tần số 60 lần mỗi giây.
Note:
Demo Intel mà ví dụ này dựa trên sử dụng AbsoluteOrientationSensor. Trong mỗi lần đọc, nó dùng OrientationSensor.quaternion để xoay một mô hình điện thoại trực quan.
const options = { frequency: 60, referenceFrame: "device" };
const sensor = new RelativeOrientationSensor(options);
sensor.addEventListener("reading", () => {
// model is a Three.js object instantiated elsewhere.
model.quaternion.fromArray(sensor.quaternion).inverse();
});
sensor.addEventListener("error", (error) => {
if (event.error.name === "NotReadableError") {
console.log("Sensor is not available.");
}
});
sensor.start();
Ví dụ về Quyền
Sử dụng cảm biến hướng yêu cầu yêu cầu quyền cho nhiều cảm biến thiết bị. Vì giao diện Permissions sử dụng promise, cách tốt để yêu cầu quyền là dùng Promise.all.
const sensor = new RelativeOrientationSensor();
Promise.all([
navigator.permissions.query({ name: "accelerometer" }),
navigator.permissions.query({ name: "gyroscope" }),
]).then((results) => {
if (results.every((result) => result.state === "granted")) {
sensor.start();
// …
} else {
console.log("No permissions to use RelativeOrientationSensor.");
}
});
Thông số kỹ thuật
| Thông số kỹ thuật |
|---|
| Orientation Sensor> # relativeorientationsensor-interface> |