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:
|
While both JavaScript and ReactJS are crucial for contemporary web development, their capabilities are incredibly unique. It is fundamental for developers to understand these distinctions to make versatile, dynamic, and viable online apps. This article investigates the key qualifications, applications, and advantages among React.js and JavaScript. What...
11 min read
Asynchronous programming is essential in JavaScript for building responsive and efficient applications. Performing tasks asynchronously enables certain actions to run separately from the program's main flow, which is crucial for managing lengthy processes without disrupting the user interface. Using timers is a crucial element in overseeing asynchronous...
8 min read
What is a JavaScript file? JavaScript are files that contain the code for the execution on the webpage. JavaScript files are saved with the.js extension. Suppose you want to include JavaScript inside the HTML document you can use the <script></script> tag or you can include the...
4 min read
Introduction Designing websites has been implemented by a lot of tech companies where the programming language JavaScript has become efficient usage. Its inherent single-threading is one of its fundamental qualities, despite its diversity. JavaScript runs code in a single-threaded environment, in contrast to languages like C++ or...
7 min read
The date is an essential function for user interaction form, table and database. We can work on the date in the format of the user's requirement. The users mostly use the following date format to display the date. dd/mm/yyyy format mm/dd/yyyy format yyyy/dd/mm format dd/month_name /yyyy format Methods to Convert...
5 min read
The hash is essential information for displaying, operating, and handling the data. We can get the keys of the values of the hash set using the javascript function. There are various methods to get keys from objects in JavaScript by their value, and we can see...
9 min read
Clear Intervals are fundamental additives of JavaScript that might be used to manage movements that want to be finished often or after a put-off and to manipulate the drift of execution. These capabilities are provided through the setInterval and setTimeout features, which let programmers write code...
9 min read
Introduction Data structures and algorithms structure the foundation of software engineering and programming. They are fundamental apparatuses for taking care of complicated issues productively and are urgent in building vigorous and versatile applications. In this masterclass, we will dive into the universe of JavaScript algorithms and data...
19 min read
Introduction to JavaScript Modules In present day JavaScript development, modularization is key to composing spotless, viable code. Modules permit developers to part their code into more modest, reusable pieces, making it simpler to oversee conditions and scale applications. The import and export keywords assume a focal...
9 min read
We will understand the JavaScript square in this article. Squaring a number When the number is multiplied by itself then it is called squaring a number. Example: Let us consider the number "a". We can multiply the number "a" by itself like this (a * a). In mathematics, it...
3 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