Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

canvas: arcTo draws the corner's short arc, as a browser does (#97)

shinyobjectzc89de2a778ed
diff --git a/sdk/ui/src/canvas/context.ts b/sdk/ui/src/canvas/context.ts
index adbbb156..96f94cbb 100644
--- a/sdk/ui/src/canvas/context.ts
+++ b/sdk/ui/src/canvas/context.ts
@@ -349,7 +349,8 @@ export class CanvasRenderingContext2D_ {
const cx = p0[0] + n[0] * r, cy = p0[1] + n[1] * r;
const a0 = Math.atan2(p0[1] - cy, p0[0] - cx), a1 = Math.atan2(p1[1] - cy, p1[0] - cx);
P.line(p0[0], p0[1]);
- P.ellipse(cx, cy, r, r, 0, a0, a1, cross < 0);
+ // the short way round, between the tangent points: clockwise on screen (y down) when the corner turns that way
+ P.ellipse(cx, cy, r, r, 0, a0, a1, cross > 0);
}
// -------- drawing
diff --git a/sdk/ui/test/canvas-arcto.test.ts b/sdk/ui/test/canvas-arcto.test.ts
new file mode 100644
index 00000000..868c20ae
--- /dev/null
+++ b/sdk/ui/test/canvas-arcto.test.ts
@@ -0,0 +1,58 @@
+// arcTo draws the corner's short arc, as a browser does (#97): one quarter circle between the tangent points for a
+// right angle, whichever way the corner turns, never three quarters of the circle through the far side.
+import { describe, expect, test } from "bun:test";
+import { CanvasRenderingContext2D_ } from "../src/canvas/context";
+import { C } from "../src/canvas/paths";
+
+function path(build: (c: CanvasRenderingContext2D_) => void): number[] {
+ const c = new CanvasRenderingContext2D_({ width: 100, height: 100 } as any);
+ c.beginPath();
+ build(c);
+ return (c as any).path.d as number[];
+}
+
+/** The path's commands, with each cubic's end point. */
+function cubics(d: number[]): [number, number][] {
+ const out: [number, number][] = [];
+ for (let i = 0; i < d.length; ) {
+ const op = d[i];
+ const len = op === C ? 6 : op === 4 ? 0 : op === 2 ? 4 : 2;
+ if (op === C) out.push([d[i + 5], d[i + 6]]);
+ i += 1 + len;
+ }
+ return out;
+}
+
+const near = (p: [number, number], q: [number, number]) => {
+ expect(p[0]).toBeCloseTo(q[0], 6);
+ expect(p[1]).toBeCloseTo(q[1], 6);
+};
+
+describe("arcTo", () => {
+ test("a corner turning clockwise on screen is one quarter arc", () => {
+ const arcs = cubics(path((c) => { c.moveTo(0, 0); c.arcTo(10, 0, 10, 10, 5); }));
+ expect(arcs.length).toBe(1);
+ near(arcs[0], [10, 5]);
+ });
+
+ test("the mirrored corner, turning the other way, is one quarter arc too", () => {
+ const arcs = cubics(path((c) => { c.moveTo(0, 0); c.arcTo(10, 0, 10, -10, 5); }));
+ expect(arcs.length).toBe(1);
+ near(arcs[0], [10, -5]);
+ });
+
+ test("a rounded rectangle drawn with four arcTo corners has four quarter arcs", () => {
+ const arcs = cubics(path((c) => {
+ c.moveTo(20, 10);
+ c.arcTo(90, 10, 90, 90, 10);
+ c.arcTo(90, 90, 10, 90, 10);
+ c.arcTo(10, 90, 10, 10, 10);
+ c.arcTo(10, 10, 90, 10, 10);
+ }));
+ expect(arcs.length).toBe(4);
+ near(arcs[0], [90, 20]);
+ near(arcs[1], [80, 90]);
+ near(arcs[2], [10, 80]);
+ near(arcs[3], [20, 10]);
+ });
+});