dots Created with Sketch.
Yogesh Chauhan's Blog

5 Steps to Create a Line using Canvas Tag in HTML5

in HTML on October 25, 2019

Here are the 5 quick steps to draw a line in HTML5 using <canvas> tag.

Step 1: Add the canvas tag in HTML page


<canvas id="Canvas1" width="200" height="200" style="border:1px solid black;">

To learn more about why I have assigned height and width and border, read this first article in the canvas series HERE.

Step 2: Get the canvas tag in JavaScript using getElementById


var canvas = document.getElementById("Canvas1");

Step 3: Create a drawing object using getContext method


var canvasObject = canvas.getContext("2d");

Again, if you want to understand why I have passed the value "2d", read this first article in the canvas series HERE.

Step 4: Specify the starting and ending point for the line using moveTo and lineTo methods.


canvasObject.moveTo(0,0);
canvasObject.lineTo(200,200);

Step 5: Draw the line using stroke method.


canvasObject.stroke();

 

amazon

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Jul 22 Is there a CSS parent selector? #Jul 22 Difference between :where and :is in CSS #Jul 22 Does :is() pseudo selector hint at CSS preprocessing in the future? #Jul 22 Control Scrolling with CSS Scroll Snap #Jul 21 Control rendering using CSS content-visibility property #Jul 21 How to use @supports rule in CSS?
You might also like these
WordPress: How to find all posts from a custom post type with specific custom field?WordPressThe Complete Basic Explanation of PHP SessionsPHPWindow setTimeout() Method in JavaScriptJavaScriptWhere is the PHP log file located on Mac OS?PHPAlways add associated labels to your Form elementsUI/UXthe box-sizing property in CSSCSSHow to disable scrolling on html body on menu click using JavaScript?JavaScriptHow to Use the EXISTS and NOT EXISTS Operator with a Subquery in SQL and MySQL?SQL/MySQLWhy does MOV matter in IT or any kind of projects?MiscellaneousHow to calculate elapsed time in JavaScript?JavaScriptFive common features of Angular template syntax (with examples)AngularOptional arguments, Default parameters and REST parameters in JavaScriptJavaScript