/** * The one place geometry crosses between the canvas frame and the host page. * * The canvas renders inside an iframe and the editor's chrome — the insertion * indicator, selection outlines, drag affordances — is drawn in the host * document above it. Every one of those has to answer the same question: where * is this canvas rectangle, in host coordinates? * * **Asked here and nowhere else.** Two modules computing that separately agree * on the day they are written and drift the first time anything changes — a * scroll offset one of them forgot, a zoom the other did not apply — and the * symptom is an indicator drawn a few pixels off the gap it names. That class of * bug is not caught by either module's own tests, because each is correct about * the question it asked. * * How much of that is ENFORCED, stated plainly because the difference matters — * and the honest answer is: none of it, by a boundary. * * A sibling test scans for a rectangle READ across the frame elsewhere in this * package, and it recognises a bounded set of spellings: a property access, a * string element access, a destructured binding. A name assembled at runtime, a * `Reflect.get` or a property descriptor all walk past it, and its own tests say * so. It narrows the easy paths; it is not a wall. * * Nothing checks the other half at all. A module handed an origin and a scale can * open-code the arithmetic without touching the DOM, and two numbers multiplied * and added look like any other code. * * What holds instead is narrower and true: the e2e helper adapts these * functions rather than restating them, and every caller needing a content * origin asks {@link frameContentOrigin} for one. * * The functions are pure and take plain numbers rather than DOM nodes, so the * mapping can be exercised without a browser and the DOM reads stay at the edge. * * @module geometry */ /** A point in whichever coordinate space the caller is working in. */ export interface Point { readonly x: number; readonly y: number; } /** A rectangle, in the shape `getBoundingClientRect` reports. */ export interface Rect { readonly x: number; readonly y: number; readonly width: number; readonly height: number; } /** Four physical edge lengths, in unscaled CSS pixels. */ export interface EdgeLengths { readonly top: number; readonly right: number; readonly bottom: number; readonly left: number; } /** * How much bigger a measured rectangle is than the layout it came from. * * Per-axis because a transform scales the two independently. `{ x: 1, y: 1 }` * for an untransformed element, which is the ordinary case. */ export interface Scale { readonly x: number; readonly y: number; } /** * How the canvas frame sits inside the host page. * * `origin` is where the frame's CONTENT viewport lands in host coordinates, and * the word content is load-bearing. `getBoundingClientRect` on an iframe reports * its BORDER box, while every rectangle read inside the frame is relative to the * content viewport — so on a frame with any border or padding the two differ, * and an overlay built from the border box sits a couple of scaled pixels out * at every point. Measure that difference with {@link frameInsetOf}; the inset * is border PLUS padding, and `clientLeft`/`clientTop` alone are short by the * padding. A canvas that never sets * `border: none` gets the browser default and the fault is present from the * first render, which is exactly the sort of near-miss that reads as "the * indicator feels slightly off" rather than as a bug. * * Build one with {@link frameContentOrigin} rather than adding that correction * at the call site. Reading `clientLeft` needs a browser; turning it into an * origin is arithmetic over plain numbers, and splitting the two puts the sum * in every caller that measured a frame — where each writes it separately and * a correction reaches only the one being edited. * * `scale` is the visual scale the host applies to the frame: a zoomed-out canvas * at 50% has `scale: 0.5`. * * Scroll INSIDE the frame is deliberately not a field. A rectangle read from * inside the frame is already relative to the frame's viewport, so subtracting * its scroll would count it twice — the mistake that makes an overlay drift as * the canvas scrolls, rather than being wrong by a constant. */ export interface FrameGeometry { readonly origin: Point; readonly scale: number; } /** * A frame geometry that cannot describe a mapping. * * Thrown rather than defaulted, because every value that could stand in is * wrong in a way that looks right: a scale of zero maps the whole canvas onto a * point, a negative one mirrors it, and a non-finite one yields `NaN` * coordinates that place an overlay nowhere and report no error. An overlay * silently drawn at the wrong place is the exact failure this module exists to * prevent, so an unusable frame has to be loud. */ export declare class FrameGeometryError extends Error { constructor(message: string); } /** * How far the frame's content viewport sits inside its border box. * * The COMPLETE offset, border plus padding. A border alone is the tempting * reading, because `clientLeft`/`clientTop` report exactly that and are the * obvious things to reach for — but an iframe's nested viewport begins at the * content box, so padding displaces it too. Measured in Chromium: an 8px border * with 12px padding puts the content 20px in, while `clientLeft` reports 8. * * In CSS pixels of the FRAME's own untransformed space, which is the whole * reason this type exists rather than the caller passing two numbers. Every * other coordinate in this module is host space, and mixing the two is the * mistake {@link frameContentOrigin} exists to prevent. */ export interface FrameInset { readonly left: number; readonly top: number; } /** * Where the frame's content viewport starts, in host coordinates. * * `borderBox` is the corner `getBoundingClientRect` reports for the frame * element, and `inset` is its border-to-content offset. That offset is laid out in the * frame's own pixels, so a host that has scaled the frame scales the border with * everything else: at 50% a 2px border occupies 1 host pixel. Adding the inset * unscaled therefore misplaces the origin by `(1 - scale) * inset`, which is * zero at 100% and grows as the canvas zooms out — a fault that is invisible in * exactly the configuration people develop in. * * Both facts are needed together and neither is guessable from the other, which * is why this is a function rather than a note telling callers to add them up. */ export declare function frameContentOrigin(borderBox: Point, inset: FrameInset, scale: number): Point; /** A point inside the canvas frame, in host coordinates. */ export declare function pointToHost(point: Point, frame: FrameGeometry): Point; /** * A point on the host page, in the canvas frame's coordinates. * * The exact inverse of {@link pointToHost}: a pointer event arrives in host * coordinates and has to be compared against rectangles read inside the frame, * which is the same mapping run backwards rather than a second one written to * match. */ export declare function pointToCanvas(point: Point, frame: FrameGeometry): Point; /** * A rectangle inside the canvas frame, in host coordinates. * * Its size scales with the frame. An overlay sized from the unscaled rectangle * would be correct only at 100%, and would look correct there, which is the * one configuration a canvas is usually looked at in. */ export declare function rectToHost(rect: Rect, frame: FrameGeometry): Rect; //# sourceMappingURL=geometry.d.ts.map