Skip to content

Commit

Permalink
perf(hiccup-canvas): update rect(), use native rounded rect drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed May 7, 2024
1 parent 7f82cb2 commit 5540510
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions packages/hiccup-canvas/src/rect.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
import type { IObjectOf } from "@thi.ng/api";
import type { ReadonlyVec } from "@thi.ng/vectors";
import { path } from "./path.js";
import { __endShape } from "./internal/end-shape.js";

export const rect = (
ctx: CanvasRenderingContext2D,
attribs: IObjectOf<any>,
pos: ReadonlyVec,
w: number,
h: number,
r = 0
radii?:
| number
| [number, number]
| [number, number, number]
| [number, number, number, number]
) => {
let v: any;
if (r > 0) {
r = Math.min(Math.min(w, h) / 2, r);
w -= 2 * r;
h -= 2 * r;
return path(ctx, attribs, [
["M", [pos[0] + r, pos[1]]],
["h", w],
// FIXME need new type ID for circular arcs
// see issues #69 & #418
["a", [r, 0], [r, r], r],
["v", h],
["a", [0, r], [-r, r], r],
["h", -w],
["a", [-r, 0], [-r, -r], r],
["v", -h],
["a", [0, -r], [r, -r], r],
]);
if (radii !== undefined) {
ctx.beginPath();
ctx.roundRect(pos[0], pos[1], w, h, radii);
__endShape(ctx, attribs);
return;
}
if ((v = attribs.fill) && v !== "none") {
ctx.fillRect(pos[0], pos[1], w, h);
Expand Down

0 comments on commit 5540510

Please sign in to comment.