Draw a line using HTML5 Canvas, we can use the beginPath(), moveTo(), lineTo(), and stroke() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<!DOCTYPE HTML> <html> <head> <style> body { margin: 0px; padding: 0px; } </style> </head> <body> <canvas id="myCanvas" width="578" height="200"></canvas> <script> var canvas = document.getElementById('myCanvas'); var context = canvas.getContext('2d'); context.beginPath(); context.moveTo(50, 150); context.lineTo(440, 100); context.stroke(); </script> </body> </html> |
Result
Property
context.lineWidth = 15;
context.strokeStyle = ‘#ff0000’;
context.stroke();
context.lineWidth = 20;
context.strokeStyle = ‘#0000ff’;
context.lineCap = ’round’;
context.stroke();
More Stories
Amazing html canvas examples
html5 capture image from camera
Canvas Element