A Programmer's Guide to Canvas
Written by Ian Elliot   
Article Index
A Programmer's Guide to Canvas
Active and Passive

Active and passive view

The transformation operations are all very easy but it is worth recalling that transformations have two completely different interpretations and uses.

  • The first is often called the active view which is that the transformation moves and modifies what is being drawn.
  • The second is often called the passive view and corresponds to thinking of the transformation as a change in the co-ordinate system.

Both views are useful when programming - lets take a look at an application of each in turn.

The active view of transformation uses them to draw an graphics object at another location.

For example to draw a rectangle at a different angle we could use:

ctx.fillRect (0, 0, 55, 50);

This first moves the origin to 100,100 and then rotates by 45 degrees - the rectangle is now drawn with its top lefthand corner at 100,100 and rotated.

Notice that this is not the same as rotating then translating - try it and see.



The passive view of the transformation simply changes the co-ordinate system and you just draw using the new co-ordinates.

Now let's try a change in the co-ordinate system to make plotting a graph easy. In principle you could plot a sin function using:

for(var i=0;i<4*Math.PI;i+=0.1){

However, if you try it you will just see a small splodge at the top of the canvas. The reason is, of course that the x value varies from 0 to about 12 and the y value varies between +1 and -1.

You can use constants to scale and shift the graph to make it fit in with the current co-ordinate system if you want to. For example:

for(var i=0;i<4*Math.PI;i+=0.1){

This produces a reasonable sine wave but a much cleaner method of plotting graphs is to make use of the transformation to provide the co-ordinates that we would really like to use.

We would like a co-ordinate system that varied between 0 and 12 in the x direction and +1 and -1 in the y direction.  If we assume that the canvas is 400x400 then the change in the co-ordinate system can be created using:

for(var i=0;i<4*Math.PI;i+=0.1){

First we scale so that the x axis runs from 0 to 12 and the y axis from 0 to 2. Then we translate so the the y axis runs from -1 to 1.

The only complication is that that the transformation affects every measured aspect of the drawing, including line width. So we have to reduce the line width back to roughly one pixel in expressed in the new co-ordinate system.


In general to create a co-ordinate system that runs from xmin to xmax and ymin to ymax with a canvas of size width by height  you need to first perform a scaling :


followed by a translate:






JavaScript Graphics
SVG & Canvas




  1. JavaScript Graphics
  2. Canvas v SVG
  3. A Programmer's Guide to Canvas*
  4. Canvas bitmap operations - bitblt in JavaScript *
  5. Reading A BMP File In JavaScript
  6. Getting Started With SVG*
  7. SVG, JavaScript and the DOM
  8. Using Inkscape
  9. Getting started with WebGL
  10. Getting Started with Box2D in JavaScript

*Recently Updated





To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on, Twitter, Facebook or Linkedin.





or email your comment to: comments@i-programmer.info


JavaScript Async - DoEvents & Microtasks

DoEvents is generally regarded as a construct to be avoided. Like many such feared ideas it is only to be feared if you don't understand it. Correctly used it can be a simplification. This  extra [ ... ]

Just JavaScript - Execution Context

Closures are logical but subtle and you need to make sure that you really understand them. This is made easier if you think about a closure as a preserved execution context.  In this extract from [ ... ]

Other Articles

Last Updated ( Thursday, 24 January 2019 )