is html5 ready? (workshop)

151
Is HTML5 ready for production?

Upload: remy-sharp

Post on 15-May-2015

2.302 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Is HTML5 Ready? (workshop)

Is HTML5 ready for

production?

Page 2: Is HTML5 Ready? (workshop)

Hi, I’m Remy

@[email protected]

I <3 JavaScript

Questions: interrupt & ask!

Page 3: Is HTML5 Ready? (workshop)
Page 4: Is HTML5 Ready? (workshop)
Page 5: Is HTML5 Ready? (workshop)

There's a lot more down here.

Page 6: Is HTML5 Ready? (workshop)

HTML5 is a spec

Page 7: Is HTML5 Ready? (workshop)

"HTML5" is a brandsort of

^

Page 8: Is HTML5 Ready? (workshop)

HTML5

Offline applications

Offline events

Canvas

Video

Web Form

s

"HTML5"

Web Storage

Web SQ

L Databases

Web Sockets

Web W

orkersG

eolocationM

OA

R!!!

Page 9: Is HTML5 Ready? (workshop)

NOT HTML5

NOT HTML5

Page 10: Is HTML5 Ready? (workshop)

CSS != HTMLBut maybe we should have been more careful

Page 11: Is HTML5 Ready? (workshop)

caniuse.com

Page 12: Is HTML5 Ready? (workshop)

When can I use "HTML5"?

Page 13: Is HTML5 Ready? (workshop)
Page 14: Is HTML5 Ready? (workshop)
Page 15: Is HTML5 Ready? (workshop)
Page 16: Is HTML5 Ready? (workshop)

Making it work in the "other" browser

Page 17: Is HTML5 Ready? (workshop)

PolyfillA shim that mimics a futureAPI providing a fallback to

older browsershttp://goo.gl/0Z9eI

Page 18: Is HTML5 Ready? (workshop)

ToolsModernizr to detect support

yepnode.js to conditionally load(available as part of Modernizr)

Page 19: Is HTML5 Ready? (workshop)

ToolsModernizr.load({ test: Modernizr.geolocation, nope: 'geo-polyfill.js', complete: initMyGeoApp});

Page 20: Is HTML5 Ready? (workshop)

Oh, and learn JavaScript

Page 21: Is HTML5 Ready? (workshop)

Web Storage

Page 22: Is HTML5 Ready? (workshop)

Cookies suck.

Page 23: Is HTML5 Ready? (workshop)

Cookies suck.

Not the edible ones, duh.

Page 24: Is HTML5 Ready? (workshop)

The code for cookies is a pain - I always

google it.

Page 25: Is HTML5 Ready? (workshop)

"Session" cookies leak across sessions.

Page 26: Is HTML5 Ready? (workshop)

Persistent cookies require special date

format

Page 27: Is HTML5 Ready? (workshop)

Deleting a cookie, isn't really deleting, but setting in the past.

Page 28: Is HTML5 Ready? (workshop)

Varying degrees of limitations on size and

number.

Page 29: Is HTML5 Ready? (workshop)

Fuck cookies.

Page 30: Is HTML5 Ready? (workshop)

Sexy Web Storage FTW

Page 31: Is HTML5 Ready? (workshop)

One Interface

localStoragesessionStorage

http://dev.w3.org/html5/webstorage/

Page 32: Is HTML5 Ready? (workshop)

localStorage

• Persists

• Applied to document origin, i.e. scheme/host/port tuple

• No expiry

Page 33: Is HTML5 Ready? (workshop)

sessionStorage

• Lasts whilst on the document origin

• Doesn't leak

• Exactly the same API as localStorage

Page 34: Is HTML5 Ready? (workshop)

How much space do you want?

5mb?Done! \o/However: utf-16 ∴ 2½Mb

Page 35: Is HTML5 Ready? (workshop)

APIvoid setItem(key, value)

any* getItem(key)

void removeItem(key)

string key(index)

void clear()

readonly number length

Page 36: Is HTML5 Ready? (workshop)

var store = sessionStorage;

store.setItem('name','rem');

store.getItem('name');

store.removeItem('name');

Play

Do it in the console!

Page 37: Is HTML5 Ready? (workshop)

APIsetter setItem

getter getItem

deleter removeItem

Page 38: Is HTML5 Ready? (workshop)

var store = sessionStorage;

store.name = 'rem';

store.name;

delete store.name;

Play

Do it in the console!

Page 39: Is HTML5 Ready? (workshop)

// Safari debugger broken:

ss.setItem('key', 12);

tipThere's no security protecting the API

Page 40: Is HTML5 Ready? (workshop)

Values are strings

Work around: JSON(and http://www.json.org/json2.js)

Page 41: Is HTML5 Ready? (workshop)

var store = sessionStorage,

user = { screen_name : ‘rem’,

rating : 11 };

store.user = JSON.stringify(user));

var gotUser = JSON.parse(store.user);

alert(gotUser.screen_name);

Play

Page 42: Is HTML5 Ready? (workshop)
Page 43: Is HTML5 Ready? (workshop)

Events too

window.addEventListener('storage', function (event) {

console.log(event);

}, false);

http://jsbin.com/ahafa3

Page 44: Is HTML5 Ready? (workshop)
Page 45: Is HTML5 Ready? (workshop)

Storage in all browsers

Page 46: Is HTML5 Ready? (workshop)

window.name

Page 47: Is HTML5 Ready? (workshop)

sessionStorage = (function () { var data = window.name ? JSON.parse(window.name) : {};

return { clear: function () { data = {}; window.top.name = ''; }, getItem: function (key) { return data[key] || null; }, removeItem: function (key) { delete data[key]; window.top.name = JSON.stringify(data); }, setItem: function (key, value) { data[key] = value; window.top.name = JSON.stringify(data); } };})(); http://gist.github.com/350433

Page 48: Is HTML5 Ready? (workshop)

Firefox cookie security applies to Storage too :(

tip

Page 49: Is HTML5 Ready? (workshop)

tipvar cookiesEnabled = (function () { // the id is our test value var id = +new Date();

// generate a cookie to probe cookie access document.cookie = '__cookieprobe=' + id + ';path=/';

// if the cookie has been set, then we're good return (document.cookie.indexOf(id) !== -1);})();

Page 50: Is HTML5 Ready? (workshop)

Web SQL Databases

http://flic.kr/p/drmCJ

Page 51: Is HTML5 Ready? (workshop)

IndexedDB

http://flic.kr/p/5KXFwB

Page 52: Is HTML5 Ready? (workshop)

Questions?

Page 53: Is HTML5 Ready? (workshop)

Ca

nv

as

Cooler than this guy.

Page 54: Is HTML5 Ready? (workshop)

Canvas SVG

Page 55: Is HTML5 Ready? (workshop)

http://vimeo.com/6691519

Page 56: Is HTML5 Ready? (workshop)

• It's not one is better than the other, they do different things

• Select canvas when it makes sense

• Don't assume interactive means canvas

• Check out raphaeljs.com

Page 57: Is HTML5 Ready? (workshop)
Page 58: Is HTML5 Ready? (workshop)

http://mrdoob.com

Page 59: Is HTML5 Ready? (workshop)

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8" /><title>Canvas</title></head><body> <canvas></canvas></body></html>

Play

canvas-1.html

http://dev.w3.org/html5/canvas-api/canvas-2d-api.html

Page 60: Is HTML5 Ready? (workshop)

2D API

ctx = canvas.getContext('2d')

Page 61: Is HTML5 Ready? (workshop)

Start Simple

Page 62: Is HTML5 Ready? (workshop)

fillRect, strokeRect & colours

Page 63: Is HTML5 Ready? (workshop)

ctx.fillRect(10, 10, 100, 100);

Page 64: Is HTML5 Ready? (workshop)

ctx.fillRect(10, 10, 100, 100);

ctx.fillStyle = '#c00';

Page 65: Is HTML5 Ready? (workshop)

ctx.fillRect(10, 10, 100, 100);

ctx.fillStyle = '#c00';

ctx.fillRect(100, 75, 50, 50);

Page 66: Is HTML5 Ready? (workshop)

ctx.fillRect(10, 10, 100, 100);

ctx.fillStyle = '#c00';

ctx.fillRect(100, 75, 50, 50);

ctx.strokeStyle = '#0c0';

Page 67: Is HTML5 Ready? (workshop)

ctx.fillRect(10, 10, 100, 100);

ctx.fillStyle = '#c00';

ctx.fillRect(100, 75, 50, 50);

ctx.strokeStyle = '#0c0';

ctx.lineWidth = 5;

Page 68: Is HTML5 Ready? (workshop)

ctx.fillRect(10, 10, 100, 100);

ctx.fillStyle = '#c00';

ctx.fillRect(100, 75, 50, 50);

ctx.strokeStyle = '#0c0';

ctx.lineWidth = 5;

ctx.arc(100,50,25,0,Math.PI*2,true);

Page 69: Is HTML5 Ready? (workshop)

ctx.fillRect(10, 10, 100, 100);

ctx.fillStyle = '#c00';

ctx.fillRect(100, 75, 50, 50);

ctx.strokeStyle = '#0c0';

ctx.lineWidth = 5;

ctx.arc(100,50,25,0,Math.PI*2,true);

ctx.stroke();

Page 70: Is HTML5 Ready? (workshop)

Math.PI == 180°

tip

Page 71: Is HTML5 Ready? (workshop)

Math.PI *2 == 360°

tip

Page 72: Is HTML5 Ready? (workshop)

d * Math.PI / 180 == radian

tip

Page 73: Is HTML5 Ready? (workshop)

tip CSS stretches

Page 74: Is HTML5 Ready? (workshop)

Gradient fills

Page 75: Is HTML5 Ready? (workshop)

1. Create a gradient object

2.Add colour stops

3.Set the gradient to the fillStyle

4.Draw

Page 76: Is HTML5 Ready? (workshop)

var w = canvas.width, h = canvas.height;

var gradient = ctx.createLinearGradient(0, 0, w, h);

Page 77: Is HTML5 Ready? (workshop)

var w = canvas.width, h = canvas.height;

var gradient = ctx.createLinearGradient(0, 0, w, h);gradient.addColorStop(0, '#fff');gradient.addColorStop(0.5, '#f00');gradient.addColorStop(1, '#000');

Page 78: Is HTML5 Ready? (workshop)

var w = canvas.width, h = canvas.height;

var gradient = ctx.createLinearGradient(0, 0, w, h);gradient.addColorStop(0, '#fff');gradient.addColorStop(0.5, '#f00');gradient.addColorStop(1, '#000');

ctx.fillStyle = gradient;

Page 79: Is HTML5 Ready? (workshop)

var w = canvas.width, h = canvas.height;

var gradient = ctx.createLinearGradient(0, 0, w, h);gradient.addColorStop(0, '#fff');gradient.addColorStop(0.5, '#f00');gradient.addColorStop(1, '#000');

ctx.fillStyle = gradient;ctx.fillRect(0, 0, w, h);

Page 80: Is HTML5 Ready? (workshop)

Play

createRadialGradient(x0,y0,r0,x1,y1,r1)

Page 81: Is HTML5 Ready? (workshop)

Pattern Fills

Page 82: Is HTML5 Ready? (workshop)

var img = new Image();

img.src = url;

var pattern = ctx.createPattern(img,'repeat');

ctx.fillStyle = pattern;

ctx.fillRect(0, 0, w, h);

Page 83: Is HTML5 Ready? (workshop)

tip invalid_state

img.onload = function () { // now use image for canvas};

Page 84: Is HTML5 Ready? (workshop)

Playing with paths

Page 85: Is HTML5 Ready? (workshop)

• For drawing irregular shapes

• Can be filled

• ...or stroked.

Page 86: Is HTML5 Ready? (workshop)

lineWidth

lineTo(x,y)

moveTo(x,y)

beginPath()

closePath()

rect(x,y,h,w)

arc(x,y,r,s,e, anticw)

fill()

stroke()

Page 87: Is HTML5 Ready? (workshop)

drawImage

Page 88: Is HTML5 Ready? (workshop)

<canvas>

<img> <video><canvas>

Page 89: Is HTML5 Ready? (workshop)

drawImage(src, dx, dy)

drawImage(src, dx, dy, dw, dh)

drawImage(src, sx, sy, sw, sh, dx, dy, dw, dh)

Page 90: Is HTML5 Ready? (workshop)
Page 91: Is HTML5 Ready? (workshop)

Play

canvas-10.html

img.onload = function () { // this == img canvas.width = this.width; canvas.height = this.height; ctx.drawImage(this, 0, 0);};

Page 92: Is HTML5 Ready? (workshop)

pixelpushing

Page 93: Is HTML5 Ready? (workshop)

ctx.getImageData(0,0,w,h)

Page 94: Is HTML5 Ready? (workshop)

ctx.getImageData(0, 0, w, h);

0 1 2 3

i = 0 r g b a

i = 1 r g b a

i... r g b a

Page 95: Is HTML5 Ready? (workshop)

pixels.data[i * 4 + 0];

0 1 2 3

i = 0 r g b a

i = 1 r g b a

i... r g b a

Page 96: Is HTML5 Ready? (workshop)

pixels.data[i * 4 + 1];

0 1 2 3

i = 0 r g b a

i = 1 r g b a

i... r g b a

Page 97: Is HTML5 Ready? (workshop)

pixels.data[i * 4 + 2];

0 1 2 3

i = 0 r g b a

i = 1 r g b a

i... r g b a

Page 98: Is HTML5 Ready? (workshop)

pixels.data[i * 4 + 3];

0 1 2 3

i = 0 r g b a

i = 1 r g b a

i... r g b a

Page 99: Is HTML5 Ready? (workshop)

var pixels = ctx.getImageData(0, 0, w, h), l = pixels.data.length, i;

for (i = 0; i < l; i += 4) {

}

Page 100: Is HTML5 Ready? (workshop)

var pixels = ctx.getImageData(0, 0, w, h), l = pixels.data.length, i;

for (i = 0; i < l; i += 4) {

}

This says loop over each pixel

Page 101: Is HTML5 Ready? (workshop)

var pixels = ctx.getImageData(0, 0, w, h), l = pixels.data.length, i;

for (i = 0; i < l; i += 4) { // red: pixels.data[i+0]

}

Page 102: Is HTML5 Ready? (workshop)

var pixels = ctx.getImageData(0, 0, w, h), l = pixels.data.length, i;

for (i = 0; i < l; i += 4) { // red: pixels.data[i+0] // green: pixels.data[i+1]

}

Page 103: Is HTML5 Ready? (workshop)

var pixels = ctx.getImageData(0, 0, w, h), l = pixels.data.length, i;

for (i = 0; i < l; i += 4) { // red: pixels.data[i+0] // green: pixels.data[i+1] // blue: pixels.data[i+2]

}

Page 104: Is HTML5 Ready? (workshop)

var pixels = ctx.getImageData(0, 0, w, h), l = pixels.data.length, i;

for (i = 0; i < l; i += 4) { // red: pixels.data[i+0] // green: pixels.data[i+1] // blue: pixels.data[i+2] // alpha: pixels.data[i+3]}

Page 105: Is HTML5 Ready? (workshop)

ctx.putImageData(pixels, 0, 0)

Page 106: Is HTML5 Ready? (workshop)

ctx.putImageData(pixels, 0, 0)

Needs to be a CanvasPixelArray

object

Page 107: Is HTML5 Ready? (workshop)

tip security_err

To use getImageData, your document

must be served over http (or https) -

i.e. it doesn't work offline.

Page 108: Is HTML5 Ready? (workshop)

Play

http://jsbin.com/aguho3/2/edit

greyscale = r*.3 + g*.59 + b*.11;

r = g = b = greyscale;

saturation = (Math.max(r,g,b) + Math.min(r,g,b))/2;

r = g = b = saturation;

/workshop/authors-large.jpg

Page 109: Is HTML5 Ready? (workshop)

canvas.toDataURL('image/png');

Page 110: Is HTML5 Ready? (workshop)

save.onclick = function () { window.open( canvas.toDataURL('image/png') );};

Play

canvas-13.html

Page 111: Is HTML5 Ready? (workshop)

Questions?

Page 112: Is HTML5 Ready? (workshop)

Offline Applications

Page 113: Is HTML5 Ready? (workshop)

Offline Apps

• Application cache / manifest

• Events: offline, online

• navigator.onLine property

Page 114: Is HTML5 Ready? (workshop)

http://icanhaz.com/rubiks

Page 115: Is HTML5 Ready? (workshop)

Using a Manifest<!DOCTYPE html>

<html manifest="my.appcache">

<body>

<!-- my page -->

</body>

</html>

Page 116: Is HTML5 Ready? (workshop)

CACHE MANIFEST

app.html

css/style.css

js/app.js

#version 13

my.appcache

Page 117: Is HTML5 Ready? (workshop)

The Manifest

1. Serve as text/manifest, by adding to mime.types:

text/cache-manifest appcache

Page 118: Is HTML5 Ready? (workshop)

<IfModule mod_expires.c>

ExpiresActive on

ExpiresByType text/cache-manifest

↪ “access plus 0 seconds”

</IfModule>

tip Firefox caching

Page 119: Is HTML5 Ready? (workshop)

The Manifest

2. First line must be:

CACHE MANIFEST

Page 120: Is HTML5 Ready? (workshop)

The Manifest

3. Including page is implicitly included in the cache.

Page 121: Is HTML5 Ready? (workshop)

The Manifest

4. Two futher namespaces: NETWORK & FALLBACK

FALLBACK:/ offline.html

Page 122: Is HTML5 Ready? (workshop)

CACHE MANIFEST

/index.htmlrange.jsdatastore.js

FALLBACK:# force everything through # the index url/ /

# this won't match # anything unless it's on # another domainNETWORK:*

# v4

Page 123: Is HTML5 Ready? (workshop)

CACHE MANIFEST

/index.htmlrange.jsdatastore.js

FALLBACK:# force everything through # the index url/ /

# this won't match # anything unless it's on # another domainNETWORK:*

# v4

Served from cache

Page 124: Is HTML5 Ready? (workshop)

CACHE MANIFEST

/index.htmlrange.jsdatastore.js

FALLBACK:# force everything through # the index url/ /

# this won't match # anything unless it's on # another domainNETWORK:*

# v4

Requests for files not

found in the cache, are

directed to /

i.e. index.html

(when offline).

Page 125: Is HTML5 Ready? (workshop)

CACHE MANIFEST

/index.htmlrange.jsdatastore.js

FALLBACK:# force everything through # the index url/ /

# this won't match # anything unless it's on # another domainNETWORK:*

# v4

Any requests to urls

that don't match / -

i.e. on another

domain, will be

served through the

web.

Page 126: Is HTML5 Ready? (workshop)

CACHE MANIFEST

/index.htmlrange.jsdatastore.js

FALLBACK:# force everything through # the index url/ /

# this won't match # anything unless it's on # another domainNETWORK:*

# v4

Also ensures

browser even

attempts to load the

asset

Page 127: Is HTML5 Ready? (workshop)

CACHE MANIFEST

/index.htmlrange.jsdatastore.js

FALLBACK:# force everything through # the index url/ /

# this won't match # anything unless it's on # another domainNETWORK:*

# v4

The contents of the

manifest must

change to trigger an

update

Page 128: Is HTML5 Ready? (workshop)

The Manifest

5. Include some versioning to cache bust your manifest

# version 16

Page 129: Is HTML5 Ready? (workshop)

The process

Page 130: Is HTML5 Ready? (workshop)

Browser: request Server: serve allBrowser: I have a manifest, cache

assets

Server: serve manifest assets

Browser: applicationCache

updatedBrowser: reload

Browser: serve locally

Browser: only request manifest

file

Server: 304 Not Modified

Page 131: Is HTML5 Ready? (workshop)

Browser: request Server: serve allBrowser: I have a manifest, cache

assets

Server: serve manifest assets

Browser: applicationCache

updatedBrowser: reload

Browser: serve locally

Browser: only request manifest

file

Server: 304 Not Modified

Problem:Change of contentrequires 2 refreshes

Page 132: Is HTML5 Ready? (workshop)

document.body.onOnline = function () { // fire an update to the cache applicationCache.update();};

Page 133: Is HTML5 Ready? (workshop)

applicationCache.onUpdateReady = function () { if (confirm("New version ready. Refresh?")) { // reload window.location.refresh(); }};

Page 134: Is HTML5 Ready? (workshop)

tip

Do offline last

Page 135: Is HTML5 Ready? (workshop)

Questions?

Page 136: Is HTML5 Ready? (workshop)

Web Sockets

Page 137: Is HTML5 Ready? (workshop)

Native or polyfilled

Page 139: Is HTML5 Ready? (workshop)

new WebSocket(url)

http://dev.w3.org/html5/websockets/

Page 140: Is HTML5 Ready? (workshop)

ws://node.remysharp.com:8000

http://github.com/miksago/node-websocket-server

Page 141: Is HTML5 Ready? (workshop)

onopen

onmessage

onclose

onerror

Page 142: Is HTML5 Ready? (workshop)

var data = JSON.parse(event.data);

Page 143: Is HTML5 Ready? (workshop)

.send

Page 144: Is HTML5 Ready? (workshop)

var url = 'ws://node.remysharp.com:8000',

conn = new WebSocket(url);

conn.onopen = function () {

conn.send('hello world');

};

conn.onmessage = function (event) {

console.log(event.data);

};

Page 145: Is HTML5 Ready? (workshop)

var url = 'ws://node.remysharp.com:8000',

conn = new WebSocket(url);

conn.onopen = function () {

conn.send('hello world');

};

conn.onmessage = function (event) {

console.log(event.data);

};

Page 146: Is HTML5 Ready? (workshop)

var url = 'ws://node.remysharp.com:8000',

conn = new WebSocket(url);

conn.onopen = function () {

conn.send('hello world');

};

conn.onmessage = function (event) {

console.log(event.data);

};

Page 147: Is HTML5 Ready? (workshop)

var url = 'ws://node.remysharp.com:8000',

conn = new WebSocket(url);

conn.onopen = function () {

conn.send('hello world');

};

conn.onmessage = function (event) {

console.log(event.data);

};

Page 148: Is HTML5 Ready? (workshop)

var url = 'ws://node.remysharp.com:8000',

conn = new WebSocket(url);

conn.onopen = function () {

conn.send('hello world');

};

conn.onmessage = function (event) {

console.log(event.data);

};

Page 149: Is HTML5 Ready? (workshop)

var url = 'ws://node.remysharp.com:8000',

conn = new WebSocket(url);

conn.onopen = function () {

conn.send('hello world');

};

conn.onmessage = function (event) {

console.log(event.data);

};

Page 150: Is HTML5 Ready? (workshop)

var url = 'ws://node.remysharp.com:8000',

conn = new WebSocket(url);

conn.onopen = function () {

conn.send('hello world');

};

conn.onmessage = function (event) {

console.log(event.data);

};

Play

Page 151: Is HTML5 Ready? (workshop)

Questions?To contact me after my presentation – text NHT to INTRO (46876)

Or [email protected]@rem