Académique Documents
Professionnel Documents
Culture Documents
PredicthanddistancewiththedevicelightJavaScriptAPI
Back
Blog Resources
devicelight
The device light event is a native JavaScript API accessing the light sensor built into
mobile phones and a few laptops. There are two events to use and lightlevel only
returns three states: dim, normal and bright. This is great but we are interested in
raw values. The other event called devicelight gives us exactly that. Here are the
examples outputting the returned values of both events:
if('ondevicelight'inwindow){
window.addEventListener('devicelight',function(event){
//lightlevelisreturnedinluxunits
console.log(event.value);
});
}
http://www.webondevices.com/predicthanddistancewithdevicelightjavascriptapi/
1/4
22/9/2016
PredicthanddistancewiththedevicelightJavaScriptAPI
if('onlightlevel'inwindow){
Back
Blog
window.addEventListener('lightlevel',function(event){
Resources
//lightvaluecanbedim,normalorbright
console.log(event.value);
Download FREE Ebook: Introduction to JavaScript Electronics
});
}
varaudioCtx=new(AudioContext||webkitAudioContext)(),
osc=audioCtx.createOscillator();
osc.type='triangle';
osc.connect(audioCtx.destination);
osc.start(0);
osc.frequency.value=numberFromLightSensor;
http://www.webondevices.com/predicthanddistancewithdevicelightjavascriptapi/
2/4
22/9/2016
PredicthanddistancewiththedevicelightJavaScriptAPI
You might ask why we didnt use the Proximity API for this task. Firstly, the
Leave a Reply
You must be logged in to post a comment.
Free Ebook
Step up your web developer career and learn hardware prototyping.
This ebook will get you started with JavaScript Arduino electronics development in a
couple of hours.
Email address
http://www.webondevices.com/predicthanddistancewithdevicelightjavascriptapi/
3/4
22/9/2016
PredicthanddistancewiththedevicelightJavaScriptAPI
Back
Blog Resources
Web on Devices
Electronics Hacking with JavaScript and other Web Technologies
Twitter
Mate Marschalko
Front-end Web Developer, Creative Technologist and Maker. Builds Internet connected devices for the Internet of
Things.
http://www.webondevices.com/predicthanddistancewithdevicelightjavascriptapi/
4/4