breaking javascript limits on mobile html5

161
Max Firtman @firt BREAKING LIMITS ON JAVASCRIPT FOR MOBILE HTML5 San Francisco, May 28th, 2013 Tuesday, May 28, 13

Upload: maximiliano-firtman

Post on 14-May-2015

1.865 views

Category:

Technology


1 download

DESCRIPTION

Companion presentation for the workshop at Fluent, May 28th 2013.

TRANSCRIPT

Page 1: Breaking JavaScript Limits on Mobile HTML5

Max Firtman @firt

BREAKING LIMITS

ON JAVASCRIPT FOR

MOBILE HTML5

San Francisco, May 28th, 2013

Tuesday, May 28, 13

Page 2: Breaking JavaScript Limits on Mobile HTML5

mobile+web developer

maximiliano @firt

Tuesday, May 28, 13

Page 3: Breaking JavaScript Limits on Mobile HTML5

Tuesday, May 28, 13

Page 4: Breaking JavaScript Limits on Mobile HTML5

Tuesday, May 28, 13

Page 5: Breaking JavaScript Limits on Mobile HTML5

we’ll talk about mobile

1- Mobile HTML52- JavaScript on Mobile3- UI4- Device

Tuesday, May 28, 13

Page 6: Breaking JavaScript Limits on Mobile HTML5

material

Presentationonline tool (Chrome/FF)

smartphone/tablet?

Tuesday, May 28, 13

Page 7: Breaking JavaScript Limits on Mobile HTML5

Tuesday, May 28, 13

Page 8: Breaking JavaScript Limits on Mobile HTML5

1- mobile html5

Tuesday, May 28, 13

Page 9: Breaking JavaScript Limits on Mobile HTML5

mobile

1- websites2- apps

Tuesday, May 28, 13

Page 10: Breaking JavaScript Limits on Mobile HTML5

mobile

1- websites2- apps

Tuesday, May 28, 13

Page 11: Breaking JavaScript Limits on Mobile HTML5

mobile

1- websites2- apps

Tuesday, May 28, 13

Page 12: Breaking JavaScript Limits on Mobile HTML5

with html5we can create...

Tuesday, May 28, 13

Page 13: Breaking JavaScript Limits on Mobile HTML5

we can create

1- websites

Tuesday, May 28, 13

Page 14: Breaking JavaScript Limits on Mobile HTML5

Using the browser

URL

Web Server

1- websiteswe can create

Tuesday, May 28, 13

Page 15: Breaking JavaScript Limits on Mobile HTML5

we can create

2- webapps

Tuesday, May 28, 13

Page 16: Breaking JavaScript Limits on Mobile HTML5

Browser to install

Full-screen

Icon on home screen

Web Server

2- webappswe can create

Tuesday, May 28, 13

Page 17: Breaking JavaScript Limits on Mobile HTML5

full screen webapps

home screen webapps

webapps are also known as(

)

Tuesday, May 28, 13

Page 18: Breaking JavaScript Limits on Mobile HTML5

we can create

3- native webapps

Tuesday, May 28, 13

Page 19: Breaking JavaScript Limits on Mobile HTML5

Package, compile, sign

Icon on home screen

App Store

No web server

3- native webappswe can create

Tuesday, May 28, 13

Page 20: Breaking JavaScript Limits on Mobile HTML5

hybrid apps

packaged webapps

native webapps are also known as

(

)

Tuesday, May 28, 13

Page 21: Breaking JavaScript Limits on Mobile HTML5

mobile

1- websites2- apps

Tuesday, May 28, 13

Page 22: Breaking JavaScript Limits on Mobile HTML5

a- nativeb- webappsc- native webapps

mobile

1- websites2- apps

Tuesday, May 28, 13

Page 23: Breaking JavaScript Limits on Mobile HTML5

web platforms

Tuesday, May 28, 13

Page 24: Breaking JavaScript Limits on Mobile HTML5

How many browsers do you know on desktop?

web platforms

Tuesday, May 28, 13

Page 25: Breaking JavaScript Limits on Mobile HTML5

5web platforms

Tuesday, May 28, 13

Page 26: Breaking JavaScript Limits on Mobile HTML5

How many browsers do you know on mobile?

web platforms

Tuesday, May 28, 13

Page 27: Breaking JavaScript Limits on Mobile HTML5

...web platforms

Tuesday, May 28, 13

Page 28: Breaking JavaScript Limits on Mobile HTML5

Tuesday, May 28, 13

Page 29: Breaking JavaScript Limits on Mobile HTML5

Android Browser

Tuesday, May 28, 13

Page 30: Breaking JavaScript Limits on Mobile HTML5

Android Browser 2.2

Tuesday, May 28, 13

Page 31: Breaking JavaScript Limits on Mobile HTML5

Android Browser 2.3

Tuesday, May 28, 13

Page 32: Breaking JavaScript Limits on Mobile HTML5

Android Browser 4.x

Tuesday, May 28, 13

Page 33: Breaking JavaScript Limits on Mobile HTML5

Did I mention it is NOTGoogle Chrome?

(

)

Tuesday, May 28, 13

Page 34: Breaking JavaScript Limits on Mobile HTML5

Google Chrome

Tuesday, May 28, 13

Page 35: Breaking JavaScript Limits on Mobile HTML5

Google Chrome Android

Tuesday, May 28, 13

Page 36: Breaking JavaScript Limits on Mobile HTML5

Google Chrome iOS

Tuesday, May 28, 13

Page 37: Breaking JavaScript Limits on Mobile HTML5

Safari on iOS

Tuesday, May 28, 13

Page 38: Breaking JavaScript Limits on Mobile HTML5

Opera

Tuesday, May 28, 13

Page 39: Breaking JavaScript Limits on Mobile HTML5

Opera Mobile

Tuesday, May 28, 13

Page 40: Breaking JavaScript Limits on Mobile HTML5

Opera Mini

Tuesday, May 28, 13

Page 41: Breaking JavaScript Limits on Mobile HTML5

Opera for Android

Tuesday, May 28, 13

Page 42: Breaking JavaScript Limits on Mobile HTML5

Firefox

Tuesday, May 28, 13

Page 43: Breaking JavaScript Limits on Mobile HTML5

Firefox OS

Tuesday, May 28, 13

Page 44: Breaking JavaScript Limits on Mobile HTML5

Internet Explorer

Tuesday, May 28, 13

Page 45: Breaking JavaScript Limits on Mobile HTML5

BlackBerry Browser 5-7.x

Tuesday, May 28, 13

Page 46: Breaking JavaScript Limits on Mobile HTML5

BlackBerry Browser 5-7.x

Tuesday, May 28, 13

Page 47: Breaking JavaScript Limits on Mobile HTML5

BlackBerry Browser PB

Tuesday, May 28, 13

Page 48: Breaking JavaScript Limits on Mobile HTML5

BlackBerry Browser BB10

Tuesday, May 28, 13

Page 49: Breaking JavaScript Limits on Mobile HTML5

Nokia Browser

Tuesday, May 28, 13

Page 50: Breaking JavaScript Limits on Mobile HTML5

Nokia Browser Symbian

Tuesday, May 28, 13

Page 51: Breaking JavaScript Limits on Mobile HTML5

Nokia Browser MeeGo

Tuesday, May 28, 13

Page 52: Breaking JavaScript Limits on Mobile HTML5

Nokia Browser Series 40

Tuesday, May 28, 13

Page 53: Breaking JavaScript Limits on Mobile HTML5

Silk

Tuesday, May 28, 13

Page 54: Breaking JavaScript Limits on Mobile HTML5

UC Browser

Tuesday, May 28, 13

Page 55: Breaking JavaScript Limits on Mobile HTML5

Dol!n

Tuesday, May 28, 13

Page 56: Breaking JavaScript Limits on Mobile HTML5

should I continue?

(

)

Tuesday, May 28, 13

Page 57: Breaking JavaScript Limits on Mobile HTML5

Proxy browsers

web platforms

Tuesday, May 28, 13

Page 58: Breaking JavaScript Limits on Mobile HTML5

And it’s not just browsers!

web platforms

Tuesday, May 28, 13

Page 59: Breaking JavaScript Limits on Mobile HTML5

Web View

web platforms

Tuesday, May 28, 13

Page 60: Breaking JavaScript Limits on Mobile HTML5

Web View

web platforms

Tuesday, May 28, 13

Page 61: Breaking JavaScript Limits on Mobile HTML5

Web View

web platforms

Tuesday, May 28, 13

Page 62: Breaking JavaScript Limits on Mobile HTML5

Web View

web platforms

Tuesday, May 28, 13

Page 63: Breaking JavaScript Limits on Mobile HTML5

Official (native) webapp platforms

web platforms

Tuesday, May 28, 13

Page 64: Breaking JavaScript Limits on Mobile HTML5

Official (native) webapp platforms

web platforms

Tuesday, May 28, 13

Page 65: Breaking JavaScript Limits on Mobile HTML5

unknown future

Tuesday, May 28, 13

Page 66: Breaking JavaScript Limits on Mobile HTML5

2013: glasses

Tuesday, May 28, 13

Page 67: Breaking JavaScript Limits on Mobile HTML5

Tuesday, May 28, 13

Page 68: Breaking JavaScript Limits on Mobile HTML5

web platforms

- too many- different versions- different scenarios- not just the browser

Tuesday, May 28, 13

Page 69: Breaking JavaScript Limits on Mobile HTML5

“change is the only constant“Heraclitus

Tuesday, May 28, 13

Page 70: Breaking JavaScript Limits on Mobile HTML5

2- javascript on mobile

Tuesday, May 28, 13

Page 71: Breaking JavaScript Limits on Mobile HTML5

Origins & security

js on mobile

Tuesday, May 28, 13

Page 72: Breaking JavaScript Limits on Mobile HTML5

WebsiteWebappNative webapp

origins & security

Tuesday, May 28, 13

Page 73: Breaking JavaScript Limits on Mobile HTML5

Debugging(demo)

js on mobile

Tuesday, May 28, 13

Page 74: Breaking JavaScript Limits on Mobile HTML5

Power consumption

js on mobile

Tuesday, May 28, 13

Page 75: Breaking JavaScript Limits on Mobile HTML5

battery consumption

Amazon consumes 17% energy in a non used JS

jQuery.js ~ 4 joules (0,02%)~5000 jQuery parsings per charge

Who Killed My Battery ~ mobilexweb.com/go/battery

Tomorrow, 1.45pm @ FluentTuesday, May 28, 13

Page 76: Breaking JavaScript Limits on Mobile HTML5

JavaScript lifetime

js on mobile

Tuesday, May 28, 13

Page 77: Breaking JavaScript Limits on Mobile HTML5

classic web?

js lifetime

Tuesday, May 28, 13

Page 78: Breaking JavaScript Limits on Mobile HTML5

mobile is quite different(demo)

js lifetime

Tuesday, May 28, 13

Page 79: Breaking JavaScript Limits on Mobile HTML5

let's start coding and testing

Tuesday, May 28, 13

Page 80: Breaking JavaScript Limits on Mobile HTML5

page visibility api

Tuesday, May 28, 13

Page 81: Breaking JavaScript Limits on Mobile HTML5

awake detection

Tuesday, May 28, 13

Page 82: Breaking JavaScript Limits on Mobile HTML5

noti!cation api

Tuesday, May 28, 13

Page 83: Breaking JavaScript Limits on Mobile HTML5

zombie window

Tuesday, May 28, 13

Page 84: Breaking JavaScript Limits on Mobile HTML5

background

Tuesday, May 28, 13

Page 85: Breaking JavaScript Limits on Mobile HTML5

background

Tuesday, May 28, 13

Page 86: Breaking JavaScript Limits on Mobile HTML5

Tuesday, May 28, 13

Page 87: Breaking JavaScript Limits on Mobile HTML5

<blink>Welcome to my website!</blink>

Tuesday, May 28, 13

Page 88: Breaking JavaScript Limits on Mobile HTML5

<bgsound src="welcome.wav">

Tuesday, May 28, 13

Page 89: Breaking JavaScript Limits on Mobile HTML5

<font family="Arial" size="20">

Tuesday, May 28, 13

Page 90: Breaking JavaScript Limits on Mobile HTML5

background

<meta http-equiv="refresh" content="60">

Tuesday, May 28, 13

Page 91: Breaking JavaScript Limits on Mobile HTML5

background<meta http-equiv="refresh" content="2">

Tuesday, May 28, 13

Page 92: Breaking JavaScript Limits on Mobile HTML5

background<meta http-equiv="refresh" content="2">

<script>var mr = document.querySelector("meta");setInterval(function() { mr.content=mr.content; }, 1000); </script>

Tuesday, May 28, 13

Page 93: Breaking JavaScript Limits on Mobile HTML5

Tuesday, May 28, 13

Page 94: Breaking JavaScript Limits on Mobile HTML5

background

Tuesday, May 28, 13

Page 95: Breaking JavaScript Limits on Mobile HTML5

DISCLAIMER

Tuesday, May 28, 13

Page 96: Breaking JavaScript Limits on Mobile HTML5

only from iOS 6.1(5.0+ similar)

Tuesday, May 28, 13

Page 97: Breaking JavaScript Limits on Mobile HTML5

Progressive enhancement

js on mobile

Tuesday, May 28, 13

Page 98: Breaking JavaScript Limits on Mobile HTML5

js on mobile

- security domains- debugging- battery consumption- background execution- progressive enhancement

Tuesday, May 28, 13

Page 99: Breaking JavaScript Limits on Mobile HTML5

3- UI on mobile

Tuesday, May 28, 13

Page 100: Breaking JavaScript Limits on Mobile HTML5

UI

Viewport y pixels

Tuesday, May 28, 13

Page 101: Breaking JavaScript Limits on Mobile HTML5

UI

Full screen

Tuesday, May 28, 13

Page 102: Breaking JavaScript Limits on Mobile HTML5

full screen

Tuesday, May 28, 13

Page 103: Breaking JavaScript Limits on Mobile HTML5

full screen

4 solutionsTuesday, May 28, 13

Page 104: Breaking JavaScript Limits on Mobile HTML5

full screen

Solution #1

Tuesday, May 28, 13

Page 105: Breaking JavaScript Limits on Mobile HTML5

full screen

<meta name="apple-mobile-web-app-capable" content="yes">

Tuesday, May 28, 13

Page 106: Breaking JavaScript Limits on Mobile HTML5

full screen

<meta name="apple-mobile-web-app-capable" content="yes">

if (navigator.standalone) { }

Tuesday, May 28, 13

Page 107: Breaking JavaScript Limits on Mobile HTML5

<meta name="apple-mobile-web-app-capable" content="yes">

<meta name="viewport" content="width=device-width">

Tuesday, May 28, 13

Page 108: Breaking JavaScript Limits on Mobile HTML5

Tuesday, May 28, 13

Page 109: Breaking JavaScript Limits on Mobile HTML5

Tuesday, May 28, 13

Page 110: Breaking JavaScript Limits on Mobile HTML5

full screen

<meta name="apple-mobile-web-app-capable" content="yes"><meta name="viewport" content="width=320.1">

Tuesday, May 28, 13

Page 111: Breaking JavaScript Limits on Mobile HTML5

full screen

<meta name="apple-mobile-web-app-capable" content="yes">

<meta name="viewport" content="width=device-width"><meta name="viewport" content="..." media="device-height: 568px">

Tuesday, May 28, 13

Page 112: Breaking JavaScript Limits on Mobile HTML5

full screen

Tuesday, May 28, 13

Page 113: Breaking JavaScript Limits on Mobile HTML5

full screen

Solution #2

Tuesday, May 28, 13

Page 114: Breaking JavaScript Limits on Mobile HTML5

full screen

Tuesday, May 28, 13

Page 115: Breaking JavaScript Limits on Mobile HTML5

full screen

@media (orientation: landscape) and (height: 214px), (orientation: landscape) and (height: 181px) {}

Tuesday, May 28, 13

Page 116: Breaking JavaScript Limits on Mobile HTML5

full screen

Solution #3

future platforms

Tuesday, May 28, 13

Page 117: Breaking JavaScript Limits on Mobile HTML5

full screenvar body = document.documentElement;

if (body.requestFullScreen) { body.requestFullScreen();}

Tuesday, May 28, 13

Page 118: Breaking JavaScript Limits on Mobile HTML5

full screenvar body = document.documentElement;

if (body.requestFullScreen) { body.requestFullScreen();} else if (body.webkitRequestFullScreen) { body.webkitRequestFullScreen();}

Tuesday, May 28, 13

Page 119: Breaking JavaScript Limits on Mobile HTML5

full screenvar body = document.documentElement;

if (body.requestFullScreen) { body.requestFullScreen();} else if (body.webkitRequestFullScreen) { body.webkitRequestFullScreen();} else if (body.mozRequestFullScreen) { body.mozRequestFullScreen();}

Tuesday, May 28, 13

Page 120: Breaking JavaScript Limits on Mobile HTML5

full screen

Solution #4

...

Tuesday, May 28, 13

Page 121: Breaking JavaScript Limits on Mobile HTML5

full screen

window.addEventListener("load", function() { window.scrollTo(0, 0); });

// use with caredocument.addEventListener("touchmove", function(e) { e.preventDefault() });

Tuesday, May 28, 13

Page 122: Breaking JavaScript Limits on Mobile HTML5

UI

images for different screen densities

Tuesday, May 28, 13

Page 123: Breaking JavaScript Limits on Mobile HTML5

screen densities

<img src="photo.png" width="300">

Tuesday, May 28, 13

Page 124: Breaking JavaScript Limits on Mobile HTML5

screen densities

Tuesday, May 28, 13

Page 125: Breaking JavaScript Limits on Mobile HTML5

300 CSS pixels

300 1.00390 1.30450 1.50600 2.00672 2.24900 3.00

device px px ratio

screen densities

Tuesday, May 28, 13

Page 126: Breaking JavaScript Limits on Mobile HTML5

Solution #1

Tuesday, May 28, 13

Page 127: Breaking JavaScript Limits on Mobile HTML5

screen densities

use vector images <img src="photo.svg" width="300">

<svg></svg>

@font-face {}

Tuesday, May 28, 13

Page 128: Breaking JavaScript Limits on Mobile HTML5

screen densities

Tuesday, May 28, 13

Page 129: Breaking JavaScript Limits on Mobile HTML5

screen densities

Tuesday, May 28, 13

Page 130: Breaking JavaScript Limits on Mobile HTML5

Solution #2

Tuesday, May 28, 13

Page 131: Breaking JavaScript Limits on Mobile HTML5

screen densities

<img src="photo.png" width="300">

if (window.devicePixelRatio > 1.5) { // change URL}

Tuesday, May 28, 13

Page 132: Breaking JavaScript Limits on Mobile HTML5

Solution #3

Tuesday, May 28, 13

Page 133: Breaking JavaScript Limits on Mobile HTML5

screen densities

<div id="photoContainer">

#photoContainer { background-image: -webkit-image-set(url('photo-lo.png') 1x, url('photo-hi.png') 2x); width: 300px; height: 200px;}

Tuesday, May 28, 13

Page 134: Breaking JavaScript Limits on Mobile HTML5

Solution #4

Tuesday, May 28, 13

Page 135: Breaking JavaScript Limits on Mobile HTML5

screen densities

<div id="photoContainer">

#photoContainer { background-image: url('photo-lo.png'); width: 300px; height: 200px;}

Tuesday, May 28, 13

Page 136: Breaking JavaScript Limits on Mobile HTML5

screen densities

<div id="photoContainer">

@media (-webkit-min-device-pixel-ratio: 1.5) {#photoContainer { background-image: url('photo-hi.png'); background-size: 100%; width: 300px; height: 200px;}

}

Tuesday, May 28, 13

Page 137: Breaking JavaScript Limits on Mobile HTML5

screen densities

<div id="photoContainer">

@media (-webkit-min-device-pixel-ratio: 1.5), (min--moz-device-pixel-ratio: 1.5) {

}

Tuesday, May 28, 13

Page 138: Breaking JavaScript Limits on Mobile HTML5

screen densities

<div id="photoContainer">

@media (-webkit-min-device-pixel-ratio: 1.5), (min--moz-device-pixel-ratio: 1.5), (-o-min-device-pixel-ratio: 1/2) {

}

Tuesday, May 28, 13

Page 139: Breaking JavaScript Limits on Mobile HTML5

screen densities

<div id="photoContainer">

@media (-webkit-min-device-pixel-ratio: 1.5), (min--moz-device-pixel-ratio: 1.5), (-o-min-device-pixel-ratio: 1/2), (min-resolution: 1.5dppx) {

}

Tuesday, May 28, 13

Page 140: Breaking JavaScript Limits on Mobile HTML5

always query on ranges

@media (-webkit-device-pixel-ratio: 2)

Tuesday, May 28, 13

Page 141: Breaking JavaScript Limits on Mobile HTML5

always query on ranges

@media (-webkit-min-device-pixel-ratio: 1.7)

Tuesday, May 28, 13

Page 142: Breaking JavaScript Limits on Mobile HTML5

!nd the balance300x300 900x900

Tuesday, May 28, 13

Page 143: Breaking JavaScript Limits on Mobile HTML5

UI

High resolutioncanvas

Tuesday, May 28, 13

Page 144: Breaking JavaScript Limits on Mobile HTML5

hi-res canvas

<canvas width="300" height="200"></canvas>

300px

Tuesday, May 28, 13

Page 145: Breaking JavaScript Limits on Mobile HTML5

hi-res canvas

300 CSS pixels

300 1.00390 1.30450 1.50600 2.00672 2.24900 3.00

device px px ratio

Tuesday, May 28, 13

Page 146: Breaking JavaScript Limits on Mobile HTML5

hi res canvas

<canvas width="300" height="200"> </canvas>

300px

Tuesday, May 28, 13

Page 147: Breaking JavaScript Limits on Mobile HTML5

hi res canvas

var devPxRatio = window.devicePixelRatio;

var canvasPxRatio = document.querySelector("canvas") .getContext("2d") .webkitBackingStorePixelRatio;

Tuesday, May 28, 13

Page 148: Breaking JavaScript Limits on Mobile HTML5

hi res canvas

<canvas width="300" height="200"> </canvas>

300px

devPxRatio = 2canvasPxRatio = 1

Tuesday, May 28, 13

Page 149: Breaking JavaScript Limits on Mobile HTML5

hi res canvas

<canvas width="300" height="200"> </canvas>

300px

devPxRatio >= 1canvasPxRatio = undefined

Tuesday, May 28, 13

Page 150: Breaking JavaScript Limits on Mobile HTML5

Solution #1

hi res canvas

Tuesday, May 28, 13

Page 151: Breaking JavaScript Limits on Mobile HTML5

hi res canvas

<meta name="viewport" content="width=640">

<canvas width="600" height="400"></canvas>

600px

Tuesday, May 28, 13

Page 152: Breaking JavaScript Limits on Mobile HTML5

Solution #2

hi res canvas

Tuesday, May 28, 13

Page 153: Breaking JavaScript Limits on Mobile HTML5

hi res canvas

<script>document.querySelector("canvas") .getContext("2d") .setScale(2, 2);</script>

300px

Tuesday, May 28, 13

Page 154: Breaking JavaScript Limits on Mobile HTML5

hi res canvas

<canvas width="600" height="400"></canvas>

300px

<script>document.querySelector("canvas") .getContext("2d") .setScale(2, 2);</script>

Tuesday, May 28, 13

Page 155: Breaking JavaScript Limits on Mobile HTML5

hi res canvas

<canvas width="600" height="400" style="width: 300px; height: 200px"></canvas>

300px

<script>document.querySelector("canvas") .getContext("2d") .setScale(2, 2);</script>

Tuesday, May 28, 13

Page 156: Breaking JavaScript Limits on Mobile HTML5

multi-platform &multi-resolution

Tuesday, May 28, 13

Page 157: Breaking JavaScript Limits on Mobile HTML5

execution &memory

Tuesday, May 28, 13

Page 158: Breaking JavaScript Limits on Mobile HTML5

wrapping up

Tuesday, May 28, 13

Page 159: Breaking JavaScript Limits on Mobile HTML5

Tuesday, May 28, 13

Page 160: Breaking JavaScript Limits on Mobile HTML5

“change is the only constant“Heraclitus

Tuesday, May 28, 13

Page 161: Breaking JavaScript Limits on Mobile HTML5

you can reach a good experience

Pictures)from)freedigitalphotos.net)

thank you!

[email protected] @!rt

!rt.mobi/pmw

Tuesday, May 28, 13