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!

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.

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:

<canvas id="myCanvas" width="500" height="500"></canvas>
Understanding Canvas Properties

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>

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).

Drawing a Simple Rectangle
Here's a simple example to start with:




















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!