JSFiddle - React, Tailwind, and code Playground
HTML
<canvas id="thecanvas" width="400" height="200"></canvas>
JavaScript
var canvas = document.getElementById("thecanvas");
var sample = canvas.getContext("2d");
function drawLine(x1, y1, x2, y2) {
sample.strokeStyle = '#000000';
sample.beginPath();
sample.moveTo(x1, y1);
sample.lineTo(x2, y2);
sample.lineWidth = 2;
sample.stroke();
sample.beginPath();
sample.arc(x1, y1, 4, 0, 2 * Math.PI, false);
sample.fillStyle = "#FFFFFF";
sample.fill();
sample.lineWidth = 1;
sample.stroke();
}
function drawInfLine(x1, y1, x2, y2) {
var xstep = x2 - x1;
var ystep = y2 - y1;
var lastx = x1;
var lasty = x2;
var currx;
var curry; // yum
// Draw forwards
while (lastx <= canvas.width && lasty <= canvas.height) {
currx = lastx + xstep;
curry = lasty + ystep;
drawLine(lastx, lasty, currx, curry);
lastx = currx;
lasty = curry;
}
// Reset initial drawing point
lastx = x1;
lasty = x2;
// Draw backwards
while (lastx >= 0 && lasty >= 0) {
currx = lastx - xstep;
curry = lasty - ystep;
drawLine(lastx, lasty, currx, curry);
lastx = currx;
lasty = curry;
}
}
drawInfLine(50, 0, 110, 5);