Skip to content

Instantly share code, notes, and snippets.

@Fabiantjoeaon
Last active October 30, 2016 16:02
Show Gist options
  • Save Fabiantjoeaon/1c3d7fd00198d5d6cb74e7c57de45b81 to your computer and use it in GitHub Desktop.
Save Fabiantjoeaon/1c3d7fd00198d5d6cb74e7c57de45b81 to your computer and use it in GitHub Desktop.
Useful HTML5 Web API uses, most change in the future, and have limited browser support, so be sure to look these up before implementing them. src: https://www.youtube.com/watch?v=NCGLPp778JY
// Listen for ambient lighting in room
window.addEventListener('devicelight', (e) => {
console.log(`${e.value} lux`);
});
// Check battery status of device through the browser
navigator.getBattery().then((battery) => {
console.log(`${battery.level * 100}%`);
battery.addEventListener('levelchange', () => {
console.log(`${this.level * 100}%`);
});
});
// Listen for device orientatin
window.addEventListener('deviceorientation', (e) => {
console.log('Gamma: ', e.gamma);
console.log('Beta: ', e.beta);
console.log('Gamma: ', e.alpha);
});
// Check if user is online of offline
console.log(navigator.onLine ? 'online' : 'offline');
// Event listener switch for visible tab using Page Visibility
window.addEventListener('visibilitychange', () => {
switch(document.visibilityState) {
case 'prerender':
console.log('Tab is pre-rendering');
break;
case 'hidden':
console.log('Tab is hidden');
break;
case 'visible':
console.log('Tab is focused');
break;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment