Essentially the browser gives you a rectanglar area on the screen that you can draw into
Canvas was originally created by Apple for its Dashboard widgets, but it has since been
adopted by every major browser vendor and is now part of the HTML 5 spec
Default canvas dimensions are (x, y) = (300, 150)
The upper-left corner of the canvas has the coordinates (x,
y) = (0,
0)
SVG vs Canvas
SVG is a vector API that draws shapes. Each shape has an object that you can
attach
event handlers to.
If you zoom in
the shape stays smooth, whereas Canvas would become pixelated
Useful Commands
strokeRect
The strokeRect(x, y,
width, height) method draws the outline of a rectangle
fillRect
The
fillRect(x, y, width, height) method draws a filled
rectangle
ctx.fillStyle = 'value';
Holds the style value for filled objects
Possible values are CSS colors, a gradient or a pattern
The default value is black
ctx.strokeStyle = "red"
Holds the style value for stroke objects
May be a CSS color, a gradient or a pattern
The default style value is black
clearRect ctx.clearRect(x, y, width, height)
clears the rectangle area defined by left upper corner (x, y)
getContext('2d')
Returns the context, i.e. an object that exposes an API for drawing
on the
canvas
arc(x, y, radius, startAngle, endAngle, anticlockwise)
Defines a piece of a circle
The center of this circle is (x,y)
The radius is r
The start and end point of the arc are given as angles
in radians
The optional boolean anticlockwise parameter defines if the arcs
are
measured counterclockwise (true) or clockwise (value
false;
the default)
A call of arc() is part of a path
declaration, the actual drawing is done with a call of
stroke(),
drawing a piece of the
circle line, or fill(), which
draws a section of the circle disk
linewidth="value"
stroke()
Draws the outline of the shape defined by the current path
The style settings for the actual drawing are the ones that are stored in
strokeStyle, and the current properties for lines and shadows
fill()
Draws the full area of the shape defined by the current path
The style settings for the actual drawing are the ones that are stored in
fillStyle and the current properties for shadows
beginPath()
Begins a new or resets the current path
A new path is then built with
moveTo()
lineTo()
rect()
quadraticCurveTo()
bezierCurveTo()
arc()
arcTo()
The actual drawing of the path is done with
stroke()
or
fill()
A call of
closePath()
does close the new path, but in the sense that the current point is connected to
the intial
starting point
It does not cause the path to be drawn
closePath()
In an open path, this method call "closes" the path in the sense that it
connects the current point to the starting point of the path
Note that closePath() does not "finish" the path in the sense that
it draws it
To do that, you still need to call the stroke() or
fill() method
moveTo(x, y)
Moves the path to the new point (x,y)
Different to lineTo(x,y), the moveTo(x,y) call does
not create
a line to the new point
lineTo(x, y)
Adds a line to the path from the current point to the new point (x,y)
strokeText(text, x, y, maxWidth)
Draw the given text string at the (x,y) position in stroke
characters on the canvas
If the optional (floating point number) maxWidth is set, the text
is scaled accordingly
By default, x is the start point of the text string (see textAlign)
and y is the alphabetic baseline
fillText (text, x, y, maxWidth)
Draw the given text string at the (x, y) position in filled
characters on the canvas
If the optional (floating point number) maxWidth is set, the text
is scaled accordingly
By default, x is the start point of the text string and y is the
alphabetic baseline
Safari Notes on Canvas
To draw a rectangle, specify the x and y coordinates and the
height and width of the rectangle
The strokeRect(x, y, width, height) method draws the outline of a rectangle
The fillRect(x, y, width, height) method draws a filled rectangle
You draw shapes other than rectangles by creating a path, adding line segments, curves,
or arcs, and closing the path
Begin a path using beginPath(). Set the starting point, or start a
discontinuous subpath, by calling the moveTo(x, y) method
The closePath() method draws a line from the current endpoint to the
starting point of the path, creating a closed shape
The path is not actually drawn until you call stroke() or
fill()
The stroke or fill style specifies the color the element is
drawn in
Colors are specified using the same naming conventions as CSS —
ctx.strokeStyle = "black", for example, or
ctx.fillStyle = "rgba(128, 128, 128, 0.5)"
You can also set the line width for strokes. For example, ctx.lineWidth = 2
You can add a shadow to any shape, or make any shape into a mask by designating it as
the clipping region for drawing operations
Canvas supports matrix transforms—anything you draw can be translated, rotated, scaled,
and more