Skip to content

Commit 3b59517

Browse files
author
Raphael Kubo da Costa
committed
Update examples to show Media Capture integration.
1 parent 42297a2 commit 3b59517

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

index.bs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -142,27 +142,22 @@ Examples {#examples}
142142
it is printed to the console.
143143

144144
<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+
});
149151
</pre>
150152
</div>
151153

152154
<div class="example">
153155
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.
158158

159159
<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(() => {
166161
const als = new AmbientLightSensor({frequency: 20});
167162
als.addEventListener('activate', () => console.log('Ready to measure EV.'));
168163
als.addEventListener('error', event => console.log(\`Error: ${event.error.name}\`));
@@ -186,16 +181,20 @@ Examples {#examples}
186181
to recommended workplace light levels.
187182

188183
<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+
}
199198

200199
function luxToWorkplaceLevel(lux) {
201200
if (lux > 20 && lux < 100) {

0 commit comments

Comments
 (0)