All files / src Slate.ts

97.35% Statements 994/1021
92.61% Branches 251/271
98.75% Functions 79/80
97.35% Lines 994/1021

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 10221x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 478321x 478321x 478321x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 16913x 16913x 16913x 478210x 15x 15x 15x 478210x 16913x 16913x 16913x 14x 16913x 14x 14x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 985x 985x 985x 985x 985x     985x 985x 985x 985x 985x 985x 985x 985x 985x 985x 985x 985x 985x 985x 985x 985x 985x 985x 985x 985x 985x 3940x 3940x 3940x 3940x 3940x 3940x 3940x 985x 985x 985x 985x 985x 985x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x           19x 19x 19x       19x 19x 19x           19x 19x 19x 19x 19x               19x 19x 19x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 4228x 4228x 1x 1x 1x 4228x 4164x 4164x 1x 1x 1x 4228x 4164x 1x 1x 1x 5860x 5860x 1x 1x 1x 1x 1x 1x 4895x 4895x 1x 1x 703x 703x 1x 1x 5466x 5466x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 1x 1x 32475x 505325x 32430x 32430x 505325x 45x 45x 45x 45x 32475x 12x 88x 88x 1x 34x 34x 1x 1x 1x 1x 5x 5x 1x 1x 12x 12x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 33x 33x 1x 1x 2x 2x 1x 1x 4x 4x 1x 1x 12x 12x 1x 1x 3x 3x 1x 1x 1x 1x 1x 1x 13x 13x 1x 1x 12x 12x 1x 1x 3x 3x 1x 1x 42x 42x 1x 1x 66x 66x 1x 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 27x 27x 27x 1x 1x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12012x 12012x 1x 1x 1x 1x 1x 1x 1x 30x 30x 30x 1x 1x 1x 1x 1x 12538x 12538x 12538x 1x 12538x 12538x 12538x 1x 1x 1x 1x 29x 29x 29x 29x 1x 1x 1x 1x 14x 14x 14x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 1x 1x 1x 1x 1x 1x 14x 14x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 35x 35x 11x 35x 35x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 1x 1x 15x 15x 1x 1x 1x 1x 1x 1x 29x 29x 29x 163x 163x 29x 29x 251x 251x 251x 251x 12x 251x 1x 1x 1x 106x 20x 20x 105x 85x 85x 251x 29x 26x 26x 1x 1x 1x 1x 1x 1x 4x 4x 10x 4x 4x 4x 1x 1x 1x 1x 13x 13x 13x 13x 13x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 33x 33x 33x 4x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 1x 1x 1x 1x 9x 9x 9x 9x 9x 1x 1x 1x 1x 1x 1x 2x 17x 17x 2x 1x 1x 1x 1x 1x 1x 1x 16246x 16246x 16246x 16246x 46400x 46400x 31863x 31863x 31863x 31863x 29231x 31863x 901x 901x 901x 2610x 1709x 1709x 31841x 46400x 14536x 14536x 46400x 1x 46400x 46377x 16223x 16223x 1x 1x 16223x 374784x 16216x 16216x 374784x 7x 7x 1x 1x 16246x 16246x 16246x 16246x 16246x 16246x 7x 7x 1x 1x 7x 7x 16216x 16216x 16216x 16216x 16216x 16216x 16246x 129x 129x 16216x 16216x 16246x 16166x 16166x 16166x 16216x 16246x 16216x 16216x 1x 1x 4x 55x 55x 4x 4x 1x 1x 793x 793x 793x 1x 1x 12083x 12012x 12012x 12012x 12012x 12012x 12012x 12012x 12012x 12012x 12012x 12012x 12083x 12083x 12083x 12083x 12083x 12083x 12083x 12083x 12083x 12083x 12083x 12083x 12083x 12083x 12012x 12012x 12083x 12012x 12012x 12012x 12012x 12083x 12083x 12083x 12083x 12012x 12012x 12012x 12012x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12012x 12012x 341586x 341586x 341586x 12012x 12012x 728x 728x 728x 12012x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 57x 57x 52x 52x 52x 52x 52x 52x 52x 52x 1x 1x 1x 701x 701x 1x 1x 1x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 1x 1x 1x 2x 2x 1x 1x 2x 2x 1x 1x 55x 3x 3x 55x 1x 1x 1x 1x 1x 55x 55x 55x           1x 1x 1x 1x 1x 1x 5643x 79766x 79766x 79766x 79766x 5643x 5643x 1x 1x 11x 11x 11x 235x 235x 235x 235x 11x 11x 1x 1x 4233x 4233x 4233x 4233x 4233x 4233x 4233x 4233x 4233x 4233x 4233x 119962x 119962x 119962x 57103x 119962x 59321x 59321x 3538x 4233x 4233x 4233x 4233x 4167x 4167x 1x 1x 12556x 4228x 4228x 4228x 4164x 4164x 4164x 1x 1x 1x 1x 1x 1x 1x 98x 98x 98x 98x 98x 98x 98x 4x 4x 4x 4x 4x 98x 94x 94x 94x 94x 98x 1x 1x 12556x 12492x 12492x 12492x 12492x 12492x 12492x 12556x 12556x 12556x 12556x 12492x 12556x 12556x 6700x 6700x 12556x 5776x 5618x 5776x 158x 158x 12556x 16x 16x 6x 16x 10x 10x 10x 10x 5634x 5634x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 123x 123x 123x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x  
import {GeomElement} from "./elements/GeomElement";
import {PlaneElement} from "./elements/plane/PlaneElement";
import {FixedPoint} from "./elements/point/FixedPoint";
import {AllConstructions, Construction, SortedParams, constructions, getConstructionName} from "./elements/Constructions";
import {PointElement} from "./elements/point/PointElement";
import {Canvas} from "canvas";
import {LineElement} from "./elements/line/LineElement";
import {PolygonElement} from "./elements/polygon/PolygonElement";
import {CircleElement} from "./elements/circle/CircleElement";
import {ISlide, ISlideAnimation, IAnimationConfig} from "./index";
import {SlateAnimator} from "./SlateAnimator";
import {DiagnosticLog, IDiagnostic, IDiagnosticInput, DiagnosticSeverity,
        diagnosticEventDetail} from "./Diagnostics";
 
export type SlateCanvas = HTMLCanvasElement | Canvas;
 
export type ResolveJustification = (ref: string) => string | null | undefined;
 
// #140 — an element that is highlighted or mid-animation is what the viewer
// is meant to be looking at, so it should not be painted over by whatever
// happens to be declared after it in the deck's `elements` array.
//
// `emphasized` is `_emphasisAmount > 0`, so this stays true through the
// post-animation fade taper — an element can't drop back down in z-order
// while it is still visibly gold. `drawProgress < 1` is the independent
// "currently being drawn in" flag: it defaults to 1 and every animation's
// finalise()/cancel resets it to 1, so it covers an animation that drives
// partial geometry without touching emphasis.
export function isPromoted(e: GeomElement): boolean {
    return e.shouldHighlight || e.emphasized || e.drawProgress < 1;
}
 
/**
 * Reorder one draw pass so promoted elements paint last (on top), while
 * both groups keep their existing relative order.
 *
 * The promotion is deliberately *within a pass*, not to the top of the whole
 * figure: `drawElements` runs face → edge → vertex → name, so a promoted
 * face still stays under every edge. Highlighting a filled polygon therefore
 * can't hide the lines drawn across it.
 *
 * When nothing is promoted this returns the input order, so the render is
 * bit-for-bit unchanged for every static figure and every snapshot golden.
 */
export function promotedDrawOrder(elements: GeomElement[]): GeomElement[] {
    let promoted: GeomElement[] | null = null;
    for (let e of elements) {
        if (isPromoted(e)) {
            if (promoted == null) promoted = [];
            promoted.push(e);
        }
    }
    // Nothing highlighted or animating — the overwhelmingly common case.
    // Hand back the original array untouched (no allocation, no reorder).
    if (promoted == null) return elements;
    const rest: GeomElement[] = [];
    for (let e of elements) if (!isPromoted(e)) rest.push(e);
    return rest.concat(promoted);
}
 
export class Slate {
 
    protected _elements : GeomElement[];
    // Name aliases — secondary names that resolve to existing elements
    // without adding new ones to _elements. The original Geometry Applet
    // and Euclid's *Elements* refer to the same circle by every
    // permutation of its three named points (BCD ≡ CDB ≡ DBC ≡ …) and
    // to lines by either endpoint order (AB ≡ BA). Authors register
    // those equivalences here so the prose-side `{NAME}` shortcode can
    // light up the canonical element without needing a parallel
    // invisible duplicate per alias.
    private _aliases : Map<string, string> = new Map();
    // #140 — draw highlighted / mid-animation elements last within each
    // pass so they aren't painted over by later-declared neighbours.
    // On by default; set false to keep the strict declaration order.
    private _highlightOnTop : boolean = true;
    // Optional slideshow walk-through; populated from init().slides.
    // Empty array means no presentation mode is available; SlateControls
    // skips the "▶ Present" button.
    private _slides : ISlide[] = [];
    // Consumer-supplied callback resolving a justification ref label
    // (e.g. "I.Post.3") to a URL. Returns null/undefined when the ref
    // isn't recognised, in which case the Presentation overlay renders
    // the ref as plain text. Undefined means no resolver was passed.
    private _resolveJustification : ResolveJustification | null = null;
    // Slide-transition animation config (rates, durations, modifiers).
    // The slate ships a default; init() merges in whatever the
    // consumer provides via IInitialization.animationConfig.
    private _animationConfig : IAnimationConfig = {};
    // The frame-driver. Lazy-created on first animateTo() so a
    // headless test slate that never animates doesn't allocate a
    // requestAnimationFrame loop.
    private _animator : SlateAnimator | null = null;
    // Ephemeral helper elements an animation can spawn for its
    // lifetime (compass arms, guide circles, straightedge bars).
    // Drawn on top of _elements in the same face → edge → vertex →
    // name order; never appear in lookupElement; cleared on
    // animator.cancel() as a safety net.
    private _ephemerals : GeomElement[] = [];
    // View transform: model (logical) coords → display px is
    // F·model + T, where F = _viewScale (uniform) and T = (_viewOffsetX,
    // _viewOffsetY). drawElements then scales by dpr to the bitmap; the
    // pick path inverts it. T centres the figure when maximized (#99/#107);
    // F shrinks the figure to fit a narrow display (#71). Defaults
    // F=1, T=(0,0) are a no-op, so every existing render path — and the
    // headless snapshot path — is bit-for-bit unchanged.
    // #138 — the bounding box the maximized / presentation centring was
    // computed from. Captured ONCE on entering the maximized view (#114
    // centres once, not per slide) and cleared on leaving, so the centre
    // can't drift as slides reveal and hide elements, and so cloneAside's
    // centre phase (#99) eases to exactly the same target.
    private _centringBounds : { minX: number, maxX: number, minY: number, maxY: number } | null = null;
    private _viewOffsetX : number = 0;
    private _viewOffsetY : number = 0;
    private _viewScale : number = 1;
    // The construction's authored coordinate extent (logical px). Element
    // coords live here. Defaults to the canvas bitmap so the headless /
    // snapshot path has logical == display == bitmap (fit-scale 1). init()
    // overrides it with the authored size on a real DOM canvas (#71).
    private _logicalWidth : number = 0;
    private _logicalHeight : number = 0;
    // Last-dispatched highlighted-name key, so the geomlib:highlight event
    // (#108) fires only when the SET of highlighted elements changes — not
    // on every redraw frame / emphasisAmount fade.
    private _lastHighlightedKey : string = "\u0000";
    // #154 — what this slate has complained about, so a badge can show it
    // and a consumer can read it. Per-slate on purpose: the dedupe flags
    // this replaced were module-global, so one figure's warning silenced
    // every other figure on the page.
    private _diagnostics : DiagnosticLog = new DiagnosticLog();
    private _diagnosticListeners : Array<(s: Slate) => void> = [];
    protected _screen : PlaneElement;
    protected _pick : PointElement;
    protected _canvas : SlateCanvas;
    private _htmlCanvas : HTMLCanvasElement = null;
    protected _bgcolor : string;
    public    inTest : boolean = false;
    private static numSlate : number = 0;
    private _itsNumSlate : number = -1;
 
    constructor(canvas: SlateCanvas) {
        Slate.numSlate += 1;
        this._itsNumSlate = Slate.numSlate;
 
        this._elements = [];
        if(canvas == null) {
            throw new TypeError("canvas cannot be null or undefined.");
        }
        this._canvas = canvas;
 
        let screen_origin = new FixedPoint({x:0,y:0,z:0});
        screen_origin.name = "screen_origin";
        let screen_x      = new FixedPoint({x:1,y:0,z:0});
        screen_x.name = "screen_x";
        let screen_y      = new FixedPoint({x:0,y:1,z:0});
        screen_y.name = "screen_y";
 
        let screen = new PlaneElement({
                A: screen_origin,
                B: screen_x,
                C: screen_y
        });
        screen.name = "screen";
        screen.nameColor = null;
        screen.isScreen = true;
        this._screen = screen;
        this._pick = null;
 
        for(let e of [screen_origin, screen_x, screen_y, screen]) {
            e.nameColor = null;
            e.vertexColor = null;
            e.vertexHighlightColor = null;
            e.faceColor = null;
            e.faceHighlightColor = null;
            this._elements.push(e);
        }
        let slate = this;
 
        let cnv : HTMLCanvasElement = this._canvas as HTMLCanvasElement;
        this._htmlCanvas = cnv;
 
        if(this._htmlCanvas.addEventListener == null) return;
 
        // Pointer Events unify mouse / touch / stylus into one event model.
        // Setting touch-action: none here (rather than relying on
        // consumer-side CSS) tells the browser not to claim drag gestures
        // for scroll/zoom; calling setPointerCapture on pointerdown ensures
        // we keep getting move/up events even when the finger leaves the
        // canvas bounds. See issue #55 for the design rationale.
        this._htmlCanvas.style.touchAction = "none";
 
        // Only the primary pointer drives the drag pipeline for now.
        // Multi-touch (two-finger rotation etc.) is intentionally not yet
        // wired — design questions tracked in issue #57.
        let activePointerId: number | null = null;
 
        this._htmlCanvas.addEventListener("pointerdown", (ev: PointerEvent) => {
            if (activePointerId !== null) return; // already tracking a primary
            activePointerId = ev.pointerId;
            try { slate._htmlCanvas.setPointerCapture(ev.pointerId); } catch (_) { /* ignore */ }
            let [x, y] = slate._getCanvasPosition(ev.clientX, ev.clientY);
            slate._onMouseDown(x, y);
        });
 
        this._htmlCanvas.addEventListener("pointermove", (ev: PointerEvent) => {
            if (ev.pointerId !== activePointerId) return;
            let [x, y] = slate._getCanvasPosition(ev.clientX, ev.clientY);
            slate._onMouseDrag(x, y);
        });
 
        let endPointer = (ev: PointerEvent) => {
            if (ev.pointerId !== activePointerId) return;
            let [x, y] = slate._getCanvasPosition(ev.clientX, ev.clientY);
            slate._onMouseUp(x, y);
            try { slate._htmlCanvas.releasePointerCapture(ev.pointerId); } catch (_) { /* ignore */ }
            activePointerId = null;
        };
 
        this._htmlCanvas.addEventListener("pointerup", endPointer);
        this._htmlCanvas.addEventListener("pointercancel", endPointer);
        this._htmlCanvas.addEventListener("lostpointercapture", (ev: PointerEvent) => {
            if (ev.pointerId === activePointerId) {
                // Browser revoked capture (e.g., system gesture); clear pick state.
                if (slate._pick != null) {
                    slate._pick = null;
                }
                activePointerId = null;
            }
        });
 
    }
 
    _getCanvasPosition(x: number, y: number) : [number, number] {
        let r = this._htmlCanvas.getBoundingClientRect();
        // Map display px → model (logical) coords: invert the view
        // transform F·model + T, so a click lands on the real element even
        // while the figure is fit-scaled (#71) or slid to centre (#99/#107).
        // getBoundingClientRect is display px; dpr never enters here.
        // F = 1, T = 0 → today's plain rect subtraction.
        const F = this._viewScale || 1;
        return [(x - r.left - this._viewOffsetX) / F,
                (y - r.top  - this._viewOffsetY) / F];
    }
 
    _onMouseDown(x: number, y: number) {
        this._pick = null;
        this.movePick(x, y);
    };
 
    _onMouseUp(x: number, y: number) {
        if (this._pick == null) return;
        this.movePick(x, y);
        this._pick = null;
    };
 
    _onMouseDrag(x: number, y: number) {
        if (this._pick == null) return;
        this.movePick(x, y);
    };
 
    get elements() : GeomElement[] {
        return this._elements;
    }
 
    set highlightOnTop(value: boolean) {
        this._highlightOnTop = value;
    }
 
    get highlightOnTop() : boolean {
        return this._highlightOnTop;
    }
 
    set bgcolor(value: string ) {
        this._bgcolor = value;
    }
 
    get bgcolor() : string {
        return this._bgcolor;
    }
 
    // Public accessor for the underlying canvas. Consumer-side code on
    // multi-canvas pages (e.g. the {NAME} shortcode on euclids-elements.org)
    // needs to compare DOM positions of each slate's canvas against the
    // prose-side <span> to pick the right slate. Was protected before
    // 0.4.0; the few prior callers (s.canvas in elem-ref-highlight.js)
    // were just reading undefined and falling through to a brute-force
    // walk of geomlib.slates.
    get canvas() : SlateCanvas {
        return this._canvas;
    }
 
    lookupElement(name: string) : GeomElement {
        for (let elem of this._elements) {
            if (elem.name == name) {
                return elem;
            }
        }
        // Direct miss — follow the alias map and look up the canonical
        // name. One hop only: we don't chase chains because aliases are
        // expected to point at real element names, not at each other.
        let canonical = this._aliases.get(name);
        if (canonical != null) {
            for (let elem of this._elements) {
                if (elem.name == canonical) return elem;
            }
        }
        return null;
    }
 
    // Register a single secondary name → canonical name mapping. Idempotent.
    // Called by init() when the user passes an `aliases` field.
    addAlias(from: string, to: string) : void {
        this._aliases.set(from, to);
    }
 
    addAliases(aliases: {[from: string]: string}) : void {
        for (let from in aliases) this._aliases.set(from, aliases[from]);
    }
 
    // Draggable elements named here are excluded from the slideshow's
    // every-slide auto-union and follow slide visible sets like any
    // other element — for proof-introduced draggables ("Take an
    // arbitrary point F") that shouldn't show before their moment.
    // They stay fully draggable whenever visible (#89). Populated by
    // init() from the `deferDraggables` field.
    private _deferredDraggables : Set<string> = new Set();
 
    get deferredDraggables() : Set<string> {
        return this._deferredDraggables;
    }
 
    deferDraggables(names: string[]) : void {
        for (const n of names || []) this._deferredDraggables.add(n);
    }
 
    get slides() : ISlide[] {
        return this._slides;
    }
 
    set slides(value: ISlide[]) {
        this._slides = value || [];
    }
 
    get resolveJustification() : ResolveJustification | null {
        return this._resolveJustification;
    }
 
    set resolveJustification(value: ResolveJustification | null) {
        this._resolveJustification = value;
    }
 
    get animationConfig() : IAnimationConfig {
        return this._animationConfig;
    }
 
    set animationConfig(value: IAnimationConfig) {
        this._animationConfig = value || {};
    }
 
    get animator() : SlateAnimator | null {
        return this._animator;
    }
 
    get ephemerals() : GeomElement[] {
        return this._ephemerals;
    }
 
    addEphemeral(e: GeomElement) : void {
        this._ephemerals.push(e);
    }
 
    // Register a pre-built element as a persistent (non-ephemeral) member of
    // the figure — drawn in the normal pass, addressable by name, and governed
    // by the slideshow visible-set. Used by macro animations (e.g.
    // A.Circle.compassTransfer's keepCircles) to leave named scaffolding that a
    // later slide can re-show, where addEphemeral's wipe-on-advance won't do.
    addElement(e: GeomElement) : void {
        if (this._elements.indexOf(e) === -1) this._elements.push(e);
    }
 
    removeEphemeral(e: GeomElement) : void {
        const i = this._ephemerals.indexOf(e);
        if (i >= 0) this._ephemerals.splice(i, 1);
    }
 
    clearEphemerals() : void {
        this._ephemerals.length = 0;
    }
 
    get viewOffsetX() : number { return this._viewOffsetX; }
    get viewOffsetY() : number { return this._viewOffsetY; }
    get viewScale() : number { return this._viewScale; }
 
    // Device pixel ratio (#71). The canvas BITMAP is sized to display px ×
    // dpr so drawing lands on native device pixels (crisp on HiDPI/retina).
    // Headless / no window → 1, so node-canvas rendering (and the snapshot
    // goldens) is unchanged.
    private _dpr() : number {
        return (typeof window !== "undefined" && window.devicePixelRatio) || 1;
    }
 
    // The authored coordinate extent (logical px) — the space element
    // coords live in. Falls back to the bitmap when unset (headless /
    // snapshot path), so logical == bitmap there. (#71)
    get logicalWidth() : number { return this._logicalWidth || this._canvas.width; }
    get logicalHeight() : number { return this._logicalHeight || this._canvas.height; }
    setLogicalSize(w: number, h: number) : void {
        if (w > 0) this._logicalWidth = w;
        if (h > 0) this._logicalHeight = h;
    }
 
    // The canvas DISPLAY size in CSS px (responsive; clientWidth on a real
    // DOM canvas, the fixed bitmap headless). The figure is fit-scaled from
    // logical → display. (#71)
    get displayWidth() : number {
        const c = this._canvas as any;
        return (typeof c.clientWidth === "number" && c.clientWidth) || this._canvas.width;
    }
    get displayHeight() : number {
        const c = this._canvas as any;
        return (typeof c.clientHeight === "number" && c.clientHeight) || this._canvas.height;
    }
 
    // Recompute the fit-scale F = min(1, display / logical): shrink the
    // figure to fit a narrow display, never grow it. Called on init/resize.
    recomputeFitScale() : void {
        const sx = this.displayWidth / this.logicalWidth;
        const sy = this.displayHeight / this.logicalHeight;
        this._viewScale = Math.min(1, sx, sy);
    }
 
    // Set the view transform's translate T (display px). drawElements
    // applies F·model + T; the pick path inverts it. (#99/#107)
    setViewOffset(x: number, y: number) : void {
        this._viewOffsetX = x;
        this._viewOffsetY = y;
    }
 
    clearViewOffset() : void {
        this._viewOffsetX = 0;
        this._viewOffsetY = 0;
    }
 
    // Axis-aligned bounding box (canvas px) of the visible figure —
    // every named, visible, non-screen element. Returns null when the
    // figure is empty. Used by cloneAside auto-place for the centre
    // target and the fit check (#99). Points/lines/polygons/circles are
    // measured exactly; sector markers sit inside their vertex (already
    // counted) so they're not separately walked.
    visibleBounds() : { minX: number, maxX: number, minY: number, maxY: number } | null {
        return this._bounds(true);
    }
 
    // Like visibleBounds() but over EVERY named, non-screen element
    // regardless of its `visible` flag — a stable bound that doesn't drift
    // as a presentation reveals/hides elements slide-to-slide. Used to
    // centre the figure in a maximized / presentation view (#107).
    figureBounds() : { minX: number, maxX: number, minY: number, maxY: number } | null {
        return this._bounds(false);
    }
 
    // #138 — does this element put any ink on the canvas?
    //
    // Deck figures carry construction helpers that exist only to define a
    // direction or a slider's track — a "point at infinity" aim, the two
    // anchors a lineSlider runs between — authored with zero colours so they
    // draw nothing. They are still `visible`, so a visibility filter does NOT
    // exclude them; only asking what actually paints does.
    //
    // The test is per type because a colour slot an element never uses tells
    // us nothing: PointElement.drawEdge / drawFace are no-ops, so a point's
    // edgeColor (which defaults to black when the author omits the field)
    // must not count as ink — that default is exactly what made these helpers
    // look paintable.
    private _paintsInk(elem: GeomElement) : boolean {
        if (!elem.visible) return false;
        if (elem.nameColor != null) return true;
        if (elem instanceof PointElement) return elem.vertexColor != null;
        return elem.edgeColor != null || elem.faceColor != null;
    }
 
    // Bounding box to centre the figure on in the maximized / presentation
    // view (#107). Measures only elements that actually paint, so an
    // off-screen zero-colour helper can't inflate the box and translate the
    // real geometry off-canvas — the #138 blank-slideshow bug, hit on I.35,
    // I.42 and I.43.
    //
    // Captured once per maximized session rather than recomputed: the result
    // must not move as slides change visibility, and cloneAside reads the
    // same captured box so its centre phase lands where the base centring
    // already is. Falls back to the all-elements bounds if nothing paints at
    // all (a figure built entirely from helpers), so the caller always gets
    // the same shape of answer as before.
    captureCentringBounds() : { minX: number, maxX: number, minY: number, maxY: number } | null {
        const inked = this._bounds(false, (e) => this._paintsInk(e));
        this._centringBounds = inked != null ? inked : this._bounds(false);
        return this._centringBounds;
    }
 
    get centringBounds() : { minX: number, maxX: number, minY: number, maxY: number } | null {
        return this._centringBounds;
    }
 
    clearCentringBounds() : void {
        this._centringBounds = null;
    }
 
    private _bounds(visibleOnly: boolean,
                    accept?: (e: GeomElement) => boolean) : { minX: number, maxX: number, minY: number, maxY: number } | null {
        let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity;
        const add = (x: number, y: number) => {
            if (x < minX) minX = x; if (x > maxX) maxX = x;
            if (y < minY) minY = y; if (y > maxY) maxY = y;
        };
        for (let elem of this._elements) {
            if (elem.name == null || elem.name.startsWith("screen")) continue;
            if (visibleOnly && !elem.visible) continue;
            if (accept != null && !accept(elem)) continue;
            if (elem instanceof PolygonElement) {
                for (let v of elem.V) add(v.x, v.y);
            } else if (elem instanceof CircleElement) {
                const r = elem.radius;
                add(elem.Center.x - r, elem.Center.y - r);
                add(elem.Center.x + r, elem.Center.y + r);
            } else if (elem instanceof LineElement) {
                add(elem.A.x, elem.A.y);
                add(elem.B.x, elem.B.y);
            } else if (elem instanceof PointElement) {
                add(elem.x, elem.y);
            }
        }
        if (minX === Infinity) return null;
        return { minX, maxX, minY, maxY };
    }
 
    // All names that resolve to the element named `canonical`: the
    // element's own name plus every alias whose target is it. Used to tag
    // the geomlib:highlight event so a consumer can match a prose ref by
    // any spelling (BCD ≡ CDB ≡ DBC). (#108)
    namesFor(canonical: string) : string[] {
        const names = [canonical];
        this._aliases.forEach((target, from) => {
            if (target === canonical && from !== canonical) names.push(from);
        });
        return names;
    }
 
    // Lazy-create the animator on first use. Each slate owns at most
    // one — cancel() preserves it for re-use across slide transitions.
    private _ensureAnimator() : SlateAnimator {
        if (this._animator == null) {
            this._animator = new SlateAnimator(this);
        }
        return this._animator;
    }
 
    // Public entry point for slideshow controllers. Diff the current
    // visibility / highlight state against the target, run any slide-
    // listed animations against the elements becoming visible. Newly-
    // visible elements without an explicit animation entry pop in
    // instantly (4b parity). Returns a Promise that resolves when the
    // animator completes the run or cancel() is called.
    animateTo(
        targetVisible: Set<string>,
        targetHighlighted: Set<string>,
        slideAnimations: ISlideAnimation[],
        mode: "cascade" | "parallel",
    ) : Promise<void> {
        return this._ensureAnimator().run(
            targetVisible, targetHighlighted, slideAnimations, mode,
        );
    }
 
    // Hide every named element whose name is not in the supplied set.
    // Names not matching any element are silently ignored; elements
    // without a name (intermediate construction outputs) are left alone.
    // Screen-helper elements (screen / screen_origin / screen_x /
    // screen_y) follow the same rule: their visible flag does flip, but
    // since they're created with all-null colours they never draw
    // visible pixels anyway. Called by the presentation overlay per
    // slide; consumers can call directly too.
    setVisibleNames(names: string[]) : void {
        const visible = new Set(names);
        for (let elem of this._elements) {
            if (elem.name == null) continue;
            elem.visible = visible.has(elem.name);
        }
    }
 
    // Names of elements that start hidden (angle markers by default, or
    // anything in init's `initiallyHidden`). clearVisibility restores to
    // this baseline rather than blanket-true, so hidden elements stay
    // hidden after a presentation walk. (#100)
    private _initiallyHidden : Set<string> = new Set();
 
    get initiallyHidden() : Set<string> {
        return this._initiallyHidden;
    }
 
    // Mark a named element hidden in the initial figure: flips its
    // visible flag off now and remembers it for clearVisibility.
    setInitiallyHidden(name: string) : void {
        if (name == null) return;
        this._initiallyHidden.add(name);
        const elem = this.lookupElement(name);
        if (elem != null) elem.visible = false;
    }
 
    // Restore every element's visible flag to its initial baseline —
    // true, except names registered as initially hidden. Counterpart to
    // setVisibleNames; called on presentation exit so the canvas
    // returns to its inline default.
    clearVisibility() : void {
        for (let elem of this._elements) {
            elem.visible = !(elem.name != null && this._initiallyHidden.has(elem.name));
        }
    }
 
    // Sort params into P[] (points), E[] (other elements), N[] (integers),
    // matching Java's selectDataChoice (Slate.java lines 344-393).
    // LineElements are expanded into their two endpoint PointElements, unless
    // the target construction opts out (keepLineElements — e.g. curvedTriangle/
    // curvedPolygon take a line intact as a straight-side carrier).
    convertParams(params: any[], keepLineElements: boolean = false) : SortedParams {
        let P: PointElement[] = [];
        let E: GeomElement[] = [];
        let N: number[] = [];
        for(let param of params) {
            switch(typeof(param)) {
                case "string":
                    let g : GeomElement = this.lookupElement(param);
                    if ( g == null )
                        throw new TypeError(`Element with name ${param} not found.`);
                    if (g instanceof PointElement) {
                        P.push(g);
                    } else if (g instanceof LineElement && !keepLineElements) {
                        // LineElement expands to two PointElements (matching Java)
                        P.push(g.A);
                        P.push(g.B);
                    } else {
                        E.push(g);
                    }
                    break;
                case "number":
                    N.push(param);
                    break;
                default:
                    throw new TypeError("Expecting only named elements (strings) or numbers.");
            }
        }
        return {P, E, N};
    }
 
    findConstruction(cm : AllConstructions, sp: SortedParams) : Construction {
        for(let c of constructions) {
            if(c.validateSignature(cm, sp)) {
                return c;
            }
        }
        return null;
    }
 
    createElement(cm : AllConstructions, params: any[], name?: string) : GeomElement {
        // A construction for this method may want LineElement params kept
        // intact (not expanded to endpoints) — see Construction.keepsLineElements.
        let keepLines = constructions.some(c => c.constructionMethod === cm && c.keepsLineElements);
        let sp : SortedParams = this.convertParams(params, keepLines);
        let c : Construction = this.findConstruction(cm, sp);
        if(c == null) {
            let cName : String = getConstructionName(cm);
            if (name == null) {
                name = "";
            }
            throw new TypeError(`Construction not found for "${name}" ${cName} with params P=[${sp.P}] E=[${sp.E}] N=[${sp.N}]`);
        }
        let [gs, g] = c.construct(this._screen, sp.P, sp.E, sp.N);
        // Mark as preexisting if this element was already in _elements
        // before this construction (e.g., point;first returns an existing
        // PointElement). Preexisting elements are skipped during
        // rotate/translate to avoid double-movement.
        // (Java: Slate.java preexists[] array)
        if (this._elements.indexOf(g) !== -1) {
            g.preexists = true;
        }
        if(name != null)
            g.name = name;
        for (let elem of gs) {
            if (this._elements.indexOf(elem) == -1)
                this._elements.push(elem);
        }
        if(this._elements.indexOf(g) == -1)
            this._elements.push(g);
        return g;
    }
 
    reset() : void {
        for (let element of this._elements) {
            element.reset();
        }
        this.update();
    }
 
    update() : void {
        for(let element of this._elements) element.update();
        this.drawElements();
    }
 
    drawElements(): void {
        if(this.inTest) return;
        let ctx : CanvasRenderingContext2D = this._canvas.getContext("2d") as CanvasRenderingContext2D;
        // View transform (#71/#99/#107): model (logical) coords map to
        // device px as dpr·(F·model + T), where F = _viewScale (fit) and
        // T = view offset (centre). Clear + background fill cover the whole
        // bitmap under identity; the figure + ephemerals then draw under the
        // model→device transform. Headless dpr=1, F=1, T=0 → identity, so
        // node-canvas rendering (and every snapshot golden) is unchanged.
        const dpr = this._dpr(), F = this._viewScale;
        const tx = this._viewOffsetX, ty = this._viewOffsetY;
        // Keep decorations (labels, dots, line widths) a constant on-screen
        // size while the geometry is fit-scaled by F (#71). 1/F ≥ 1; F=1 → 1.
        GeomElement.styleScale = F > 0 ? 1 / F : 1;
        ctx.setTransform(1, 0, 0, 1, 0, 0);
        ctx.clearRect(0, 0, this._canvas.width, this._canvas.height);
        ctx.fillStyle = this._bgcolor;
        ctx.fillRect(0, 0, this._canvas.width, this._canvas.height);
        ctx.setTransform(dpr * F, 0, 0, dpr * F, dpr * tx, dpr * ty);
        // #140 — highlighted / mid-animation elements paint last within each
        // pass, so a later-declared neighbour can't chop the gold stroke
        // where it crosses. Identity when nothing is promoted.
        const order = this._highlightOnTop
            ? promotedDrawOrder(this._elements) : this._elements;
        for(let element of order) element.drawFace(this._canvas);
        for(let element of order) element.drawEdge(this._canvas);
        for(let element of order) element.drawVertex(this._canvas);
        // and then draw their names.
        for(let element of order)
            element.drawName(this._canvas);
        // Ephemeral animation helpers render on top of the permanent
        // figure (compass arms, guide circles, straightedge bars).
        // Same face → edge → vertex → name pass order so a guide arc
        // overlays correctly without re-clearing the canvas.
        for(let e of this._ephemerals) e.drawFace(this._canvas);
        for(let e of this._ephemerals) e.drawEdge(this._canvas);
        for(let e of this._ephemerals) e.drawVertex(this._canvas);
        for(let e of this._ephemerals) e.drawName(this._canvas);
        ctx.setTransform(1, 0, 0, 1, 0, 0);   // reset so nothing leaks
        GeomElement.styleScale = 1;
        this._dispatchHighlightChange();
    }
 
    // #108 — bidirectional highlight binding. After each real draw, if the
    // SET of highlighted elements (shouldHighlight || emphasized — the
    // boolean intent, ignoring the fading emphasisAmount) has changed,
    // emit a `geomlib:highlight` CustomEvent on the canvas so a consuming
    // page can light up every textual reference to those elements. The
    // event carries each element's canonical name + all its aliases. Fires
    // on any highlight source (prose-ref hover, slide `highlighted`) since
    // it lives in the shared redraw path; idempotent (no re-emit on an
    // unchanged set → no feedback loop). No-op when the canvas can't
    // dispatch DOM events (node-canvas in tests).
    private _dispatchHighlightChange() : void {
        const names: string[] = [];
        for (let elem of this._elements) {
            if (elem.name == null || elem.name.startsWith("screen")) continue;
            if (elem.shouldHighlight || elem.emphasized) names.push(elem.name);
        }
        const key = names.sort().join("|");
        if (key === this._lastHighlightedKey) return;
        this._lastHighlightedKey = key;
        const canvas: any = this._canvas;
        if (!canvas || typeof canvas.dispatchEvent !== "function"
            || typeof CustomEvent !== "function") return;
        const highlighted = names.map(n => ({ name: n, aliases: this.namesFor(n) }));
        canvas.dispatchEvent(new CustomEvent("geomlib:highlight", {
            bubbles: true,
            detail: { highlighted },
        }));
    }
 
    // ---- Diagnostics (#154) ------------------------------------------
    //
    // geomlib skips what it cannot resolve and keeps rendering, which is
    // the right behaviour — a deck with one bad name should still draw.
    // The cost is that the only evidence was a console line nobody has
    // open. Reporting through here records it too, so SlateControls can
    // badge the canvas and a consumer can read the list.
 
    /**
     * Record a diagnostic and mirror it to the console.
     *
     * Deduped per key, per slate: repeat reports bump a counter without
     * a second console line. That replaces two module-global dedupe
     * flags which silenced a warning for every OTHER figure on the page
     * once any one figure tripped them.
     */
    reportDiagnostic(input: IDiagnosticInput) : void {
        const rec = this._diagnostics.report(input, Date.now());
        if (rec == null) return;   // already reported on this slate
 
        if (typeof console !== "undefined") {
            const fn = rec.severity === "error" ? console.error : console.warn;
            if (fn) fn("[geomlib] " + rec.message);
        }
        this._dispatchDiagnostic(rec);
        this._notifyDiagnosticListeners();
    }
 
    /** Everything this slate has reported, oldest first. */
    get diagnostics() : IDiagnostic[] {
        return this._diagnostics.entries;
    }
 
    /** Worst severity reported, or null when the slate is clean. */
    get diagnosticSeverity() : DiagnosticSeverity | null {
        return this._diagnostics.severity;
    }
 
    /**
     * Forget everything reported so far.
     *
     * NOT called from reset(): minimize() and presentation-exit both run
     * reset() internally, and construction-time diagnostics never fire
     * again, so clearing there would silently discard exactly the
     * warnings worth keeping. SlateControls calls this from the explicit
     * reset control / `r` key instead — a deliberate viewer gesture.
     */
    clearDiagnostics() : void {
        this._diagnostics.clear();
        this._dispatchDiagnostic(null);
        this._notifyDiagnosticListeners();
    }
 
    /** Subscribe to diagnostic changes. Returns an unsubscribe fn. */
    onDiagnosticsChanged(listener: (s: Slate) => void) : () => void {
        this._diagnosticListeners.push(listener);
        return () => {
            const i = this._diagnosticListeners.indexOf(listener);
            if (i >= 0) this._diagnosticListeners.splice(i, 1);
        };
    }
 
    private _notifyDiagnosticListeners() : void {
        for (const l of this._diagnosticListeners.slice()) {
            try { l(this); } catch (_) { /* a listener must not break reporting */ }
        }
    }
 
    // Same capability guard as the geomlib:highlight dispatch above — a
    // node-canvas Canvas has no dispatchEvent, so the headless path
    // short-circuits here exactly as it does for highlights.
    private _dispatchDiagnostic(d: IDiagnostic | null) : void {
        const canvas: any = this._canvas;
        if (!canvas || typeof canvas.dispatchEvent !== "function"
            || typeof CustomEvent !== "function") return;
        canvas.dispatchEvent(new CustomEvent("geomlib:diagnostic", {
            bubbles: true,
            detail: diagnosticEventDetail(this._diagnostics, d),
        }));
    }
 
    // Update coordinates starting with element[i+1], matching Java's
    // Slate.java updateCoordinates (lines 808-814). Only elements AFTER
    // the picked element are recomputed — elements before it are left
    // untouched to avoid reprojection artifacts after rotation.
    updateCoordinates(i : number) {
        for(++i; i < this._elements.length; i++) {
            if(!this._elements[i].defined())
                this._elements[i].reset();
            this._elements[i].update();
        }
        this.drawElements();
    }
 
    translateCoordinates(dx : number, dy: number) {
        // translate all non-preexisting elements
        // (Java: Slate.java line 819 — if (!preexists[i]) element[i].translate(...))
        for(let i = 0; i < this._elements.length; i++) {
            let elem = this._elements[i];
            if (!elem.preexists)
                elem.translate(dx, dy);
        }
        this.update();
    }
 
    closestVisiblePoint(elements : GeomElement[], p : PointElement, tolerance : number = 30) : PointElement {
        // Use 2D screen distance (x,y only, ignoring z) for picking,
        // matching the Java applet's movePick() at Slate.java:887-889.
        // The canvas is a 2D projection; a 3D point at (x,y,z) renders
        // at screen position (x,y) regardless of z.
        let screenDist2 = (a: PointElement, b: PointElement) =>
            (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
        let sortedDistanceElements = elements
            .filter(e => e instanceof PointElement)
            .map(e => e as PointElement)
            .filter(e => e.vertexColor != null)
            .sort((a,b) => {
                let adcp = screenDist2(a, p);
                let bdcp = screenDist2(b, p);
                if(adcp < bdcp) {
                    return -1;
                } else if (bdcp < adcp) {
                    return 1;
                }
                return 0;
            });
        if (sortedDistanceElements.length == 0) return null;
        let bestDistPoint = sortedDistanceElements[0];
        if(Math.sqrt(screenDist2(bestDistPoint, p)) > tolerance) return null;
        return bestDistPoint;
    }
 
    private _getPick(c: number, d: number) : PointElement {
        if (this._pick != null) return this._pick;
        let currentPoint = new PointElement({x:c,y:d});
        let closestVisiblePoint = this.closestVisiblePoint(this._elements, currentPoint);
        if(closestVisiblePoint == null) return;
        this._pick = closestVisiblePoint;
        return this._pick;
    }
 
    // Set the pivot point for scene rotation.
    // Format: "pointName" or "pointName,planeName"
    // Without a plane, the pivot is set on the screen plane.
    // With a plane, the pivot is set on that plane (for 3D rotation).
    // (Java: Slate.java setPivot, lines 787-801)
    setPivot(param : string) : void {
        let parts = param.split(",");
        let pointName = parts[0].trim();
        let e : GeomElement = this.lookupElement(pointName);
        if (e == null || !(e instanceof PointElement)) return;
        let piv : PointElement = e as PointElement;
 
        if (parts.length > 1) {
            // "A,xyplane" — set pivot on a specific plane
            let planeName = parts[1].trim();
            let p : GeomElement = this.lookupElement(planeName);
            if (p == null || !(p instanceof PlaneElement)) return;
            (p as PlaneElement).pivot = piv;
        } else {
            // "A" — set pivot on screen plane
            piv._AP = this._screen;
            this._screen.pivot = piv;
        }
    }
 
    movePick(c: number, d: number) : void {
        if(this._getPick(c, d) == null) return;
        let picki = this._elements.indexOf(this._pick);
 
        // Clamp the drag to the on-screen region in MODEL coords. Under the
        // view transform F·model + T, the display spans model
        // [−T/F, (display − T)/F] — so the fit-scale (#71) and centre offset
        // (#99/#107) both shift/scale the bounds. F=1, T=0 → [0, display].
        const F = this._viewScale || 1;
        let ox = this._viewOffsetX, oy = this._viewOffsetY;
        let loX = -ox / F, hiX = (this.displayWidth - ox) / F;
        if (c < loX) c = loX; else if (c > hiX) c = hiX;
        let loY = -oy / F, hiY = (this.displayHeight - oy) / F;
        if (d < loY) d = loY; else if (d > hiY) d = hiY;
        if (Math.abs(c - this._pick.x) + Math.abs(d - this._pick.y) < 1.0) {
            return; // no motion
        }
        if ( this._pick.draggable ) {
            if(this._pick.drag(c,d)) {
                this.updateCoordinates(picki);
            } else {
                return;
            }
        } else if (this._pick.AP != null 
          && this._pick.AP.pivot != null
          && this._pick.AP.pivot != this._pick) { // rotate around the pivot
              this.rotateCoordinates(c,d);
        } else {
            let dx = c - this._pick.x;
            let dy = d - this._pick.y;
            this.translateCoordinates(dx,dy);
        }
        this.drawElements();
    }
 
    rotateCoordinates(c : number, d : number) : void {
      // rotate space according to how pick goes around pivot in the plane
      let pick : PointElement = this._pick;
      let piv : PointElement = pick.AP.pivot;
      // compute old and new pick's 3D coordinates relative to the pivot
      let oldP : PointElement = PointElement.difference(pick,piv);
      let newx : number = c-piv.x;
      let newy : number = d-piv.y;		//(newz is irrelevant)
      // find their 2D coordinates on the plane
      let S : PointElement = pick.AP.S;
      let T : PointElement = pick.AP.T;
      let olds : number = PointElement.dot(oldP,S);
      let oldt : number = PointElement.dot(oldP,T);
      let den  : number = S.x * T.y - S.y * T.x;
      let news : number = (newx*T.y - newy*T.x)/den;
      let newt : number = (newy*S.x - newx*S.y)/den;
      // compute the scale&rotation factors
      den = olds*olds + oldt*oldt;
      let ac : number = (news*olds + newt*oldt)/den;
      let as : number = (newt*olds - news*oldt)/den;
      // Rotate all non-preexisting elements directly.
      // (Java: Slate.java line 843-845)
      for (let elem of this._elements) {
          if (!elem.preexists)
              elem.rotate(piv, ac, as);
      }
      // After direct rotation, recompute derived elements from their (now
      // rotated) parents. Java doesn't need this because element[] carries
      // duplicate entries for shared objects — a Layoff created by
      // line;extend and re-referenced by point;last appears at two indices,
      // one with preexists=false that gets rotated directly. TS dedupes
      // _elements, so a shared object marked preexists=true is skipped,
      // which means Layoff-style points backed by bare LineElement wrappers
      // would never get moved directly (bare LineElement.rotate is a no-op).
      // Running update() rebuilds them from their (rotated) parents, which
      // produces the same result because rotation is a linear operation
      // and Layoff.update() derives its position from parent coords.
      this.update();
    }
 
}