84
The things browsers can do ! G o play with the web Chris Heilmann, SAE Alumni Convention, Berlin, Germany, 23/10/2014

The things browsers can do! SAE Alumni Convention 2014

Embed Size (px)

DESCRIPTION

A talk about allowing HTML5 to graduate from a toy language of web enthusiasts to something used in every day web products. Shows how far browsers have come in support and how to use the standards without resorting to lots of abstraction libraries

Citation preview

Page 1: The things browsers can do! SAE Alumni Convention 2014

The things browsers can do! Go play with the web

Chris Heilmann, SAE Alumni Convention, Berlin, Germany, 23/10/2014

Page 2: The things browsers can do! SAE Alumni Convention 2014

@codepo8

Chris Heilmann

Page 3: The things browsers can do! SAE Alumni Convention 2014
Page 4: The things browsers can do! SAE Alumni Convention 2014
Page 5: The things browsers can do! SAE Alumni Convention 2014
Page 6: The things browsers can do! SAE Alumni Convention 2014

?

Page 7: The things browsers can do! SAE Alumni Convention 2014

Libraries, Polyfills, Frameworks!

Page 8: The things browsers can do! SAE Alumni Convention 2014

It’s a mobile game!

Page 9: The things browsers can do! SAE Alumni Convention 2014

Of course, we moved on…

Page 10: The things browsers can do! SAE Alumni Convention 2014
Page 11: The things browsers can do! SAE Alumni Convention 2014

CSS Stuff

Page 12: The things browsers can do! SAE Alumni Convention 2014

<div id="#really" class="no idea how i do" data-foo="thismarkupthing"> […]</div>

Page 13: The things browsers can do! SAE Alumni Convention 2014

p {}p.oop {}p#tag {}p[class] {}p[title="hi there"]{}p[title^="hi"]{}p[title$="hi"]{}p[title~="hi"]{}

Page 14: The things browsers can do! SAE Alumni Convention 2014

p + p {}p > span {}p ~ div {}

Page 15: The things browsers can do! SAE Alumni Convention 2014

http://www.smashingmagazine.com/2013/08/20/semantic-css-with-intelligent-selectors/

Page 16: The things browsers can do! SAE Alumni Convention 2014
Page 17: The things browsers can do! SAE Alumni Convention 2014

[href$=".zip"]:before,[href$=".gz"]:before { content: 'E004'; /* unicode for the zip folder icon */}

Page 18: The things browsers can do! SAE Alumni Convention 2014

<a href="http://twitter.com/heydonworks" target="_blank" class="new-window-icon twitter-icon">@heydonworks</a>

Page 19: The things browsers can do! SAE Alumni Convention 2014

<a href="http://twitter.com/heydonworks" target="_blank">@heydonworks</a>

Page 20: The things browsers can do! SAE Alumni Convention 2014

<ul> <li class="list-item-first"></li> <li class="list-item"></li> <li class="list-item"></li> <li class="list-item"></li> <li class="list-item"></li> <li class="list-item-last"></li></ul>

&#128169;

Page 21: The things browsers can do! SAE Alumni Convention 2014

!

:link :visited :active :hover :focus

:first-child :last-child :nth-child :nth-last-child :nth-of-type :first-of-type :last-of-type :empty :target :checked :enabled :disabled :not()

Page 22: The things browsers can do! SAE Alumni Convention 2014

https://medium.com/@mjtweaver/the-css-that-you-dont-know-about-d5945cea1c94

Page 23: The things browsers can do! SAE Alumni Convention 2014

<ul> <li></li> <li></li> <li></li> <li></li> ul > li:first-child {} <li></li> <li></li> <li></li></ul>

Page 24: The things browsers can do! SAE Alumni Convention 2014

<ul> <li></li> <li></li> <li></li> <li></li> ul > li:last-child {} <li></li> <li></li> <li></li></ul>

Page 25: The things browsers can do! SAE Alumni Convention 2014

<ul> <li></li> <li></li> <li></li> <li></li> ul > li:nth-child(odd) {} <li></li> <li></li> <li></li></ul>

Page 26: The things browsers can do! SAE Alumni Convention 2014

<ul> <li></li> <li></li> <li></li> <li></li> ul > li:nth-child(even) {} <li></li> <li></li> <li></li></ul>

Page 27: The things browsers can do! SAE Alumni Convention 2014

<ul> <li></li> <li></li> <li></li> <li></li> ul > li:nth-child(3n) {} <li></li> <li></li> <li></li></ul>

Page 28: The things browsers can do! SAE Alumni Convention 2014

<ul> <li></li> <li></li> <li></li> <li></li> ul > li:nth-child(3n+1) {} <li></li> <li></li> <li></li></ul>

Page 29: The things browsers can do! SAE Alumni Convention 2014

Support = epic

Page 30: The things browsers can do! SAE Alumni Convention 2014

https://vimeo.com/101718785

Page 31: The things browsers can do! SAE Alumni Convention 2014

http://alistapart.com/article/axiomatic-css-and-lobotomized-owls

Page 32: The things browsers can do! SAE Alumni Convention 2014

CSS calc()

padding: 40px; width: calc(100% - 80px);

Page 33: The things browsers can do! SAE Alumni Convention 2014

Support = now

Page 34: The things browsers can do! SAE Alumni Convention 2014

DOM manipulation

Page 35: The things browsers can do! SAE Alumni Convention 2014

querySelector();querySelectorAll();!

…and that is all.

Page 36: The things browsers can do! SAE Alumni Convention 2014

classList (add, remove, toggle, contains)

Page 37: The things browsers can do! SAE Alumni Convention 2014

<p class="bovine" data-sound="moo">cow</p>!

p.dataset.sound => "moo"

Page 38: The things browsers can do! SAE Alumni Convention 2014

http://christianheilmann.com/2012/10/10/data-attributes-rock-as-both-css-and-javascript-know-them/

Page 39: The things browsers can do! SAE Alumni Convention 2014

<div id="dataplayer" data-name="Joe" data-score="100"></div>

var player = document.querySelector('#dataplayer');alert('Score:' + player.dataset.score);alert('Name:' + player.dataset.name);player.dataset.score = 10;alert('Score:' + player.dataset.score);

Page 40: The things browsers can do! SAE Alumni Convention 2014

<div id="dataplayer" data-name="Joe" data-score="100"></div>

#dataplayer[data-score='10'] { color: #c00;}

Page 41: The things browsers can do! SAE Alumni Convention 2014

<div id="dataplayer" data-name="Joe" data-score="100"></div>

#dataplayer::after { content: attr(data-name); position: absolute; left: -50px;}#dataplayer::before { opacity: 0; content: attr(data-score); position: absolute; left: 100px;}

Page 42: The things browsers can do! SAE Alumni Convention 2014

MediaQueries

Page 43: The things browsers can do! SAE Alumni Convention 2014

@media only screen and (min-device-width : 320px) and (max-device-width : 480px){ …}

Page 44: The things browsers can do! SAE Alumni Convention 2014

Support = epic

Page 45: The things browsers can do! SAE Alumni Convention 2014

No support = opportunity!

@media all and (min-width:0) { …}@media only { …}

Page 46: The things browsers can do! SAE Alumni Convention 2014

http://christianheilmann.com/2012/12/19/conditional-loading-of-resources-with-mediaqueries/

Page 47: The things browsers can do! SAE Alumni Convention 2014

Inline media queries?<!DOCTYPE HTML><html lang="en-US"><head> <meta charset="UTF-8"> <link rel="stylesheet" media="screen and (min-width: 600px)" href="small.css"> <link rel="stylesheet" media="screen and (min-width: 4000px)" href="big.css"> <title>CSS files with media queries</title></head><body></body></html>

Page 48: The things browsers can do! SAE Alumni Convention 2014

Gotta load them all! <link rel="stylesheet" media="screen and (min-width: 600px)" href="small.css"> <link rel="stylesheet" media="screen and (min-width: 4000px)" href="big.css">

Page 49: The things browsers can do! SAE Alumni Convention 2014

matchMedia = the JS brother

if (window.matchMedia('screen and (min-width: 600px)')){ document.write('<link rel="stylesheet" href="small.css">');}

Page 50: The things browsers can do! SAE Alumni Convention 2014

Support = meh IE?

Page 51: The things browsers can do! SAE Alumni Convention 2014

document.yougottobekiddin?

<link rel="stylesheet" class="mediaquerydependent" data-media="screen and (min-width: 600px)" data-href="green.css"><link rel="stylesheet" class="mediaquerydependent" data-media="screen and (min-width: 4000px)" data-href="blue.css">

Page 52: The things browsers can do! SAE Alumni Convention 2014

mediaQuery all the things!

<img data-src="http://placekitten.com/500/500" data-alt="kitten" class="mediaquerydependent" data-media="screen and (min-width: 600px)">

Page 53: The things browsers can do! SAE Alumni Convention 2014

match and apply… var qs = document. querySelectorAll('.mediaquerydependent'), all = qs, cur = null, attr = null; while (all--) { cur = qs[all]; if (cur.dataset.media && window.matchMedia(cur.dataset.media).matches) { for (attr in cur.dataset) { if (attr !== 'media') { cur.setAttribute(attr, cur.dataset[attr]); } } } }

Page 54: The things browsers can do! SAE Alumni Convention 2014

but JS is bad!

<link rel="stylesheet" class="mediaquerydependent" href="standard.css" data-media="screen and (min-width: 600px)" data-href="green.css">

Page 55: The things browsers can do! SAE Alumni Convention 2014

Video

Page 56: The things browsers can do! SAE Alumni Convention 2014
Page 57: The things browsers can do! SAE Alumni Convention 2014

<img src="meh.jpg" alt="cute kitten photo">

Fallbacks are good!

Page 58: The things browsers can do! SAE Alumni Convention 2014

var img = document.querySelector('img');img.addEventListener('error', function(ev) { if (this.naturalWidth === 0 && this.naturalHeight === 0) { console.log('Image ' + this.src + ' not loaded'); }}, false);

Testable fallbacks!

Page 59: The things browsers can do! SAE Alumni Convention 2014

<video controls> <source src="dynamicsearch.mp4" type="video/mp4"> </source> <a href="dynamicsearch.mp4"> <img src="dynamicsearch.jpg" alt="Dynamic app search in Firefox OS"> </a> <p>Click image to play a video demo of dynamic app search</p></video>

Bullet proof video?

Page 60: The things browsers can do! SAE Alumni Convention 2014

Fallback for an extinct case

How about writing a JavaScript that tests for the video support?!I mean, there is !canPlayType(type),right?

Page 61: The things browsers can do! SAE Alumni Convention 2014

The canPlayType(type) method must return the empty string if type is a type that the user agent knows it cannot render or is the type "application/octet-stream"; it must return "probably" if the user agent is confident that the type represents a media resource that it can render if used in with this audio or video element; and it must return "maybe" otherwise.!!

W3C media elements spec

Codecs are hard ;)

Page 62: The things browsers can do! SAE Alumni Convention 2014
Page 63: The things browsers can do! SAE Alumni Convention 2014

Bullet proof video!

var v = document.querySelector('video'), sources = v.querySelectorAll('source'), lastsource = sources[sources.length-1];lastsource.addEventListener('error', function(ev) { var d = document.createElement('div'); d.innerHTML = v.innerHTML; v.parentNode.replaceChild(d, v);}, false);

Page 64: The things browsers can do! SAE Alumni Convention 2014

Canvas

Page 65: The things browsers can do! SAE Alumni Convention 2014

http://phaser.io/

Page 66: The things browsers can do! SAE Alumni Convention 2014

http://www.pixijs.com/

Page 67: The things browsers can do! SAE Alumni Convention 2014

http://hub.gravit.io/browser/

Page 68: The things browsers can do! SAE Alumni Convention 2014

A very basic painting API…

Page 69: The things browsers can do! SAE Alumni Convention 2014

A collection of pixels!

Page 70: The things browsers can do! SAE Alumni Convention 2014

http://thewebrocks.com/demos/zoom-and-pick/

Zoom and pick…

Page 71: The things browsers can do! SAE Alumni Convention 2014

https://github.com/codepo8/zoom-and-pick

And generate…

Page 72: The things browsers can do! SAE Alumni Convention 2014

Canvas + FileReader =

Page 73: The things browsers can do! SAE Alumni Convention 2014

https://www.youtube.com/watch?v=gnbLLQwZxeA

Page 74: The things browsers can do! SAE Alumni Convention 2014

http://removephotodata.com

Page 75: The things browsers can do! SAE Alumni Convention 2014

https://github.com/jseidelin/exif-js/

Make: LGE! Model: Nexus 5! XResolution: 72! YResolution: 72! ResolutionUnit: 2! YCbCrPositioning: 1! ExifIFDPointer: 134! GPSInfoIFDPointer: 462! ExposureTime: 0.009523809523809525! FNumber: 2.4! ISOSpeedRatings: 104! ExifVersion: 0220! DateTimeOriginal: 2014:10:19 17:28:22! DateTimeDigitized: 2014:10:19 17:28:22! ComponentsConfiguration: YCbCr! ShutterSpeedValue: 6.713! ApertureValue: 2.52! ExposureBias: 0! Flash: Flash did not fire! FocalLength: 3.97! FlashpixVersion: 0100! ColorSpace: 1! PixelXDimension: 1944! PixelYDimension: 2592! InteroperabilityIFDPointer: 432

Page 76: The things browsers can do! SAE Alumni Convention 2014

c = document.querySelector('canvas');cx = c.getContext('2d');c.width = w = img.naturalHeight;c.height = h = img.naturalWidth;cx.drawImage(img, 0, 0, w, h);!

<a href="' + c.toDataURL('image/jpeg', 0.9) + '" '+ 'download="' + dlname + '">Download clean image</a>

[EXIF]

Page 77: The things browsers can do! SAE Alumni Convention 2014

https://github.com/eligrey/FileSaver.js

Support = good

Page 78: The things browsers can do! SAE Alumni Convention 2014

https://github.com/eligrey/FileSaver.js

Support = good

Page 79: The things browsers can do! SAE Alumni Convention 2014

https://github.com/eligrey/FileSaver.js

Support = eek

Page 80: The things browsers can do! SAE Alumni Convention 2014

http://stuk.github.io/jszip/

http://makethumbnails.com http://stuk.github.io/jszip/

Page 81: The things browsers can do! SAE Alumni Convention 2014

<tag> You’re it!

Page 82: The things browsers can do! SAE Alumni Convention 2014

Share, find, re-use…

Page 83: The things browsers can do! SAE Alumni Convention 2014

Share, find, re-use…