Code & Coffeeption
Gmail (2004)
Flex (2004)
When to use?
Show information based on location
var info = document.getElementById('info');
navigator.geolocation.getCurrentPosition(function(position) {
var lat = position.coords.latitude;
var lon = position.coords.longitude;
info.innerHTML = 'Latitude: ' + lat + ' - longitude: ' + lon;
});
Chrome 10 | Safari 5 | Firefox 3.5 | IE 9 |
When to use?
Enhance UX in slideshows or videos
window.addEventListener('visibilitychange', function() {
if (document.hidden) {
document.title = 'Tab is hidden';
} else {
document.title = 'Tab is focused';
}
});
Chrome 16 | Safari 6.1 | Firefox 10 | IE 10 |
When to use?
Take photographs or record videos
var video = document.getElementById('video');
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var button = document.getElementById('snap');
navigator.mediaDevices.getUserMedia({video: true}).then(function(stream){
video.src = window.URL.createObjectURL(stream);
video.play();
});
button.addEventListener('click', function() {
context.drawImage(video, 0, 0, 640, 480);
});
Chrome 17 | -- | Firefox 17 | Edge 12 |
When to use?
Provide feedback to user
var info = document.getElementById('info');
var status = navigator.onLine ? 'online' : 'offline';
info.innerHTML = status;
window.addEventListener('offline', networkStatus);
window.addEventListener('online', networkStatus);
function networkStatus(e) {
info.innerHTML = e.type;
}
Chrome 16 | Safari 5 | Firefox 41 | IE 9 |
When to use?
Alert wrong data in forms
// Vibrate for 1 second
navigator.vibrate(1000);
// Vibrate with a pattern (vibrate, wait...)
navigator.vibrate([400, 300, 300, 200, 500]);
Chrome 30 | -- | Firefox 11 | -- |
When to use?
Alert user to charge their devices
Increase product prices 👹
var info = document.getElementById('info');
info.innerHTML = status;
navigator.getBattery().then(function(battery) {
info.innerHTML = battery.level * 100;
battery.addEventListener('levelchange', function() {
info.innerHTML = battery.level * 100;
});
});
Chrome 38 | -- | Firefox 43 | -- |