2. Rob Hawkes
@robhawkes for you social media folk
rawkes.com if you want to see more
The  place  to  be Yes,  thatâs  me
looking  horrible
AKA.  Layabout Guess  my
middle  name
3. âCanvas is my favourite part
of HTML5, alongside its
video and audio supportâ
Myself, at some point
5. An overview of canvas
â˝ 2D drawing platform within the browser
â˝ Uses nothing more than JavaScript and
HTML â no plugins
â˝ Extensible through a JavaScript API
â˝ Created by Apple for dashboard widgets
â˝ Now openly developed as a W3C spec
6. Bitmap vs. vector
â˝ Canvas is a bitmap system
⢠Everything is drawn as a single, ďŹat, picture
⢠Changes require the whole picture to be redrawn
â˝ SVG is a vector system
⢠Elements to be drawn are separate DOM objects
⢠They can be manipulated individually
â˝ SVG isnât part of HTML5
⢠Future isnât as rosy as canvasâ
7. Browser support
â˝ Most modern browsers
⢠Safari
⢠Chrome
⢠Firefox
⢠Opera
â˝ No Internet Explorer support by default
⢠However, there are hacks to get it working
15. Created using the new HTML5 tag
<canvas height=â600â width=â800â></canvas>
Height  and  width  need  to  be  set  explicitly
16. (0,0)
x
y
Uses the standard screen-based
coordinate system
17. Everything is drawn onto the
2D rendering context (ctx)
2D rendering context
Canvas
18. Use getContext() to access the
2D rendering context
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
This  is  your  friend
19. ctx.fillStyle = 'rgb(255, 0, 0)';
ctx.strokeStyle = 'rgba(0, 255, 0, 0.5)';
Use  RGBA  for  alpha
transparency
fillStyle() and strokeStyle() define
the style of shapes to be drawn
20. Simple shapes
Method Action
ďŹllRect(x, y, w, h) Draws a rectangle using the current ďŹll style
strokeRect(x, y, w, h) Draws the outline of a rectangle using the current
stroke style
clearRect(x, y, w, h) Clears all pixels within the given rectangle
Simple shapes are drawn without
effecting the current path
22. Complex shapes & paths
â˝ Paths are a list of subpaths
â˝ Subpaths are one or more points
connected by straight or curved lines
â˝ Rendering context always has a current
path
â˝ A new path should be created for each
individual shape
23. Complex shapes & paths
Method Action
beginPath() Resets the current path
closePath() Closes the current subpath and starts a new one
moveTo(x, y) Creates a new subpath at the given point
ďŹll() Fills the subpaths with the current ďŹll style
stroke() Outlines the subpaths with the current stroke style
24. Complex shapes & paths
Method Action
lineTo(x, y) Draws a straight line from the previous point
rect(x, y, w, h) Adds a new closed rectangular subpath
arc(x, y, radius, startAngle, Adds a subpath along the circumference of
endAngle, anticlockwise) the described circle, within the angles deďŹnes
arcTo(x1, y1, x2, y2, radius) Adds a subpath connecting two points by an
arc of the deďŹned radius
bezierCurveTo(cp1x, cp1y, Adds a cubic BĂŠzier curve with the given
cp2x, cp2y, x, y) control points
quadraticCurveTo(cpx, Adds a quadratic BĂŠzier curve with the given
cpy, x, y) control points
34. Harnessing the power
â˝ Remember all the shapes on the canvas
â˝ Move them, transform them, and make
them interact
â˝ Redraw the all the shapes in their new
positions
â˝ Rinse and repeat, really quickly
35. My workflow
â˝ Treat each shape as a JavaScript object
â˝ Each shape object has position values
â˝ Store the shape objects in an array
â˝ Run a timeout function every 40 ms
â˝ Clear the canvas
â˝ Make any changes to the shape objects
â˝ Loop through and redraw every shape
37. The future of canvas
â˝ OOP programming allows much to be
achieved through canvas
â˝ Itâs ďŹexible and powerful
⢠Animation engines
⢠Pseudo 3D graphics
â˝ Reading pixel values opens a lot of doors
â˝ Integration with other HTML5 elements is
a killer feature
39. Canvas vs. Flash
â˝ Canvas will ďŹourish with future
development of frameworks
â˝ Its animation capabilities are only just
being realised
â˝ Flash has tight integration with the ofďŹine
world, but HTML5 is changing that
â˝ There is a gap in the market for a GUI for
developing canvas applications