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