--- title: Eventos de Luz Ambiente slug: Web/API/Eventos_de_Luz_Ambiente tags: - Luz Ambiente translation_of: Web/API/Ambient_Light_Events ---
{{DefaultAPISidebar("Ambient Light Events")}}{{SeeCompatTable}}

Os eventos de luz ambiente são uma maneira prática de tornar uma página da Web ou um aplicação ciente de qualquer alteração na intensidade da luz. Isto permite-lhes reagir a essa alteração, por exemplo, alterando o contraste da cor da interface do utilizador (IU) ou alterando a exposição necessária para tirar uma fotografia.

Eventos de Luz

When the light sensor of a device detects a change in the light level, it notifies the browser of that change. When the browser gets such a notification, it fires a {{domxref("DeviceLightEvent")}} event that provides information about the exact light intensity.

This event can be captured at the window object level by using the {{domxref("EventTarget.addEventListener","addEventListener")}} method (using the {{event("devicelight")}} event name) or by attaching an event handler to the {{domxref("window.ondevicelight")}} property.

Once captured, the event object gives access to the light intensity expressed in lux through the {{domxref("DeviceLightEvent.value")}} property.

Exemplo

if ('ondevicelight' in window) {
  window.addEventListener('devicelight', function(event) {
    var body = document.querySelector('body');
    if (event.value < 50) {
      body.classList.add('darklight');
      body.classList.remove('brightlight');
    } else {
      body.classList.add('brightlight');
      body.classList.remove('darklight');
    }
  });
} else {
  console.log('devicelight event not supported');
}

Especificações

Especificação Estado Comentário
{{SpecName("AmbientLight", "", "Ambient Light Events")}} {{Spec2("AmbientLight")}} Initial definition

Compatibilidade de navegador

{{Compat("api.DeviceLightEvent")}}

 

Consultar também