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.

LR10: the 2D context's roundRect and arcTo geometry move to paths.ts beside the Path they draw on (context.ts was 517 lines with letterSpacing and wordSpacing, over the 500 limit); the motion guide's rule 2 (sdk/guides/craft/motion.md, which raea-skills copies) says what a composition's DOM answers

shinyobjectz9632d542cd6a
diff --git a/sdk/guides/craft/motion.md b/sdk/guides/craft/motion.md
index 2157c907..dbf29155 100644
--- a/sdk/guides/craft/motion.md
+++ b/sdk/guides/craft/motion.md
@@ -42,6 +42,14 @@ Read the files a task needs with `raea guide motion/NAME`, one or two at a time,
A frame that needs one more step before it's taken (text measured in an effect, a fetch) holds it with
`delayRender("what it waits for")` and releases it with `continueRender(handle)` (`raea/hold`); a hold never
released fails the render after 30 s, naming its label.
+ Components written for a web page measure as they do in Chrome: a ref's `getBoundingClientRect`, `offsetWidth`
+ and `style`, `querySelector`, `document.createElement` (a hidden copy to measure), `document.createRange`, an svg
+ `<text>`'s `getComputedTextLength`, a canvas's `measureText`, `ResizeObserver`, `requestAnimationFrame`,
+ `new Image()` and an `<img>`'s `onLoad` answer from the page that draws the frame (installed for you:
+ `raea/dom-reads`, `raea/dom-tree`, `raea/dom-style`, `raea/dom-document`, `raea/dom-range`, `raea/dom-svgtext`,
+ `raea/canvas2d`, `raea/dom-observers`, `raea/frame-globals` and `raea/dom-load`). Load a web font with
+ `loadFont` (`raea/text`) so it's measured in its own faces. `MutationObserver` and `IntersectionObserver` never
+ call back.
3. **Clamp every interpolate.** The default extrapolation extends the line forever, so use
`{extrapolateLeft: "clamp", extrapolateRight: "clamp"}` unless you mean otherwise. Inputs must strictly increase.
4. **Settle with `Easing.out(Easing.cubic)`.** The plain curves (`Easing.cubic`, `Easing.quad`) ease in, and
diff --git a/sdk/ui/src/canvas/context.ts b/sdk/ui/src/canvas/context.ts
index e5c63ad6..f2dec86a 100644
--- a/sdk/ui/src/canvas/context.ts
+++ b/sdk/ui/src/canvas/context.ts
@@ -1,7 +1,7 @@
// CanvasRenderingContext2D.
import * as wire from "../wire";
import { base64, encodePng } from "./png";
-import { COMPOSITES, CanvasHost, Gradient, ID, ImageData_, Paint, Path, Path2D_, S, State, fresh, measure, mul, paintOf, parseColor, parseFilter, parseFont, parseSpacing, unb64 } from "./all";
+import { COMPOSITES, CanvasHost, Gradient, ID, ImageData_, Paint, Path, Path2D_, S, State, fresh, measure, mul, paintOf, parseColor, parseFilter, parseFont, parseSpacing, roundRectPath, arcToPath, unb64 } from "./all";
export class CanvasRenderingContext2D_ {
private s: State = fresh();
@@ -333,22 +333,7 @@ export class CanvasRenderingContext2D_ {
this.path.close();
}
roundRect(x: number, y: number, w: number, h: number, radii: number | number[] = 0) {
- const r = Array.isArray(radii) ? radii : [radii];
- const [tl, tr, br, bl] = r.length === 1 ? [r[0], r[0], r[0], r[0]] : r.length === 2 ? [r[0], r[1], r[0], r[1]] : r.length === 3 ? [r[0], r[1], r[2], r[1]] : [r[0], r[1], r[2], r[3]];
- const scale = Math.min(1, w / (tl + tr || 1), w / (bl + br || 1), h / (tl + bl || 1), h / (tr + br || 1));
- const [a, b, c, d] = [tl, tr, br, bl].map((v) => (typeof v === "number" ? v : 0) * scale);
- const P = this.path;
- P.move(x + a, y);
- P.line(x + w - b, y);
- if (b) P.ellipse(x + w - b, y + b, b, b, 0, -Math.PI / 2, 0, false);
- P.line(x + w, y + h - c);
- if (c) P.ellipse(x + w - c, y + h - c, c, c, 0, 0, Math.PI / 2, false);
- P.line(x + d, y + h);
- if (d) P.ellipse(x + d, y + h - d, d, d, 0, Math.PI / 2, Math.PI, false);
- P.line(x, y + a);
- if (a) P.ellipse(x + a, y + a, a, a, 0, Math.PI, Math.PI * 1.5, false);
- P.close();
- P.move(x, y);
+ roundRectPath(this.path, x, y, w, h, radii);
}
arc(x: number, y: number, r: number, a0: number, a1: number, ccw = false) {
this.path.ellipse(x, y, r, r, 0, a0, a1, ccw);
@@ -357,24 +342,7 @@ export class CanvasRenderingContext2D_ {
this.path.ellipse(x, y, rx, ry, rot, a0, a1, ccw);
}
arcTo(x1: number, y1: number, x2: number, y2: number, r: number) {
- const P = this.path;
- if (!P.last) P.move(x1, y1);
- const [x0, y0] = P.last!;
- const v0 = [x0 - x1, y0 - y1], v1 = [x2 - x1, y2 - y1];
- const l0 = Math.hypot(v0[0], v0[1]), l1 = Math.hypot(v1[0], v1[1]);
- const cross = v0[0] * v1[1] - v0[1] * v1[0];
- if (!r || !l0 || !l1 || Math.abs(cross) < 1e-9) return P.line(x1, y1);
- const ang = Math.acos(Math.max(-1, Math.min(1, (v0[0] * v1[0] + v0[1] * v1[1]) / (l0 * l1))));
- const t = r / Math.tan(ang / 2);
- const p0 = [x1 + (v0[0] / l0) * t, y1 + (v0[1] / l0) * t];
- const p1 = [x1 + (v1[0] / l1) * t, y1 + (v1[1] / l1) * t];
- // the centre: from p0, perpendicular toward the inside of the corner
- const n = cross > 0 ? [-v0[1] / l0, v0[0] / l0] : [v0[1] / l0, -v0[0] / l0];
- 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]);
- // 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);
+ arcToPath(this.path, x1, y1, x2, y2, r);
}
// -------- drawing
diff --git a/sdk/ui/src/canvas/paths.ts b/sdk/ui/src/canvas/paths.ts
index dda6bcc7..074d4592 100644
--- a/sdk/ui/src/canvas/paths.ts
+++ b/sdk/ui/src/canvas/paths.ts
@@ -168,3 +168,45 @@ export interface CanvasHost {
_send?(list: { gen: number; w: number; h: number; ops: unknown[] }): void;
getAttribute(k: string): string | null;
}
+
+/** roundRect's outline on `P`: each corner's radius (one, two, three or four, as CSS gives them) scaled down together
+ * where two would overlap, then the pen back at the rectangle's origin, as Chrome leaves it. */
+export function roundRectPath(P: Path, x: number, y: number, w: number, h: number, radii: number | number[]) {
+ const r = Array.isArray(radii) ? radii : [radii];
+ const [tl, tr, br, bl] = r.length === 1 ? [r[0], r[0], r[0], r[0]] : r.length === 2 ? [r[0], r[1], r[0], r[1]] : r.length === 3 ? [r[0], r[1], r[2], r[1]] : [r[0], r[1], r[2], r[3]];
+ const scale = Math.min(1, w / (tl + tr || 1), w / (bl + br || 1), h / (tl + bl || 1), h / (tr + br || 1));
+ const [a, b, c, d] = [tl, tr, br, bl].map((v) => (typeof v === "number" ? v : 0) * scale);
+ P.move(x + a, y);
+ P.line(x + w - b, y);
+ if (b) P.ellipse(x + w - b, y + b, b, b, 0, -Math.PI / 2, 0, false);
+ P.line(x + w, y + h - c);
+ if (c) P.ellipse(x + w - c, y + h - c, c, c, 0, 0, Math.PI / 2, false);
+ P.line(x + d, y + h);
+ if (d) P.ellipse(x + d, y + h - d, d, d, 0, Math.PI / 2, Math.PI, false);
+ P.line(x, y + a);
+ if (a) P.ellipse(x + a, y + a, a, a, 0, Math.PI, Math.PI * 1.5, false);
+ P.close();
+ P.move(x, y);
+}
+
+/** arcTo on `P`: a line to the first tangent point and the arc of radius `r` round the corner at (x1, y1), or a line to
+ * the corner when there's no arc to make (no radius, or the three points in a line). */
+export function arcToPath(P: Path, x1: number, y1: number, x2: number, y2: number, r: number) {
+ if (!P.last) P.move(x1, y1);
+ const [x0, y0] = P.last!;
+ const v0 = [x0 - x1, y0 - y1], v1 = [x2 - x1, y2 - y1];
+ const l0 = Math.hypot(v0[0], v0[1]), l1 = Math.hypot(v1[0], v1[1]);
+ const cross = v0[0] * v1[1] - v0[1] * v1[0];
+ if (!r || !l0 || !l1 || Math.abs(cross) < 1e-9) return P.line(x1, y1);
+ const ang = Math.acos(Math.max(-1, Math.min(1, (v0[0] * v1[0] + v0[1] * v1[1]) / (l0 * l1))));
+ const t = r / Math.tan(ang / 2);
+ const p0 = [x1 + (v0[0] / l0) * t, y1 + (v0[1] / l0) * t];
+ const p1 = [x1 + (v1[0] / l1) * t, y1 + (v1[1] / l1) * t];
+ // the centre: from p0, perpendicular toward the inside of the corner
+ const n = cross > 0 ? [-v0[1] / l0, v0[0] / l0] : [v0[1] / l0, -v0[0] / l0];
+ 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]);
+ // 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);
+}