UNPKG

@thi.ng/text-canvas

Version:

Text based canvas, drawing, plotting, tables with arbitrary formatting (incl. ANSI/HTML)

40 lines (39 loc) 1.02 kB
import { peek } from "@thi.ng/arrays/peek"; import { liangBarsky2Raw } from "@thi.ng/geom-clip-line/liang-barsky"; import { Canvas } from "./canvas.js"; import { charCode } from "./utils.js"; const line = (canvas, ax, ay, bx, by, char, format = canvas.format) => { const { x1, y1, x2, y2 } = peek(canvas.clipRects); const clipped = liangBarsky2Raw(ax, ay, bx, by, x1, y1, x2, y2); if (!clipped) return; ax = clipped[0] | 0; ay = clipped[1] | 0; bx = clipped[2] | 0; by = clipped[3] | 0; const dx = Math.abs(bx - ax); const dy = -Math.abs(by - ay); const w = canvas.width; let sx = ax < bx ? 1 : -1; let sy = ay < by ? 1 : -1; let err = dx + dy; char = charCode( char !== void 0 ? char : peek(canvas.styles).dot, format ); while (true) { canvas.data[ax + ay * w] = char; if (ax === bx && ay === by) return; let t = err << 1; if (t < dx) { err += dx; ay += sy; } if (t > dy) { err += dy; ax += sx; } } }; export { line };