How to draw a line using javascript18 Mar 2025 | 7 min read The drawing line is an easy and simple method using the canvas function on a web page using javascript. Canvas tag uses to draw the line with different javascript properties and methods. The javascript sets the line's width, structure, display, and color. JavaScript line drawing instructionsYou can create a line on a canvas by using the procedures below:
SyntaxThe following syntax shows the drawn line in the canvas. Set the canvas variable to draw the line. Use the line width property of the 2D drawing context to set the line's width.
ct_var1.stroke(); Description
ExamplesThe examples display single and multiple lines with the angle and style. Example 1: the following example shows the basic horizontal line on the canvas. The canvas shows light grey color, and the line displays in black color. Output: The image shows a straight black line on the canvas. ![]() Example 2: the following example shows the basic sliding line on the canvas. The canvas shows with grey color, and the line displays a yellow color. Output: The image shows the black sliding on the canvas. ![]() Example 3: the following example shows the basic vertical line on the canvas. The canvas shows grey color, and the line displays an orange color. Output The image shows the vertical line on the canvas. ![]() Example 4: the following example shows various lines with different colors and styles on the canvas. The canvas shows grey color, and the line displays in multiple colors. We can see different sizes and different positions of the lines on the canvas. Output The image shows the multiple lines on the canvas. ![]() Example 5: the following example shows the basic line on the canvas with the arguments in the function. The canvas shows grey color, and the line displays an orange color. We can use single or multiple functions with the input argument. Here, the function uses the stroke line's positive and negative start and end positions. Output The image shows a simple single line on the canvas. ![]() Example 6: the following example shows various lines on the canvas with the arguments in the function. The canvas shows multiple colors and different sizes, and different format lines. Here, the input argument of the function uses the same start point and different co-ordination points to set the multiple lines together. Output The image shows the multiple lines on the canvas. ![]() Example 7: the following example shows given lines on the canvas with the arguments in the function. The canvas shows multiple colors and different sizes, and different format lines. We can use different widths with the same line start point on the canvas. The different line co-ordination used on the page for the line position. Output The image shows multiple lines with different widths and co-ordination on the canvas. ![]() ConclusionThe draw line using javascript is a simple and easy method for the users and developers. The canvas uses to get the platform to draw a line in different sizes, shapes, and positions. We can show different sizes and coordinate lines with different widths to create particular shapes. |
? JavaScript is a light-weight object-oriented programming language that is used by several websites for scripting the webpages. It is an interpreted, full-fledged programming language. JavaScript enables dynamic interactivity on websites when it is applied to an HTML document. JavaScript helps the users to build modern web...
3 min read
displays the y-coordinates or height of the given element. The read-only MouseEvent offsetY attribute returns the y-coordinate of the mouse cursor for the target element. We can add the event function on the div tag, web page, or other elements to display y-coordination. Syntax The following...
3 min read
The hashchange event works to identify th the URL changes starting with the hashtag. The hash (#) tag starts with the URL or follows the URL to change the one to another link. Syntax The following event uses in the html with the script tag. We can...
4 min read
? A checkbox is a selection box that allows the users to make the binary choice (true or false) by checking and unchecking it. Basically, a checkbox is an icon, which is frequently used in GUI forms and application to get one or more inputs from...
5 min read
? If you are a Smartphone user, it is quite possible that your Smartphone may work on Android OS. However, if your phone's browser is configured to disable JavaScript, you will certainly experience an unsatisfactory web browsing. So, it is necessary to use the web browser...
2 min read
This article will discuss the Fibonacci series and how we can generate the . It is a series that generates subsequent series of numbers by the addition of the two ious numbers. The first two terms of the Fibonacci series are zero & one, respectively. And...
7 min read
The () is a method that is used for inserting a child node before another node of a specified parent node. In this section, we will learn about the insertBefore () method and look at an example to understand the implementation of the insertBefore ()...
3 min read
In JavaScript, we have learned the creation of nodes where we create several nodes in which one is known as the Parent node or Root node, and all other nodes coming from the parent node are known as Child nodes. Sometimes there may be cases...
4 min read
In this article, we will explain about the javaScript and the ripple effect. After that, we will learn the various practical examples of ripple effects with the help of JavaScript. What do you mean by JavaScript? JavaScript is a client-side high-level, interpreted programming and scripting language used...
6 min read
The JavaScript Console is provided easy methods with a quick and effective way to run JavaScript code just inside the browser. It is frequently used for many different things, such as logging some code's output or debugging code via the Console. In addition to the...
9 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