How to Draw a Rectangle in JavaScript: Step-by-Step Guide

Bobby Jul 13, 2026

Hello, aspiring JavaScript developers! Today, we're going to learn how to draw a rectangle using JavaScript, a crucial skill for creating interactive web applications and games. So, let's dive right in!

How to Code a Rectangle with JavaScript - Our Family Code
How to Code a Rectangle with JavaScript - Our Family Code

Before we start drawing, ensure you have HTML and a bit of CSS knowledge. JavaScript works best when it interacts with HTML content, and CSS helps make your rectangle visually appealing. Let's first set up our basic HTML canvas.

Build Your Own Online Drawing Tool with JavaScript 🎨🖌️
Build Your Own Online Drawing Tool with JavaScript 🎨🖌️

Setting Up the HTML Canvas

The HTML5 canvas is a powerful tool for drawing on webpages. It's essentially a blank slate where you can draw using JavaScript. Here's how to include it in your HTML:

Pirple.com: Sharpen your skills.
Pirple.com: Sharpen your skills.

<canvas id="myCanvas" width="500" height="500"></canvas>

Understanding Canvas Properties

Introducing ``Pickcode'', a visual language perfect for introductory programming, promoting the transition to more advanced programming languages such as Python and JavaScript
Introducing ``Pickcode'', a visual language perfect for introductory programming, promoting the transition to more advanced programming languages such as Python and JavaScript

The id attribute makes the canvas accessible via JavaScript. The width and height attributes set the size of the canvas. You can also adjust these properties dynamically using JavaScript later on.

Now, let's link this canvas with JavaScript:

<script> var canvas = document.getElementById('myCanvas'); var ctx = canvas.getContext('2d'); </script>

Draw rectangles using a mouse | PyQt5 Tutorial
Draw rectangles using a mouse | PyQt5 Tutorial

Drawing a Rectangle

Great! We have our canvas set up and connected to JavaScript. Now let's draw a rectangle using the fillRect() method.

The fillRect(x, y, width, height) method draws a rectangle with the given width and height at the specified position (x, y).

How to Code a Circle with JavaScript - Our Family Code
How to Code a Circle with JavaScript - Our Family Code

Drawing a Simple Rectangle

Here's a simple example to start with:

the instructions for how to draw an animal with numbers and symbols on it's sides
the instructions for how to draw an animal with numbers and symbols on it's sides
JavaScript Basics | Chapter 1 📖 | Part 1
JavaScript Basics | Chapter 1 📖 | Part 1
How to Create a Function in JavaScript: Step-by-Step Guide 🚀
How to Create a Function in JavaScript: Step-by-Step Guide 🚀
JavaScript Basics | Chapter 1 📖 | Part 2
JavaScript Basics | Chapter 1 📖 | Part 2
JavaScript Arrow Functions: Write Shorter & Cleaner Code 🚀
JavaScript Arrow Functions: Write Shorter & Cleaner Code 🚀
Creative Side Projects: A How-To Guide - Shopify
Creative Side Projects: A How-To Guide - Shopify
the instructions for how to draw eyes in pixel art, with pictures and text below
the instructions for how to draw eyes in pixel art, with pictures and text below
JavaScript Tips to Change Border Radius & Background Color
JavaScript Tips to Change Border Radius & Background Color
How to Draw a Rectangular Prism
How to Draw a Rectangular Prism
the diagram shows how to draw a cat using only straight lines design in theory and practice 1910
the diagram shows how to draw a cat using only straight lines design in theory and practice 1910
Javascript Pixel Art
Javascript Pixel Art
JavaScript Template Literals: Write Cleaner & Dynamic Strings 📝
JavaScript Template Literals: Write Cleaner & Dynamic Strings 📝
a black and white line drawing of a rectangle shape with vertical lines on it
a black and white line drawing of a rectangle shape with vertical lines on it
an image of different types of cats
an image of different types of cats
Javascript Explained | Sketch-notes PART3
Javascript Explained | Sketch-notes PART3
a piece of paper with four squares drawn on it, and one square is in the middle
a piece of paper with four squares drawn on it, and one square is in the middle
Hatchmark Lightswitch
Hatchmark Lightswitch
How to Draw Rectangular Prism
How to Draw Rectangular Prism
Javascript Cheet cheet
Javascript Cheet cheet
an old computer screen with the words aspire tip on it and some text below
an old computer screen with the words aspire tip on it and some text below

ctx.fillRect(10, 10, 100, 50);

This code draws a rectangle starting from the coordinates (10, 10) and has a width of 100 and a height of 50. You can run this JavaScript in your browser's developer console to see the output.

Styling the Rectangle

Rectangles can be filled or stroked with different colors. The fillStyle property sets the color, style, and alpha of the filled shapes. The strokeStyle property does the same for the outlines. Here's an example:

ctx.fillStyle = 'red'; ctx.fillRect(120, 10, 150, 60); ctx.strokeStyle = 'black'; ctx.strokeRect(120, 10, 150, 60);

We first set the fill color to 'red' and draw a rectangle. Then, we set the stroke color to 'black' and stroke the same rectangle. Now, you should see a red rectangle with a black outline.

Responding to User Input

Dynamic rectangle drawing comes in handy when you want to create interactive applications. The W3C's MouseEvent object lets you capture mouse movements and other actions. Here's how you can draw a rectangle based on user cursor coordinates:

Drawing on Mouse Move

In this example, we get the current mouse position on the canvas and use it to draw a rectangle using the mousemove event:

canvas.addEventListener('mousemove', function(e) {

var rect = ctx strokesRect (e.clientX, e.clientY); }, false;

We use e.clientX and e.clientY for the rectangle's top left corner coordinates. The mouse cursor's x and y positions on the canvas are delivered to these properties.

You can experiment with more advanced properties like line caps, line joins, text drawing, transformations, and even animations to create impressive visual effects.

Mastering JavaScript's graphics capabilities opens up a world of possibilities for web development. Happy coding, and keep exploring, because the possibilities are endless!