Drawing Revisited, Events

Let’s Draw

What HTML element do we use to draw on a web page?

Setting up a Canvas

Again, a canvas is an html element - it’s a tag. You can draw on it by using JavaScript. How do we set it up? →

  1. create an html page
  2. add your canvas tags
  3. add your script tags
  4. tell the page to call your code once the whole page is loaded
  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>

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="800" height="600">
</canvas>

Script Tags

As usual, add your script tags:

<script>
</script>

Events

We can have JavaScript run whenever a specific even happens. We’ll use document.addEventListener to monitor for events.

document.addEventListener('DOMContentLoaded', main);

Events Continued

document.addEventListener('DOMContentLoaded', main);

Define our Drawing Function

function main() {
	// draw stuff here
}

Get the Canvas Element and Context

// within your main function
var sketch = document.getElementById('sketch');
var context = sketch.getContext("2d");

A Template (Somewhat Updated)

<html>
<head>
    <title></title>
</head>
<body>
<canvas id="sketch" width="800" height="600">
</canvas>
<script>
document.addEventListener('DOMContentLoaded', main);

function main() {
	var sketch = document.getElementById('sketch');
	var context = sketch.getContext("2d");
	// your code goes 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();

Let’s Make Some Functions to Draw Stuff

A Circle Function

First… it’s tough to make a circle. Lets try making a few functions to reduce our code. →

document.addEventListener('DOMContentLoaded', main);

function main() {
	var sketch = document.getElementById('sketch');
	var context = sketch.getContext("2d");
	draw_circle(context, 300, 300, 100);
}

function draw_circle(context, x, y, r) {
	context.beginPath();
	context.arc(x, y, r, 0, 2 * Math.PI, true);
	context.closePath();
	context.fill();
}

A Crescent

document.addEventListener('DOMContentLoaded', main);

function main() {
	var sketch = document.getElementById('sketch');
	var context = sketch.getContext("2d");
	draw_moon(context, 300, 300, 100);
}

function draw_circle(context, x, y, r) {
	context.beginPath();
	context.arc(x, y, r, 0, 2 * Math.PI, true);
	context.closePath();
	context.fill();
}

function draw_moon(context, x, y, r) {
	draw_circle(context, x, y, r)
	context.fillStyle =	"#ffffff";
	draw_circle(context, x + 50, y + 50, r)
}

The Full Program (Part #1)

<html>
<head>
    <title></title>
</head>
<body>
<canvas id="sketch" width="800" height="600">
</canvas>
<script>
document.addEventListener('DOMContentLoaded', main);

function main() {
	var sketch = document.getElementById('sketch');
	var context = sketch.getContext("2d");
	draw_moon(context, 300, 300, 100);
}

function draw_circle(context, x, y, r) {
	context.beginPath();
	context.arc(x, y, r, 0, 2 * Math.PI, true);
	context.closePath();
	context.fill();

The Full Program (Part #2)


}

function draw_moon(context, x, y, r) {
	draw_circle(context, x, y, r)
	context.fillStyle =	"#ffffff";
	draw_circle(context, x + 50, y + 50, r)
}

</script>
</body>
</html>

See this example in action!