The very first thing that we need to do is to define an object, known as the "graphics context". This object is related to the html canvas on which we wish to draw. Let's suppose that this html canvas is identified by the id "canvas" (I know, not very original...). The javascript code needed to obtain this graphics context (usually denoted by the variable ctx) would be


This is for drawing in 2 dimensions ("2d") which is the focus of this tutorial as we mentioned in the introduction.

Since jQuery is so popular, you will often see the following jQuery "shortcut" instead


Please notice ...

This works well for "normal" web pages. In this tutorial, I make use of an embedded iframe to display a fake web page, containing the drawing canvas, in its own "window". For this reason, the code I have to use is slightly more complicated and looks like the following:


You probably will never need to use something like this but I thought I should point it out now so that you know where the variable ctx, that I will use in the rest of this tutorial, comes from.


Your notebook

Keep your personal notes here. These notes are stored by your browser on your own computer, and will be available from any page on this site. You can choose to use html syntax for formatting.


HTML Add Note


  1. Introduction
  2. Before we begin
  3. Drawing simple lines
  4. Drawing coloured lines
  5. Line width
  6. Line cap
  7. Multiple lines
  8. Joining lines
  9. Curved lines: arcTo()
  10. Curved lines: arc()
  11. Curved lines: quadraticCurveTo()
  12. Curved lines: bezierCurveTo()
  13. Drawing rectangles
  14. Filling shapes
  15. Linear Gradient
  16. Radial Gradient