AmbientLightSensor

Limited availability

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.

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

Giao diện AmbientLightSensor của Sensor APIs trả về mức sáng hiện tại, hay độ rọi, của ánh sáng xung quanh thiết bị đang lưu trữ.

Để dùng cảm biến này, người dùng phải cấp quyền cho cảm biến thiết bị 'ambient-light-sensor' thông qua Permissions API.

Tính năng này có thể bị chặn bởi một Permissions Policy được đặt trên máy chủ của bạn.

EventTarget Sensor AmbientLightSensor

Bộ khởi tạo

AmbientLightSensor() Experimental

Tạo một đối tượng AmbientLightSensor mới.

Thuộc tính thể hiện

AmbientLightSensor.illuminance Read only Experimental

Trả về mức sáng hiện tại tính bằng lux của ánh sáng xung quanh thiết bị đang lưu trữ.

Phương thức thể hiện

AmbientLightSensor không có phương thức riêng. Tuy nhiên, nó kế thừa các phương thức từ các giao diện cha là SensorEventTarget.

Sự kiện

AmbientLightSensor không có sự kiện riêng. Tuy nhiên, nó kế thừa các sự kiện từ giao diện cha là Sensor.

Ví dụ

js
if ("AmbientLightSensor" in window) {
  const sensor = new AmbientLightSensor();
  sensor.addEventListener("reading", (event) => {
    console.log("Current light level:", sensor.illuminance);
  });
  sensor.addEventListener("error", (event) => {
    console.log(event.error.name, event.error.message);
  });
  sensor.start();
}

Thông số kỹ thuật

Specification
Ambient Light Sensor
# ambient-light-sensor-interface

Tương thích trình duyệt