---
type: snippet
tags: ['web-api', 'ambient-light-sensor']
status: release
ctime: 2023-03-24
mtime: 2024-03-22
---

```js
async function main({ onActivate, onError, onReading, onDenied }) {
  const result = await navigator.permissions.query({
    name: 'ambient-light-sensor',
  })

  if (result.state === 'denied') {
    onDenied()
    return
  }

  const ambientLightSensor = new AmbientLightSensor({ frequency: 20 })
  
  ambientLightSensor.addEventListener('activate', onActivate)
  ambientLightSensor.addEventListener('error', onError)
  ambientLightSensor.addEventListener('reading', () => {
    const ISO = 100
    const C = 250

    const EV = Math.round(Math.log2((ambientLightSensor.illuminance * ISO) / C))

    onReading(EV)
  })

  ambientLightSensor.start()
}
```

---

- [AmbientLightSensor - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/AmbientLightSensor)
- [Ambient Light Sensor](https://www.w3.org/TR/ambient-light/)
- [Sensors for the web - Chrome Developers](https://developer.chrome.com/articles/generic-sensor/)[^257-1]
- [Ambient Light Sensor | Can I use... Support tables for HTML5, CSS3, etc](https://caniuse.com/ambient-light)

---

[^257-1]: 주변광 센서 인터페이스가 포함된 일반 센서 API를 사용하는 방법을 설명