Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save harpreetkhalsagtbit/d4931038cdf3e2ec5aafb2c20da45218 to your computer and use it in GitHub Desktop.
Save harpreetkhalsagtbit/d4931038cdf3e2ec5aafb2c20da45218 to your computer and use it in GitHub Desktop.
Bresenham's Line Algorithm canvas api
// code taken from
// https://stackoverflow.com/a/50625904
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
function BMFastPixelArtLine(ctx, x1, y1, x2, y2) {
x1 = Math.round(x1);
y1 = Math.round(y1);
x2 = Math.round(x2);
y2 = Math.round(y2);
const dx = Math.abs(x2 - x1);
const sx = x1 < x2 ? 1 : -1;
const dy = Math.abs(y2 - y1);
const sy = y1 < y2 ? 1 : -1;
var error, len, rev, count = dx;
ctx.beginPath();
if (dx > dy) {
error = dx / 2;
rev = x1 > x2 ? 1 : 0;
if (dy > 1) {
error = 0;
count = dy - 1;
do {
len = error / dy + 2 | 0;
ctx.rect(x1 - len * rev, y1, len, 1);
x1 += len * sx;
y1 += sy;
error -= len * dy - dx;
} while (count--);
}
if (error > 0) { ctx.rect(x1, y2, x2 - x1, 1) }
} else if (dx < dy) {
error = dy / 2;
rev = y1 > y2 ? 1 : 0;
if (dx > 1) {
error = 0;
count--;
do {
len = error / dx + 2 | 0;
ctx.rect(x1, y1 - len * rev, 1, len);
y1 += len * sy;
x1 += sx;
error -= len * dx - dy;
} while (count--);
}
if (error > 0) { ctx.rect(x2, y1, 1, y2 - y1) }
} else {
do {
ctx.rect(x1, y1, 1, 1);
x1 += sx;
y1 += sy;
} while (count--);
}
ctx.fill();
}
BMFastPixelArtLine(ctx, 50, 50, 150, 150)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment