YogeshChauhan . com

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.


Step 5: Draw the line using stroke method.




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 uninstall Cocoapods from the Mac OS? #4 How to add Read More Read Less Button using JavaScript? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

May 5 Use inline if to make a shorter conditional syntax in React May 4 What is Conditional Rendering in React? May 3 How does @extend rule work in SCSS (Sass)? May 2 How different is Handling Events in React vs HTML DOM? May 2 How to create bouncing balls using HTML canvas and JavaScript? Apr 30 HTML canvas methods

You might also like these

How to create a Random Hex Color generator using JavaScript?JavaScriptHow to center an image horizontally and vertically?CSSHow to get Current Year, Month and Date in JavaScript?JavaScriptHow to show confirmation alerts with OK and cancel buttons using Swift 5?SwiftHow to apply style to a specific child element using CSS?CSSIntroduction to components and templates Part 4: Pipes and DirectivesAngularWhat’s a Strict mode in JavaScript?JavaScriptHow does AdSense calculate page loading time?JavaScriptPHP Login System using PDO Part 2: Login using Email or UsernamePHPHow to create a simple stopwatch using JavaScript?JavaScriptWhat is Host Hardening and What are some Important Hardening Steps?MiscHow to get the height and width of an element using JavaScript?JavaScript