173
Max Firtman @firt BREAKING LIMITS ON MOBILE HTML5 Tallinn, Nov 6, 2013

Breaking Limits on Mobile HTML5 - TopConf Tallinn

Embed Size (px)

DESCRIPTION

Hacks for mobile web development and HTML5 that you might not known. Android Browser+Chrome+iOS+Firefox+others.

Citation preview

Page 1: Breaking Limits on Mobile HTML5 - TopConf Tallinn

Max Firtman @firt

!

BREAKING LIMITS

ON MOBILE

HTML5

!

!

!Tallinn, Nov 6, 2013

Page 2: Breaking Limits on Mobile HTML5 - TopConf Tallinn
Page 3: Breaking Limits on Mobile HTML5 - TopConf Tallinn
Page 4: Breaking Limits on Mobile HTML5 - TopConf Tallinn
Page 5: Breaking Limits on Mobile HTML5 - TopConf Tallinn
Page 6: Breaking Limits on Mobile HTML5 - TopConf Tallinn

questions

yes, please !

QA at the end

Page 7: Breaking Limits on Mobile HTML5 - TopConf Tallinn

agenda

Hacks for breaking limits - UI - device interaction - enhancing apps - tools

Page 8: Breaking Limits on Mobile HTML5 - TopConf Tallinn

my goal

Page 9: Breaking Limits on Mobile HTML5 - TopConf Tallinn

hacks, why?

Page 10: Breaking Limits on Mobile HTML5 - TopConf Tallinn
Page 11: Breaking Limits on Mobile HTML5 - TopConf Tallinn

1- UI hacks

Page 12: Breaking Limits on Mobile HTML5 - TopConf Tallinn

UI

Full screen

Page 13: Breaking Limits on Mobile HTML5 - TopConf Tallinn

full screen

Page 14: Breaking Limits on Mobile HTML5 - TopConf Tallinn

full screen

3 solutions

Page 15: Breaking Limits on Mobile HTML5 - TopConf Tallinn

full screen

Solution #1

Page 16: Breaking Limits on Mobile HTML5 - TopConf Tallinn

full screen

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

Page 17: Breaking Limits on Mobile HTML5 - TopConf Tallinn

full screen

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

Page 18: Breaking Limits on Mobile HTML5 - TopConf Tallinn

full screen

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

if (navigator.standalone) { }

Page 19: Breaking Limits on Mobile HTML5 - TopConf Tallinn

demo

Page 20: Breaking Limits on Mobile HTML5 - TopConf Tallinn

full screen

Solution #2

future platforms

Page 21: Breaking Limits on Mobile HTML5 - TopConf Tallinn

full screenvar body = document.documentElement; !

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

Page 22: Breaking Limits on Mobile HTML5 - TopConf Tallinn

full screenvar body = document.documentElement; !

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

Page 23: Breaking Limits on Mobile HTML5 - TopConf Tallinn

full screenvar body = document.documentElement; !

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

Page 24: Breaking Limits on Mobile HTML5 - TopConf Tallinn

full screen

Solution #3

...

Page 25: Breaking Limits on Mobile HTML5 - TopConf Tallinn

full screen

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

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

Page 26: Breaking Limits on Mobile HTML5 - TopConf Tallinn

UI

Snapped mode Windows 8

Page 27: Breaking Limits on Mobile HTML5 - TopConf Tallinn

snapped mode

Page 28: Breaking Limits on Mobile HTML5 - TopConf Tallinn

snapped mode

Page 29: Breaking Limits on Mobile HTML5 - TopConf Tallinn

snapped mode

Solution

Page 30: Breaking Limits on Mobile HTML5 - TopConf Tallinn

snapped mode

@media only screen and (max-width: 400px) { @-ms-viewport {  width: 320px;  } }

Page 31: Breaking Limits on Mobile HTML5 - TopConf Tallinn

snapped mode

Page 32: Breaking Limits on Mobile HTML5 - TopConf Tallinn

UI

images for different screen densities

Page 33: Breaking Limits on Mobile HTML5 - TopConf Tallinn

screen densities

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

Page 34: Breaking Limits on Mobile HTML5 - TopConf Tallinn

screen densities

Page 35: Breaking Limits on Mobile HTML5 - TopConf Tallinn

300 CSS pixels

300 1.00 390 1.30 450 1.50 600 2.00 672 2.24 900 3.00 !

!

device px px ratio

screen densities

Page 36: Breaking Limits on Mobile HTML5 - TopConf Tallinn

300 CSS pixels

300 1.00 390 1.30 450 1.50 600 2.00 672 2.24 900 3.00 !

!

device px px ratio

screen densities

Firefox OS

Galaxy SIIiPhone 5BB Z10Nexus 5

Nexus 7

Page 37: Breaking Limits on Mobile HTML5 - TopConf Tallinn

Solution #1

Page 38: Breaking Limits on Mobile HTML5 - TopConf Tallinn

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

<svg></svg>

@font-face {}

Page 39: Breaking Limits on Mobile HTML5 - TopConf Tallinn

screen densities

Page 40: Breaking Limits on Mobile HTML5 - TopConf Tallinn

screen densities

Page 41: Breaking Limits on Mobile HTML5 - TopConf Tallinn

Solution #2

Page 42: Breaking Limits on Mobile HTML5 - TopConf Tallinn

screen densities

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

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

Page 43: Breaking Limits on Mobile HTML5 - TopConf Tallinn

screen densities

devicePixelRatio = window.screen.availWidth / document.documentElement.clientWidth

Page 44: Breaking Limits on Mobile HTML5 - TopConf Tallinn

Solution #3

Page 45: Breaking Limits on Mobile HTML5 - TopConf Tallinn

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; }

Page 46: Breaking Limits on Mobile HTML5 - TopConf Tallinn

Solution #4

Page 47: Breaking Limits on Mobile HTML5 - TopConf Tallinn

screen densities

<div id="photoContainer">

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

Page 48: Breaking Limits on Mobile HTML5 - TopConf Tallinn

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; }

}

Page 49: Breaking Limits on Mobile HTML5 - TopConf Tallinn

screen densities

<div id="photoContainer">

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

!

!

!

!

}

Page 50: Breaking Limits on Mobile HTML5 - TopConf Tallinn

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) { !

!

!

!

}

Page 51: Breaking Limits on Mobile HTML5 - TopConf Tallinn

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) { !

!

!

}

Page 52: Breaking Limits on Mobile HTML5 - TopConf Tallinn
Page 53: Breaking Limits on Mobile HTML5 - TopConf Tallinn

always query on ranges

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

Page 54: Breaking Limits on Mobile HTML5 - TopConf Tallinn

always query on ranges

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

Page 55: Breaking Limits on Mobile HTML5 - TopConf Tallinn

find the balance300x300 900x900

Page 56: Breaking Limits on Mobile HTML5 - TopConf Tallinn

UI

High resolution canvas

Page 57: Breaking Limits on Mobile HTML5 - TopConf Tallinn

hi-res canvas

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

300px

Page 58: Breaking Limits on Mobile HTML5 - TopConf Tallinn

hi-res canvas

300 CSS pixels

300 1.00 390 1.30 450 1.50 600 2.00 672 2.24 900 3.00 !

!

device px px ratio

Page 59: Breaking Limits on Mobile HTML5 - TopConf Tallinn

hi res canvas

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

300px

Page 60: Breaking Limits on Mobile HTML5 - TopConf Tallinn

hi res canvas

var devPxRatio = window.devicePixelRatio; !

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

Page 61: Breaking Limits on Mobile HTML5 - TopConf Tallinn

hi res canvas

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

300px

devPxRatio = 2 canvasPxRatio = 1

Page 62: Breaking Limits on Mobile HTML5 - TopConf Tallinn

hi res canvas

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

300px

devPxRatio >= 1 canvasPxRatio = undefined

Page 63: Breaking Limits on Mobile HTML5 - TopConf Tallinn

Solution #1

hi res canvas

Page 64: Breaking Limits on Mobile HTML5 - TopConf Tallinn

hi res canvas

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

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

600px

Page 65: Breaking Limits on Mobile HTML5 - TopConf Tallinn

Solution #2

hi res canvas

Page 66: Breaking Limits on Mobile HTML5 - TopConf Tallinn

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

300px

Page 67: Breaking Limits on Mobile HTML5 - TopConf Tallinn

hi res canvas

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

300px

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

Page 68: Breaking Limits on Mobile HTML5 - TopConf Tallinn

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>

Page 69: Breaking Limits on Mobile HTML5 - TopConf Tallinn

multi-platform & multi-resolution

Page 70: Breaking Limits on Mobile HTML5 - TopConf Tallinn

execution & memory

Page 71: Breaking Limits on Mobile HTML5 - TopConf Tallinn

UI

truly numeric field

Page 72: Breaking Limits on Mobile HTML5 - TopConf Tallinn

numeric

<input type="number">

Page 73: Breaking Limits on Mobile HTML5 - TopConf Tallinn

numeric

<input type="number">

Page 74: Breaking Limits on Mobile HTML5 - TopConf Tallinn

numeric

<input type="number">

Page 75: Breaking Limits on Mobile HTML5 - TopConf Tallinn

Solution

Page 76: Breaking Limits on Mobile HTML5 - TopConf Tallinn

numeric

<input type="number" pattern="[0-9]*">

Page 77: Breaking Limits on Mobile HTML5 - TopConf Tallinn

numeric

<input type="password" pattern="[0-9]*">

Page 78: Breaking Limits on Mobile HTML5 - TopConf Tallinn

UI

where is my date picker?

Page 79: Breaking Limits on Mobile HTML5 - TopConf Tallinn

date

<input type="date">

Page 80: Breaking Limits on Mobile HTML5 - TopConf Tallinn

date

<input type="datetime">

Page 81: Breaking Limits on Mobile HTML5 - TopConf Tallinn

date

<input type="datetime">

Page 82: Breaking Limits on Mobile HTML5 - TopConf Tallinn

Solution

Page 83: Breaking Limits on Mobile HTML5 - TopConf Tallinn

date

<input type="datetime-local">

Page 84: Breaking Limits on Mobile HTML5 - TopConf Tallinn

UI

rich editor

Page 85: Breaking Limits on Mobile HTML5 - TopConf Tallinn

rich editor

<ul contenteditable> <li>First item </ul>

Page 86: Breaking Limits on Mobile HTML5 - TopConf Tallinn

rich editor

<ul contenteditable> <li>First item </ul>

Page 87: Breaking Limits on Mobile HTML5 - TopConf Tallinn

rich editor

<ul contenteditable> <li>First item </ul>

Page 88: Breaking Limits on Mobile HTML5 - TopConf Tallinn

UI

zombie tab

Page 89: Breaking Limits on Mobile HTML5 - TopConf Tallinn

background

Page 90: Breaking Limits on Mobile HTML5 - TopConf Tallinn

background

Page 91: Breaking Limits on Mobile HTML5 - TopConf Tallinn

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

Page 92: Breaking Limits on Mobile HTML5 - TopConf Tallinn

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

Page 93: Breaking Limits on Mobile HTML5 - TopConf Tallinn

background

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

Page 94: Breaking Limits on Mobile HTML5 - TopConf Tallinn

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

Page 95: Breaking Limits on Mobile HTML5 - TopConf Tallinn

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

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

Page 96: Breaking Limits on Mobile HTML5 - TopConf Tallinn
Page 97: Breaking Limits on Mobile HTML5 - TopConf Tallinn

background

Page 98: Breaking Limits on Mobile HTML5 - TopConf Tallinn

DISCLAIMER

Page 99: Breaking Limits on Mobile HTML5 - TopConf Tallinn

on iOS 6 on iOS 7

Page 100: Breaking Limits on Mobile HTML5 - TopConf Tallinn

2- device interaction hacks

Page 101: Breaking Limits on Mobile HTML5 - TopConf Tallinn

device

media capture

Page 102: Breaking Limits on Mobile HTML5 - TopConf Tallinn

media capture

<input type="file">

Page 103: Breaking Limits on Mobile HTML5 - TopConf Tallinn

Solution

Page 104: Breaking Limits on Mobile HTML5 - TopConf Tallinn

media capture

<input type="file" accept="image/*">

<input type="file" accept="video/*">

<input type="file" accept="audio/*">

Page 105: Breaking Limits on Mobile HTML5 - TopConf Tallinn

media capture

<input type="file" accept="image/*" capture>

<input type="file" accept="video/*" capture>

<input type="file" accept="video/*" capture>

(old spec)

Page 106: Breaking Limits on Mobile HTML5 - TopConf Tallinn

media capture

Live demo

Page 107: Breaking Limits on Mobile HTML5 - TopConf Tallinn

device

interacting with native apps

Page 108: Breaking Limits on Mobile HTML5 - TopConf Tallinn

native integration

Page 109: Breaking Limits on Mobile HTML5 - TopConf Tallinn
Page 110: Breaking Limits on Mobile HTML5 - TopConf Tallinn

Solution, part I

Page 111: Breaking Limits on Mobile HTML5 - TopConf Tallinn

DON'T DO THAT

Page 112: Breaking Limits on Mobile HTML5 - TopConf Tallinn

Solution, part II

Page 113: Breaking Limits on Mobile HTML5 - TopConf Tallinn

native integration

<meta name="apple-itunes-app" content="app-id=999">

Page 114: Breaking Limits on Mobile HTML5 - TopConf Tallinn

native integration

Page 115: Breaking Limits on Mobile HTML5 - TopConf Tallinn

native integration

<meta name="apple-itunes-app" content="app-id=999">

<meta name="app-argument" content="">

Page 116: Breaking Limits on Mobile HTML5 - TopConf Tallinn

native integration

<meta name="msApplication-ID" content="App"> <meta name="msApplication-PackageFamilyName" content="myPackage">

Page 117: Breaking Limits on Mobile HTML5 - TopConf Tallinn

native integration

Page 118: Breaking Limits on Mobile HTML5 - TopConf Tallinn

native integration

Page 119: Breaking Limits on Mobile HTML5 - TopConf Tallinn

native integration

Page 120: Breaking Limits on Mobile HTML5 - TopConf Tallinn

native integration

<meta name="msApplication-ID" content="App"> <meta name="msApplication-PackageFamilyName" content="myPackage"> !

<meta name="msApplication-Arguments" content=""> !

Page 121: Breaking Limits on Mobile HTML5 - TopConf Tallinn

no api no android

Page 122: Breaking Limits on Mobile HTML5 - TopConf Tallinn

3- enhancing your app hacks

Page 123: Breaking Limits on Mobile HTML5 - TopConf Tallinn

push notification

enhance your app

Page 124: Breaking Limits on Mobile HTML5 - TopConf Tallinn

push

<a href="suscription.passbook"> Subscribe to this site </a>

Page 125: Breaking Limits on Mobile HTML5 - TopConf Tallinn

push

Page 126: Breaking Limits on Mobile HTML5 - TopConf Tallinn

push

Page 127: Breaking Limits on Mobile HTML5 - TopConf Tallinn

enhance your app

iOS home screen title

Page 128: Breaking Limits on Mobile HTML5 - TopConf Tallinn

home screen

Page 129: Breaking Limits on Mobile HTML5 - TopConf Tallinn

home screen

Page 130: Breaking Limits on Mobile HTML5 - TopConf Tallinn

home screen

<title>My long title for SEO</title> <meta name="apple-web-app-title" content="My App">

UNDOCUMENTED

Page 131: Breaking Limits on Mobile HTML5 - TopConf Tallinn

enhance your app

Changing tint iOS 7

Page 132: Breaking Limits on Mobile HTML5 - TopConf Tallinn

tint

body { background-color: red; }

body { background-color: yellow; }

Page 133: Breaking Limits on Mobile HTML5 - TopConf Tallinn

tint

Page 134: Breaking Limits on Mobile HTML5 - TopConf Tallinn

tintbody { background-color: blue; /* for the tint */ background-image: linear-gradient(to bottom, green 0%, green 100%); /* for the body */ }

Page 135: Breaking Limits on Mobile HTML5 - TopConf Tallinn

enhance your app

IE10 Live Tile

Page 136: Breaking Limits on Mobile HTML5 - TopConf Tallinn

live tile

Page 137: Breaking Limits on Mobile HTML5 - TopConf Tallinn

live tile

Page 138: Breaking Limits on Mobile HTML5 - TopConf Tallinn

live tile

<meta name="msapplication-TileImage" content="tile.png"> !

<meta name="msapplication-TileColor" content="#ef0303">

Page 139: Breaking Limits on Mobile HTML5 - TopConf Tallinn

enhance your app

You've said live tile!!!

Page 140: Breaking Limits on Mobile HTML5 - TopConf Tallinn

live tile

<meta name="msapplication-badge" content="frequency=60;polling-uri=XXX"> !

!

Page 141: Breaking Limits on Mobile HTML5 - TopConf Tallinn

live tile<meta name="msapplication-badge" content="frequency=60;polling-uri=XXX"> !

<?xml version="1.0" ?> <badge value="3" /> !

Page 142: Breaking Limits on Mobile HTML5 - TopConf Tallinn

live tile<meta name="msapplication-badge" content="frequency=60;polling-uri=XXX"> !

<?xml version="1.0" ?> <badge value="newMessage" /> !

Page 143: Breaking Limits on Mobile HTML5 - TopConf Tallinn

enhance your app

It’s even better on IE11

Page 144: Breaking Limits on Mobile HTML5 - TopConf Tallinn

live tile

<meta name="msapplication-notification" content=“frequency=30;polling-uri=“> !

!

Page 145: Breaking Limits on Mobile HTML5 - TopConf Tallinn

live tile

document.addEventListener('mssitepinned', startPeriodicUpdateBatch, false); !

!

Page 146: Breaking Limits on Mobile HTML5 - TopConf Tallinn

live tile

document.addEventListener('mssitepinned', startPeriodicUpdateBatch, false); !

!

Page 147: Breaking Limits on Mobile HTML5 - TopConf Tallinn

buildmypinnedsite.com

Page 148: Breaking Limits on Mobile HTML5 - TopConf Tallinn

enhance your app

Storage limits

Page 149: Breaking Limits on Mobile HTML5 - TopConf Tallinn

storage

AppCache, localStorage, WebSQL, IDB

Page 150: Breaking Limits on Mobile HTML5 - TopConf Tallinn

storage

Different domains, iframes and Cross Document Messaging API !

Page 151: Breaking Limits on Mobile HTML5 - TopConf Tallinn

storage

Page 152: Breaking Limits on Mobile HTML5 - TopConf Tallinn

this might not work in the future

Page 153: Breaking Limits on Mobile HTML5 - TopConf Tallinn

do you really need more space?

Page 154: Breaking Limits on Mobile HTML5 - TopConf Tallinn

4- tools

Page 155: Breaking Limits on Mobile HTML5 - TopConf Tallinn

Tools

Bandwidth simulators

Page 156: Breaking Limits on Mobile HTML5 - TopConf Tallinn

netlimiter.com

Page 157: Breaking Limits on Mobile HTML5 - TopConf Tallinn

slowyapp.com

Page 158: Breaking Limits on Mobile HTML5 - TopConf Tallinn

charlesproxy.com

Page 159: Breaking Limits on Mobile HTML5 - TopConf Tallinn

Tools

Remote debuggers

Page 160: Breaking Limits on Mobile HTML5 - TopConf Tallinn

time for demo?

Page 161: Breaking Limits on Mobile HTML5 - TopConf Tallinn

Tools

Virtual Mobile Labs

Page 162: Breaking Limits on Mobile HTML5 - TopConf Tallinn

developer.nokia.com

Page 163: Breaking Limits on Mobile HTML5 - TopConf Tallinn

developer.samsung.com

Page 164: Breaking Limits on Mobile HTML5 - TopConf Tallinn

keynotedeviceanywhere.com

Page 165: Breaking Limits on Mobile HTML5 - TopConf Tallinn

Tools

Live Reload

Page 166: Breaking Limits on Mobile HTML5 - TopConf Tallinn

livereload.com

Page 167: Breaking Limits on Mobile HTML5 - TopConf Tallinn

wrapping up

Page 168: Breaking Limits on Mobile HTML5 - TopConf Tallinn

we need hacks because

• browsers are different • no enough information • undocumented features • buggy

Page 169: Breaking Limits on Mobile HTML5 - TopConf Tallinn

however

• usability and Performance matters • be careful • your app should work anyway • use feature detection

Page 170: Breaking Limits on Mobile HTML5 - TopConf Tallinn
Page 171: Breaking Limits on Mobile HTML5 - TopConf Tallinn

“change is the only constant“Heraclitus

Page 172: Breaking Limits on Mobile HTML5 - TopConf Tallinn

“in the mobile world, change is the only constant“

Page 173: Breaking Limits on Mobile HTML5 - TopConf Tallinn

you can reach a good experience

Pictures)from)freedigitalphotos.net)

thank you!

[email protected] @firt

firt.mobi/pmw Tomorrow

Google Glass talk#TopConf