Create a Rectangular Shape in JavaScript2 Mar 2025 | 4 min read We can create a rectangle with the help of JavaScript Canvas Rectangle. As we know rectangle is drawn using width and height and here in this article, we will draw a rectangle using those parameters. The three most common methods of drawing rectangles on canvas are:
The rect() MethodA rectangle is defined by the rect() method. We can define the coordinates of the upper-left corner of the rectangle with the rect(x,y, width, height) method. We can also define the width and height of the rectangle. Syntax:The parameters for the rect() method are as follows: x: The rectangle's upper-left corner's coordinate. y: The rectangle's upper-left corner's y-coordinate. width: The width of the rectangle is measured in pixels. height: The rectangle's height is measured in pixels. Demo: This demo utilizes Rect() to define the rectangle on the canvas.We will start at position (20, 20) and utilize rect() to define a 140*90 pixel rectangle. The rectangle is then actually drawn utilizing stroke(): Code: Output: ![]() fillRect() methodWe are utilizing the fillRect() method to draw a filled rectangle. The fillStyle property is utilized to define the color. The fillStyle property specifies the fill-color. The fill-color defaults to black in the absence of the fillStyle property. Syntax:Parametersx: In fillRect() method, the x-coordinate is one of the parameters of the rectangle's upper-left corner. y: In fillRect() method, the y-coordinate is one of the parameters of the upper-left corner of the rectangle. width: The width of the rectangle is expressed in pixels. height: The height of the rectangle is measured in pixels. Demo 1: This demo utilizes fillRect() to display the rectangle on the canvas.Drawing a filled rectangle with 140 by 90 pixels utilizing fillRect() and beginning at position (8, 8): Code: Output: ![]() Demo 2We will utilize the fillStyle property to set the fill-color: Code: Output: ![]() strokeRect() methodThe stroked (outlined) rectangle is drawn utilizing the strokeRect() function. The strokeStyle property is utilized to define the stroke's color and black is the default color for the stroke. Syntax:Parametersx: In strokeRect() method, the x-coordinate is one of the rectangle's upper-left corner parameters. y: In strokeRect() method, the y-coordinate is one of the rectangle's upper-left corner parameters. width: It is expressed in pixels. height: It is expressed in pixels. linewidth: Display the stroke's thickness. The strokeStyle property specifies the stroke-color. The default stroke-color is black if the strokeStyle property is left empty. Demo 1We will draw a 140 by 90 pixel rectangle with strokes beginning at position (8, 8) utlizing strokeRect(): Code: Output: ![]() Demo 2We will utilize the strokeStyle property to change the outline's color: Code: Output: ![]() Additional DemoWe can create three rectangles utilizing the rect() method: Code: Output: ![]() ConclusionWe have comprehended the following points from this article:
|
JavaScript is a very powerful and popular programming language with a lot of applications in terms of web development. It allows developers to create dynamic and interactive content that can be performed in web pages making it an excellent user experience. Performing all kinds of operations...
8 min read
What is an Index? An index in programming denotes a specific location internal to the structure of a record. In arrays, strings, and other iterable collections, it serves as a pointer to the factors. Because indices in JavaScript are totally zero-based, counting starts at 0 rather than...
12 min read
Function Overloading is a feature found in several object-oriented programming languages, which permits multiple functions to share the same name but vary in the number or type of parameters. Although native support for function overloading exists in languages like C++ and Java, it does not exist...
5 min read
We will understand how to check for undefined value in JavaScript in this article. Let us comprehend what an undefined value is. Undefined The "undefined" is a global property that represents a primitive type value. It is utilized to represent the absence of any value. When a declared...
3 min read
What is a Hash Table in JavaScript? In JavaScript, a Hash table, also known as a hash map, is considered a data structure that helps us store key-value pairs more efficiently. Using hash functions, we can map keys to indices in an array, which allows for faster...
8 min read
A relatively simple data-interchange format known as JSON (JavaScript Object Notation) is essential to contemporary web development. It makes data structuring simpler and more readable for humans, which makes it suited for information transfer between a client and a server. Because of its ease of use...
7 min read
? Perhaps you're curious about how a website appears with or without JavaScript. JavaScript is enabled by default on Chrome, but you can quickly disable it to observe how it affects the functionality and appearance of a website. Why is JavaScript disabled? You might want to ent JavaScript...
4 min read
In HTML, we can use the reset button to reset the form. In this article, we are discussing how to reset the form using JavaScript. In JavaScript, the reset() method does the same thing as the HTML reset button. It is used to clear all the...
2 min read
JavaScript is a programming language used extensively in web development due to its flexibility and power. It works with almost all web browsers Replit is one of the best platforms for learning, practicing and excelling in JavaScript. Replit (IDE) Replit is a cloud-based integrated development environment,...
13 min read
JavaScript is a scripting language that enables developers to write interactive web applications that run in a web browser. A very common operation one would perform with JavaScript is subtracting two dates. This report will discuss the rationale for subtracting dates, the approaches to performing this...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India