What HTML element do we use to draw on a web page?
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? →
Let’s start with our usual template….
<html>
<head>
<title></title>
</head>
<body>
<script>
</script>
</body>
</html>
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>
As usual, add your script tags:
<script>
</script>
We can have JavaScript run whenever a specific even happens. We’ll use document.addEventListener to monitor for events.
document.addEventListener('DOMContentLoaded', main);
document.addEventListener('DOMContentLoaded', main);
function main() {
// draw stuff here
}
// within your main function
var sketch = document.getElementById('sketch');
var context = sketch.getContext("2d");
<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>
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);
fillRect creates a rectangle. It takes 4 arguments:
context.fillRect(x, y, width, height);
A circle is a bit more complicated:
context.beginPath();
context.arc(x, y, radius, start angle, end angle, clockwise);
context.closePath();
context.fill();
context.beginPath();
context.arc(30, 10, 10, 0, 2 * Math.PI, true);
context.closePath();
context.fill();
You can color your shapes by setting fillStyle:
context.fillStyle = "#00ff00"
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();
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();
}
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)
}
<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();
}
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>