HTML5 APIs You’ve Never Heard Of

A presentation at PHP Yorkshire in April 2017 in York, UK by Drew McLellan

Slide 1

Slide 1

HTML5 APIs YOU’VE NEVER HEARD OF

  • PHP YORKSHIRE 2017

Slide 2

Slide 2

HTML5… APIs?

Slide 3

Slide 3

Front end Back end HTML5 APIs

Slide 4

Slide 4

The web is changing rapidly

Slide 5

Slide 5

Ch-ch-ch-changes Screens are getting smaller, and bigger, and rounder, and wider, and taller, and wearable. Pointing devices are becoming meatier. We have access to many more hardware features. Web browsers are on the move. Power consumption has become a concern.

Slide 6

Slide 6

Things we can do Access device sensors like the gyroscope, compass, light meter, GPS, camera, microphone. Control device outputs like the speaker, vibration motor and screen. Establish more app-like control of the environment our code is running in.

Slide 7

Slide 7

Page Visibility https://www.w3.org/TR/page-visibility/

Slide 8

Slide 8

Page Visibility Enables us to programmatically determine if a page is currently visible. A page might be hidden if the window is minimised, if the page is in a background tab, or if the lock screen is shown.
(Plus a few transitionary states.)

Slide 9

Slide 9

Testing for visibility // Is the document visible? var visible

! document .hidden; // Listen for changes document .addEventListener( "visibilitychange" , function (){ console.log( 'Visibility changed!' ); }); The visibility of the document can be tested. You can add an event listener to be informed of when the visibility changes.

Slide 10

Slide 10

When is it useful? Stopping ‘expensive’ operations like animation. Ensuring that the user sees important information like fl ash noti fi cations or alerts. Pausing media, where appropriate.

Slide 11

Slide 11

Browser support

Slide 12

Slide 12

Use it today! There are lots of small cases where using this simple API will provide a better experience for your users.

Slide 13

Slide 13

Device Orientation https://www.w3.org/TR/orientation-event/

Slide 14

Slide 14

Device Orientation DOM events that provide information about the physical orientation and motion of a device. This is mostly useful for mobile phones and tablets. Enables us to write code that detects physical movements like rotation around a point and rate of rotation. Not to be confused with screen orientation (portrait / landscape).

Slide 15

Slide 15

Device Orientation // Test for support if ( 'ondeviceorientation'

in

window ) {

// we have support for 'deviceorientation' events }; // Listen for orientation changes document .addEventListener( "deviceorientation" , 


function (event){ console.log(event); }); The API provides browser DOM events that we can attach listeners to. The events are fi red rapidly, so might need to be throttled (like we do with window scroll events).

Slide 16

Slide 16

Device Orientation // Access orientation properties function (event){

var alpha

event.alpha;

var beta

event.beta;

var gamma

event.gamma; }; // A device flat on a horizontal surface { alpha :

90 , beta :

0 , gamma :

0

} var compass_heading

( 360

alpha); Orientation values are reported as alpha, beta and gamma properties. These are a series of rotations from a local coordinate frame. They can be used to calculate compass headings with some crazy mathematics… which is all very usefully in the spec.

Slide 17

Slide 17

Device Orientation Orientation is expressed in a di ff erence between the Earth frame and the device frame. Here they are aligned. This horrible image is from the spec, sorry.

Slide 18

Slide 18

Device Orientation This marvellous work of art is showing the device rotated around the Z axis. The value of Z remains the same, and X and Y change. This results in a change to the alpha value.

Slide 19

Slide 19

Device Orientation The beta value changes with rotation around the X axis.

Slide 20

Slide 20

Device Orientation The gamma value changes with rotation around the Y axis. You’re probably best to just try it. It makes more sense in action.

Slide 21

Slide 21

When is it useful? Good for creating ‘physical world’ interactions. It’s the same sensors that the Facebook mobile app uses for displaying panoramas. Could be used for game control. Makes physical gestures possible (e.g. shake to undo). Align a map to match reality…

Slide 22

Slide 22

Browser support

Slide 23

Slide 23

Browser support Support for orientation is pretty wide in mobile browsers. Missing support is often for the ever so exciting compassneedscalibration event.

Slide 24

Slide 24

Start experimenting! Browser support is not too bad. Could be interesting to use for prototypes and small projects. There might be ways motion detection could be useful for applications other than just updating a view on the screen. Lots of potential uses in mapping, gaming and health applications.

Slide 25

Slide 25

Battery Status http://www.w3.org/TR/battery-status/

Slide 26

Slide 26

Battery Status Enables us to programatically monitor the status of the device’s battery. We can see if the battery is charging or discharging, how long it will take to charge or discharge, and what the current battery level is. The interface is Promise-based.

Slide 27

Slide 27

Battery status navigator.getBattery().then( function (battery) { console.log(battery.level);

// Listen for updates battery.addEventListener( 'levelchange' , function (){ console.log( this .level); }); }); The navigator object exposes a getBattery promise. If the device has multiple batteries, the browser’s BatteryManager interface exposes a uni fi ed view. Battery level is between 0 and 1.

Slide 28

Slide 28

Battery status navigator.getBattery().then( function (battery) {

if (battery.charging) { 
 console.log( '%d mins until full' , 


Math .floor(battery.chargingTime / 60 )); 
 } else { 
 console.log( '%d mins until empty' , 


Math .floor(battery.dischargingTime / 60 )); 
 } }); By checking if the battery is charging or discharging, we can then get the time left until that action completes. If the battery is charging and we ask for the discharge time, it will be positive in fi nity which is useful to no one. The charging and discharging times are in seconds.

Slide 29

Slide 29

When is it useful? If a user’s battery is low, you might scale back on any battery-intensive actions. You might want to save the user’s progress to the server or local storage if the battery is critically low. You might perform network polls frequently when charging, but infrequently when discharging.

Slide 30

Slide 30

Browser support

Slide 31

Slide 31

Use it when available If the battery status is available, you can make use of it. If not, just carry on with whatever you were doing before. Those with supporting devices will get the bene fi t, and that’s all you can do. Not just phones - laptops too!

Slide 32

Slide 32

Vibration https://www.w3.org/TR/vibration/

Slide 33

Slide 33

Vibration Gives us access to the vibration mechanism of the device. That’s usually a phone or perhaps a tablet. Designed for simple tactile feedback only, nothing fancy.

Slide 34

Slide 34

Vibration // Vibrate for 1000 ms navigator.vibrate( 1000 ); // Vibration to a pattern navigator.vibrate([ 150 , 50 , 150 ]); // Cancel any vibrations navigator.vibrate( 0 ); Vibration time is set in milliseconds. When an array is given, the even items are vibrations, the odd items are pauses. This enables more complex patterns. Any ongoing vibration can be cancelled.

Slide 35

Slide 35

When is it useful? Providing tactile feedback for important actions. Could be used as a rumble in games. Create a cool Morse code device?

Slide 36

Slide 36

Browser support

Slide 37

Slide 37

Use it! Works in most mobile browsers other than iOS Safari. Should be safe to use as an extra where it is supported. Don’t design interactions that rely on it, and maybe check battery status too!

Slide 38

Slide 38

Web Noti fi cations http://www.w3.org/TR/noti fi cations/

Slide 39

Slide 39

Web Noti fi cations Enable us to issue an alert to the user outside the context of the web page. This is normally through the operating system’s standard alerts mechanism. Users must grant permission before noti fi cations can be shown.

Slide 40

Slide 40

Web Noti fi cations if ( 'Notification'

in

window ) {

// Notifications are supported! } Notification .requestPermission( function (status) {

if (status

'granted' ) {

// We have permission to notify! }; }); We can test for the Noti fi cation property of the window object to see if we have support. Before sending a noti fi cation, we need to request permission. This call returns either ‘granted’, ‘denied’ or ‘default’. We can only send a noti fi cation when the result is ‘granted’.

Slide 41

Slide 41

Web Noti fi cations var notification

new Notification( 


'Your life is in danger' , 
 { body :

'You forgot to take your pills' , icon :

'skull-and-crossbones.png'

} 

); The Noti fi cation constructor takes a title, and then an object containing options. Basic options are ‘body’ for the message and ‘icon’ for an icon to show with the noti fi cation.

Slide 42

Slide 42

Web Noti fi cations var notification

new Notification(

'Your life is in danger' , { body :

'You forgot to take your pills' , icon :

'skull-and-crossbones.png' , tag :

'pills-warning' , lang :

'en-US' , dir :

'ltr'

} 

); The ‘tag’ option acts like an ID for the noti fi cation. If there are multiple instances of your code running (e.g. two browser windows) the tag prevents the noti fi cation being duplicated. It can also be used to address the noti fi cation to cancel it.

Slide 43

Slide 43

What are they good for? Notifying the user of background task completion, e.g. encoding has fi nished, upload has completed. Notifying of incoming activity, e.g. a message has been received, a user has logged in.

Slide 44

Slide 44

Browser support

Slide 45

Slide 45

Use them! Pretty great support on desktop. Judge carefully when to ask permission to display noti fi cations. Do it before you need to send, but not before the user trusts you or they’ll decline.

Slide 46

Slide 46

Web MIDI https://www.w3.org/TR/webmidi/

Slide 47

Slide 47

MIDI?!

Slide 48

Slide 48

Musical Instrument Digital Interface MIDI is a very well established protocol for sending event messages about musical notes, control signals and clock signals. It’s used by musical keyboards, synths, drum machines, digital control surfaces, theatre lighting and sound systems, and most importantly…

Slide 49

Slide 49

Keytars!

Slide 50

Slide 50

Web MIDI MIDI sends note-on and note-o ff events (with pitch and velocity), and change events for any number of other controls. It’s basically a well de fi ned protocol for event based input and output for physical buttons and switches. Which makes it quite exciting.

Slide 51

Slide 51

Web MIDI if (navigator.requestMIDIAccess) {

// We have MIDI support! } if (navigator.requestMIDIAccess) { navigator.requestMIDIAccess() .then(success, failure); } We fi rst need to request access to MIDI devices. This returns a promise, with a success and failure callback. Code sample references work by Stuart Memo on sitepoint.com

Slide 52

Slide 52

Web MIDI function failure() {

// MIDI access denied :( } function success(midi) {

var inputs

midi.inputs.values();

for ( var input

inputs.next(); input &&

! input.done; input

inputs.next()) {

input.value.onmidimessage 

= messageReceived; } } If we have access to MIDI, our success callback gets a MIDIAccess object. From this we can get all the di ff erent MIDI inputs we have access to, using an interator. This code loops through the inputs adding an event listener for the onmidimessage event.

Slide 53

Slide 53

Web MIDI function messageReceived(message) { console.log(message.data); } [ 144 , 61 , 95 ] [ 128 , 61 , 0 ] [ eventCode , note , velocity ] Now we can receive MIDI messages! They look weird. The format is event code, note number, velocity. 144 is note on. 128 is note o ff .

Slide 54

Slide 54

Demo!

Slide 55

Slide 55

When is it useful? Simple integration between physical devices and the browser. There are lots of MIDI devices and most are very robust. Designed to be hit with sticks etc. Perfect for children’s games, controls for those with disabilities, kiosk applications, keytars.

Slide 56

Slide 56

When is it useful? You can also play notes out, enabling you to play instruments, control theatre lighting, sound e ff ects, video playback. It will not give you any musical talent. Sorry.

Slide 57

Slide 57

Browser support

Slide 58

Slide 58

Play with it Could be fun for hack projects, and controlled environments. Might not quite be ready for the open web until all computers ship with keytars. Keytars!

Slide 59

Slide 59

Payment Request http://www.w3.org/TR/payment-request/

Slide 60

Slide 60

Payment Request Enables us to collect payment method (card number, token), shipping, and contact information for a transaction directly from the browser. Saves the user needing to re-enter common personal information, especially on mobile. Provides a neat ‘saved card’ UI without the site needing to save anything.

Slide 61

Slide 61

What it isn’t The Payment Request API isn’t a payment gateway. It doesn’t take payments. It doesn’t integrate with payment gateways either. It simply gathers the payment details from the user and hands them to your application.

Slide 62

Slide 62

Payment Request if ( window .PaymentRequest) {

// Payments are supported! } var pr

new PaymentRequest( methodData,
transactionDetails,
options ); We can test for the availability by looking for PaymentRequest in window. The request starts with a new PaymentRequest object, which takes arguments for supported payment methods, details of the transaction, and some options.

Slide 63

Slide 63

Payment methods var methodData

[ { supportedMethods : [ 'visa' , 'mastercard' , 'amex' ], },
{ supportedMethods : [ 'https://andriod.pay/pay' ], data : { marchantID :

'1234'

} 

} ]; This is used to describe which payment methods you can accept. This will depend on your payment provider. At the moment, Payment Request works with standard card payments and AndroidPay. The data property contains any information speci fi c to that payment method.

Slide 64

Slide 64

Transaction details var transactionDetails

{ total : { label :

'Total' , amount : { currency :

'GBP' , value :

'99.00' } }, displayItems : [ { label :

'Subtotal' , amount : { currency :

'GBP' , value :

'99.00' } } ], }; Here we provide the speci fi cs of the transaction; how much to charge, the currency and so on. We can also provide line items, which are displayed for the user.

Slide 65

Slide 65

Options var options

{ requestShipping :

true , requestPayerEmail :

true , requestPayerPhone :

false , requestPayerName :

true , }; Lastly, we can set a small number of boolean options.

Slide 66

Slide 66

Payment Request var pr

new PaymentRequest(methodData,
transactionDetails,
options); pr.show().then( function (paymentResponse) {

// send the card details to your gateway

// and then: paymentResponse.complete( 'success' ); }); The show() method shows the browser payment UI and kicks of the process from the user’s point of view. It returns a promise, with a callback that contains the payment response. We call the complete() method to let the browser know the result so it can update the UI for the user.

Slide 67

Slide 67

Shipping events pr.addEventListener( 'shippingaddresschange' , function (e){ transactionDetails.displayItems.push({ label :

'Shipping' , amount : { currency :

'GBP' , value :

'10.00' } });

transactionDetails.total.amount.value

'109.00' ;

e.updateWith( Promise .resolve(transactionDetails)); }); An event is fi red when the user changes their shipping address. We can add a listener for this, and update the transaction details at that point if required. This helps with shipping costs that change based on location.

Slide 68

Slide 68

Slide 69

Slide 69

Slide 70

Slide 70

Slide 71

Slide 71

Slide 72

Slide 72

Slide 73

Slide 73

When is it useful? Provides a very native feeling payment UI for mobile, where fi lling out forms can be very tedious. Enables you to o ff er those users the convenience of using a saved card, without saving cards.

Slide 74

Slide 74

Browser support

Slide 75

Slide 75

Start experimenting! Payment Request is very new and support is experimental. The API is well designed and shouldn’t change to drastically. It will provide a major advantage for mobile users, so it’s worth knowing. Kick the tyres and give feedback.

Slide 76

Slide 76

Phew.

Slide 77

Slide 77

HTML5 APIs Page Visibility Device Orientation Battery Status Vibration Web Noti fi cations Web MIDI Payment Request Ambient Light Geolocation Web Audio Web Share Screen Orientation

Slide 78

Slide 78

HTML5 APIs Clipboard Speech synthesis Speech detection Media capture streams Proximity Network information File & File System Drag and drop Fullscreen Web workers

Slide 79

Slide 79

Thanks! @drewm speakerdeck.com/drewm