Canvas Draw Lines

How to Draw a Line

To draw a line, follow these steps

  1. canvas - Assigning the canvas element to a variable.
  2. ctx - Assigning the 2d rendering context to another variable by calling the getContext('2d') method.
  3. beginPath() - Use beginPath() canvas method to start a new path by emptying the list of sub-paths.
  4. moveTo() - Use moveTo() canvas method to begin a new sub-path at the point specified by the given (x, y) coordinates.
  5. lineTo() - Use lineTo() canvas method to add a straight line to the current sub-path by connecting the sub-path's last point to the specified (x, y) coordinates.
  6. stroke() - Use stroke() canvas method to stroke (outline) the current or given path with the current strokeStyle. The default strokeStyle is 'black'.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <body> <canvas width="160" height="110" id="point"> Your browser does not support the canvas element. </canvas> <script> var canvas = document.getElementById('point'); var ctx = canvas.getContext('2d'); ctx.beginPath(); // Start a new path ctx.moveTo(0, 0); // Move the pen to (0, 0) ctx.lineTo(150, 100); // Draw a line to (150, 100) ctx.stroke(); </script> </body> </html>

Syntax

ctx.lineTo(x, y)

Attributes Value

ValueExplanation
xSpecifies the x-axis coordinate of the line's end point.
ySpecifies the y-axis coordinate of the line's end point.

How to Custom Color the Line

To custom color the line, follow these steps

  1. canvas - Assigning the canvas element to a variable.
  2. ctx - Assigning the 2d rendering context to another variable by calling the getContext('2d') method.
  3. beginPath() - Use beginPath() canvas method to start a new path by emptying the list of sub-paths.
  4. moveTo() - Use moveTo() canvas method to begin a new sub-path at the point specified by the given (x, y) coordinates.
  5. lineTo() - Use lineTo() canvas method to add a straight line to the current sub-path by connecting the sub-path's last point to the specified (x, y) coordinates.
  6. strokeStyle - Use strokeStyle canvas property to specify the color to use for the strokes (outlines) around shapes. The default color is 'black'.
  7. lineWidth - Use lineWidth canvas property to set the thickness of lines.
  8. stroke() - Use stroke() canvas method to stroke (outline) the current or given path with the current strokeStyle. The default strokeStyle is 'black'.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <body> <canvas width="160" height="110" id="point"> Your browser does not support the canvas element. </canvas> <script> var canvas = document.getElementById('point'); var ctx = canvas.getContext('2d'); ctx.beginPath(); ctx.moveTo(5, 5); ctx.lineTo(150, 100); ctx.strokeStyle = "#339933"; ctx.lineWidth = 5; ctx.stroke(); </script> </body> </html>

How to Draw Square using Line

Use 4 lineTo() canvas method to draw a square using Line.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <body> <canvas width="160" height="110" id="point"> Your browser does not support the canvas element. </canvas> <script> var canvas = document.getElementById('point'); var ctx = canvas.getContext('2d'); ctx.beginPath(); ctx.moveTo(5, 5); ctx.lineTo(5, 100); ctx.lineTo(105, 100); ctx.lineTo(105, 5); ctx.lineTo(5, 5); ctx.stroke(); </script> </body> </html>

Reminder

Hi Developers, we almost covered 93% of Canvas Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in Canvas.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Meet the Author