Drawing basic 2D shapes on a canvas.
Tags: webdev, javascript, tutorial, programming
Published: 2023-05-19 17:48:15

**Table of content:**
1. [Introduction](#item-one)
    - [Do's and don'ts](#item-two)
2. [Primitive Shapes](#item-three)
    - [Rectangles](#item-four)
    - [Paths](#item-five)

<a id="item-one"></a>
## Introduction
Hello 👋, Welcome back. In our previous article, we discussed what 2D shapes are, what a canvas is, and how to use coordinates on a canvas.

In this tutorial, we will be drawing basic shapes. We will work on improving our understanding of drawing shapes on our canvas.

Before we begin drawing, there are a few things you should know about using a canvas.

<a id="item-two"></a>
**do's**
- Remember to always close your canvas with the `</canvas>` tag.
- Sketch what you want to draw on a piece of paper so you will know where to put your coordinates.

**don'ts**
- The size of the shape you're drawing should be no larger than the size of your canvas. If your object is larger than your canvas, the area that the canvas cannot contain will not be shown. 
The diagram below shows what happens when the canvas is smaller than the drawing.


![error](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n82qqufbypuc5uh38nc6.png)


![result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s5srnukxsewu9zak849d.png)


<a id="item-three"></a>
## Primitive shapes.

`<canvas>` supports only two primitive shapes: 
- rectangles and 
- paths (lists of points connected by lines). 
All other shapes are created by combining one or more paths.

<a id="item-four"></a>
### Drawing a rectangle.

Because a rectangle is a primitive shape on canvas, it has its own set of drawing methods. There are three functions on the canvas that draw rectangles:

- fillRect(x, y, width, height). This method creates a filled rectangle.

- strokeRect(x, y, width, height). This creates the rectangle's outline.

- clearRect(x, y, width, height). Clears the chosen rectangular region, making it transparent.

These three functions all use the same parameters. x and y define the top-left corner of the rectangle's position on the canvas (relative to the origin). The width and height define the size of the rectangle.


![html](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/asrxwb6w1vazxvcttwn3.png)

In our script file.

![script](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k2gvx4ovjpzzqhmz4jlf.png)

<a id="item-five"></a>
### Drawing other shapes (using Paths).

In contrast to the rectangle, every other shape must be created by joining one or more paths. A path, as we know, is a list of points connected by lines.

Let's draw a line to see what a path is.

![line](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4lmw6mn04mf58blk0215.png)
**code**

![line](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ndv3ndj90irp4uypktjv.png)

A point, as shown in the diagrams above, is made up of an x and y coordinate. A path is formed by connecting these points with a line.

**another example**

![line2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y0i6c29dui222thbf6gk.png)
**code**

![line2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gf3ka33yesye7nb9fu8i.png)

From our codes above, we noticed new methods:
- **beginPath()**
- **lineTo()**
and 
- **stroke()**

**beginPath()** Makes a new path. 

**lineTo()** Adds a new point to the canvas.

**stroke()** Draws the shape by tracing its outline.


## Shape 1: a triangle
**code**

![triangle](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qzsd800pxxcauluxsqwp.png)
**result**


![triangle](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iea8hk86mit2ddu1vik4.png)

## Shape 2: an arrow

**code**

![arrow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0uxr9dwu3geh3js7t6ae.png)
**result**


![arrow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yagw9n4owh4jpmfkb1wc.png)

## Shape 3: a star
**code**

![star](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tmdnr3nnxz5374tp203q.png)
**result**


![star](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jltqm51z4i86azxjig9z.png)

## Conclusion
At the end of this tutorial, we learned how to draw some simple shapes. In our next tutorial, we'll look at adding color to our forms to make them more dynamic.
Please leave a comment with an image of a shape you created on your canvas.