Published
Edited
Jun 6, 2020
Insert cell
md`# Sensors?`
Insert cell
{
navigator.permissions.query({ name: 'accelerometer' })
.then(result => {
if (result.state === 'denied') {
console.log('Permission to use accelerometer sensor is denied.');
return;
}
// Use the sensor.
});
}
Insert cell
sensors =({})
Insert cell
{
function orientationhandler(evt){


// For FF3.6+
if (!evt.gamma && !evt.beta) {
evt.gamma = -(evt.x * (180 / Math.PI));
evt.beta = -(evt.y * (180 / Math.PI));
}
console.log(evt)
sensors.or = evt

// use evt.gamma, evt.beta, and evt.alpha
// according to dev.w3.org/geo/api/spec-source-orientation


}

window.addEventListener('deviceorientation', orientationhandler, false);
window.addEventListener('MozOrientation', orientationhandler, false);
}
Insert cell
{
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success);
}

function success(position) {
console.log('Latitude: ' + position.coords.latitude);
console.log('Longitude: ' + position.coords.longitude);
sensors.location = position.coords
}
}
Insert cell
{
if (window.DeviceOrientationEvent) {
window.addEventListener('deviceorientation', function(eventData) {
// gamma is the left-to-right tilt in degrees
console.log(eventData.gamma);

// beta is the front-to-back tilt in degrees
console.log(eventData.beta);

// alpha is the compass direction the device is facing in degrees
console.log(eventData.alpha);
sensors.orient = eventData
}, false);
}
}

Insert cell
navigator.DeviceOrientationEvent
Insert cell
{
if (window.DeviceMotionEvent) {
window.addEventListener('devicemotion', function(eventData) {
// Acceleration
console.log(eventData.acceleration.x);
console.log(eventData.acceleration.y);
console.log(eventData.acceleration.z);
// Acceleration including gravity
console.log(eventData.accelerationIncludingGravity.x);
console.log(eventData.accelerationIncludingGravity.y);
console.log(eventData.accelerationIncludingGravity.z);

// Rotation rate
console.log(eventData.rotationRate.alpha);
console.log(eventData.rotationRate.beta);
console.log(eventData.rotationRate.gamma);
sensors.motion = eventData
}, false);
}
}
Insert cell
{
var vibrate = navigator.vibrate || navigator.mozVibrate;

// vibrate for 1 second
vibrate(1000);

// vibrate for 1 second, then pause for half, then vibrate for another 1 second
vibrate([1000, 500, 2000]);
}
Insert cell
{
var battery = navigator.battery || navigator.webkitBattery || navigator.mozBattery;

function logBattery(battery) {
console.log(battery.level);
console.log(battery.charging);
console.log(dischargingTime);

battery.addEventListener('chargingchange', function() {
console.log('Battery chargingchange event: ' + battery.charging);
}, false);
}

if (navigator.getBattery) {
navigator.getBattery().then(logBattery);
} else if (battery) {
logBattery(battery);
}
}
Insert cell
{
if('ondevicelight' in window) {
window.addEventListener("devicelight", function(event) {
//light level is returned in lux units
console.log(event.value + " lux");
});
}

if('onlightlevel' in window){
window.addEventListener("lightlevel", function(event) {
//light value can be dim, normal or bright
console.log(event.value);
});
}
}
Insert cell
{
if('ondeviceproximity' in window) {
// Fired when object is in the detection zone
window.addEventListener('deviceproximity', function(event) {
// Object distance in centimeters
console.log(event.value + " centimeters");
});
} else {
console.log("deviceproximity not supported");
}

if('ondeviceproximity' in window){
// Fired when object is in the detection zone
window.addEventListener('userproximity', function(event) {
if(event.near == true) {
console.log("Object is near");
} else {
console.log("Object is far");
}
});
} else {
console.log("userproximity not supported");
}
}
Insert cell
{
window.addEventListener("deviceorientation", handleOrientation, true);
function handleOrientation(event) {
var absolute = event.absolute;
var alpha = event.alpha;
var beta = event.beta;
var gamma = event.gamma;
sensors.ori = event

// Do stuff with the new orientation data
}
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more