Skip to content

Commit 06e40ff

Browse files
author
Raphael Kubo da Costa
committed
Update examples to show Media Capture integration.
1 parent 6d3001d commit 06e40ff

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
@@ -143,27 +143,22 @@ Examples {#examples}
143143
it is printed to the console.
144144

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

153155
<div class="example">
154156
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.
159159

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

189184
<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+
}
200199

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

0 commit comments

Comments
 (0)