Draw Lines Using HTML5 Canvas



The HTML5 <canvas> tag is used to draw graphics, animations, etc. using scripting. It is a new tag introduced in HTML5. Use the lineTo() method to draw lines using HTML5 canvas.

You can try to run the following code to learn how to draw lines using HTML5 Canvas 

Example

<!DOCTYPE html>
<html>
   <head>
      <title>HTML5 Canvas Tag</title>
   </head>

   <body>
      <canvas id="newCanvas" width="400" height="300" style="border:1px
         solid #000000;"></canvas>
      <script>
         var c = document.getElementById('newCanvas');
         var ctx = c.getContext('2d');

         // Drawing lines
         ctx.beginPath();
         ctx.moveTo(30, 30);
         ctx.lineTo(180, 100);
         ctx.moveTo(30, 10);
         ctx.lineTo(260, 100);
         ctx.stroke();
      </script>
   </body>
</html>

Output

Updated on: 2021-12-16T12:24:21+05:30

652 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements