Source Code:
(back to article)
Submit
Result:
Report an issue
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Line Drawing Example</title> <style> canvas { border: 1px solid black; } </style> </head> <body> <canvas id="lineCanvas" width="200" height="200"></canvas> <script> const canvas = document.getElementById('lineCanvas'); const ctx = canvas.getContext('2d'); ctx.beginPath(); // Start the path ctx.moveTo(50, 50); // Move the pen to (50, 50) ctx.lineTo(150, 50); // Draw a line to (150, 50) ctx.stroke(); // Render the line visible </script> </body> </html>