Graphics

Basics of HTML5 canvas

Javascript

Introduction for beginners. https://developer.mozilla.org/en-US/docs/JavaScript/Guide http://teamtreehouse.com/library/websites/javascript-foundations%C2%A0 Minimal example.

The javascript console can often be opened in a browser by pressing F12.

The canvas element

"The canvas element provides scripts with a resolution-dependent bitmap canvas, which can be used for rendering graphs, game graphics, or other visual images on the fly."

The canvas has been designed for pixel-based graphics, while SVG (Scalable Vector Graphics, another W3C standard) is for vector-based graphics.

Data visualization.

Supports 3D graphics with packages such as BabylonJS and ThreeJS.

Good resources:

Accessibility

http://www.creativebloq.com/web-design/canvas-element-accessibility-41514740 http://www.w3.org/html/wg/wiki/AddedElementCanvas

Canvas cheatsheet

https://simon.html5.org/dump/html5-canvas-cheat-sheet.html

The canvas coordinate system

X axis is horizontal, directed to the right

Y axis is vertical, directed downwards

(0 , 0) is in the top left corner

Basic example

http://jsbin.com/susica/1/edit?html,output

Only access elements when the DOM is ready: It's a good practice to have a init function, as we cannot access the elements of the page before the page has been loaded entirely and before the DOM is ready.

Summary:

Drawing Principles

Changing the coordinate system

http://jsbin.com/vinuqa/1/edit

Wrap in a function: http://jsbin.com/vinuqa/2/edit

Draw a monster. Moving the monster. Rotating the monster.

BEWARE: all drawings to come will be in the modified coordinate system!

Saving and restoring the context

There are two methods for saving and restoring the context properties: ctx.save() and ctx.restore().

Best practice: save the context at the beginning of any function that changes the context, restore it at the end of the function!

http://jsbin.com/moporu/2/edit

Project ideas

Immediate drawing mode: rectangles, text and images

As soon as the ctx.strokeRect(x, y, width, height) or the ctx.fillRect(x, y, width, height) method is called, a rectangle is indeed drawn immediately in the canvas.

Example with rectangles.

Drawing text

The canvas API provides two main methods for drawing text: ctx.strokeText(message, x, y) and ctx.fillText(message, x, y).

Example with text.

The font can be set like so: context.font = "italic bold 36px Arial";

Text example with maxWidth.

The ctx.measureText() method can be used to get the current width in pixels of a given text.

Changing text baseline. Horizontal alignment.

Drawing images

Load images in the background, wait for them to be loaded before drawing!

Example with onload function. drawImage(img, sx, sy, sw, sh, dx, dy, dw, dh): for drawing sub-images, (sx, sy, sw, sh) define the source rectangle, while dx, dy, dw, sh define the target rectangle. If these rectangles don't have the same size, the source sub-image is resized. Resizing images.

Best practice: only draw an image that is fully loaded, use the onload callback! Like this.

Drawing images from a video stream

The drawImage(...) function can take a video element as its first parameter. WARNING: starts loading video.

Project ideas

Path drawing mode: lines, circles, arcs and curves

Path mode = fill a buffer then execute all buffered orders at once to enable optimization and parallelism.

Example drawing 1000 rectangles by first storing them in memory and then drawing all at once.

Summary of Path mode principles:

Warning

Drawing arcs is usually done through a higher level package rather than drawing the arcs one at a time by hand, see the following examples as references and do not try to memorize them.

Lines

Draw a grid. Comparison to immediate drawing. Disconnected lines.

Important: If you do not want to draw parts of the same path several times, you need to draw two different paths, using the ctx.beginPath() method, as shown in the next example.

http://jsbin.com/niceqo/2/edit http://jsbin.com/faxago/1/edit

Arrows

http://jsbin.com/qekuqotumu/1/edit http://www.dbp-consulting.com/tutorials/canvas/CanvasArrow.html

Closing a path

http://jsbin.com/jocina/1/edit

Arcs

http://www.html5canvastutorials.com/tutorials/html5-canvas-arcs/ http://jsbin.com/tikite/1/edit http://jsbin.com/gazuba/2/edit

Project idea: make a small program that draws a smiling head.

Rounded rectangles

http://www.dbp-consulting.com/tutorials/canvas/CanvasArcTo.html http://jsbin.com/bocomu/1/edit http://jsbin.com/kuqalu/1/edit http://jsbin.com/kuqalu/edit?html,console,output

Quadratic curves

http://www.html5canvastutorials.com/tutorials/html5-canvas-quadratic-curves/ http://jsbin.com/vefivu/1/edit http://jsbin.com/sibuse/1/edit Curved arrow.

Bezier curves

http://blogs.sitepointstatic.com/examples/tech/canvas-curves/bezier-curve.html http://www.html5canvastutorials.com/tutorials/html5-canvas-bezier-curves/ http://lib.ivank.net/?p=demos&d=bezier Video tutorial. http://jsbin.com/hodawa/2/edit http://jsbin.com/wifowa/1/edit Interactive bezier tool.

Project ideas

Colors, gradients, patterns, shadows, etc.

You can use the same syntax for colors that is supported by CSS3. Drawing transparent rectangles.

Gradients

Gradient example.

The concept of linear gradient is seen as an "invisible" rectangle in which a set of colors are interpolated along a line.

Changing gradient direction. Chopping up the gradient rectangle. Like previous but with a loop. Gradient wireframes. Narrow gradient. Try making it wide. Many small gradient rectangles. Sharper transitions. Radial rainbow gradient. Asymetric.

Painting with patterns

http://jsbin.com/qezojo/1/edit Repeating, try: repeat-x, repeat-y or no-repeat in the createPattern function.

Multiple image loader

For drawing with multiple patterns.

Shadows

http://jsbin.com/wivubi/3/edit Unwanted shadow. Hiding unwanted shadows.

Styling lines

Line thickness. Line end caps. Corners. Limiting corner length in narrow angles.

Project ideas