@@ -143,27 +143,22 @@ Examples {#examples}
143
143
it is printed to the console.
144
144
145
145
<pre highlight="js">
146
- const sensor = new AmbientLightSensor();
147
- sensor.onreading = () => console.log(sensor.illuminance);
148
- sensor.onerror = event => console.log(event.error.name, event.error.message);
149
- sensor.start();
146
+ navigator.mediaDevices.getUserMedia({video: true}).then(() => {
147
+ const sensor = new AmbientLightSensor();
148
+ sensor.onreading = () => console.log(sensor.illuminance);
149
+ sensor.onerror = event => console.log(event.error.name, event.error.message);
150
+ sensor.start();
151
+ });
150
152
</pre>
151
153
</div>
152
154
153
155
<div class="example">
154
156
In this example, the exposure value (EV) at ISO 100 is calculated from
155
- the ambient light [=sensor readings=] . Initially, we check that the user
156
- agent has permissions to access ambient light [=sensor readings=] . Then,
157
- the {{AmbientLightSensor/illuminance!!attribute}} value is converted to the
158
- closest exposure value.
157
+ the ambient light [=sensor readings=] . The {{AmbientLightSensor/illuminance}}
158
+ value is converted to the closest exposure value.
159
159
160
160
<pre highlight="js">
161
- navigator.permissions.query({ name: 'ambient-light-sensor' }).then(result => {
162
- if (result.state === 'denied' ) {
163
- console.log('Permission to use ambient light sensor is denied.' );
164
- return;
165
- }
166
-
161
+ navigator.mediaDevices.getUserMedia({ video: true }).then(() => {
167
162
const als = new AmbientLightSensor({frequency: 20});
168
163
als.addEventListener('activate' , () => console.log('Ready to measure EV.' ));
169
164
als.addEventListener('error' , event => console.log(\`Error: ${event.error.name}\`));
@@ -187,16 +182,20 @@ Examples {#examples}
187
182
to recommended workplace light levels.
188
183
189
184
<pre highlight="js">
190
- const als = new AmbientLightSensor();
191
-
192
- als.onreading = () => {
193
- let str = luxToWorkplaceLevel(als.illuminance);
194
- if (str) {
195
- console.log(\`Light level is suitable for: ${str}.\`);
196
- }
197
- };
198
-
199
- als.start();
185
+ try {
186
+ const stream = await navigator.mediaDevices.getUserMedia({video: true});
187
+
188
+ const als = new AmbientLightSensor();
189
+ als.onreading = () => {
190
+ let str = luxToWorkplaceLevel(als.illuminance);
191
+ if (str) {
192
+ console.log(\`Light level is suitable for: ${str}.\`);
193
+ }
194
+ };
195
+ als.start();
196
+ } catch (err) {
197
+ console.error(err);
198
+ }
200
199
201
200
function luxToWorkplaceLevel(lux) {
202
201
if (lux > 20 && lux < 100) {
0 commit comments