(container: HTMLElement)
| 35 | }; |
| 36 | |
| 37 | export function createTextOverlay(container: HTMLElement): TextOverlay { |
| 38 | const computedStyle = getComputedStyle(container); |
| 39 | const computedPosition = computedStyle.position; |
| 40 | const computedOverflow = computedStyle.overflow; |
| 41 | |
| 42 | const didSetRelative = computedPosition === 'static'; |
| 43 | const didSetOverflowVisible = computedOverflow === 'hidden' || computedOverflow === 'scroll' || computedOverflow === 'auto'; |
| 44 | |
| 45 | const previousInlinePosition = didSetRelative ? container.style.position : null; |
| 46 | const previousInlineOverflow = didSetOverflowVisible ? container.style.overflow : null; |
| 47 | |
| 48 | if (didSetRelative) { |
| 49 | container.style.position = 'relative'; |
| 50 | } |
| 51 | |
| 52 | if (didSetOverflowVisible) { |
| 53 | container.style.overflow = 'visible'; |
| 54 | } |
| 55 | |
| 56 | const overlay = document.createElement('div'); |
| 57 | overlay.style.position = 'absolute'; |
| 58 | overlay.style.inset = '0'; |
| 59 | overlay.style.pointerEvents = 'none'; |
| 60 | overlay.style.overflow = 'visible'; |
| 61 | overlay.style.zIndex = '10'; // Above zoom slider (z-index: 4) and other overlays |
| 62 | container.appendChild(overlay); |
| 63 | |
| 64 | let disposed = false; |
| 65 | |
| 66 | const clear = (): void => { |
| 67 | if (disposed) return; |
| 68 | overlay.replaceChildren(); |
| 69 | }; |
| 70 | |
| 71 | const addLabel: TextOverlay['addLabel'] = (text, x, y, options) => { |
| 72 | if (disposed) { |
| 73 | // Keep it non-throwing so callsites don't need try/catch in teardown paths. |
| 74 | return document.createElement('span'); |
| 75 | } |
| 76 | |
| 77 | const span = document.createElement('span'); |
| 78 | span.textContent = text; |
| 79 | span.style.position = 'absolute'; |
| 80 | span.style.left = `${x}px`; |
| 81 | span.style.top = `${y}px`; |
| 82 | span.style.pointerEvents = 'none'; |
| 83 | span.style.userSelect = 'none'; |
| 84 | span.style.whiteSpace = 'nowrap'; |
| 85 | span.style.lineHeight = '1'; |
| 86 | |
| 87 | if (options?.fontSize != null) span.style.fontSize = `${options.fontSize}px`; |
| 88 | if (options?.color != null) span.style.color = options.color; |
| 89 | |
| 90 | const rotation = options?.rotation ?? 0; |
| 91 | const anchor = options?.anchor ?? 'start'; |
| 92 | const { translateX, originX } = getAnchorTransform(anchor); |
| 93 | |
| 94 | span.style.transformOrigin = `${originX} 50%`; |
no outgoing calls
no test coverage detected