Last active
January 11, 2021 19:04
-
-
Save bellbind/c60d7008e86c34a76aa1 to your computer and use it in GitHub Desktop.
[html5] Sensors API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<script src="script.js"></script> | |
</head> | |
<body> | |
<pre id="log" style="color: red; background-color: blue;"></pre> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.addEventListener("load", function (ev) { | |
"use strict"; | |
var log = document.getElementById("log"); | |
// https://dvcs.w3.org/hg/dap/raw-file/tip/sensor-api/Overview.html | |
window.addEventListener("devicetemperature", function (ev) { | |
log.textContent += "devicetemperature " + ev.value + "\n"; | |
}, false); | |
window.addEventListener("devicepressure", function (ev) { | |
log.textContent += "devicepressure " + ev.value + "\n"; | |
}, false); | |
window.addEventListener("devicelight", function (ev) { | |
log.textContent += "devicelight " + ev.value + "\n"; | |
// toy tric | |
log.style.color = "rgb(" + (255 - 2*ev.value) + ",0,0)"; | |
log.style.backgroundColor = "rgb(0,0," + (2*ev.value) + ")"; | |
}, false); | |
window.addEventListener("deviceproximity", function (ev) { | |
log.textContent += "deviceproximity " + ev.value + "\n"; | |
// toy tric | |
if (ev.value < 3) navigator.vibrate([300, 100, 100]); | |
}, false); | |
window.addEventListener("devicenoise", function (ev) { | |
log.textContent += "devicenoise " + ev.value + "\n"; | |
}, false); | |
window.addEventListener("devicehumidity", function (ev) { | |
log.textContent += "devicehumidity " + ev.value + "\n"; | |
}, false); | |
//https://wiki.mozilla.org/Magnetic_Field_Events | |
window.addEventListener("devicemagneticfield", function (ev) { | |
log.textContent += "devicemagneticfield " + [ev.x, ev.y, ev.x]+ "\n"; | |
}, false); | |
// https://dvcs.w3.org/hg/dap/raw-file/default/pressure/Overview.html | |
window.addEventListener("atmpressure", function (ev) { | |
log.textContent += "atmpressure " + ev.value + "\n"; | |
}, false); | |
// https://dvcs.w3.org/hg/dap/raw-file/tip/humidity/Overview.html | |
window.addEventListener("humidity", function (ev) { | |
log.textContent += "humidity " + ev.value + "\n"; | |
}, false); | |
// https://dvcs.w3.org/hg/dap/raw-file/tip/temperature/Overview.html | |
window.addEventListener("temperature", function (ev) { | |
log.textContent += "temperature " + [ev.f, ev.c, ev.k, ev.value] + "\n"; | |
}, false); | |
// https://dvcs.w3.org/hg/dap/raw-file/tip/battery/Overview.html | |
try { | |
if (typeof navigator.getBattery === "function") { | |
navigator.getBattery().then(function (battery) { | |
log.textContent += "battery.level " + battery.level + "\n"; | |
log.textContent += "battery.charging " + battery.charging + "\n"; | |
log.textContent += "battery.chargeTime " + battery.chargeTime + "\n"; | |
log.textContent += "battery.dischargeTime " + battery.dischargeTime + "\n"; | |
battery.addEventListener("levelcharge", function (ev) { | |
log.textContent += "change battery.level " + battery.level + "\n"; | |
}, false); | |
}).catch(function (err) { | |
log.textContent += err.toString() + "\n"; | |
}); | |
} else { | |
log.textContent += "navigator.getBattery is not existed\n"; | |
} | |
} catch (ex) { | |
log.textContent += ex.toString() + "\n"; | |
} | |
}, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this.
Do you have any insight into which sources of DOM sensor data {devicetemperature, devicepressure, devicehumidity, devicelight, devicenoise, deviceproximity} are available on which hardware devices, operating systems, etc.? Someone with substantial traffic must also be sampling this, right?
REVISED: http://mobilehtml5.org/
Ben