Download - Browsers with Wings

Transcript
Page 1: Browsers with Wings

Browserswith

Wings:HTML5 & APIs

Page 2: Browsers with Wings
Page 3: Browsers with Wings
Page 4: Browsers with Wings
Page 5: Browsers with Wings
Page 6: Browsers with Wings

HTML5 brand

Page 7: Browsers with Wings
Page 8: Browsers with Wings
Page 9: Browsers with Wings

HTML5 is: markup, JavaScript, CSS, SVG, jQuery & your dinner.

Page 10: Browsers with Wings

HTML5 is: markup, JavaScript, CSS, SVG, jQuery & your dinner.

Lie.Don't be stoopid, but...

Page 11: Browsers with Wings

HTML5 is anything & everything to mere mortal beings.

Page 12: Browsers with Wings

Today

Page 13: Browsers with Wings

Media

Page 14: Browsers with Wings

<video src=bruce.mp4>

Page 15: Browsers with Wings
Page 16: Browsers with Wings

<video src=bruce.mp4> <a href="bruce.mp4">Download</a></video>

Page 17: Browsers with Wings

Codec Wars

Page 18: Browsers with Wings

<video> <source src=bruce.mp4> <source src=bruce.ogv></video>

Page 19: Browsers with Wings

<video> <source src=bruce.webm> <source src=bruce.mp4> <source src=bruce.ogv></video>

Page 20: Browsers with Wings

<video controls> <source src=bruce.mp4> <source src=bruce.ogv></video>

Page 21: Browsers with Wings
Page 22: Browsers with Wings
Page 23: Browsers with Wings
Page 24: Browsers with Wings
Page 25: Browsers with Wings
Page 26: Browsers with Wings
Page 27: Browsers with Wings
Page 28: Browsers with Wings

<video> works in IE6

Page 29: Browsers with Wings

<video> works in IE6Lie.

Page 30: Browsers with Wings

<video width="640" height="360" controls preload="none"> <!-- MP4 must be first for iPad! --> <source src="__VIDEO__.MP4" type="video/mp4" /><!-- WebKit video --> <source src="__VIDEO__.OGV" type="video/ogg" /><!-- Firefox / Opera --> <!-- fallback to Flash: --> <object width="640" height="384" type="application/x-shockwave-flash" data="__FLASH__.SWF"> <!-- Firefox uses the `data` attribute above, IE/Safari uses the param below --> <param name="movie" value="__FLASH__.SWF" /> <param name="flashvars" value="image=__POSTER__.JPG&amp;file=__VIDEO__.MP4" /> <!-- fallback image. note the title field below, put the title of the video there --> <img src="__VIDEO__.JPG" width="640" height="360" alt="__TITLE__" title="No video playback capabilities, please download the video below" /> </object></video><!-- you *must* offer a download link as they may be able to play the file locally. customise this bit all you want --><p> <strong>Download Video:</strong> Closed Format: <a href="__VIDEO__.MP4">"MP4"</a> Open Format: <a href="__VIDEO__.OGV">"OGG"</a></p>

http://camendesign.com/code/video_for_everybody

Page 31: Browsers with Wings

Custom player style

Page 32: Browsers with Wings

function toggle() { if (video.paused) { video.play(); this.innerHTML = 'Pause'; } else { video.pause(); this.innerHTML = 'Play'; }}

var play = document.getElementById('play');play.onclick = toggle;

Page 33: Browsers with Wings

function toggle() { if (video.paused) { video.play(); this.innerHTML = 'Pause'; } else { video.pause(); this.innerHTML = 'Play'; }}

var play = document.getElementById('play');play.onclick = toggle;

Page 34: Browsers with Wings

function toggle() { if (video.paused) { video.play(); this.innerHTML = 'Pause'; } else { video.pause(); this.innerHTML = 'Play'; }}

var play = document.getElementById('play');play.onclick = toggle;

Page 35: Browsers with Wings

function toggle() { if (video.paused) { video.play(); this.innerHTML = 'Pause'; } else { video.pause(); this.innerHTML = 'Play'; }}

var play = document.getElementById('play');play.onclick = toggle;

Page 36: Browsers with Wings

// get & setvideo.currentTime

// half speedvideo.playbackRate = 0.5

// actual loaded videovideo.currentSrc

// etc

Page 37: Browsers with Wings

video.ontimeupdate = function () { updatePlayhead(this.currentTime);};

Page 38: Browsers with Wings

Fullscreen?

Page 39: Browsers with Wings

⚠Warning! User agents should not provide a public API to cause videos to be shown full-screen. A script, combined with a carefully crafted video file, could trick the user into thinking a system-modal dialog had been shown, and prompt the user for a password. There is also the danger of "mere" annoyance, with pages launching full-screen videos when links are clicked or pages navigated. Instead, user-agent specific interface features may be provided to easily allow the user to obtain a full-screen playback mode.

Page 40: Browsers with Wings
Page 41: Browsers with Wings
Page 42: Browsers with Wings

Ca

nv

as

Cooler than a fake Han Solo.

Page 43: Browsers with Wings

First consider SVG: Standard Vertical Graphing

Page 44: Browsers with Wings

First consider SVG: Standard Vertical Graphing Lie.

Page 45: Browsers with Wings

First consider SVG: Standard Vertical Graphing Lie.

Not a lie.

Page 46: Browsers with Wings

SVG: Vector based, good for simple and interactive

Canvas: Pixel based, good for pixel manipulation & high animation

Check out raphaeljs.com

Page 47: Browsers with Wings
Page 48: Browsers with Wings

Mix & match to the technology's strength

Page 49: Browsers with Wings

pixelpushing

Page 50: Browsers with Wings

http://mugtug.com/darkroom

Page 51: Browsers with Wings

var ctx = canvas.getContext('2d');

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

Page 52: Browsers with Wings

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 53: Browsers with Wings

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 54: Browsers with Wings

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 55: Browsers with Wings

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 56: Browsers with Wings

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 57: Browsers with Wings

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

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

}

Page 58: Browsers with Wings

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

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

}

This says loop over each pixel

Page 59: Browsers with Wings

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

for (i = 0; i < l; i += 4) { px.data[i+0] = 255 - px.data[i+0];

}

Page 60: Browsers with Wings

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

for (i = 0; i < l; i += 4) { px.data[i+0] = 255 - px.data[i+0]; px.data[i+1] = 255 - px.data[i+1];

}

Page 61: Browsers with Wings

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

for (i = 0; i < l; i += 4) { px.data[i+0] = 255 - px.data[i+0]; px.data[i+1] = 255 - px.data[i+1]; px.data[i+2] = 255 - px.data[i+2];

}

Page 62: Browsers with Wings

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

for (i = 0; i < l; i += 4) { px.data[i+0] = 255 - px.data[i+0]; px.data[i+1] = 255 - px.data[i+1]; px.data[i+2] = 255 - px.data[i+2]; // don't need to do the alphachannel}

Page 63: Browsers with Wings

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

for (i = 0; i < l; i += 4) { px.data[i+0] = 255 - px.data[i+0]; px.data[i+1] = 255 - px.data[i+1]; px.data[i+2] = 255 - px.data[i+2]; // don't need to do the alphachannel}

ctx.putImageData(px, 0, 0);

Page 64: Browsers with Wings
Page 65: Browsers with Wings
Page 66: Browsers with Wings

You can do that in SVG, but you can't do this:

Page 67: Browsers with Wings

http://mrdoob.com

Page 68: Browsers with Wings

canvas.toDataURL()

Page 69: Browsers with Wings

canvas.toDataURL()

Page 70: Browsers with Wings

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAGQCAYAAACAvzbMAAAgAElEQVR4Ae2dCaBd073/f2e692YghpAYQuYQQaP9U8UrnbyktEWjraFNVaU8Q4gSwXNbREIaqVKlpVFPq6G0pMmjeIpXw1NjBkmIVIhMZJDc3OEM/99v7b3P2efcfc49e5+9z9nDd4d79tl7r7V+67PWWd+95liOD8IBAiAAAiAAAjYJxG0+j8dBAARAAARAQBGAgCAjgAAIgAAIOCIAAXGEDY5AAARAAAQgIMgDIAACIAACjghAQBxhgyMQAAEQAAEICPIACIAACICAIwIQEEfY4AgEQAAEQAACgjwAAiAAAiDgiAAExBE2OAIBEAABEICAIA+AAAiAAAg4IgABcYQNjkAABEAABCAgyAMgAAIgAAKOCEBAHGGDIxAAARAAAQgI8gAIgAAIgIAjAhAQR9rVjTqHDxx5Kq9bzaC45+BVqcP

Page 71: Browsers with Wings

1. Silverlight Bridge2. excanvas.js

Shims

Page 72: Browsers with Wings

Storage

Page 73: Browsers with Wings

Cookies suck.

(for most situations)

Page 74: Browsers with Wings

The code for cookies is painful: so we google it.

Page 75: Browsers with Wings

"Session" cookies

leak across "sessions".

Page 76: Browsers with Wings

Non-session cookies require "special" date format

Page 77: Browsers with Wings

Deleting cookies,

doesn't delete, but sets it

in the past.

Page 78: Browsers with Wings

Fuck cookies.

Page 79: Browsers with Wings

Sexy Web Storage FTW

Page 80: Browsers with Wings

Key/value pair

Page 81: Browsers with Wings

One APIsetItem(key, value)

Page 82: Browsers with Wings

One APIsetItem(key, value)

string* getItem(key)

Page 83: Browsers with Wings

One APIsetItem(key, value)

string* getItem(key)

removeItem(key)

Page 84: Browsers with Wings

One APIsetItem(key, value)

string* getItem(key)

removeItem(key)

string key(index)

Page 85: Browsers with Wings

One APIsetItem(key, value)

string* getItem(key)

removeItem(key)

string key(index)

clear()

Page 86: Browsers with Wings

One APIsetItem(key, value)

string* getItem(key)

removeItem(key)

string key(index)

clear()

.length

Page 87: Browsers with Wings

Two instances

localStoragesessionStorage

Page 88: Browsers with Wings

localStorage

• Persists

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

• No expiry

Page 89: Browsers with Wings

sessionStorage

• Lasts whilst on the document origin

• Doesn't leak

• Exactly the same API as localStorage

Page 90: Browsers with Wings

var ss = sessionStorage;

ss.setItem('version', 12);

ss.getItem('version');

Page 91: Browsers with Wings

Values are strings

Warning!

Page 92: Browsers with Wings

Values are strings

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

Warning!

Page 93: Browsers with Wings

var ss = sessionStorage,

user = { screen_name : ‘rem’,

rating : 11 };

ss.setItem(‘user’, JSON.stringify(user));

var obj = JSON.parse(ss.getItem(‘user’));

alert(obj.screen_name);

Page 94: Browsers with Wings
Page 95: Browsers with Wings
Page 96: Browsers with Wings
Page 97: Browsers with Wings

window.name shim

http://gist.github.com/350433

Page 98: Browsers with Wings

Alternatives

Page 99: Browsers with Wings

Web SQL Database

Page 100: Browsers with Wings

IndexedDB

Page 101: Browsers with Wings

Geolocation

Page 102: Browsers with Wings
Page 103: Browsers with Wings

navigator.geolocation

Page 104: Browsers with Wings

getCurrentPosition

watchPosition

clearPosition

Page 105: Browsers with Wings

var geo = navigator.geolocation;

geo.getCurrentPosition(function(data){

map(data.coords.latitude,

data.coords.longitude);

});

Page 106: Browsers with Wings

var geo = navigator.geolocation;

geo.getCurrentPosition(function(data){

map(data.coords.latitude,

data.coords.longitude);

});

Page 107: Browsers with Wings

var geo = navigator.geolocation;

geo.getCurrentPosition(function(data){

map(data.coords.latitude,

data.coords.longitude);

});

Page 108: Browsers with Wings

var geo = navigator.geolocation;

geo.getCurrentPosition(function(data){

map(data.coords.latitude,

data.coords.longitude);

});

Page 109: Browsers with Wings

var geo = navigator.geolocation;

geo.getCurrentPosition(function(data){

map(data.coords.latitude,

data.coords.longitude);

});

Page 110: Browsers with Wings
Page 111: Browsers with Wings

Check your accuracy

Page 112: Browsers with Wings

if (!navigator.geolocation) { navigator.geolocation = (function (window) { function getCurrentPosition(callback) { // source: http://www.maxmind.com/app/javascript_city var geourl = 'http://j.maxmind.com/app/geoip.js_' + Math.random(), iframe = document.createElement('iframe'), doc, win;

iframe.style.display = 'none'; window.document.body.appendChild(iframe); doc = iframe.contentDocument || iframe.contentWindow.document; win = iframe.contentWindow; // once the script has loaded, it triggers an onload event iframe.onload = function () { // assign all the captured values across to our geo object var geo = { coords: { latitude: win.geoip_latitude(), longitude: win.geoip_longitude() // other values are supported, i.e. accuracy, speed, heading, etc }, timestamp: (new Date()).getTime() }; // then remove the iframe from the body to clear the memory...I hope! window.document.body.removeChild(iframe); callback.call(callback, geo); }; // create a document on the fly doc.open(); doc.write('<' + 'script src="' + geourl + '"><' + '/script>'); doc.close(); } return { clearWatch: function () {}, getCurrentPosition: getCurrentPosition, // TODO shouldn't be too difficult :) watchPosition: function () {} };})(this); }

http://j.mp/geoshim

Page 113: Browsers with Wings

Sock

ets

Page 114: Browsers with Wings

Move over

comet.

Page 115: Browsers with Wings

Low latency.Direct connection.Simple API.

Page 116: Browsers with Wings

var url = 'ws://apps.leftlogic.com:8000/',

conn = new WebSocket(url);

conn.onmessage = function (event) {

console.log(JSON.parse(event.data));

};

conn.send('hello world');

Page 117: Browsers with Wings

var url = 'ws://apps.leftlogic.com:8000/',

conn = new WebSocket(url);

conn.onmessage = function (event) {

console.log(JSON.parse(event.data));

};

conn.send('hello world');

Page 118: Browsers with Wings

var url = 'ws://apps.leftlogic.com:8000/',

conn = new WebSocket(url);

conn.onmessage = function (event) {

console.log(JSON.parse(event.data));

};

conn.send('hello world');

Page 119: Browsers with Wings

var url = 'ws://apps.leftlogic.com:8000/',

conn = new WebSocket(url);

conn.onmessage = function (event) {

console.log(JSON.parse(event.data));

};

conn.send('hello world');

Page 120: Browsers with Wings

var url = 'ws://apps.leftlogic.com:8000/',

conn = new WebSocket(url);

conn.onmessage = function (event) {

console.log(JSON.parse(event.data));

};

conn.send('hello world');

Page 121: Browsers with Wings

var url = 'ws://apps.leftlogic.com:8000/',

conn = new WebSocket(url);

conn.onmessage = function (event) {

console.log(JSON.parse(event.data));

};

conn.send('hello world');

Page 122: Browsers with Wings

http://rem.im/collab-drawing

Page 123: Browsers with Wings

http://rem.im/collab-drawing

Page 124: Browsers with Wings

http://rem.im/collab-drawing

Page 126: Browsers with Wings

Partial

Page 127: Browsers with Wings

Offline

Page 128: Browsers with Wings

Using a Manifest<!DOCTYPE html>

<html manifest="my.manifest">

<body>

<!-- my page -->

</body>

</html>

Page 129: Browsers with Wings

CACHE MANIFEST

app.html

css/style.css

js/app.js

#version 13

my.manifest

Page 130: Browsers with Wings

The Manifest

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

text/cache-manifest manifest

Page 131: Browsers with Wings

<IfModule mod_expires.c>

ExpiresActive on

ExpiresByType text/cache-manifest

↪ “access plus 0 seconds”

</IfModule>

tip Firefox caching

Page 132: Browsers with Wings

The Manifest

2. First line must be:

CACHE MANIFEST

Page 133: Browsers with Wings

The Manifest

3. Including page is implicitly included in the cache.

Page 134: Browsers with Wings

The Manifest

4. Include some versioning to cache bust your manifest

# version 16

Page 135: Browsers with Wings

The Manifest

5. Two futher namespaces: NETWORK & FALLBACK

FALLBACK:/ offline.html

Page 136: Browsers with Wings

CACHE MANIFEST

CACHE:app.jsapp.cssindex.html

NETWORK:/live/*

FALLBACK:* offline.html

Page 137: Browsers with Wings

CACHE MANIFEST

CACHE:app.jsapp.cssindex.html

NETWORK:/live/*

FALLBACK:* offline.html

Served from cache

Page 138: Browsers with Wings

CACHE MANIFEST

CACHE:app.jsapp.cssindex.html

NETWORK:/live/*

FALLBACK:* offline.html

Requests to

http://mysite.com/live/x

must go via the web

Page 139: Browsers with Wings

CACHE MANIFEST

CACHE:app.jsapp.cssindex.html

NETWORK:/live/*

FALLBACK:/ offline.html

Requests for files not

found in the cache,

are directed to

offline.html (when

offline).

Page 140: Browsers with Wings

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 141: Browsers with Wings

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 142: Browsers with Wings

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 143: Browsers with Wings

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 144: Browsers with Wings

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 145: Browsers with Wings

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 146: Browsers with Wings

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 147: Browsers with Wings

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 148: Browsers with Wings

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 149: Browsers with Wings

File API

Page 150: Browsers with Wings

files[0].getAsDataURL()

Page 151: Browsers with Wings

files[0].getAsDataURL()

Page 152: Browsers with Wings
Page 153: Browsers with Wings

Link prefetching

Web Workers

Web Forms

Hash change event, history state management

Contenteditable

Native drag and drop - embedding of data

Microdata

Cross server messaging

embedded attribute data

mime-type registration

DXHTML6

Page 154: Browsers with Wings

Link prefetching

Web Workers

Web Forms

Hash change event, history state management

Contenteditable

Native drag and drop - embedding of data

Microdata

Cross server messaging

embedded attribute data

mime-type registration

DXHTML6Lie.

Page 155: Browsers with Wings

"Should I be using HTML5today?"

Page 156: Browsers with Wings

1. doctype, script & styles only

Page 157: Browsers with Wings

1. doctype, script & styles only

2. New HTML5 elements

Page 158: Browsers with Wings

1. doctype, script & styles only

2. New HTML5 elements

3. Existing APIs

Page 159: Browsers with Wings

1. doctype, script & styles only

2. New HTML5 elements

3. Existing APIs

4. Shims

Page 160: Browsers with Wings

Yes.