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