KEMBAR78
HTML5 tutorial: canvas, offfline & sockets | PDF
HTML5
Huh, What is it good for?
Remy Sharp
@rem

   I wrote a book on it :)
It's more than
๏ HTML        ๏ Buzzword
๏ New HTML5   ๏ Simple
  elements      doctype
JavaScript
๏Offline
๏App   cache

๏WebStorage
๏WebSQL
๏IndexedDB
๏Multimedia
๏Video    & Audio
๏X-domain




                     JavaScript
  messaging

๏CORS
๏File    API

๏Drag    & Drop

๏History       API

๏Lots,    lots
 more.
JavaScript
๏Canvas API
๏Offline apps
๏Web Sockets
When can I
use "HTML5"?
Making it
work in
the "other"
browser
Polyfills
will/might save you
A shim that mimics a future
API providing a fallback to
       older browsers
    http://goo.gl/0Z9eI
Oh, and learn
JavaScript
Canvas
Han Solo of HTML5
Canvas   SVG
๏ 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Canvas</title>
</head>
<body>
  <canvas></canvas>
</body>
</html>
2D API
ctx = canvas.getContext('2d')
๏Gradients
๏Pixel manipulation
๏Paths
๏Shadows
๏Export to data url
๏State saving
Gradients
var rainbox = ctx.createLinearGradient(0, 0, w, h),
    colours = {
       0: 'red',
       0.167: 'orange',
       0.333: 'yellow',
       // etc
    };

for (var key in colours) {
  rainbow.addColorStop(key, colours[key]);
}

ctx.fillStyle = rainbox;
ctx.fillRect(0, 0, w, h);
Pixel Pushing
var pixels = ctx.getImageData(0, 0, w, h),
    len = pixels.data.length;

for (var i = 0; i < len; i += 4) {
  var rgb = 'rgb(' + // ↵
    [
      pixels.data[i],   // red
      pixels.data[i+1], // green
      pixels.data[i+2] // blue
    ].join(',') + ')';
}
Paths
var pinsize = 26,
    pinedge = pinsize * 0.1,
    centre = pinsize/2,
    radius = centre - pinedge,
    circle = Math.PI * 2;

pinctx.beginPath();
pinctx.arc(centre, centre, // ↵
           radius, 0, circle, true);
pinctx.closePath();
pinctx.fill();
T ip
  Math.PI == 180°
T ip
Math.PI * 2 == 360°
T ip
d * Math.PI / 180
    == radian
Shadows
pinctx.shadowBlur = 2;
pinctx.shadowOffsetX = 2;
pinctx.shadowOffsetY = 2;
pinctx.shadowColor = 'rgba(0,0,0,.7)';
Export
pinctx.canvas.toDataURL('image/png')



data:image/png;base64,...
T ip
  context.canvas

  All context contain back-
  reference to it's canvas
State Saving
pinctx.fillStyle = '#fff';
pinctx.save();
pinctx.fillStyle = '#f00';
// do something
pinctx.restore();
// now fillStyle is #fff
Offline Applications
Using a Manifest
<!DOCTYPE html>
<html manifest="my.appcache">
<body>
<!-- my page -->
</body>
</html>
my.appcache
CACHE MANIFEST
app.html
css/style.css
js/app.js
#version 13
The Manifest

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

text/cache-manifest appcache
Firefox caching

<IfModule mod_expires.c>
 ExpiresActive on
 ExpiresByType text/cache-manifest
↪ “access plus 0 seconds”
</IfModule>
The Manifest

2. First line must be:

     CACHE MANIFEST
The Manifest

3. Including page is implicitly
   included in the cache.
The Manifest

4. Two futher namespaces:
   NETWORK & FALLBACK
CACHE MANIFEST

CACHE:
app.js
app.css
index.html

NETWORK:
http://*
https://*

FALLBACK:
/ offline.html
CACHE MANIFEST


What to   CACHE:
          app.js

cache
          app.css
          index.html

          NETWORK:
          http://*
          https://*

          FALLBACK:
          / offline.html
CACHE MANIFEST

                 CACHE:
App cache        app.js
                 app.css
requires all     index.html


resources are    NETWORK:
                 http://*

accounted for.   https://*

                 FALLBACK:
                 / offline.html
CACHE MANIFEST

                  CACHE:
                  app.js
Requests for      app.css

files not found   index.html

in the cache,     NETWORK:
                  http://*
are directed      https://*

to offline.html   FALLBACK:
(when offline).   / offline.html
CACHE MANIFEST

CACHE:
app.js
app.css
index.html

NETWORK:
http://*
https://*

FALLBACK:
/ offline.html
The Manifest

5. Include some versioning to
   cache bust your manifest

      # version 16
The Process
Browser: I have a
Browser: request    Server: serve all       manifest, cache
                                                assets



                       Browser:
 Server: serve
                    applicationCache       Browser: reload
manifest assets
                        updated



Browser: serve        Browser: only        Server: 304 Not
    locally        request manifest file      Modified
When your app updates

applicationCache.onUpdateReady = function () {
   if (confirm("New version ready. Refresh?")) {
     // reload
     window.location = window.location;
   }
};
T ip
   Do offline last
Web Sockets
Two way real-time comms




           http://www.flickr.com/photos/44442915@N00/4960579336
In a nutshell
๏Persistent connection
๏Tiny chunks of data exchanged
๏Bi-directional & no origin rules
Some Uses
๏Chat / "hello world"
๏Streaming data like stock share prices
๏Multi-player game state
๏Google Wave   remember? :)
Native or
polyfilled
  IE6✓ :'(
http://github.com/gimite/web-socket-js/
new WebSocket(url)


      http://dev.w3.org/html5/websockets/
ws://node.remysharp.com:8000




      http://github.com/miksago/node-websocket-server
onopen
onmessage
onclose
onerror
var data = JSON.parse(event.data);
.send
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);
};
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);
};
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);
};
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);
};
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);
};
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);
};
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);
};
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);
};


              Let's play
Questions?
@rem
remy@leftlogic.com

HTML5 tutorial: canvas, offfline & sockets