Fun with Browsers and Sensor APIs

A presentation at Smashing Meets Day Two in May 2020 in by Mandy Michael

Slide 1

Slide 1

Fun with Sensors and Browser Apis

Slide 2

Slide 2

Twitter: @mandy_kerr Codepen: @mandymichael

Slide 3

Slide 3

Jello the little fluffy fellow - my dog! :D Instagram: adognamedjello

Slide 4

Slide 4

Experimental Features

What i would like you to remember is that the talk is called “FUN” with sensors and browser apis, because i like to play and tinker, so that is what my examples are going to be. I haven’t built anything particularly serious with it, but I see this as a good opportunity to start thinking about how you might be able to make the most of them in your work, experiments or side projects.

Some of my examples will have really good browser support, and some are still very experimental, but i’d say remember that Everything we have on the web started out as a piece of experimental technology so let’s see what we can do with it and have a little fun along the way.

Slide 5

Slide 5

Support Legend

full support (Logo on its own) behind a flag (logo with a flag) coming soon (logo with a star)

Slide 6

Slide 6

Slide 7

Slide 7

“ The Web Speech API enables you to incorporate voice data into web apps. ” @mandy_kerr

Slide 8

Slide 8

Slide 9

Slide 9

new SpeechRecognition(); //constructor for SpeechRecognition object instance @mandy_kerr

Slide 10

Slide 10

recognition.start(); // method to start speech recognition service recognition.stop(); // stops the speech recognition service from listening to audio returns a result @mandy_kerr

Slide 11

Slide 11

recognition.onresult = function(event) {} // event handler when a word or phrase has been recognised recognition.onspeechend = function(event) {} // will run when the speech recognition service has stopped being detected @mandy_kerr

Slide 12

Slide 12

// get SpeechRecognitionResultList object const resultList = event.results; // get first SpeechRecognitionResult object const firstResult = resultList[0]; // get this result’s first SpeechRecognitionAlternative object const firstAlternative = firstResult[0].transcript; @mandy_kerr

Slide 13

Slide 13

Result Alternative List Object const speechResult = event.results[0][0].transcript; Result Text content @mandy_kerr

Slide 14

Slide 14

var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition; function testSpeech() { const recognition = new SpeechRecognition(); const diagnostic = document.querySelector(‘.output’); recognition.start(); recognition.onresult = function(event) { const speechResult = event.results[0][0].transcript; diagnostic.textContent = speechResult; } recognition.onspeechend = function() { recognition.stop(); } } document.body.onclick = function() { testSpeech(); } @mandy_kerr

Slide 15

Slide 15

Slide 16

Slide 16

@mandy_kerr

Slide 17

Slide 17

“ Audio & speech allow for more interactive and more accessible experiences. ” @mandy_kerr

Slide 18

Slide 18

n o i t a t n e ri O @mandy_kerr

Slide 19

Slide 19

“ The accelerometer detects a change to the orientation of the device. ” @mandy_kerr

Slide 20

Slide 20

DeviceOrientationEvent @mandy_kerr

Slide 21

Slide 21

event.alpha; // The direction the device is facing // Z Axis // Ranging from 0 to 360 @mandy_kerr

Slide 22

Slide 22

event.beta; // The front to back motion of the device // X Axis // Ranging from -180 to 180 @mandy_kerr

Slide 23

Slide 23

event.gamma; // The left to right motion of the device // Y Axis // Ranging from -90 to 90 @mandy_kerr

Slide 24

Slide 24

if (window.DeviceOrientationEvent) { window.addEventListener(‘deviceorientation’, deviceOrientationFunction); } @mandy_kerr

Slide 25

Slide 25

function deviceOrientationHandler(event) { const currentGamma = event.gamma; if (currentGamma < -50) { // do something } } @mandy_kerr

Slide 26

Slide 26

@mandy_kerr

Slide 27

Slide 27

@mandy_kerr

Slide 28

Slide 28

@mandy_kerr

Slide 29

Slide 29

@mandy_kerr

Slide 30

Slide 30

“ We can move on from static, predetermined interactions and allow the user to shape the experience. ” @mandy_kerr

Slide 31

Slide 31

Slide 32

Slide 32

“ A photodetector that is used to sense the amount of ambient light present. ” @mandy_kerr

Slide 33

Slide 33

A second attempt at the ambient light sensor. @mandy_kerr

Slide 34

Slide 34

Addressing the security concerns of light sensors in the browser @mandy_kerr

Slide 35

Slide 35

const sensor = new AmbientLightSensor(); // Creates a new AmbientLightSensor object. @mandy_kerr

Slide 36

Slide 36

sensor.onreading // returns the current light level @mandy_kerr

Slide 37

Slide 37

sensor.illuminance // returns the current light level (lux) @mandy_kerr

Slide 38

Slide 38

if ( ‘AmbientLightSensor’ in window ) { const sensor = new AmbientLightSensor(); sensor.onreading = () => { if ( sensor.illuminance < 20) { element.style.setProperty(‘—bg’, ‘black’); } else { element.style.setProperty(‘—bg’, ‘blue’); } }; sensor.start(); } @mandy_kerr

Slide 39

Slide 39

Slide 40

Slide 40

Font: Tiny by Jack Halten Fahnestock @mandy_kerr

Slide 41

Slide 41

Font: BloomGX by Typearture (Arthur Reinders Folmer) @mandy_kerr

Slide 42

Slide 42

“ Don’t be limited by what we can already do, the web is still young and there is so much for us to create. ” @mandy_kerr

Slide 43

Slide 43

@mandy_kerr

Slide 44

Slide 44

Slide 45

Slide 45

@mandy_kerr

Slide 46

Slide 46

developer.mozilla.org/docs/Web/API developer.mozilla.org/docs/Web/API/Sensor_APIs developer.mozilla.org/docs/Web/API/SpeechRecognition developer.mozilla.org/docs/Web/API/AmbientLightSensor developer.mozilla.org/docs/Web/API/DeviceOrientationEvent @mandy_kerr