@thi.ng/hiccup-canvas
Version:
Hiccup shape tree renderer for vanilla Canvas 2D contexts
29 lines (28 loc) • 672 B
JavaScript
import { TAU } from "@thi.ng/math/api";
const points = (ctx, attribs, opts, pts) => {
let v;
if ((v = attribs.fill) && v !== "none") {
__drawPoints(ctx, opts, pts, "fill", "fillRect");
}
if ((v = attribs.stroke) && v !== "none") {
__drawPoints(ctx, opts, pts, "stroke", "strokeRect");
}
};
const __drawPoints = (ctx, opts, pts, cmd, cmdR) => {
const s = opts?.size || 1;
if (opts.shape === "circle") {
for (const p of pts) {
ctx.beginPath();
ctx.arc(p[0], p[1], s, 0, TAU);
ctx[cmd]();
}
} else {
const r = s / 2;
for (const p of pts) {
ctx[cmdR](p[0] - r, p[1] - r, s, s);
}
}
};
export {
points
};