explain the canvas drawing rectangle with example

Learn to Draw Rectangles on Canvas: A Simple Step-by-Step Example

By Jessica Thomas
AD

The concept of a rectangle in drawing, particularly in digital mediums like the HTML Canvas API, often stirs up visuals of rectangular boxes. But a rectangle isn't merely about four equal lines. It's a fundamental shape in computing, building blocks for everything from buttons to entire windows.

In this simplified guide, we'll break down the 'rectangle' drawing process on the HTML Canvas API, discuss its properties, and walk through practical, real-world examples. So, let's dive right in.

Understanding the Rectangle on Canvas API

The HTML Canvas API provides a simple rectangle() method to draw rectangles. It's syntax is `.rectangle(x, y, width, height);`.

AD
a painting of two bears and one bear with hearts on their chest, in three different colors
a painting of two bears and one bear with hearts on their chest, in three different colors
a boat sitting on top of a beach next to the ocean with lines in front of it
a boat sitting on top of a beach next to the ocean with lines in front of it
a drawing of a lighthouse on an island
a drawing of a lighthouse on an island

Here, 'x' and 'y' denote the coordinate where the rectangle's top-left corner starts, while 'width' and 'height' specify the rectangle's dimensions.

Absence of Fill or Stroke

When using rectangle(), there's no fill or stroke by default. If you want your rectangle to actually manifest, you need to first set your `fillStyle` and/or `strokeStyle`.

four squares that are drawn on paper
four squares that are drawn on paper
✨🕊️
✨🕊️
the words rectangle are written in different languages and have eyes, nose, mouth
the words rectangle are written in different languages and have eyes, nose, mouth

For instance, `.fillStyle = 'red';` before `rectangle()` draws a red rectangle, and `.strokeStyle = 'blue';` before rectangle() outlines it in blue.

Rectangles with Borders

Using `stroke()`, rectangles have borders. `strokeRect(x, y, width, height);`

AD

Similar to rectangle(), make sure you've set your `strokeStyle` before calling `strokeRect()`.

5th Grade Math Anchor Chart: Properties of Squares & Rectangles
5th Grade Math Anchor Chart: Properties of Squares & Rectangles
Rectangle Poster with Attributes Math Anchor Chart 18x24
Rectangle Poster with Attributes Math Anchor Chart 18x24
a drawing of a lighthouse in the ocean
a drawing of a lighthouse in the ocean
some type of art that is being drawn
some type of art that is being drawn
an acrylic painting of a church by the sea with red flowers on it
an acrylic painting of a church by the sea with red flowers on it
two pieces of art that are made out of colored paper with squares and rectangles
two pieces of art that are made out of colored paper with squares and rectangles

HTML:

```html ```

JavaScript:

```javascript const canvas = document.getElementById('myCanvas'); const ctx = canvas.getContext('2d'); ctx.fillStyle = 'blue'; ctx.fillRect(50, 50, 150, 50); ctx.strokeStyle = 'black'; ctx.strokeRect(50, 50, 150, 50); ```

Here, we create a button (Blue rectangle) with a black border (Another rectangle). Thus, even complex UI structures can be broken down to basic shapes like rectangles.

Incorporating rectangles into your graphics is as fundamental and impactful as it is in the real world. It's the building block powering your web experience. Start experimenting with them as part of your restituKoloration scheme, or even for creative designs.

AD