Book
Submodule 15.1: Canvas HTML element
Submodule 15.1: Canvas HTML element
Completion requirements
View
- Text
- Drawing
- Image
Canvas drawing
Here we will start actually drawing on our canvas. The first step is to learn how to draw a line.
The JavaScript code that we will need for that is:
/* draw lines between opposite corners to make an X: */
/*Let's set some styles */
context0.strokeStyle = "red";
context0.lineWidth = "2";
/* call beginPath() to start the drawing path*/
context0.beginPath();
/* call moveTo() to position the pen at the starting point at one corner*/
context0.moveTo(0, 0);
/* call lineTo() to define a line to the opposite corner*/
context0.lineTo(context0.canvas.width, context0.canvas.height);
/* call moveTo() to position the pen at the starting point at one corner*/
context0.moveTo(context0.canvas.width, 0);
/* call lineTo() to define a line to the opposite corner*/
context0.lineTo(0, context0.canvas.height);
/* call stroke() to draw it */
context0.stroke();
the above will create two lines that appear as an "X" in our canvas: