The canvas is a two-dimensional grid. The coordinate (0, 0) is at the upper-left corner of the canvas. Along the x-axis, values increase toward the right edge of the canvas. Along the y-axis, values increase toward the bottom edge of the canvas.
The coordinate diagram in Figure 4-2 was drawn with a <canvas>
element. It comprises:
A set of off-white vertical lines
A set of off-white horizontal lines
Two black horizontal lines
Two small black diagonal lines that form an arrow
Two black vertical lines
Two small black diagonal lines that form another arrow
The letter âxâ
The letter âyâ
The text â(0, 0)â near the upper-left corner
The text â(500, 375)â near the lower-right corner
A dot in the upper-left corner, and another in the lower-right corner
In the following sections, weâll explore how to create the effect
shown in this figure. First, we need to define the <canvas>
element itself. The <canvas>
element defines the width
and height
of the rectangle, and the id
so we can find it later:
<canvas id="c" width="500" height="375"></canvas>
Then we need a script to find the <canvas>
element in the DOM and get its
drawing context:
var c_canvas = document.getElementById("c"); var context = c_canvas.getContext("2d");
Now we can start drawing lines.
Get HTML5: Up and Running now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.