Skip to content

packages/core/src/lib/manipulation/handles.ts

Read as Markdown

This is the source snapshot used to build these API details. View this revision on GitHub.

Back to reference

1 import type { PibblResizePosition } from "./types.js";
2 import type { PibblManipulationWhen } from './types.js';
3 import type { PibblManipulableProps } from './manipulable.js';
4 import { createElement } from "../element/create-element.js";
5 import {
6   createGroupTransform,
7   setPrimitivePresentation,
8 } from "../primitive-presentation.js";
9 import { getCurrentWorldTransform } from "../render-target.js";
10 import { GLOBAL_STATE } from "../global-state.js";
11 import { definePrimitive } from "../define-primitive.js";
12 import { useEventListener } from "../hooks/use-event-listener.js";
13 import { useEventTarget } from "../hooks/use-event-target.js";
14 import { useSignal } from "../hooks/use-signal.js";
15 import { useConst } from "../hooks/use-const.js";
16 import type { GStyle } from "../components/g.js";
17 import { stripLayoutParticipation } from "../style/resolve-built-in.js";
18 import type { ResolvedDrawingStyle } from "../style/types.js";
19 import { getLayoutBox } from "../layout/context.js";
20 import type { PibblNode } from "../element/types.js";
21 import type { RenderingContext2D, SystemStyle } from "../types.js";
22 import type { PibblManipulationController } from "./controller-types.js";
23 import {
24   isManipulationSourceActive,
25   manipulationHandlers,
26   type ManipulationState,
27 } from "./controller.js";
28 import { requireManipulationFrame } from "./frame.js";
29 
30 /**
31  * Authored inputs for DragHandle, including the declared data and presentation options.
32  *
33  * @see {@link PibblManipulationController}
34  * @see {@link PibblNode}
35  * @see {@link DragHandle}
36  * @see {@link PibblManipulationWhen}
37  */
38 export interface PibblDragHandleProps {
39   /**
40    * Formats the value shown by the manipulation readout. See {@link PibblDragHandleProps}.
41    * @param value - Current left and top coordinates.
42    * @returns Text to display in the drag readout.
43    */
44   readonly formatReadout?: (value: {
45     /** Left edge coordinate or inset. See {@link PibblDragHandleProps}. */
46     readonly left: number;
47     /** Top edge value or top envelope guide. See {@link PibblDragHandleProps}. */
48     readonly top: number;
49   }) => string;
50   /**
51    * Controller owning the current interaction or mounted scenario. See
52    * {@link PibblManipulationController}.
53    */
54   readonly controller: PibblManipulationController;
55   /** Descendant content or the callback that supplies it. See {@link PibblNode}. */
56   readonly children?: PibblNode;
57 }
58 /**
59  * Supported geometry and presentation properties for DragHandle.
60  *
61  * @see {@link GStyle}
62  * @see {@link PibblManipulationReadoutStyle}
63  * @see {@link DragHandle}
64  * @see {@link PibblManipulableProps}
65  */
66 export interface PibblDragHandleStyle extends GStyle {
67   /** Presentation of the manipulation value readout. See {@link PibblManipulationReadoutStyle}. */
68   readonly readout?: false | true | PibblManipulationReadoutStyle;
69 }
70 type ResolvedDragStyle = ResolvedDrawingStyle<PibblDragHandleStyle>;
71 interface CssHandleContentProps {
72   readonly children?: PibblNode;
73 }
74 interface CssHandleContentStyle extends SystemStyle {
75   readonly transform: DOMMatrix;
76 }
77 /** Private CSS-pixel basis for author-provided handle visuals. */
78 const CssHandleContent = definePrimitive<
79   CssHandleContentProps,
80   CssHandleContentStyle
81 >(
82   function CssHandleContent(props) {
83     return props.children;
84   },
85   { childInput: "structural" },
86 );
87 setPrimitivePresentation<CssHandleContentStyle>(CssHandleContent, (style) => ({
88   transform: style.transform,
89 }));
90 
91 /**
92  * Draws an interactive handle that starts translation of its manipulation target.
93  *
94  * @param props - Authored component inputs, supplied through JSX or createElement. See the linked
95  * props and style types.
96  * @throws When called directly; Pibbl mounts this component through JSX or createElement.
97  *
98  * @see {@link PibblDragHandleProps}
99  * @see {@link PibblDragHandleStyle}
100  */
101 export const DragHandle = definePrimitive<
102   PibblDragHandleProps,
103   PibblDragHandleStyle,
104   PibblDragHandleStyle,
105   ResolvedDragStyle
106 >(
107   function DragHandle(props, style) {
108     requireManipulationFrame(props.controller);
109     const focused = useSignal(false);
110     const source = useConst(() => ({}));
111     const handlers = manipulationHandlers(
112       props.controller,
113       "move",
114       undefined,
115       source,
116     );
117     useEventListener({
118       ...handlers,
119       onFocusIn: () => focused.set(true),
120       onFocusOut: () => focused.set(false),
121     });
122     const box = getLayoutBox();
123     return [
124       props.children,
125       createElement(DragReadout, {
126         controller: props.controller,
127         formatReadout: props.formatReadout,
128         readout: style.readout,
129         focused: focused.get(),
130         source,
131         anchorX: box.width / 2,
132       }),
133     ];
134   },
135   {
136     childInput: "structural",
137     resolveStyle: (style) =>
138       stripLayoutParticipation(style) as ResolvedDragStyle,
139   },
140 );
141 
142 setPrimitivePresentation<ResolvedDragStyle>(DragHandle, (style) => ({
143   transform: createGroupTransform(style),
144 }));
145 
146 const DragReadout = definePrimitive<
147   PibblDragHandleProps & {
148     readonly readout?: false | true | PibblManipulationReadoutStyle;
149     readonly focused: boolean;
150     readonly source: object;
151     readonly anchorX: number;
152   }
153 >(function DragReadout(props, _style, ctx) {
154   if (!readoutEnabled(props.readout, props.formatReadout)) return;
155   const state = requireManipulationFrame(props.controller);
156   paintReadout(
157     ctx,
158     state,
159     cssPoint(state, props.anchorX, 0),
160     () => {
161       const { left, top } = state.options.box.get();
162       const value = props.formatReadout
163         ? props.formatReadout({ left, top })
164         : `left ${round(left)} · top ${round(top)} px`;
165       if (typeof value !== "string")
166         throw new Error(
167           "Manipulation readout formatReadout must return a string.",
168         );
169       return value;
170     },
171     props.readout,
172     props.focused,
173     isManipulationSourceActive(props.controller, props.source),
174     "top",
175     40,
176   );
177 });
178 
179 /**
180  * Supported geometry and presentation properties for ManipulationReadoutState.
181  *
182  * @see {@link PibblManipulationReadoutStyle}
183  */
184 export interface PibblManipulationReadoutStateStyle {
185   /** Minimum outward spacing in CSS pixels; defaults to 40 for drag, 5 otherwise. */
186   readonly gap?: number;
187   /**
188    * Canvas font string used to draw or measure text. See {@link PibblManipulationReadoutStateStyle}
189    * .
190    */
191   readonly font?: string;
192   /** Paint used for the interior. See {@link PibblManipulationReadoutStateStyle}. */
193   readonly fill?: string;
194   /** Paint used for the outline. See {@link PibblManipulationReadoutStateStyle}. */
195   readonly stroke?: string;
196   /** Width of the painted outline. See {@link PibblManipulationReadoutStateStyle}. */
197   readonly strokeWidth?: number;
198   /** Opacity of the painted result. See {@link PibblManipulationReadoutStateStyle}. */
199   readonly opacity?: number;
200   /** Whether this presentation is visible. See {@link PibblManipulationReadoutStateStyle}. */
201   readonly visible?: boolean;
202 }
203 /**
204  * Supported geometry and presentation properties for ManipulationReadout.
205  *
206  * @see {@link PibblManipulationReadoutStateStyle}
207  * @see {@link PibblDragHandleStyle}
208  * @see {@link PibblResizeHandleStyle}
209  * @see {@link PibblRotationHandleStyle}
210  */
211 export interface PibblManipulationReadoutStyle extends PibblManipulationReadoutStateStyle {
212   /**
213    * Readout presentation while no interaction is active. See
214    * {@link PibblManipulationReadoutStateStyle}.
215    */
216   readonly inactive?: PibblManipulationReadoutStateStyle;
217   /**
218    * Readout presentation while the handle has keyboard focus. See
219    * {@link PibblManipulationReadoutStateStyle}.
220    */
221   readonly focused?: PibblManipulationReadoutStateStyle;
222   /**
223    * Current active operation or its presentation configuration. See
224    * {@link PibblManipulationReadoutStateStyle}.
225    */
226   readonly active?: PibblManipulationReadoutStateStyle;
227 }
228 /**
229  * Supported geometry and presentation properties for ResizeHandle.
230  *
231  * @see {@link SystemStyle}
232  * @see {@link PibblManipulationReadoutStyle}
233  * @see {@link ResizeHandle}
234  * @see {@link PibblResizeHandlesProps}
235  */
236 export interface PibblResizeHandleStyle extends SystemStyle {
237   /** Presentation of the manipulation value readout. See {@link PibblManipulationReadoutStyle}. */
238   readonly readout?: false | true | PibblManipulationReadoutStyle;
239 }
240 /**
241  * Supported geometry and presentation properties for RotationConnector.
242  *
243  * @see {@link PibblRotationHandleStyle}
244  */
245 export interface PibblRotationConnectorStyle {
246   /** Paint used for the outline. See {@link PibblRotationConnectorStyle}. */
247   readonly stroke?: string;
248   /** Width of the painted outline. See {@link PibblRotationConnectorStyle}. */
249   readonly strokeWidth?: number;
250   /** Opacity of the painted result. See {@link PibblRotationConnectorStyle}. */
251   readonly opacity?: number;
252 }
253 /**
254  * Supported geometry and presentation properties for RotationArc.
255  *
256  * @see {@link PibblRotationHandleStyle}
257  */
258 export interface PibblRotationArcStyle {
259   /** Color used by the mark or effect. See {@link PibblRotationArcStyle}. */
260   readonly color?: string;
261   /** Angular extent of the arc. See {@link PibblRotationArcStyle}. */
262   readonly sweep?: number;
263   /** Width of the painted outline. See {@link PibblRotationArcStyle}. */
264   readonly strokeWidth?: number;
265   /** Opacity of the painted result. See {@link PibblRotationArcStyle}. */
266   readonly opacity?: number;
267 }
268 /**
269  * Supported geometry and presentation properties for RotationHandle.
270  *
271  * @see {@link SystemStyle}
272  * @see {@link PibblRotationConnectorStyle}
273  * @see {@link PibblRotationArcStyle}
274  * @see {@link PibblManipulationReadoutStyle}
275  * @see {@link RotationHandle}
276  * @see {@link PibblManipulableProps}
277  */
278 export interface PibblRotationHandleStyle extends SystemStyle {
279   /**
280    * Presentation of the line connecting the rotation handle to its target. See
281    * {@link PibblRotationConnectorStyle}.
282    */
283   readonly connector?: false | true | PibblRotationConnectorStyle;
284   /** Presentation of the rotation-angle arc. See {@link PibblRotationArcStyle}. */
285   readonly arc?: false | true | PibblRotationArcStyle;
286   /** Presentation of the manipulation value readout. See {@link PibblManipulationReadoutStyle}. */
287   readonly readout?: false | true | PibblManipulationReadoutStyle;
288 }
289 /**
290  * Authored inputs for ResizeHandle, including the declared data and presentation options.
291  *
292  * @see {@link PibblManipulationController}
293  * @see {@link PibblNode}
294  * @see {@link ResizeHandle}
295  * @see {@link PibblResizeHandlesProps}
296  * @see {@link PibblResizePosition}
297  */
298 export interface PibblResizeHandleProps {
299   /**
300    * Controller owning the current interaction or mounted scenario. See
301    * {@link PibblManipulationController}.
302    */
303   readonly controller: PibblManipulationController;
304   /**
305    * Position of the geometry, source, or selected resize handle. See {@link PibblResizeHandleProps}
306    * .
307    */
308   readonly position: import("./types.js").PibblResizePosition;
309   /** Size of the handle's interactive hit region. See {@link PibblResizeHandleProps}. */
310   readonly hitSize?: number;
311   /**
312    * Formats the value shown by the manipulation readout. See {@link PibblResizeHandleProps}.
313    * @param value - Current width and height.
314    * @returns Text to display in the resize readout.
315    */
316   readonly formatReadout?: (value: {
317     /**
318      * Horizontal extent in the units of the containing geometry or surface. See
319      * {@link PibblResizeHandleProps}.
320      */
321     readonly width: number;
322     /**
323      * Vertical extent in the units of the containing geometry or surface. See
324      * {@link PibblResizeHandleProps}.
325      */
326     readonly height: number;
327   }) => string;
328   /** Descendant content or the callback that supplies it. See {@link PibblNode}. */
329   readonly children?: PibblNode;
330 }
331 /**
332  * Authored inputs for ResizeHandles, including the declared data and presentation options.
333  *
334  * @see {@link PibblManipulationController}
335  * @see {@link PibblResizeHandleStyle}
336  * @see {@link PibblResizeHandleProps}
337  * @see {@link ResizeHandles}
338  * @see {@link PibblManipulableProps}
339  * @see {@link PibblResizePosition}
340  */
341 export interface PibblResizeHandlesProps {
342   /**
343    * Controller owning the current interaction or mounted scenario. See
344    * {@link PibblManipulationController}.
345    */
346   readonly controller: PibblManipulationController;
347   /** Edges and corners at which resize handles are created. See {@link PibblResizeHandlesProps}. */
348   readonly positions?:
349     "corners" | "edges-and-corners" | readonly import("./types.js").PibblResizePosition[];
350   /** Size of the handle's interactive hit region. See {@link PibblResizeHandlesProps}. */
351   readonly hitSize?: number;
352   /** Declared presentation and layout properties. See {@link PibblResizeHandleStyle}. */
353   readonly style?: PibblResizeHandleStyle;
354   /** Formats the value shown by the manipulation readout. See {@link PibblResizeHandleProps}. */
355   readonly formatReadout?: PibblResizeHandleProps["formatReadout"];
356 }
357 const corners = [
358   "top-left",
359   "top-right",
360   "bottom-right",
361   "bottom-left",
362 ] as const;
363 const allHandles = [
364   "top-left",
365   "top",
366   "top-right",
367   "right",
368   "bottom-right",
369   "bottom",
370   "bottom-left",
371   "left",
372 ] as const;
373 type ResizePosition = (typeof allHandles)[number];
374 interface CollectionReadout {
375   readonly controller: PibblManipulationController;
376   readonly positions: readonly ResizePosition[];
377   readonly owner: ResizePosition;
378   readonly style: PibblResizeHandleStyle | undefined;
379   readonly formatReadout: PibblResizeHandleProps["formatReadout"];
380   readonly focused: Map<ResizePosition, boolean>;
381 }
382 const collectionStack: CollectionReadout[] = [];
383 const ResizeCollection = definePrimitive<{
384   readonly collection: CollectionReadout;
385   readonly children?: PibblNode;
386 }>(
387   function ResizeCollection(props) {
388     collectionStack.push(props.collection);
389     GLOBAL_STATE.componentRefs!.onAfterRender = () => {
390       const collection = collectionStack.pop();
391       if (collection !== props.collection)
392         throw new Error(
393           "Manipulation resize collection render context was unbalanced.",
394         );
395     };
396     return props.children;
397   },
398   { childInput: "structural" },
399 );
400 
401 /**
402  * Draws an interactive handle for one edge or corner of a manipulation target.
403  *
404  * @param props - Authored component inputs, supplied through JSX or createElement. See the linked
405  * props and style types.
406  * @throws When called directly; Pibbl mounts this component through JSX or createElement.
407  *
408  * @see {@link PibblResizeHandleProps}
409  * @see {@link PibblResizeHandleStyle}
410  */
411 export const ResizeHandle = definePrimitive<
412   PibblResizeHandleProps,
413   PibblResizeHandleStyle
414 >(function ResizeHandle(props, style, ctx) {
415   const state = requireManipulationFrame(props.controller);
416   const box = getLayoutBox();
417   const position = props.position as ResizePosition;
418   if (!allHandles.includes(position))
419     throw new Error("ResizeHandle requires a valid edge or corner position.");
420   const x = position.includes("left")
421     ? 0
422     : position.includes("right")
423       ? box.width
424       : box.width / 2;
425   const y = position.includes("top")
426     ? 0
427     : position.includes("bottom")
428       ? box.height
429       : box.height / 2;
430   const hitSize = props.hitSize ?? 24;
431   if (!Number.isFinite(hitSize) || hitSize <= 0)
432     throw new Error("ResizeHandle hitSize must be positive finite CSS pixels.");
433   const handlers = manipulationHandlers(props.controller, "resize", position);
434   useEventListener(handlers);
435   const focus = useEventTarget(handlers, {
436     path: cssHandlePath(state, x, y, hitSize),
437     fill: true,
438     cursor:
439       position === "left" || position === "right"
440         ? "ew-resize"
441         : position === "top" || position === "bottom"
442           ? "ns-resize"
443           : position === "top-left" || position === "bottom-right"
444             ? "nwse-resize"
445             : "nesw-resize",
446     keyboardFocusable: true,
447     keyboardNavigationBounds: { x: x - 4, y: y - 4, width: 8, height: 8 },
448   });
449   const active = state.controller.active.get();
450   const relevant = active?.operation === "resize" && active.handle === position;
451   const collection = collectionStack.at(-1);
452   if (
453     collection?.controller === props.controller &&
454     collection.positions.includes(position)
455   ) {
456     collection.focused.set(position, focus.isFocused);
457   } else if (readoutEnabled(style.readout, props.formatReadout)) {
458     paintReadout(
459       ctx,
460       state,
461       cssPoint(state, x, y),
462       () => formatResize(state, props.formatReadout),
463       style.readout,
464       focus.isFocused,
465       relevant,
466       position,
467     );
468   }
469   if (props.children !== undefined)
470     return createElement(CssHandleContent, {
471       style: { transform: cssHandleChildTransform(state, x, y) },
472       children: props.children,
473     });
474   paintDefaultHandle(ctx, state, x, y);
475 });
476 /**
477  * Creates the configured collection of resize handles around a manipulation target.
478  *
479  * @param props - Controller, handle placement, and presentation options. See
480  * {@link PibblResizeHandlesProps} .
481  * @returns Pibbl nodes rendering the resize handles. See {@link PibblNode}.
482  *
483  * @see {@link PibblResizeHandlesProps}
484  * @see {@link PibblNode}
485  */
486 export function ResizeHandles(props: PibblResizeHandlesProps): PibblNode {
487   const positions = (
488     props.positions === undefined || props.positions === "corners"
489       ? corners
490       : props.positions === "edges-and-corners"
491         ? allHandles
492         : props.positions
493   ) as readonly ResizePosition[];
494   if (positions.length === 0) return [];
495   const owner = positions.includes("bottom-right")
496     ? "bottom-right"
497     : positions.at(-1)!;
498   const collection: CollectionReadout = {
499     controller: props.controller,
500     positions,
501     owner,
502     style: props.style,
503     formatReadout: props.formatReadout,
504     focused: new Map(),
505   };
506   return createElement(ResizeCollection, {
507     collection,
508     children: [
509       ...positions.map((position) =>
510         createElement(ResizeHandle, {
511           key: position,
512           controller: props.controller,
513           position,
514           hitSize: props.hitSize,
515           style: props.style,
516           formatReadout: props.formatReadout,
517         }),
518       ),
519       createElement(ResizeCollectionReadout, {
520         key: "readout",
521         controller: props.controller,
522         collection,
523       }),
524     ],
525   });
526 }
527 const ResizeCollectionReadout = definePrimitive<{
528   readonly controller: PibblManipulationController;
529   readonly collection: CollectionReadout;
530 }>(function ResizeCollectionReadout(props, _style, ctx) {
531   const state = requireManipulationFrame(props.controller);
532   if (
533     !readoutEnabled(
534       props.collection.style?.readout,
535       props.collection.formatReadout,
536     )
537   )
538     return;
539   const active = state.controller.active.get();
540   const position =
541     active?.operation === "resize" &&
542     active.handle &&
543     props.collection.positions.includes(active.handle)
544       ? active.handle
545       : (props.collection.positions.find((candidate) =>
546           props.collection.focused.get(candidate),
547         ) ?? props.collection.owner);
548   const box = getLayoutBox();
549   const x = position.includes("left")
550     ? 0
551     : position.includes("right")
552       ? box.width
553       : box.width / 2;
554   const y = position.includes("top")
555     ? 0
556     : position.includes("bottom")
557       ? box.height
558       : box.height / 2;
559   paintReadout(
560     ctx,
561     state,
562     cssPoint(state, x, y),
563     () => formatResize(state, props.collection.formatReadout),
564     props.collection.style?.readout,
565     props.collection.focused.get(position) ?? false,
566     active?.operation === "resize" && active.handle === position,
567     position,
568   );
569 });
570 
571 /**
572  * Authored inputs for RotationHandle, including the declared data and presentation options.
573  *
574  * @see {@link PibblManipulationController}
575  * @see {@link PibblNode}
576  * @see {@link RotationHandle}
577  * @see {@link PibblManipulableProps}
578  */
579 export interface PibblRotationHandleProps {
580   /**
581    * Controller owning the current interaction or mounted scenario. See
582    * {@link PibblManipulationController}.
583    */
584   readonly controller: PibblManipulationController;
585   /** Size of the handle's interactive hit region. See {@link PibblRotationHandleProps}. */
586   readonly hitSize?: number;
587   /** Selects `"degrees"`, `"radians"` for readout unit. See {@link PibblRotationHandleProps}. */
588   readonly readoutUnit?: "degrees" | "radians";
589   /**
590    * Formats the value shown by the manipulation readout. See {@link PibblRotationHandleProps}.
591    * @param value - Current rotation.
592    * @returns Text to display in the rotation readout.
593    */
594   readonly formatReadout?: (value: {
595     /** Rotation applied to the containing geometry. See {@link PibblRotationHandleProps}. */
596     readonly rotation: number;
597   }) => string;
598   /** Descendant content or the callback that supplies it. See {@link PibblNode}. */
599   readonly children?: PibblNode;
600 }
601 /**
602  * Draws an interactive handle for rotating a manipulation target.
603  *
604  * @param props - Authored component inputs, supplied through JSX or createElement. See the linked
605  * props and style types.
606  * @throws When called directly; Pibbl mounts this component through JSX or createElement.
607  *
608  * @see {@link PibblRotationHandleProps}
609  * @see {@link PibblRotationHandleStyle}
610  */
611 export const RotationHandle = definePrimitive<
612   PibblRotationHandleProps,
613   PibblRotationHandleStyle
614 >(function RotationHandle(props, style, ctx) {
615   const state = requireManipulationFrame(props.controller);
616   const box = getLayoutBox();
617   const anchor = rotationAnchor(state, box.width, box.height);
618   const hitSize = props.hitSize ?? 24;
619   if (!Number.isFinite(hitSize) || hitSize <= 0)
620     throw new Error(
621       "RotationHandle hitSize must be positive finite CSS pixels.",
622     );
623   const handlers = manipulationHandlers(props.controller, "rotate");
624   useEventListener(handlers);
625   const focus = useEventTarget(handlers, {
626     path: cssHandlePath(state, anchor.local.x, anchor.local.y, hitSize),
627     fill: true,
628     cursor: "grab",
629     keyboardFocusable: true,
630     keyboardNavigationBounds: {
631       x: anchor.local.x - 4,
632       y: anchor.local.y - 4,
633       width: 8,
634       height: 8,
635     },
636   });
637   const relevant = state.controller.active.get()?.operation === "rotate";
638   paintRotationDecorations(ctx, state, anchor, style);
639   if (readoutEnabled(style.readout, props.formatReadout || props.readoutUnit))
640     paintReadout(
641       ctx,
642       state,
643       anchor.css,
644       () => formatRotation(state, props),
645       style.readout,
646       focus.isFocused,
647       relevant,
648       "top",
649     );
650   if (props.children !== undefined)
651     return createElement(CssHandleContent, {
652       style: {
653         transform: cssHandleChildTransform(
654           state,
655           anchor.local.x,
656           anchor.local.y,
657         ),
658       },
659       children: props.children,
660     });
661   paintDefaultHandle(ctx, state, anchor.local.x, anchor.local.y);
662 });
663 
664 function frameToCss(state: ManipulationState): DOMMatrix {
665   const surface = state.root.surface.snapshot;
666   const world = getCurrentWorldTransform();
667   return surface
668     ? new DOMMatrix()
669         .scale(
670           1 / surface.backingPixelsPerCssPixelX,
671           1 / surface.backingPixelsPerCssPixelY,
672         )
673         .multiply(surface.renderTransform)
674         .multiply(world)
675     : world;
676 }
677 function cssPoint(state: ManipulationState, x: number, y: number): DOMPoint {
678   return frameToCss(state).transformPoint(new DOMPoint(x, y));
679 }
680 function cssBasis(state: ManipulationState): {
681   css: DOMMatrix;
682   inverse: DOMMatrix;
683   angle: number;
684 } {
685   const css = frameToCss(state);
686   const inverse = new DOMMatrix([css.a, css.b, css.c, css.d, 0, 0]).inverse();
687   if (!(
688     Number.isFinite(inverse.a) &&
689     Number.isFinite(inverse.b) &&
690     Number.isFinite(inverse.c) &&
691     Number.isFinite(inverse.d)
692   ))
693     throw new Error(
694       "Manipulation handle cannot use a singular presentation transform.",
695     );
696   return { css, inverse, angle: Math.atan2(css.b, css.a) };
697 }
698 function cssHandlePath(
699   state: ManipulationState,
700   x: number,
701   y: number,
702   size: number,
703 ): Path2D {
704   const { inverse, angle } = cssBasis(state);
705   const path = new Path2D();
706   const half = size / 2;
707   const cos = Math.cos(angle),
708     sin = Math.sin(angle);
709   for (let i = 0; i < 4; i++) {
710     const cornerX = i === 0 || i === 3 ? -half : half;
711     const cornerY = i < 2 ? -half : half;
712     const sx = cornerX * cos - cornerY * sin,
713       sy = cornerX * sin + cornerY * cos;
714     const local = inverse.transformPoint(new DOMPoint(sx, sy));
715     if (i === 0) path.moveTo(x + local.x, y + local.y);
716     else path.lineTo(x + local.x, y + local.y);
717   }
718   path.closePath();
719   return path;
720 }
721 function cssHandleChildTransform(
722   state: ManipulationState,
723   x: number,
724   y: number,
725 ): DOMMatrix {
726   const { inverse, angle } = cssBasis(state);
727   const rotate = new DOMMatrix().rotate((angle * 180) / Math.PI);
728   const basis = inverse.multiply(rotate);
729   return new DOMMatrix([basis.a, basis.b, basis.c, basis.d, x, y]);
730 }
731 function paintDefaultHandle(
732   ctx: RenderingContext2D,
733   state: ManipulationState,
734   x: number,
735   y: number,
736 ): void {
737   ctx.fillStyle = "#2563eb";
738   ctx.fill(cssHandlePath(state, x, y, 8));
739   ctx.fillStyle = "#ffffff";
740   ctx.fill(cssHandlePath(state, x, y, 6));
741 }
742 
743 function rotationAnchor(
744   state: ManipulationState,
745   width: number,
746   height: number,
747 ) {
748   const css = frameToCss(state);
749   const top = css.transformPoint(new DOMPoint(width / 2, 0));
750   const pivot = css.transformPoint(new DOMPoint(width / 2, height / 2));
751   const edge = new DOMPoint(css.a, css.b);
752   const mappedOut = new DOMPoint(-css.c, -css.d);
753   let normal = new DOMPoint(-edge.y, edge.x);
754   const length = Math.hypot(normal.x, normal.y);
755   if (!Number.isFinite(length) || length === 0)
756     throw new Error(
757       "RotationHandle cannot use a singular presentation transform.",
758     );
759   normal = new DOMPoint(normal.x / length, normal.y / length);
760   if (normal.x * mappedOut.x + normal.y * mappedOut.y < 0)
761     normal = new DOMPoint(-normal.x, -normal.y);
762   const handle = new DOMPoint(top.x + normal.x * 24, top.y + normal.y * 24);
763   const local = css.inverse().transformPoint(handle);
764   return { css: handle, local, top, pivot, normal };
765 }
766 function paintRotationDecorations(
767   ctx: RenderingContext2D,
768   state: ManipulationState,
769   anchor: ReturnType<typeof rotationAnchor>,
770   style: PibblRotationHandleStyle,
771 ): void {
772   validateRotationStyle(style);
773   const connector =
774     style.connector === undefined || style.connector === true
775       ? {}
776       : style.connector;
777   const arc = style.arc === true ? {} : style.arc;
778   if (connector === false && !arc) return;
779   withCssContext(ctx, state, () => {
780     const baseAlpha = ctx.globalAlpha;
781     if (connector !== false) {
782       ctx.strokeStyle = connector.stroke ?? "#2563eb";
783       ctx.lineWidth = connector.strokeWidth ?? 1;
784       ctx.globalAlpha *= connector.opacity ?? 1;
785       ctx.beginPath();
786       ctx.moveTo(anchor.top.x, anchor.top.y);
787       ctx.lineTo(anchor.css.x, anchor.css.y);
788       ctx.stroke();
789       ctx.globalAlpha = baseAlpha;
790     }
791     if (arc) {
792       const radius = Math.hypot(
793         anchor.css.x - anchor.pivot.x,
794         anchor.css.y - anchor.pivot.y,
795       );
796       const center = Math.atan2(
797         anchor.css.y - anchor.pivot.y,
798         anchor.css.x - anchor.pivot.x,
799       );
800       const sweep = arc.sweep ?? Math.PI / 3;
801       for (let i = 0; i < 24; i++) {
802         const a0 = center - sweep / 2 + (sweep * i) / 24,
803           a1 = center - sweep / 2 + (sweep * (i + 1)) / 24;
804         const alpha = (1 - Math.abs((i + 0.5) / 12 - 1)) * (arc.opacity ?? 1);
805         ctx.strokeStyle = arc.color ?? "#2563eb";
806         ctx.lineWidth = arc.strokeWidth ?? 1;
807         ctx.globalAlpha = baseAlpha * alpha;
808         ctx.beginPath();
809         ctx.arc(anchor.pivot.x, anchor.pivot.y, radius, a0, a1);
810         ctx.stroke();
811       }
812       ctx.globalAlpha = baseAlpha;
813     }
814   });
815 }
816 function validateReadoutStyle(
817   value: false | true | PibblManipulationReadoutStyle | undefined,
818 ): void {
819   if (!value || value === true) return;
820   validateReadoutStateStyle(value);
821   validateReadoutStateStyle(value.inactive);
822   validateReadoutStateStyle(value.focused);
823   validateReadoutStateStyle(value.active);
824 }
825 function validateReadoutStateStyle(style: PibblManipulationReadoutStateStyle | undefined): void {
826   if (!style) return;
827   if (
828       style.gap !== undefined &&
829       (!Number.isFinite(style.gap) || style.gap < 0)
830     )
831       throw new Error(
832         "Manipulation readout gap must be finite non-negative CSS pixels.",
833       );
834   if (
835       style.strokeWidth !== undefined &&
836       (!Number.isFinite(style.strokeWidth) || style.strokeWidth < 0)
837     )
838       throw new Error(
839         "Manipulation readout strokeWidth must be finite and non-negative.",
840       );
841   if (
842       style.opacity !== undefined &&
843       (!Number.isFinite(style.opacity) || style.opacity < 0)
844     )
845       throw new Error(
846         "Manipulation readout opacity must be finite and non-negative.",
847       );
848 }
849 function validateGuideDimension(value: number | undefined, property: string): void {
850   if (value !== undefined && (!Number.isFinite(value) || value < 0))
851     throw new Error(`Manipulation guide ${property} must be finite and non-negative.`);
852 }
853 function validateRotationStyle(style: PibblRotationHandleStyle): void {
854   const connector =
855     style.connector && style.connector !== true ? style.connector : undefined;
856   const arc = style.arc && style.arc !== true ? style.arc : undefined;
857   validateGuideDimension(connector?.strokeWidth, "strokeWidth");
858   validateGuideDimension(arc?.strokeWidth, "strokeWidth");
859   validateGuideDimension(connector?.opacity, "opacity");
860   validateGuideDimension(arc?.opacity, "opacity");
861   if (
862     arc?.sweep !== undefined &&
863     (!Number.isFinite(arc.sweep) || arc.sweep <= 0 || arc.sweep > Math.PI * 2)
864   )
865     throw new Error(
866       "Manipulation arc sweep must be finite, positive, and no greater than 2π radians.",
867     );
868 }
869 
870 function readoutEnabled(
871   readout: false | true | PibblManipulationReadoutStyle | undefined,
872   formatter: unknown,
873 ): boolean {
874   return (
875     readout !== false && (readout !== undefined || formatter !== undefined)
876   );
877 }
878 function formatResize(
879   state: ManipulationState,
880   format: PibblResizeHandleProps["formatReadout"],
881 ): string {
882   const box = state.options.box.get();
883   const value = format
884     ? format({ width: box.width, height: box.height })
885     : `${round(box.width)} × ${round(box.height)} px`;
886   if (typeof value !== "string")
887     throw new Error("Manipulation readout formatReadout must return a string.");
888   return value;
889 }
890 function formatRotation(
891   state: ManipulationState,
892   props: PibblRotationHandleProps,
893 ): string {
894   const rotation = state.options.rotation?.get() ?? 0;
895   const value = props.formatReadout
896     ? props.formatReadout({ rotation })
897     : props.readoutUnit === "radians"
898       ? `${round(rotation)} rad`
899       : `${round((rotation * 180) / Math.PI)}°`;
900   if (typeof value !== "string")
901     throw new Error("Manipulation readout formatReadout must return a string.");
902   return value;
903 }
904 function round(value: number): string {
905   return Number.isInteger(value)
906     ? String(value)
907     : String(Number(value.toFixed(2)));
908 }
909 function paintReadout(
910   ctx: RenderingContext2D,
911   state: ManipulationState,
912   anchor: DOMPoint,
913   format: () => string,
914   readout: false | true | PibblManipulationReadoutStyle | undefined,
915   focused: boolean,
916   active: boolean,
917   position?: ResizePosition,
918   defaultGap = 5,
919 ): void {
920   validateReadoutStyle(readout);
921   const resolved = resolveReadout(
922     readout,
923     state.controller.active.get() === null,
924     focused,
925     active,
926     defaultGap,
927   );
928   if (!resolved.visible) return;
929   const text = format();
930   if (!text) return;
931   const frameMapping = frameToCss(state);
932   withCssContext(ctx, state, () => {
933     ctx.font = resolved.font;
934     ctx.textAlign = "left";
935     ctx.textBaseline = "alphabetic";
936     const surface = state.root.surface.snapshot;
937     if (!surface) return;
938     const stroke = resolved.stroke ? resolved.strokeWidth : 0;
939     const available = surface.displayWidth - 16 - stroke;
940     const value = ellipsis(ctx, text, available);
941     if (!value) return;
942     const metrics = measureLabel(ctx, value);
943     const glyphLeft = Math.max(0, metrics.actualBoundingBoxLeft);
944     const glyphRight = Math.max(metrics.width, metrics.actualBoundingBoxRight);
945     const ascent = Math.max(0, metrics.actualBoundingBoxAscent);
946     const descent = Math.max(0, metrics.actualBoundingBoxDescent);
947     const height = ascent + descent + 6 + stroke;
948     const width = glyphLeft + glyphRight + 8 + stroke;
949     if (width > surface.displayWidth - 8 || height > surface.displayHeight - 8)
950       return;
951     const offset = outwardReadoutOffset(
952       frameMapping,
953       position,
954       width,
955       height,
956       resolved.gap,
957     );
958     let left = anchor.x + offset.x - width / 2;
959     let top = anchor.y + offset.y - height / 2;
960     left = Math.max(4, Math.min(surface.displayWidth - 4 - width, left));
961     top = Math.max(4, Math.min(surface.displayHeight - 4 - height, top));
962     ctx.globalAlpha *= resolved.opacity;
963     ctx.fillStyle = resolved.fill;
964     if (resolved.stroke) {
965       ctx.strokeStyle = resolved.stroke;
966       ctx.lineWidth = resolved.strokeWidth;
967       ctx.strokeText(
968         value,
969         left + 4 + glyphLeft + stroke / 2,
970         top + 3 + ascent + stroke / 2,
971       );
972     }
973     ctx.fillText(
974       value,
975       left + 4 + glyphLeft + stroke / 2,
976       top + 3 + ascent + stroke / 2,
977     );
978   });
979 }
980 /** Separate the upright label from the selected edges in displayed CSS space. */
981 function outwardReadoutOffset(
982   frameMapping: DOMMatrix,
983   position: ResizePosition | undefined,
984   width: number,
985   height: number,
986   gap: number,
987 ): DOMPoint {
988   if (!position) return new DOMPoint(width / 2 + gap, 0);
989   const inverse = frameMapping.inverse();
990   const normals: DOMPoint[] = [];
991   const add = (x: number, y: number, sign: number) => {
992     const length = Math.hypot(x, y);
993     normals.push(new DOMPoint((sign * x) / length, (sign * y) / length));
994   };
995   if (position.includes("left")) add(inverse.a, inverse.c, -1);
996   if (position.includes("right")) add(inverse.a, inverse.c, 1);
997   if (position.includes("top")) add(inverse.b, inverse.d, -1);
998   if (position.includes("bottom")) add(inverse.b, inverse.d, 1);
999   const support = (n: DOMPoint) =>
1000     (Math.abs(n.x) * width) / 2 + (Math.abs(n.y) * height) / 2 + gap;
1001   const [a, b] = normals;
1002   const da = support(a);
1003   if (!b) return new DOMPoint(a.x * da, a.y * da);
1004   const db = support(b),
1005     determinant = a.x * b.y - a.y * b.x;
1006   return new DOMPoint(
1007     (da * b.y - a.y * db) / determinant,
1008     (a.x * db - da * b.x) / determinant,
1009   );
1010 }
1011 function resolveReadout(
1012   value: false | true | PibblManipulationReadoutStyle | undefined,
1013   inactive: boolean,
1014   focused: boolean,
1015   active: boolean,
1016   defaultGap: number,
1017 ): Required<PibblManipulationReadoutStateStyle> {
1018   const source = value && value !== true ? value : {};
1019   const base: Required<PibblManipulationReadoutStateStyle> = {
1020     gap: source.gap ?? defaultGap,
1021     font: source.font ?? "12px sans-serif",
1022     fill: source.fill ?? "#1d4ed8",
1023     stroke: source.stroke ?? "",
1024     strokeWidth: source.strokeWidth ?? 0,
1025     opacity: source.opacity ?? 1,
1026     visible: source.visible ?? false,
1027   };
1028   const patch = (next: PibblManipulationReadoutStateStyle | undefined) => {
1029     if (next) Object.assign(base, next);
1030   };
1031   if (inactive) patch(source.inactive);
1032   if (focused) patch(source.focused);
1033   if (active) (patch({ visible: true }), patch(source.active));
1034   return base;
1035 }
1036 const labelMeasurements = new WeakMap<
1037   RenderingContext2D,
1038   Map<string, TextMetrics>
1039 >();
1040 function measureLabel(ctx: RenderingContext2D, text: string): TextMetrics {
1041   let cache = labelMeasurements.get(ctx);
1042   if (!cache) labelMeasurements.set(ctx, (cache = new Map()));
1043   const key = JSON.stringify([
1044     ctx.font,
1045     ctx.direction,
1046     ctx.fontKerning,
1047     ctx.letterSpacing,
1048     ctx.wordSpacing,
1049     text,
1050   ]);
1051   const found = cache.get(key);
1052   if (found) return found;
1053   const result = ctx.measureText(text);
1054   if (cache.size >= 64) cache.delete(cache.keys().next().value!);
1055   cache.set(key, result);
1056   return result;
1057 }
1058 function ellipsis(
1059   ctx: RenderingContext2D,
1060   text: string,
1061   width: number,
1062 ): string {
1063   const fits = (value: string) => {
1064     const m = measureLabel(ctx, value);
1065     return (
1066       Math.max(0, m.actualBoundingBoxLeft) +
1067         Math.max(m.width, m.actualBoundingBoxRight) <=
1068       width
1069     );
1070   };
1071   if (width <= 0) return "";
1072   if (fits(text)) return text;
1073   const suffix = "…";
1074   if (!fits(suffix)) return "";
1075   const points = Array.from(text);
1076   let low = 0,
1077     high = points.length;
1078   while (low < high) {
1079     const mid = Math.ceil((low + high) / 2);
1080     if (fits(points.slice(0, mid).join("") + suffix)) low = mid;
1081     else high = mid - 1;
1082   }
1083   return points.slice(0, low).join("") + suffix;
1084 }
1085 function withCssContext(
1086   ctx: RenderingContext2D,
1087   state: ManipulationState,
1088   draw: () => void,
1089 ): void {
1090   const surface = state.root.surface.snapshot;
1091   if (!surface) return;
1092   const cssToCurrent = ctx.getTransform().multiply(frameToCss(state).inverse());
1093   ctx.save();
1094   try {
1095     ctx.setTransform(cssToCurrent);
1096     ctx.beginPath();
1097     ctx.rect(0, 0, surface.displayWidth, surface.displayHeight);
1098     ctx.clip();
1099     draw();
1100   } finally {
1101     ctx.restore();
1102   }
1103 }
1104 

Documentation built with @pibbl/core 0.0.2, revision a1dcf8e. ALPHA — NOT FOR PRODUCTION USE.