Canvas

Let’s Draw

Tired of text? We can actually draw with JavaScript using a canvas.

Setting up a Canvas

again, a canvas is an html element - it’s a tag. You can draw on it by using JavaScript. Here’s how you set it up:

  1. create an html page
  2. tell the page to call your code once the whole page is loaded (right now, we’ll do this by adding an onload attribute to the body tag)
  3. add your canvas tags
  4. add your script tags
  5. create a function that will do your drawing!
  6. write some setup code so that you have access to the canvas

Our Usual Template

Let’s start with our usual template….

<html>
<head>
	<title></title>
</head>
<body>
<script>
</script>
</body>
</html>

Telling Your Page to Draw Something

We’ll have to let the page know that it should start drawing once the entire page is loaded.

<body onload="draw()">

A Canvas

Let’s add a place to draw!. Use a pair of opening and closing tags called canvas… with attributes, id, width, and height.

<canvas id="sketch" width="300" height="300">
</canvas>

Script Tags

As usual, add your script tags:

<script>
</script>

Draw Function

Within your script tags, define the function that you specified in your body tag.

<script>
function draw() {
 // your drawing goes here
}
</script>

Using Your Canvas in JavaScript

In order to draw on your canvas, you have to:

  1. retrieve the canvas element from your page using the id
  2. get the context from your canvas element (which is what we’ll be using to draw)
<script>
var sketch = document.getElementById('sketch');
var context = sketch.getContext("2d");
</script>

All Together Now…

Here’s everything put together.

<html>
<head>
	<title></title>
</head>
<body onload="draw()">
<canvas id="sketch" width="300" height="300">
</canvas>
<script>
function draw() {
	var sketch = document.getElementById('sketch');
	var context = sketch.getContext("2d");
	// draw stuff here!
}
</script>
</body>
</html>

Drawing

About the canvas…

Once you have your context, you can call methods (or functions) from the context by using the dot and the function name:

context.fillRect(30, 30, 50, 50);

A Rectangle

fillRect creates a rectangle. It takes 4 arguments:

context.fillRect(x, y, width, height);

A Circle

A circle is a bit more complicated:

context.beginPath();
context.arc(x, y, radius, start angle, end angle, clockwise);
context.closePath();
context.fill();

Circle Example

context.beginPath();
context.arc(30, 10, 10, 0, 2 * Math.PI, true);
context.closePath();
context.fill();

Colors

You can color your shapes by setting fillStyle:

context.fillStyle = "#00ff00"

More About Drawing

Each shape you create draws on top of all of your previous drawwings. In this case, the green circle is drawn over the black square:

context.fillRect(40, 30, 100, 100);

context.fillStyle = "#00ff00"
context.beginPath();
context.arc(50, 40, 40, 0, 2 * Math.PI, true);
context.closePath();
context.fill();

An Example Program:

<body onload="draw()">
<canvas id="sketch" width="300" height="300">
</canvas>
<script>
function draw() {
        var sketch = document.getElementById('sketch');
        var context = sketch.getContext("2d");

        context.fillRect(40, 30, 100, 100);

        context.fillStyle = "#00ff00"
        context.beginPath();
        context.arc(50, 40, 40, 0, 2 * Math.PI, true);
        context.closePath();
        context.fill();
}
</script>
</body>