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 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 | 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 3x 3x 3x 3x 3x 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 6x 6x 6x 2x 2x 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 11x 11x 11x 11x 11x 11x 14x 9x 9x 14x 14x 14x 14x 14x 14x 14x 11x 7x 7x 11x 11x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 5x 5x 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 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 1x 1x 1x 1x 1x 1x 1x | /*----------------------------------------------------------------------+
| Title: SlateControls.ts |
| UI overlay for geometry diagrams: reset, maximize, present. |
| Buttons are canvas-drawn icons overlaid on each Slate canvas. |
| Keyboard shortcuts: |
| r / space → reset |
| m → maximize/minimize |
| p → present (when the slate has slides) |
| Escape → leave presentation, then leave the maximized view |
| (one layer per press; see escapeAction) |
+----------------------------------------------------------------------*/
import {Slate} from "./Slate";
import {computeSlideState} from "./slideshow";
import {ISlideJust} from "./index";
import {DiagnosticSeverity} from "./Diagnostics";
interface IInitConfig {
background: string;
title: string;
align?: number;
canvasid?: string;
pivot?: string;
elements: any[];
}
// Slate-controls icon sizing. The historical default is 20 px; touch
// devices (coarse pointer) get a slightly tighter 15-px row per author
// preference — small enough to stay out of the diagram, still hittable
// with a fingertip.
function isCoarsePointer(): boolean {
return typeof window !== "undefined"
&& typeof window.matchMedia === "function"
&& window.matchMedia("(pointer: coarse)").matches;
}
const BTN_SIZE = isCoarsePointer() ? 18 : 20;
const BTN_GAP = isCoarsePointer() ? 4 : 4;
const BTN_MARGIN = isCoarsePointer() ? 7 : 8;
// #69 — keep the control buttons hidden until the canvas/wrapper is
// hovered or focused, so a page full of diagrams isn't cluttered with a
// button row on every one. CSS-only (`:hover` / `:focus-within`), no
// per-frame JS; `opacity` keeps the buttons in the layout (no reflow) and
// fades them. Injected once per document. Touch users tap the canvas
// first, which focuses it and reveals the controls.
const CONTROLS_STYLE_ID = "geomlib-controls-style";
function ensureControlsStyle(): void {
if (typeof document === "undefined") return;
if (document.getElementById(CONTROLS_STYLE_ID)) return;
const style = document.createElement("style");
style.id = CONTROLS_STYLE_ID;
style.textContent =
".geomlib-wrapper>button{opacity:0;transition:opacity .15s ease-in-out}" +
".geomlib-wrapper:hover>button,.geomlib-wrapper:focus-within>button{opacity:1}";
(document.head || document.documentElement).appendChild(style);
}
// Minimal event-target interface so this helper is testable without a real
// `Window`. Browser `Window`, `Document`, and `Element` all satisfy it.
export interface IResizeTarget {
addEventListener(type: "resize", listener: () => void): void;
removeEventListener(type: "resize", listener: () => void): void;
}
/**
* Attach `callback` to the target's "resize" event. Returns a teardown
* function that removes the listener — call it to stop tracking.
*/
export function trackWindowResize(target: IResizeTarget, callback: () => void): () => void {
const handler = () => callback();
target.addEventListener("resize", handler);
return () => target.removeEventListener("resize", handler);
}
// #139 — what one Escape press should do. Escape peels off ONE layer of
// takeover at a time: a slideshow exits to the maximized view it opened in
// (#115 — the viewer keeps their manipulation), and a second press leaves
// the maximized view. Pure so it is testable without a DOM.
export type EscapeAction = "exitPresent" | "minimize" | "none";
export function escapeAction(state: {presenting: boolean; maximized: boolean}): EscapeAction {
if (state.presenting) return "exitPresent";
if (state.maximized) return "minimize";
return "none";
}
// One piece of a rendered caption: literal text, or a reference that binds
// `display` to the slate element named `target`.
export type CaptionToken =
| { kind: "text"; text: string }
| { kind: "ref"; display: string; target: string };
// Caption token grammar.
//
// {AB} — display and element name are the same
// {ABC|angBint} — display override (#90): render "ABC" but
// bind to angBint, for prose names that
// collide with another element's name
// {the side from D to A|sideDA}
// — #136: the DISPLAY half may be a phrase.
//
// The two halves have deliberately different grammars. The element name
// stays strict (identifier-ish), because it has to resolve against the
// slate. The display half is anything up to the closing brace — it is
// prose, and restricting it to a single word forced authors to bind one
// keyword inside a phrase (`the {side|sideDA} from D to A`) when what they
// meant was to light the whole phrase.
const CAPTION_NAME = "[A-Za-z][A-Za-z0-9'\\-]*";
const CAPTION_RE = new RegExp(
"\\{([^{}|]+)\\|(" + CAPTION_NAME + ")\\}" + // {display|element}
"|\\{(" + CAPTION_NAME + ")\\}", // {element}
"g");
export function parseCaptionTokens(text: string): CaptionToken[] {
const out: CaptionToken[] = [];
const re = new RegExp(CAPTION_RE.source, "g");
let lastIndex = 0;
let m: RegExpExecArray | null;
while ((m = re.exec(text)) !== null) {
if (m.index > lastIndex) {
out.push({ kind: "text", text: text.slice(lastIndex, m.index) });
}
// Piped form fills groups 1-2; bare form fills group 3.
const piped = m[2] != null;
const display = piped ? m[1] : m[3];
const target = piped ? m[2] : m[3];
out.push({ kind: "ref", display: display, target: target });
lastIndex = m.index + m[0].length;
}
if (lastIndex < text.length) {
out.push({ kind: "text", text: text.slice(lastIndex) });
}
return out;
}
// #146 — how a slide's justification panel should lay out.
//
// A bare citation ("I.15") is a chip; several of them read fine as one
// dot-separated row. A claim turns each entry into a statement
// ("angle EGB = angle AGH — I.15"), and statements do not read as a row —
// they want a line each. Mixed slides stack too: an entry without a claim
// still belongs on its own line next to ones that have them.
//
// Pure so the layout rule is testable without a DOM.
export function justsUseStackedLayout(justs: ISlideJust[] | null | undefined): boolean {
if (justs == null) return false;
return justs.some((j) => j.claim != null && j.claim !== "");
}
export function createControls(slate: Slate, canvas: HTMLCanvasElement, config: IInitConfig): void {
// Skip in headless/test environments
if (!canvas.parentElement) return;
let controls = new SlateControls(slate, canvas, config);
controls.init();
}
class SlateControls {
private _slate: Slate;
private _canvas: HTMLCanvasElement;
private _config: IInitConfig;
private _wrapper: HTMLDivElement;
private _maximized: boolean = false;
private _savedStyles: {
wrapperPosition: string;
wrapperTop: string;
wrapperLeft: string;
wrapperWidth: string;
wrapperHeight: string;
wrapperZIndex: string;
wrapperBackground: string;
canvasStyleWidth: string;
canvasStyleHeight: string;
canvasAttrWidth: number;
canvasAttrHeight: number;
} = null;
private _stopTrackingResize: (() => void) | null = null;
// #154 — lazy: a clean diagram adds no DOM at all.
private _badge: HTMLDivElement | null = null;
private _badgeSeverity: DiagnosticSeverity | null = null;
private _stopDiagnostics: (() => void) | null = null;
private _maximizedOnKey: ((e: KeyboardEvent) => void) | null = null;
// Presentation-mode (slideshow) state. Caption + nav float over
// the maximized canvas; no fullscreen overlay, no background takeover.
private _presenting: boolean = false;
private _presentationIndex: number = 0;
private _presentationOverlay: HTMLDivElement | null = null;
private _presentationCaption: HTMLDivElement | null = null;
private _presentationJusts: HTMLDivElement | null = null;
private _presentationCounter: HTMLSpanElement | null = null;
private _presentationOnKey: ((e: KeyboardEvent) => void) | null = null;
// Single-pin sticky for caption {NAME} refs: only one element may be
// emphasized at a time. Clicking another ref clears the prior pin
// before stickying the new one; hover is suppressed while a pin is
// active so the sticky element stays the dominant one.
private _stickyRefSpan: HTMLSpanElement | null = null;
private _stickyRefElem: import("./elements/GeomElement").GeomElement | null = null;
constructor(slate: Slate, canvas: HTMLCanvasElement, config: IInitConfig) {
this._slate = slate;
this._canvas = canvas;
this._config = config;
}
init(): void {
this._wrapper = this.createWrapper();
this.createButtons();
this.addKeyboardShortcuts();
// Track window resize for the lifetime of the slate (#71): an inline
// responsive canvas (`max-width:100%`) must re-fit on orientation
// flip / column resize, not only while maximized. resizeAndRedraw
// re-syncs the bitmap, recomputes the fit-scale, and redraws.
this._stopTrackingResize = trackWindowResize(window, () => this.resizeAndRedraw());
// #154 — subscribe, then paint once. The second call matters:
// createControls is the LAST statement of initInner, so anything
// reported while the figure was being built is already in the log
// by the time this badge exists.
this._stopDiagnostics = this._slate.onDiagnosticsChanged(() => this.updateBadge());
this.updateBadge();
}
// Reconcile the badge with the slate's worst severity. Creates the
// node on first need, hides it when the slate is clean, and repaints
// in place when a warning escalates to an error.
private updateBadge(): void {
const severity = this._slate.diagnosticSeverity;
if (severity == null) {
if (this._badge) this._badge.style.display = "none";
this._badgeSeverity = null;
return;
}
if (!this._badge) {
this._badge = createDiagnosticBadge(severity);
this._badge.style.position = "absolute";
// Top-LEFT: the control buttons use style.right, and the
// presentation overlay is fixed to the bottom of the viewport,
// so nothing else claims this corner. Absolute on the wrapper
// means maximize/presentation carry it along for free.
this._badge.style.top = BTN_MARGIN + "px";
this._badge.style.left = BTN_MARGIN + "px";
this._badge.style.zIndex = "1";
this._wrapper.appendChild(this._badge);
this._badgeSeverity = severity;
return;
}
this._badge.style.display = "";
if (severity !== this._badgeSeverity) {
paintDiagnosticBadge(this._badge, severity);
this._badgeSeverity = severity;
}
}
private createWrapper(): HTMLDivElement {
ensureControlsStyle(); // #69 — hover/focus reveal stylesheet (once)
let wrapper = document.createElement("div");
wrapper.className = "geomlib-wrapper";
wrapper.style.position = "relative";
wrapper.style.display = "inline-block";
// Insert wrapper before canvas, then move canvas into it
this._canvas.parentElement.insertBefore(wrapper, this._canvas);
wrapper.appendChild(this._canvas);
// Make canvas focusable for keyboard shortcuts
this._canvas.setAttribute("tabindex", "0");
this._canvas.style.outline = "none"; // default no outline
this._canvas.addEventListener("focus", () => {
this._canvas.style.outline = "2px solid rgba(66,133,244,0.5)";
});
this._canvas.addEventListener("blur", () => {
this._canvas.style.outline = "none";
});
return wrapper;
}
private createButtons(): void {
let buttons = [
{ draw: drawResetIcon, action: () => this.onReset(), title: "Reset (r)" },
{ draw: drawMaximizeIcon, action: () => this.onMaximize(), title: "Maximize (m)" },
];
// Conditionally append a "▶ Present" button when the slate
// carries any slides. Slates without slides see the same
// three-button row as before — backwards-compatible.
if (this._slate.slides.length > 0) {
buttons.push({
draw: drawPresentIcon,
action: () => this.onPresent(),
title: "Present (p)",
});
}
for (let i = 0; i < buttons.length; i++) {
let btn = document.createElement("button");
btn.style.position = "absolute";
btn.style.top = BTN_MARGIN + "px";
btn.style.right = (BTN_MARGIN + i * (BTN_SIZE + BTN_GAP)) + "px";
btn.style.width = BTN_SIZE + "px";
btn.style.height = BTN_SIZE + "px";
btn.style.padding = "0";
btn.style.border = "none";
btn.style.cursor = "pointer";
btn.style.background = "rgba(0,0,0,0.12)";
btn.style.borderRadius = "3px";
btn.title = buttons[i].title;
btn.addEventListener("mouseenter", () => {
btn.style.background = "rgba(0,0,0,0.3)";
});
btn.addEventListener("mouseleave", () => {
btn.style.background = "rgba(0,0,0,0.12)";
});
// Draw icon on a small canvas inside the button
let iconCanvas = document.createElement("canvas");
iconCanvas.width = BTN_SIZE;
iconCanvas.height = BTN_SIZE;
iconCanvas.style.display = "block";
let ctx = iconCanvas.getContext("2d");
buttons[i].draw(ctx, BTN_SIZE);
btn.appendChild(iconCanvas);
btn.addEventListener("click", (e) => {
e.stopPropagation();
buttons[i].action();
// Return focus to the main canvas after button click
this._canvas.focus();
});
// Store reference for maximize icon swap
if (i === 1) {
(this as any)._maxBtn = btn;
(this as any)._maxIconCanvas = iconCanvas;
}
this._wrapper.appendChild(btn);
}
}
private addKeyboardShortcuts(): void {
this._canvas.addEventListener("keydown", (e: KeyboardEvent) => {
switch (e.key) {
case "r":
case "R":
case " ":
e.preventDefault();
this.onReset();
break;
case "m":
case "M":
e.preventDefault();
this.onMaximize();
break;
case "p":
case "P":
if (this._slate.slides.length > 0) {
e.preventDefault();
this.onPresent();
}
break;
}
});
}
private onReset(): void {
this._slate.reset();
// #154 — the badge latches until the viewer explicitly resets.
// Cleared HERE rather than inside Slate.reset() on purpose:
// minimize() and presentation-exit both call reset() internally,
// and construction-time diagnostics never fire again, so clearing
// there would quietly discard the warnings most worth keeping.
this._slate.clearDiagnostics();
}
private onMaximize(): void {
if (this._maximized) {
this.minimize();
} else {
this.maximize();
}
}
private maximize(): void {
// Save current styles AND canvas width/height attributes. The attrs
// are the canvas bitmap resolution; resizeAndRedraw() overwrites them
// to match the maximized CSS size, so minimize() must restore them.
this._savedStyles = {
wrapperPosition: this._wrapper.style.position,
wrapperTop: this._wrapper.style.top,
wrapperLeft: this._wrapper.style.left,
wrapperWidth: this._wrapper.style.width,
wrapperHeight: this._wrapper.style.height,
wrapperZIndex: this._wrapper.style.zIndex,
wrapperBackground: this._wrapper.style.background,
canvasStyleWidth: this._canvas.style.width,
canvasStyleHeight: this._canvas.style.height,
canvasAttrWidth: this._canvas.width,
canvasAttrHeight: this._canvas.height,
};
// Maximize wrapper to fill viewport
this._wrapper.style.position = "fixed";
this._wrapper.style.top = "0";
this._wrapper.style.left = "0";
this._wrapper.style.width = "100vw";
this._wrapper.style.height = "100vh";
this._wrapper.style.zIndex = "9999";
this._wrapper.style.background = "white";
// Expand canvas to fill wrapper
this._canvas.style.width = "100%";
this._canvas.style.height = "100%";
// Set the flag BEFORE resizeAndRedraw so its centring pass (#107)
// sees the maximized state and slides the figure to centre.
this._maximized = true;
this.resizeAndRedraw();
this.updateMaximizeIcon();
this.bindMaximizedKeys();
// Window-resize tracking is persistent (set up in init), so the
// maximized 100vw × 100vh canvas already re-syncs on viewport change.
}
// #139 — Escape while the canvas owns the viewport. Bound on `document`,
// not the canvas: the r/m/p shortcuts only fire when the canvas itself
// has focus, and maximizing by CLICKING the control leaves focus on the
// button, so a canvas-bound Escape would never fire.
//
// This handler is the single owner of Escape (bindPresentationKeys
// deliberately does not bind it). Two document-level handlers both
// matching Escape would fire on one keypress and blow through
// presentation AND maximize at once. onPresent() maximizes first, so
// presenting implies maximized — this handler is always bound while a
// slideshow is running, and escapeAction() decides which layer to peel.
private bindMaximizedKeys(): void {
if (this._maximizedOnKey) return;
const handler = (e: KeyboardEvent) => {
if (e.key !== "Escape") return;
switch (escapeAction({presenting: this._presenting, maximized: this._maximized})) {
case "exitPresent":
e.preventDefault();
this.exitPresent();
break;
case "minimize":
e.preventDefault();
this.minimize();
break;
}
};
this._maximizedOnKey = handler;
document.addEventListener("keydown", handler);
}
private unbindMaximizedKeys(): void {
if (this._maximizedOnKey) {
document.removeEventListener("keydown", this._maximizedOnKey);
this._maximizedOnKey = null;
}
}
private minimize(): void {
if (!this._savedStyles) return;
// Resize tracking stays active (persistent since init) so the inline
// canvas keeps re-fitting after we restore it.
// Restore saved styles
this._wrapper.style.position = this._savedStyles.wrapperPosition;
this._wrapper.style.top = this._savedStyles.wrapperTop;
this._wrapper.style.left = this._savedStyles.wrapperLeft;
this._wrapper.style.width = this._savedStyles.wrapperWidth;
this._wrapper.style.height = this._savedStyles.wrapperHeight;
this._wrapper.style.zIndex = this._savedStyles.wrapperZIndex;
this._wrapper.style.background = this._savedStyles.wrapperBackground;
this._canvas.style.width = this._savedStyles.canvasStyleWidth;
this._canvas.style.height = this._savedStyles.canvasStyleHeight;
// Restore the canvas bitmap resolution to match its pre-maximize
// intrinsic size. Without this, the canvas keeps the enlarged
// width/height attributes from the maximize pass and (when style
// width/height are empty) falls back to those attrs for its CSS
// size — leaving the diagram rendered full-width.
this._canvas.width = this._savedStyles.canvasAttrWidth;
this._canvas.height = this._savedStyles.canvasAttrHeight;
// Clear the flag BEFORE resizeAndRedraw so its centring pass (#107)
// restores the figure to its authored position.
this._maximized = false;
this.unbindMaximizedKeys();
this.resizeAndRedraw();
this._savedStyles = null;
this.updateMaximizeIcon();
// Exiting the maximized view also resets the construction to its
// authored state — same as the reset (r) control — so any dragging
// done while maximized / presenting is undone on the way out (#107).
this._slate.reset();
}
private resizeAndRedraw(): void {
// Sync the canvas BITMAP to CSS size × dpr so it stays crisp on
// HiDPI and after CSS scaling (#71). drawElements scales the context
// by dpr; coordinates stay in CSS px.
const dpr = (typeof window !== "undefined" && window.devicePixelRatio) || 1;
let w = Math.round(this._canvas.clientWidth * dpr);
let h = Math.round(this._canvas.clientHeight * dpr);
if (this._canvas.width !== w || this._canvas.height !== h) {
this._canvas.width = w;
this._canvas.height = h;
}
this._slate.recomputeFitScale(); // #71 — F = min(1, display/logical)
this._applyCentring();
this._slate.update();
}
// #107 — when maximized (via the control or presentation), slide the
// whole figure to the centre of the enlarged canvas; when not maximized,
// restore the authored position (translate 0; the fit-scale handles
// responsive sizing). Recomputed on every resize so it stays centred as
// the window changes. figureBounds() is in logical coords, so the centre
// target is computed in display px through the fit-scale F (#71).
private _applyCentring(): void {
if (!this._maximized) {
this._slate.clearViewOffset();
this._slate.clearCentringBounds();
return;
}
// #138 — measure once per maximized session, from elements that
// actually paint. Once, because a resize mid-walk must re-centre on
// the same box the walk started from rather than on whatever the
// current slide happens to reveal; painting elements only, because a
// zero-colour off-screen helper would otherwise define the frame and
// shove the figure off-canvas (I.35, I.42, I.43 all hit this).
const b = this._slate.centringBounds != null
? this._slate.centringBounds : this._slate.captureCentringBounds();
if (b == null) { this._slate.clearViewOffset(); return; }
const cx = (b.minX + b.maxX) / 2, cy = (b.minY + b.maxY) / 2;
const F = this._slate.viewScale;
// T (display px) so F·figureCentre + T lands at the display centre.
this._slate.setViewOffset(this._slate.displayWidth / 2 - F * cx,
this._slate.displayHeight / 2 - F * cy);
}
private updateMaximizeIcon(): void {
let iconCanvas = (this as any)._maxIconCanvas as HTMLCanvasElement;
let btn = (this as any)._maxBtn as HTMLButtonElement;
if (!iconCanvas) return;
let ctx = iconCanvas.getContext("2d");
ctx.clearRect(0, 0, BTN_SIZE, BTN_SIZE);
if (this._maximized) {
drawMinimizeIcon(ctx, BTN_SIZE);
btn.title = "Minimize (m)";
} else {
drawMaximizeIcon(ctx, BTN_SIZE);
btn.title = "Maximize (m)";
}
}
// --- Presentation mode (slideshow) ---
//
// Opens the slate's slides as a step-through. The canvas itself
// takes over the viewport via the existing maximize state (no
// separate fullscreen overlay, no black takeover) — the caption
// and nav controls float on top of the maximized canvas.
private onPresent(): void {
if (this._presenting) return;
if (this._slate.slides.length === 0) return;
if (!this._maximized) this.maximize();
// #114 — centre ONCE on entering presentation (covers the case
// where we were already maximized, so maximize() didn't run). Slide
// advances no longer re-centre. #138 — drop any box captured by an
// earlier plain maximize so present-enter measures the figure as it
// stands now.
this._slate.clearCentringBounds();
this._applyCentring();
this.buildPresentationOverlay();
this.bindPresentationKeys();
this._presenting = true;
this.showSlide(0);
}
private exitPresent(): void {
if (!this._presenting) return;
this.unbindPresentationKeys();
this.clearStickyRef();
this.removePresentationOverlay();
// #115 — leave presentation mode but STAY in the maximized view,
// keeping the viewer's manipulation. Only the maximize control
// un-maximizes (and that's what resets — see minimize()). Drop the
// parked case-variant ephemerals + the walk's highlight/visibility
// state, but keep the current view offset (the centre set on enter)
// and any dragged positions.
if (this._slate.animator != null) this._slate.animator.cancel();
this._slate.clearEphemerals();
this._slate.clearVisibility();
for (let e of this._slate.elements) {
e.shouldHighlight = false;
e.emphasized = false;
}
this._presenting = false;
this._slate.update();
}
private buildPresentationOverlay(): void {
// Floating UI panel pinned to the bottom of the viewport.
// Uses position: fixed and attaches to documentElement so it
// can't be clipped by any wrapper, transformed parent, or
// scrollable container — works the same on iOS Safari,
// Samsung Internet, Android Chrome, and desktop. Solid white
// (no transparency) so it's never mistaken for background;
// bright box-shadow so the boundary reads.
const overlay = document.createElement("div");
overlay.className = "geomlib-presentation-overlay";
overlay.style.position = "fixed";
overlay.style.left = "0";
overlay.style.right = "0";
overlay.style.bottom = "0";
overlay.style.margin = "0 auto";
// Span the bottom of the viewport so the centred contents
// can't render off-screen and there's nowhere for the panel
// to "hide". The actual visible chrome is the inner column.
overlay.style.maxWidth = "100vw";
overlay.style.background = "#ffffff";
overlay.style.color = "#222";
overlay.style.borderTop = "1px solid rgba(0,0,0,0.18)";
overlay.style.boxShadow = "0 -6px 18px rgba(0,0,0,0.18)";
overlay.style.padding = "12px 16px calc(12px + env(safe-area-inset-bottom, 0px))";
// Max possible z-index so nothing can stack above us.
overlay.style.zIndex = "2147483647";
overlay.style.fontFamily = "Georgia, 'Times New Roman', serif";
overlay.style.touchAction = "manipulation";
overlay.style.boxSizing = "border-box";
overlay.style.textAlign = "center";
const caption = document.createElement("div");
caption.className = "geomlib-presentation-caption";
caption.style.fontSize = "1.1rem";
caption.style.lineHeight = "1.45";
caption.style.textAlign = "center";
caption.style.marginBottom = "6px";
overlay.appendChild(caption);
const justs = document.createElement("div");
justs.className = "geomlib-presentation-justs";
justs.style.fontSize = "0.85rem";
justs.style.textAlign = "center";
justs.style.opacity = "0.85";
justs.style.minHeight = "1em";
justs.style.marginBottom = "10px";
overlay.appendChild(justs);
const nav = document.createElement("div");
nav.className = "geomlib-presentation-nav";
nav.style.display = "flex";
nav.style.gap = "10px";
nav.style.alignItems = "center";
nav.style.justifyContent = "center";
// #142 — these glyphs must not reach the bundle as raw UTF-8: a
// consumer page without <meta charset> decodes our <script src>
// using its own encoding and geomlib's controls turn to mojibake.
// Escaping them HERE does not help (tsc decodes the escape, the
// minifier re-normalises it) — it is enforced at emit by
// `ascii_only` in webpack.config.js, and checked by
// scripts/check-bundle-ascii.js at publish time.
const prevBtn = makePresentationButton("‹ Prev");
const counter = document.createElement("span");
counter.className = "geomlib-presentation-counter";
counter.style.minWidth = "56px";
counter.style.textAlign = "center";
counter.style.opacity = "0.7";
counter.style.fontSize = "0.9rem";
const nextBtn = makePresentationButton("Next ›");
const exitBtn = makePresentationButton("✕ Exit");
exitBtn.style.marginLeft = "16px";
prevBtn.addEventListener("click", (e) => {
e.stopPropagation();
this.showSlide(this._presentationIndex - 1);
});
nextBtn.addEventListener("click", (e) => {
e.stopPropagation();
this.showSlide(this._presentationIndex + 1);
});
exitBtn.addEventListener("click", (e) => {
e.stopPropagation();
this.exitPresent();
});
nav.appendChild(prevBtn);
nav.appendChild(counter);
nav.appendChild(nextBtn);
nav.appendChild(exitBtn);
overlay.appendChild(nav);
// Append to documentElement (html) — outside body — so a
// mobile browser's odd body sizing can't clip us.
document.documentElement.appendChild(overlay);
this._presentationOverlay = overlay;
this._presentationCaption = caption;
this._presentationJusts = justs;
this._presentationCounter = counter;
}
private removePresentationOverlay(): void {
if (this._presentationOverlay && this._presentationOverlay.parentNode) {
this._presentationOverlay.parentNode.removeChild(this._presentationOverlay);
}
this._presentationOverlay = null;
this._presentationCaption = null;
this._presentationJusts = null;
this._presentationCounter = null;
}
private bindPresentationKeys(): void {
const handler = (e: KeyboardEvent) => {
if (!this._presenting) return;
switch (e.key) {
case "ArrowLeft":
e.preventDefault();
this.showSlide(this._presentationIndex - 1);
break;
case "ArrowRight":
case " ":
e.preventDefault();
this.showSlide(this._presentationIndex + 1);
break;
// Escape is NOT bound here — the maximized-state handler
// (bindMaximizedKeys, #139) owns it and routes the first
// press to exitPresent(). Binding it in both places would
// exit presentation and minimize on a single keypress.
}
};
this._presentationOnKey = handler;
document.addEventListener("keydown", handler);
}
private unbindPresentationKeys(): void {
if (this._presentationOnKey) {
document.removeEventListener("keydown", this._presentationOnKey);
this._presentationOnKey = null;
}
}
private showSlide(index: number): void {
const slides = this._slate.slides;
if (slides.length === 0) return;
const clamped = Math.max(0, Math.min(slides.length - 1, index));
this._presentationIndex = clamped;
const state = computeSlideState(this._slate, slides, clamped);
// Drop any sticky pin from the previous slide — its span
// belongs to the prior caption and is about to be replaced
// anyway.
this.clearStickyRef();
// Wipe any ephemerals a prior slide left parked (e.g. a
// translateAside case-variant clone that persisted for its
// slide). The animated branch's animateTo→cancel also clears
// ephemerals, but an instant transition wouldn't — so clear
// here on every advance.
this._slate.clearEphemerals();
// #114 — do NOT re-centre per slide; the figure is centred once on
// enter (onPresent / maximize) and stays put so a viewer dragging
// mid-walk isn't yanked back. An autoPlace transition (#99) still
// eases from the current (centred) offset out to its case layout.
const slide = slides[clamped];
const transition = slide.transition;
const animations = transition && transition.animations;
const mode: "cascade" | "parallel" =
(transition && transition.mode === "parallel") ? "parallel" : "cascade";
// Clear per-element emphasis on every transition so a previous
// slide's hover state doesn't carry forward.
for (let e of this._slate.elements) e.emphasized = false;
if (animations && animations.length > 0) {
// Animated transition — slate.animateTo flips visibility +
// highlight per-element and runs the configured Animation
// for each entry. Newly-visible elements without an entry
// pop in instantly (handled inside animateTo).
this._slate.animateTo(state.visible, state.highlighted, animations, mode);
} else {
// Instant transition (4b behaviour) — flip every element
// synchronously, no rAF loop.
for (let e of this._slate.elements) {
if (e.name == null) continue;
e.visible = state.visible.has(e.name);
e.shouldHighlight = state.highlighted.has(e.name);
e.drawProgress = 1;
}
this._slate.update();
}
if (this._presentationCaption) {
this.renderCaptionText(this._presentationCaption, slide.text || "");
}
if (this._presentationCounter) {
this._presentationCounter.textContent = (clamped + 1) + " / " + slides.length;
}
if (this._presentationJusts) {
this._presentationJusts.innerHTML = "";
const resolve = this._slate.resolveJustification;
const justs = slide.justifications;
if (justs) {
// #146 — a claim makes each entry a statement rather than a
// bare citation, and statements don't read as a row joined
// by dots. So: any claim on the slide and every entry gets
// its own line; no claims and the panel is the same
// dot-separated row it has always been.
const stacked = justsUseStackedLayout(justs);
for (let i = 0; i < justs.length; i++) {
const host = stacked
? document.createElement("div") : this._presentationJusts;
if (!stacked && i > 0) {
const sep = document.createElement("span");
sep.textContent = " · ";
sep.style.opacity = "0.5";
host.appendChild(sep);
}
const claim = justs[i].claim;
if (claim != null && claim !== "") {
const c = document.createElement("span");
c.textContent = claim;
host.appendChild(c);
const dash = document.createElement("span");
dash.textContent = " — ";
dash.style.opacity = "0.5";
host.appendChild(dash);
}
const ref = justs[i].ref;
const url = resolve ? resolve(ref) : null;
if (url) {
const a = document.createElement("a");
a.href = url;
a.textContent = ref;
a.target = "_blank";
a.rel = "noopener";
a.style.color = "#0066cc";
host.appendChild(a);
} else {
const span = document.createElement("span");
span.textContent = ref;
host.appendChild(span);
}
if (stacked) this._presentationJusts.appendChild(host);
}
}
}
}
// Parse {NAME} tokens in caption text and emit interactive bold-
// italic spans for any that resolve to a slate element (alias
// lookups included). Unknown / unresolved refs render as plain
// text with the braces stripped. Hover / pointerdown toggles
// element.emphasized so the figure picks the ref up with an
// extra-thick gold stroke.
//
// Display override (#90): {DISPLAY|element} renders DISPLAY but
// binds the highlight to `element` — for prose names that collide
// with another element's name, e.g. "the angle {ABC|angBint}"
// where the bare token would resolve to the triangle ABC. Since #136
// the DISPLAY half may be a whole phrase; see parseCaptionTokens.
private renderCaptionText(host: HTMLElement, text: string): void {
host.innerHTML = "";
for (const tok of parseCaptionTokens(text)) {
if (tok.kind === "text") {
host.appendChild(document.createTextNode(tok.text));
continue;
}
const elem = this._slate.lookupElement(tok.target);
if (elem) {
host.appendChild(this.buildCaptionRef(tok.display, elem));
} else {
// Unknown — render plain so a typo doesn't surface as
// raw "{XYZ}" to the reader.
host.appendChild(document.createTextNode(tok.display));
}
}
}
private buildCaptionRef(label: string, elem: import("./elements/GeomElement").GeomElement): HTMLSpanElement {
const span = document.createElement("span");
span.className = "geomlib-presentation-ref";
span.textContent = label;
span.style.fontWeight = "700";
span.style.fontStyle = "italic";
span.style.cursor = "pointer";
span.style.borderBottom = "1px dotted currentColor";
span.addEventListener("mouseenter", () => {
// Hover does nothing while a sticky pin is active — the
// pinned element should remain the sole highlighted ref.
if (this._stickyRefSpan != null) return;
elem.emphasized = true;
this._slate.update();
});
span.addEventListener("mouseleave", () => {
if (this._stickyRefSpan != null) return;
elem.emphasized = false;
this._slate.update();
});
span.addEventListener("pointerdown", (e) => {
e.preventDefault();
e.stopPropagation();
const wasThis = this._stickyRefSpan === span;
this.clearStickyRef();
if (!wasThis) this.setStickyRef(span, elem);
this._slate.update();
});
return span;
}
// Drop the active sticky pin (if any): release the pinned
// element's emphasis flag and reset the span's visual state.
private clearStickyRef(): void {
if (this._stickyRefSpan != null) {
this._stickyRefSpan.classList.remove("active");
this._stickyRefSpan.style.color = "";
}
if (this._stickyRefElem != null) {
this._stickyRefElem.emphasized = false;
}
this._stickyRefSpan = null;
this._stickyRefElem = null;
}
// Pin a new sticky ref. Call clearStickyRef() first if another
// ref is currently pinned.
private setStickyRef(span: HTMLSpanElement, elem: import("./elements/GeomElement").GeomElement): void {
span.classList.add("active");
span.style.color = "#b08800";
elem.emphasized = true;
this._stickyRefSpan = span;
this._stickyRefElem = elem;
}
}
// Button styling shared by the four nav buttons in the presentation
// overlay. Matches the soft, light visual of the surrounding panel.
function makePresentationButton(label: string): HTMLButtonElement {
const b = document.createElement("button");
b.textContent = label;
b.style.background = "rgba(0,0,0,0.08)";
b.style.color = "#222";
b.style.border = "1px solid rgba(0,0,0,0.15)";
b.style.borderRadius = "4px";
// Min 44px tap target per Apple HIG / Material guidance; the
// wider padding also gives Pro Max thumbs some breathing room.
b.style.padding = "10px 16px";
b.style.minHeight = "44px";
b.style.cursor = "pointer";
b.style.fontFamily = "inherit";
b.style.fontSize = "1rem";
b.style.touchAction = "manipulation";
b.addEventListener("mouseenter", () => { b.style.background = "rgba(0,0,0,0.16)"; });
b.addEventListener("mouseleave", () => { b.style.background = "rgba(0,0,0,0.08)"; });
return b;
}
// --- Icon drawing functions ---
// All draw on a size×size canvas with 3px padding
function drawResetIcon(ctx: CanvasRenderingContext2D, size: number): void {
let cx = size / 2;
let cy = size / 2;
let r = size * 0.32;
let pad = 2;
ctx.strokeStyle = "rgba(0,0,0,0.7)";
ctx.fillStyle = "rgba(0,0,0,0.7)";
ctx.lineWidth = 1.5;
// Circular arc (270 degrees)
ctx.beginPath();
ctx.arc(cx, cy, r, -Math.PI * 0.5, Math.PI * 0.75);
ctx.stroke();
// Arrowhead at the end of the arc
let endAngle = Math.PI * 0.75;
let ax = cx + r * Math.cos(endAngle);
let ay = cy + r * Math.sin(endAngle);
let headLen = 4;
let a1 = endAngle + Math.PI * 0.3;
let a2 = endAngle + Math.PI * 0.9;
ctx.beginPath();
ctx.moveTo(ax, ay);
ctx.lineTo(ax + headLen * Math.cos(a1), ay + headLen * Math.sin(a1));
ctx.lineTo(ax + headLen * Math.cos(a2), ay + headLen * Math.sin(a2));
ctx.closePath();
ctx.fill();
}
function drawMaximizeIcon(ctx: CanvasRenderingContext2D, size: number): void {
let pad = 4;
let s = size - pad * 2;
ctx.strokeStyle = "rgba(0,0,0,0.7)";
ctx.fillStyle = "rgba(0,0,0,0.7)";
ctx.lineWidth = 1.5;
// Top-right outward arrow
let ax = pad + s * 0.55;
let ay = pad + s * 0.45;
let bx = pad + s;
let by = pad;
ctx.beginPath();
ctx.moveTo(ax, ay);
ctx.lineTo(bx, by);
ctx.stroke();
// Arrowhead
ctx.beginPath();
ctx.moveTo(bx, by);
ctx.lineTo(bx - 5, by);
ctx.lineTo(bx, by + 5);
ctx.closePath();
ctx.fill();
// Bottom-left outward arrow
ax = pad + s * 0.45;
ay = pad + s * 0.55;
bx = pad;
by = pad + s;
ctx.beginPath();
ctx.moveTo(ax, ay);
ctx.lineTo(bx, by);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(bx, by);
ctx.lineTo(bx + 5, by);
ctx.lineTo(bx, by - 5);
ctx.closePath();
ctx.fill();
}
function drawMinimizeIcon(ctx: CanvasRenderingContext2D, size: number): void {
let pad = 4;
let s = size - pad * 2;
ctx.strokeStyle = "rgba(0,0,0,0.7)";
ctx.fillStyle = "rgba(0,0,0,0.7)";
ctx.lineWidth = 1.5;
// Top-right inward arrow
let ax = pad + s;
let ay = pad;
let bx = pad + s * 0.55;
let by = pad + s * 0.45;
ctx.beginPath();
ctx.moveTo(ax, ay);
ctx.lineTo(bx, by);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(bx, by);
ctx.lineTo(bx + 5, by);
ctx.lineTo(bx, by - 5);
ctx.closePath();
ctx.fill();
// Bottom-left inward arrow
ax = pad;
ay = pad + s;
bx = pad + s * 0.45;
by = pad + s * 0.55;
ctx.beginPath();
ctx.moveTo(ax, ay);
ctx.lineTo(bx, by);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(bx, by);
ctx.lineTo(bx - 5, by);
ctx.lineTo(bx, by + 5);
ctx.closePath();
ctx.fill();
}
// #154 — the diagnostic badge.
//
// A <div>, deliberately NOT a <button>: ensureControlsStyle() hides
// `.geomlib-wrapper>button` until hover (#69), and a diagnostic that only
// appears when you already went looking for it is useless. It is also
// non-interactive — no click handler, no tab stop — so it adds nothing to
// the keyboard order and never fights the buttons' :focus-within reveal.
export const BADGE_TOOLTIP = "geomlib generated warnings - check console";
export function createDiagnosticBadge(
severity: DiagnosticSeverity,
size?: number,
): HTMLDivElement {
const px = size != null ? size : BTN_SIZE;
const badge = document.createElement("div");
badge.className = "geomlib-badge";
badge.setAttribute("role", "img");
// Native title attribute, same mechanism as the buttons (btn.title):
// no positioning code, and it survives maximize with no z-index fight.
badge.title = BADGE_TOOLTIP;
badge.setAttribute("aria-label", BADGE_TOOLTIP);
badge.style.width = px + "px";
badge.style.height = px + "px";
badge.style.lineHeight = "0";
badge.style.cursor = "default";
paintDiagnosticBadge(badge, severity, px);
return badge;
}
// Redraw an existing badge in place, so a warning that escalates to an
// error swaps its icon without churning the DOM node.
export function paintDiagnosticBadge(
badge: HTMLElement,
severity: DiagnosticSeverity,
size?: number,
): void {
const px = size != null ? size : BTN_SIZE;
let icon = badge.firstChild as HTMLCanvasElement;
if (!icon || (icon as any).tagName !== "CANVAS") {
badge.innerHTML = "";
icon = document.createElement("canvas");
badge.appendChild(icon);
}
icon.width = px;
icon.height = px;
const ctx = icon.getContext("2d");
if (!ctx) return;
ctx.clearRect(0, 0, px, px);
if (severity === "error") drawErrorIcon(ctx, px);
else drawWarningIcon(ctx, px);
}
// Icons are DRAWN, not text glyphs. A "\u26A0" in a string literal is the
// mojibake class #142 exists to prevent, and a glyph would drag in a font
// metric dependency the rest of the icon set doesn't have.
//
// Unlike the control icons (a neutral rgba(0,0,0,0.7)), these are
// coloured: the badge has to read as a diagnostic, not as another
// control the viewer can press.
export function drawWarningIcon(ctx: CanvasRenderingContext2D, size: number): void {
const pad = size * 0.06;
const left = pad, right = size - pad;
const top = pad, bottom = size - pad * 1.2;
// Amber triangle, apex up.
ctx.fillStyle = "#e6a700";
ctx.beginPath();
ctx.moveTo(size / 2, top);
ctx.lineTo(right, bottom);
ctx.lineTo(left, bottom);
ctx.closePath();
ctx.fill();
// "!" as a bar plus a dot — no fillText, so no font dependency.
const barW = Math.max(1.5, size * 0.11);
ctx.fillStyle = "#ffffff";
ctx.fillRect(size / 2 - barW / 2, size * 0.34, barW, size * 0.3);
ctx.fillRect(size / 2 - barW / 2, size * 0.71, barW, barW);
}
export function drawErrorIcon(ctx: CanvasRenderingContext2D, size: number): void {
// A stop-sign octagon with a white X. Reads as "stop" at 20px in a way
// a bare X does not, and its silhouette is distinct from the warning
// triangle even before colour registers.
const cx = size / 2, cy = size / 2;
const r = size * 0.47;
ctx.fillStyle = "#d13438";
ctx.beginPath();
for (let k = 0; k < 8; k++) {
// Start at 22.5 degrees so the octagon sits flat-topped, the way a
// road sign does, rather than resting on a vertex.
const a = Math.PI / 8 + k * Math.PI / 4;
const x = cx + r * Math.cos(a);
const y = cy + r * Math.sin(a);
if (k === 0) ctx.moveTo(x, y); else ctx.lineTo(x, y);
}
ctx.closePath();
ctx.fill();
const pad = size * 0.34;
ctx.strokeStyle = "#ffffff";
ctx.lineWidth = Math.max(1.5, size * 0.11);
ctx.lineCap = "round";
ctx.beginPath();
ctx.moveTo(pad, pad);
ctx.lineTo(size - pad, size - pad);
ctx.moveTo(size - pad, pad);
ctx.lineTo(pad, size - pad);
ctx.stroke();
}
function drawPresentIcon(ctx: CanvasRenderingContext2D, size: number): void {
// Right-pointing play triangle, centred. The button is square so a
// little leftward bias visually centres the optical mass.
let pad = size * 0.28;
let leftX = pad - 1;
let rightX = size - pad + 2;
let topY = pad;
let bottomY = size - pad;
ctx.fillStyle = "rgba(0,0,0,0.7)";
ctx.beginPath();
ctx.moveTo(leftX, topY);
ctx.lineTo(leftX, bottomY);
ctx.lineTo(rightX, size / 2);
ctx.closePath();
ctx.fill();
}
|