diff --git "a/components.json" "b/components.json" --- "a/components.json" +++ "b/components.json" @@ -5,7 +5,7 @@ "description": "Rich Viewer of Object Detection Models", "author": "hysts", "tags": [], - "category": "display", + "category": "output", "html_template": "
\n \n
\n \n
\n
\n
\n
\n
\n Detections\n \n
\n
\n \n \n \n \n
\n
\n
\n
\n
\n
\n
\n
\n
\n Keyboard Shortcuts\n \n
\n \n \n \n \n \n \n \n \n \n \n \n
?Show keyboard shortcuts
FToggle maximize
IToggle base image
AToggle all annotations
HHide selected annotation
Shift+ClickHide clicked annotation
EscDeselect / exit maximize
+ / -Zoom in / out
0Reset zoom
RReset all to defaults
\n
\n
\n
\n
No data
\n
\n", "css_template": ".pose-viewer-container {\n font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, sans-serif;\n user-select: none;\n width: 100%;\n position: relative;\n box-sizing: border-box;\n}\n\n/* \u2500\u2500 Maximized Overlay \u2500\u2500 */\n\n.pose-viewer-container.maximized {\n position: fixed;\n top: 0;\n left: 0;\n width: 100vw;\n height: 100vh;\n z-index: 9999;\n background: var(--background-fill-primary, #fff);\n display: flex;\n flex-direction: column;\n padding: 0;\n overflow: hidden;\n}\n\n.pose-viewer-container.maximized .canvas-wrapper {\n flex: 1;\n min-height: 0;\n border-radius: 0;\n align-items: center;\n}\n\n.pose-viewer-container.maximized .control-panel {\n flex-shrink: 0;\n max-height: 40vh;\n border-radius: 0;\n border-left: none;\n border-right: none;\n border-bottom: none;\n margin-top: 0;\n}\n\n.pose-viewer-container.maximized .maximize-btn {\n background: var(--color-accent, #2196F3);\n color: #fff;\n border-color: var(--color-accent, #2196F3);\n}\n\n/* \u2500\u2500 Canvas \u2500\u2500 */\n\n.pose-viewer-container .canvas-wrapper {\n display: none;\n justify-content: center;\n border-radius: 4px;\n overflow: hidden;\n}\n\n.pose-viewer-container .canvas-wrapper canvas {\n display: block;\n touch-action: none;\n}\n\n/* \u2500\u2500 Loading Indicator \u2500\u2500 */\n\n.pose-viewer-container .loading-indicator {\n display: none;\n justify-content: center;\n align-items: center;\n border: 2px dashed var(--border-color-primary, #d0d0d0);\n border-radius: 8px;\n padding: 48px 24px;\n}\n\n.pose-viewer-container .loading-indicator.visible {\n display: flex;\n}\n\n.pose-viewer-container .loading-spinner {\n width: 24px;\n height: 24px;\n border: 3px solid var(--border-color-primary, #d0d0d0);\n border-top-color: var(--color-accent, #2196F3);\n border-radius: 50%;\n animation: detection-viewer-spin 0.8s linear infinite;\n}\n\n@keyframes detection-viewer-spin {\n to { transform: rotate(360deg); }\n}\n\n/* \u2500\u2500 Placeholder \u2500\u2500 */\n\n.pose-viewer-container .placeholder {\n border: 2px dashed var(--border-color-primary, #d0d0d0);\n border-radius: 8px;\n padding: 48px 24px;\n text-align: center;\n color: var(--body-text-color-subdued, #888);\n font-size: 14px;\n}\n\n.pose-viewer-container .placeholder.hidden {\n display: none;\n}\n\n/* \u2500\u2500 Tooltip \u2500\u2500 */\n\n.pose-viewer-container .tooltip {\n display: none;\n position: absolute;\n background: rgba(0, 0, 0, 0.8);\n color: #fff;\n padding: 4px 8px;\n border-radius: 4px;\n font-size: 12px;\n pointer-events: none;\n white-space: nowrap;\n z-index: 10;\n}\n\n.pose-viewer-container .tooltip.visible {\n display: block;\n}\n\n/* \u2500\u2500 Control Panel \u2500\u2500 */\n\n.pose-viewer-container .control-panel {\n display: none;\n flex-direction: column;\n border: 1px solid var(--border-color-primary, #e0e0e0);\n border-radius: 6px;\n margin-top: 8px;\n font-size: 12px;\n overflow: hidden;\n}\n\n.pose-viewer-container .control-panel.visible {\n display: flex;\n}\n\n.pose-viewer-container .control-panel-header {\n display: flex;\n align-items: center;\n justify-content: space-between;\n padding: 6px 10px;\n background: var(--background-fill-secondary, #f7f7f7);\n border-bottom: 1px solid var(--border-color-primary, #e0e0e0);\n}\n\n.pose-viewer-container .control-panel-header-info {\n display: flex;\n align-items: center;\n gap: 8px;\n}\n\n.pose-viewer-container .control-panel-title {\n font-weight: 600;\n color: var(--body-text-color, #333);\n}\n\n.pose-viewer-container .control-panel-count {\n font-weight: 400;\n color: var(--body-text-color-subdued, #888);\n}\n\n.pose-viewer-container .control-panel-actions {\n display: flex;\n align-items: center;\n gap: 4px;\n}\n\n.pose-viewer-container .cp-btn {\n all: unset;\n display: inline-flex;\n align-items: center;\n justify-content: center;\n height: 22px;\n padding: 0 8px;\n background: transparent;\n border: 1px solid var(--border-color-primary, #d0d0d0);\n border-radius: 3px;\n cursor: pointer;\n font-size: 11px;\n line-height: 1;\n color: var(--body-text-color, #333);\n box-sizing: border-box;\n}\n\n.pose-viewer-container .cp-btn:hover {\n background: var(--background-fill-primary, #eee);\n}\n\n.pose-viewer-container .toggle-image-btn.active {\n background: var(--color-accent, #2196F3);\n color: #fff;\n border-color: var(--color-accent, #2196F3);\n}\n\n.pose-viewer-container .toggle-image-btn.active:hover {\n opacity: 0.9;\n}\n\n.pose-viewer-container .control-panel-body {\n overflow-y: auto;\n min-height: 0;\n}\n\n.pose-viewer-container .annotation-rows {\n max-height: 300px;\n overflow: hidden auto;\n}\n\n/* \u2500\u2500 Select-All Row \u2500\u2500 */\n\n.pose-viewer-container .select-all-row {\n display: flex;\n align-items: center;\n gap: 8px;\n padding: 5px 10px;\n border-bottom: 1px solid var(--border-color-primary, #e0e0e0);\n border-left: 3px solid transparent;\n padding-left: 28px;\n}\n\n.pose-viewer-container .select-all-row .select-all-checkbox {\n flex-shrink: 0;\n cursor: pointer;\n}\n\n.pose-viewer-container .select-all-row .select-all-label {\n font-size: 12px;\n font-weight: 600;\n color: var(--body-text-color, #333);\n}\n\n/* \u2500\u2500 Annotation Row \u2500\u2500 */\n\n.pose-viewer-container .annotation-row {\n display: flex;\n align-items: center;\n gap: 8px;\n padding: 5px 10px;\n cursor: pointer;\n border-left: 3px solid transparent;\n transition: background 0.15s;\n}\n\n.pose-viewer-container .annotation-row:hover {\n background: var(--background-fill-secondary, #f5f5f5);\n}\n\n.pose-viewer-container .annotation-row .ann-dot {\n width: 10px;\n height: 10px;\n border-radius: 50%;\n flex-shrink: 0;\n}\n\n.pose-viewer-container .annotation-row .ann-checkbox {\n flex-shrink: 0;\n cursor: pointer;\n}\n\n.pose-viewer-container .annotation-row .ann-label {\n flex: 1;\n white-space: nowrap;\n overflow: hidden;\n text-overflow: ellipsis;\n color: var(--body-text-color, #333);\n}\n\n.pose-viewer-container .annotation-row .ann-summary {\n color: var(--body-text-color-subdued, #888);\n white-space: nowrap;\n font-size: 11px;\n}\n\n/* \u2500\u2500 Expand Button \u2500\u2500 */\n\n.pose-viewer-container .annotation-row .ann-expand {\n all: unset;\n display: inline-flex;\n align-items: center;\n justify-content: center;\n width: 16px;\n height: 16px;\n flex-shrink: 0;\n cursor: pointer;\n font-size: 8px;\n color: var(--body-text-color-subdued, #888);\n border-radius: 3px;\n transition: transform 0.15s;\n}\n\n.pose-viewer-container .annotation-row .ann-expand:hover {\n background: var(--border-color-primary, #e0e0e0);\n color: var(--body-text-color, #333);\n}\n\n.pose-viewer-container .annotation-row .ann-expand.expanded {\n transform: rotate(90deg);\n}\n\n/* \u2500\u2500 Selected Row \u2500\u2500 */\n\n.pose-viewer-container .annotation-row.selected {\n border-left-color: var(--color-accent, #2196F3);\n background: var(--background-fill-secondary, #f0f7ff);\n}\n\n.pose-viewer-container .annotation-row.below-threshold {\n opacity: 0.4;\n}\n\n/* \u2500\u2500 Annotation Detail \u2500\u2500 */\n\n.pose-viewer-container .annotation-detail {\n display: none;\n padding: 6px 10px 8px 31px;\n font-size: 11px;\n color: var(--body-text-color-subdued, #666);\n border-bottom: 1px solid var(--border-color-primary, #eee);\n max-height: 150px;\n overflow-y: auto;\n}\n\n.pose-viewer-container .annotation-detail.visible {\n display: block;\n}\n\n.pose-viewer-container .annotation-detail table {\n width: 100%;\n border-collapse: collapse;\n}\n\n.pose-viewer-container .annotation-detail td {\n padding: 1px 6px 1px 0;\n}\n\n.pose-viewer-container .annotation-detail .detail-section-title {\n font-weight: 600;\n margin-bottom: 2px;\n color: var(--body-text-color, #333);\n}\n\n/* \u2500\u2500 Layer Toggles \u2500\u2500 */\n\n.pose-viewer-container .layer-toggles {\n display: flex;\n align-items: center;\n gap: 4px;\n padding: 6px 10px;\n border-bottom: 1px solid var(--border-color-primary, #e0e0e0);\n}\n\n.pose-viewer-container .layer-btn {\n all: unset;\n display: inline-flex;\n align-items: center;\n justify-content: center;\n height: 22px;\n padding: 0 8px;\n background: var(--background-fill-primary, #eee);\n border: 1px solid var(--border-color-primary, #d0d0d0);\n border-radius: 3px;\n cursor: pointer;\n font-size: 11px;\n line-height: 1;\n color: var(--body-text-color, #333);\n box-sizing: border-box;\n}\n\n.pose-viewer-container .layer-btn.active {\n background: var(--color-accent, #2196F3);\n color: #fff;\n border-color: var(--color-accent, #2196F3);\n}\n\n/* \u2500\u2500 Label Filters \u2500\u2500 */\n\n.pose-viewer-container .label-filters {\n display: flex;\n flex-wrap: wrap;\n align-items: center;\n gap: 4px;\n padding: 6px 10px;\n border-bottom: 1px solid var(--border-color-primary, #e0e0e0);\n}\n\n.pose-viewer-container .label-filters-title {\n font-size: 11px;\n font-weight: 600;\n color: var(--body-text-color, #333);\n margin-right: 4px;\n}\n\n.pose-viewer-container .label-filter-btn {\n all: unset;\n display: inline-flex;\n align-items: center;\n touch-action: manipulation;\n gap: 4px;\n height: 22px;\n padding: 0 8px;\n background: var(--background-fill-primary, #eee);\n border: 1px solid var(--border-color-primary, #d0d0d0);\n border-radius: 3px;\n cursor: pointer;\n font-size: 11px;\n line-height: 1;\n color: var(--body-text-color-subdued, #888);\n box-sizing: border-box;\n}\n\n.pose-viewer-container .label-filter-btn.active {\n color: var(--body-text-color, #333);\n border-color: var(--body-text-color-subdued, #888);\n}\n\n.pose-viewer-container .label-color-dot {\n display: inline-block;\n width: 8px;\n height: 8px;\n border-radius: 50%;\n flex-shrink: 0;\n}\n\n.pose-viewer-container .label-count {\n opacity: 0.6;\n}\n\n/* \u2500\u2500 Sort Controls \u2500\u2500 */\n\n.pose-viewer-container .sort-controls {\n display: flex;\n align-items: center;\n gap: 4px;\n padding: 6px 10px;\n border-bottom: 1px solid var(--border-color-primary, #e0e0e0);\n}\n\n.pose-viewer-container .sort-controls-title {\n font-size: 11px;\n font-weight: 600;\n color: var(--body-text-color, #333);\n margin-right: 4px;\n}\n\n.pose-viewer-container .sort-btn {\n all: unset;\n display: inline-flex;\n align-items: center;\n justify-content: center;\n height: 22px;\n padding: 0 8px;\n background: var(--background-fill-primary, #eee);\n border: 1px solid var(--border-color-primary, #d0d0d0);\n border-radius: 3px;\n cursor: pointer;\n font-size: 11px;\n line-height: 1;\n color: var(--body-text-color-subdued, #888);\n box-sizing: border-box;\n}\n\n.pose-viewer-container .sort-btn.active {\n color: var(--body-text-color, #333);\n border-color: var(--body-text-color-subdued, #888);\n}\n\n/* \u2500\u2500 Annotation Group Separator \u2500\u2500 */\n\n.pose-viewer-container .annotation-group-separator {\n display: flex;\n align-items: center;\n gap: 8px;\n padding: 4px 10px;\n font-size: 10px;\n color: var(--body-text-color-subdued, #888);\n}\n\n.pose-viewer-container .annotation-group-separator::before,\n.pose-viewer-container .annotation-group-separator::after {\n content: \"\";\n flex: 1;\n height: 1px;\n background: var(--border-color-primary, #e0e0e0);\n}\n\n/* \u2500\u2500 Filtered-out Row \u2500\u2500 */\n\n.pose-viewer-container .annotation-row.filtered-out {\n opacity: 0.4;\n}\n\n/* \u2500\u2500 Threshold Slider \u2500\u2500 */\n\n.pose-viewer-container .threshold-row {\n display: flex;\n align-items: center;\n gap: 8px;\n padding: 5px 10px;\n border-bottom: 1px solid var(--border-color-primary, #e0e0e0);\n font-size: 11px;\n color: var(--body-text-color, #333);\n}\n\n.pose-viewer-container .threshold-row input[type=\"range\"] {\n flex: 1;\n height: 14px;\n margin: 0;\n cursor: pointer;\n -webkit-appearance: none;\n appearance: none;\n background: transparent;\n}\n\n.pose-viewer-container .threshold-row input[type=\"range\"]::-webkit-slider-thumb {\n -webkit-appearance: none;\n appearance: none;\n width: 14px;\n height: 14px;\n border-radius: 50%;\n background: var(--color-accent, #2196F3);\n border: 2px solid #fff;\n box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);\n cursor: pointer;\n}\n\n.pose-viewer-container .threshold-row input[type=\"range\"]::-moz-range-thumb {\n width: 14px;\n height: 14px;\n border-radius: 50%;\n background: var(--color-accent, #2196F3);\n border: 2px solid #fff;\n box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);\n cursor: pointer;\n}\n\n.pose-viewer-container .threshold-row .threshold-value,\n.pose-viewer-container .threshold-row .slider-value {\n min-width: 32px;\n text-align: right;\n color: var(--body-text-color-subdued, #888);\n}\n\n/* \u2500\u2500 Dual Range Slider \u2500\u2500 */\n\n.pose-viewer-container .dual-range-wrapper {\n position: relative;\n flex: 1;\n height: 4px;\n border-radius: 2px;\n background: var(--border-color-primary, #d0d0d0);\n}\n\n.pose-viewer-container .dual-range-wrapper input[type=\"range\"] {\n position: absolute;\n top: 50%;\n left: 0;\n transform: translateY(-50%);\n width: 100%;\n height: 14px;\n margin: 0;\n padding: 0;\n background: transparent;\n pointer-events: none;\n -webkit-appearance: none;\n appearance: none;\n}\n\n.pose-viewer-container .dual-range-wrapper input[type=\"range\"]::-webkit-slider-thumb {\n -webkit-appearance: none;\n appearance: none;\n width: 14px;\n height: 14px;\n border-radius: 50%;\n background: var(--color-accent, #2196F3);\n border: 2px solid #fff;\n box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);\n cursor: pointer;\n pointer-events: all;\n}\n\n.pose-viewer-container .dual-range-wrapper input[type=\"range\"]::-moz-range-thumb {\n width: 14px;\n height: 14px;\n border-radius: 50%;\n background: var(--color-accent, #2196F3);\n border: 2px solid #fff;\n box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);\n cursor: pointer;\n pointer-events: all;\n}\n\n.pose-viewer-container .dual-range-wrapper input[type=\"range\"]::-webkit-slider-runnable-track {\n background: transparent;\n}\n\n.pose-viewer-container .dual-range-wrapper input[type=\"range\"]::-moz-range-track {\n background: transparent;\n}\n\n/* \u2500\u2500 Draw Options (collapsible) \u2500\u2500 */\n\n.pose-viewer-container .draw-options {\n border-bottom: 1px solid var(--border-color-primary, #e0e0e0);\n}\n\n.pose-viewer-container .draw-options-toggle {\n all: unset;\n display: flex;\n align-items: center;\n gap: 4px;\n width: 100%;\n padding: 4px 10px;\n cursor: pointer;\n font-size: 11px;\n color: var(--body-text-color-subdued, #888);\n box-sizing: border-box;\n}\n\n.pose-viewer-container .draw-options-toggle:hover {\n background: var(--background-fill-secondary, #f5f5f5);\n}\n\n.pose-viewer-container .draw-options-arrow {\n font-size: 8px;\n transition: transform 0.15s;\n}\n\n.pose-viewer-container .draw-options.open .draw-options-arrow {\n transform: rotate(90deg);\n}\n\n.pose-viewer-container .draw-options-body {\n display: none;\n}\n\n.pose-viewer-container .draw-options.open .draw-options-body {\n display: block;\n}\n\n.pose-viewer-container .draw-options-body .threshold-row {\n border-bottom: none;\n padding-top: 2px;\n padding-bottom: 2px;\n}\n\n.pose-viewer-container .draw-options-body .threshold-row:last-child {\n padding-bottom: 6px;\n}\n\n/* \u2500\u2500 Help Overlay \u2500\u2500 */\n\n.pose-viewer-container .help-overlay {\n display: none;\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n background: rgba(0, 0, 0, 0.4);\n z-index: 10000;\n justify-content: center;\n align-items: center;\n}\n\n.pose-viewer-container.maximized .help-overlay {\n position: fixed;\n}\n\n.pose-viewer-container .help-overlay.visible {\n display: flex;\n}\n\n.pose-viewer-container .help-dialog {\n background: var(--background-fill-primary, #fff);\n border: 1px solid var(--border-color-primary, #e0e0e0);\n border-radius: 8px;\n padding: 16px 20px;\n max-width: 320px;\n width: 90%;\n box-shadow: 0 4px 24px rgba(0, 0, 0, 0.15);\n}\n\n.pose-viewer-container .help-header {\n display: flex;\n align-items: center;\n justify-content: space-between;\n margin-bottom: 12px;\n font-size: 14px;\n font-weight: 600;\n color: var(--body-text-color, #333);\n}\n\n.pose-viewer-container .help-close-btn {\n all: unset;\n cursor: pointer;\n font-size: 18px;\n line-height: 1;\n color: var(--body-text-color-subdued, #888);\n padding: 0 4px;\n}\n\n.pose-viewer-container .help-close-btn:hover {\n color: var(--body-text-color, #333);\n}\n\n.pose-viewer-container .help-table {\n width: 100%;\n border-collapse: collapse;\n font-size: 12px;\n color: var(--body-text-color, #333);\n}\n\n.pose-viewer-container .help-table td {\n padding: 4px 0;\n}\n\n.pose-viewer-container .help-table td:first-child {\n white-space: nowrap;\n padding-right: 16px;\n}\n\n.pose-viewer-container .help-table kbd {\n display: inline-block;\n background: var(--background-fill-secondary, #f3f3f3);\n border: 1px solid var(--border-color-primary, #d0d0d0);\n border-radius: 3px;\n padding: 1px 5px;\n font-family: ui-monospace, SFMono-Regular, \"SF Mono\", Menlo, monospace;\n font-size: 11px;\n line-height: 1.4;\n}\n", "js_on_load": "// Guard: skip if already initialized\nif (element._poseInitialized) return;\nelement._poseInitialized = true;\n\n(function () {\n \"use strict\";\n\n var MAX_CANVAS_HEIGHT = 600;\n var KEYPOINT_RADIUS = 3;\n var HIT_RADIUS = 10;\n var MASK_ALPHA = 0.4;\n var CONNECTION_ALPHA = 0.7;\n var CONNECTION_WIDTH = 2;\n var BBOX_LINE_WIDTH = 2;\n var BBOX_LABEL_FONT = \"bold 11px -apple-system, BlinkMacSystemFont, sans-serif\";\n var DIM_ALPHA = 0.2;\n var MIN_ZOOM = 1;\n var MAX_ZOOM = 20;\n var ZOOM_SENSITIVITY = 0.001;\n var DRAG_THRESHOLD = 4;\n\n // \u2500\u2500 DOM References \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\n var container = element.querySelector(\".pose-viewer-container\");\n var dataScript = element.querySelector(\"script.pose-data\");\n var canvasWrapper = element.querySelector(\".canvas-wrapper\");\n var canvas = element.querySelector(\"canvas\");\n var ctx = canvas.getContext(\"2d\");\n var tooltip = element.querySelector(\".tooltip\");\n var controlPanel = element.querySelector(\".control-panel\");\n var annotationList = element.querySelector(\".annotation-list\");\n\n // Read configurable threshold defaults\n var initialScoreThresholdMin = parseFloat(container.getAttribute(\"data-score-threshold-min\")) || 0;\n var initialScoreThresholdMax = parseFloat(container.getAttribute(\"data-score-threshold-max\"));\n if (isNaN(initialScoreThresholdMax)) initialScoreThresholdMax = 1;\n var initialKeypointThreshold = parseFloat(container.getAttribute(\"data-keypoint-threshold\")) || 0;\n var initialKeypointRadius = parseInt(container.getAttribute(\"data-keypoint-radius\"), 10);\n if (isNaN(initialKeypointRadius) || initialKeypointRadius < 1) initialKeypointRadius = KEYPOINT_RADIUS;\n var toggleImageBtn = element.querySelector(\".toggle-image-btn\");\n var resetBtn = element.querySelector(\".reset-btn\");\n var maximizeBtn = element.querySelector(\".maximize-btn\");\n var helpBtn = element.querySelector(\".help-btn\");\n var helpOverlay = element.querySelector(\".help-overlay\");\n var helpCloseBtn = element.querySelector(\".help-close-btn\");\n var loadingIndicator = element.querySelector(\".loading-indicator\");\n var placeholder = element.querySelector(\".placeholder\");\n var countEl = element.querySelector(\".control-panel-count\");\n\n // \u2500\u2500 State \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\n var state = {\n image: null,\n annotations: [],\n scale: 1,\n visibility: [],\n selectedIndex: -1,\n showImage: true,\n layers: { masks: true, boxes: true, skeleton: true, keypoints: true },\n thresholdMin: initialScoreThresholdMin,\n thresholdMax: initialScoreThresholdMax,\n keypointThreshold: initialKeypointThreshold,\n keypointRadius: initialKeypointRadius,\n connectionWidth: CONNECTION_WIDTH,\n maskAlpha: MASK_ALPHA,\n connectionAlpha: CONNECTION_ALPHA,\n bboxLineWidth: BBOX_LINE_WIDTH,\n labelVisibility: {},\n maskImages: [],\n zoom: 1,\n panX: 0,\n panY: 0,\n isPanning: false,\n panStartX: 0,\n panStartY: 0,\n panStartPanX: 0,\n panStartPanY: 0,\n didDrag: false,\n maximized: false,\n sortMode: \"none\",\n sortedIndices: [],\n expandedIndex: -1,\n drawOptionsOpen: false\n };\n\n // \u2500\u2500 MutationObserver for value changes \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\n var observer = new MutationObserver(function () {\n handleValueChange();\n });\n observer.observe(dataScript, { childList: true, characterData: true, subtree: true });\n\n // Also handle initial value\n handleValueChange();\n\n // \u2500\u2500 Value Change Handler \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\n function handleValueChange() {\n var raw = dataScript.textContent.trim();\n if (!raw || raw === \"null\") {\n showPlaceholder();\n return;\n }\n\n var data;\n try {\n data = JSON.parse(raw);\n } catch (e) {\n showPlaceholder();\n return;\n }\n\n if (!data || !data.image) {\n showPlaceholder();\n return;\n }\n\n // Update score threshold if provided in payload\n if (data.scoreThresholdMin != null) {\n initialScoreThresholdMin = data.scoreThresholdMin;\n state.thresholdMin = data.scoreThresholdMin;\n }\n if (data.scoreThresholdMax != null) {\n initialScoreThresholdMax = data.scoreThresholdMax;\n state.thresholdMax = data.scoreThresholdMax;\n }\n\n // Show loading spinner while the image is being fetched.\n // Gradio's updateDOM() resets JS-managed inline styles and\n // classes back to template defaults on every value change,\n // so we must re-establish the display state here.\n showLoading();\n\n var img = new Image();\n img.onload = function () {\n state.image = img;\n state.annotations = data.annotations || [];\n state.visibility = [];\n for (var i = 0; i < state.annotations.length; i++) {\n state.visibility.push(true);\n }\n state.selectedIndex = -1;\n\n // Build label visibility map\n state.labelVisibility = {};\n for (var i = 0; i < state.annotations.length; i++) {\n var lbl = state.annotations[i].label;\n if (lbl && !state.labelVisibility.hasOwnProperty(lbl)) {\n state.labelVisibility[lbl] = true;\n }\n }\n\n // Decode RLE masks to offscreen canvases\n state.maskImages = [];\n for (var i = 0; i < state.annotations.length; i++) {\n if (state.annotations[i].mask) {\n state.maskImages[i] = createMaskCanvas(state.annotations[i].mask, state.annotations[i].color);\n } else {\n state.maskImages[i] = null;\n }\n }\n\n showContent();\n state.sortMode = getDefaultSortMode();\n requestAnimationFrame(function () {\n fitCanvas();\n state.zoom = 1;\n state.panX = 0;\n state.panY = 0;\n render();\n renderControlPanel();\n });\n };\n img.src = data.image;\n }\n\n // \u2500\u2500 Display States: placeholder / loading / content \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\n function showPlaceholder() {\n state.image = null;\n state.annotations = [];\n state.visibility = [];\n state.selectedIndex = -1;\n canvasWrapper.style.display = \"none\";\n loadingIndicator.classList.remove(\"visible\");\n controlPanel.classList.remove(\"visible\");\n tooltip.classList.remove(\"visible\");\n placeholder.classList.remove(\"hidden\");\n }\n\n function showLoading() {\n placeholder.classList.add(\"hidden\");\n canvasWrapper.style.display = \"none\";\n controlPanel.classList.remove(\"visible\");\n loadingIndicator.classList.add(\"visible\");\n }\n\n function showContent() {\n placeholder.classList.add(\"hidden\");\n loadingIndicator.classList.remove(\"visible\");\n canvasWrapper.style.display = \"flex\";\n }\n\n // \u2500\u2500 Canvas Sizing \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\n function fitCanvas() {\n if (!state.image) return;\n\n var img = state.image;\n\n if (state.maximized) {\n var wrapperW = canvasWrapper.clientWidth || window.innerWidth;\n var wrapperH = canvasWrapper.clientHeight || window.innerHeight;\n\n var w = wrapperW;\n var h = img.naturalHeight * (w / img.naturalWidth);\n\n if (h > wrapperH) {\n h = wrapperH;\n w = img.naturalWidth * (h / img.naturalHeight);\n }\n\n canvas.width = Math.round(w);\n canvas.height = Math.round(h);\n } else {\n var maxWidth = canvasWrapper.clientWidth || 800;\n\n var w = maxWidth;\n var h = img.naturalHeight * (w / img.naturalWidth);\n\n if (h > MAX_CANVAS_HEIGHT) {\n h = MAX_CANVAS_HEIGHT;\n w = img.naturalWidth * (h / img.naturalHeight);\n }\n\n canvas.width = Math.round(w);\n canvas.height = Math.round(h);\n }\n state.scale = canvas.width / img.naturalWidth;\n }\n\n // \u2500\u2500 Zoom/Pan Helpers \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\n function clientToCanvas(clientX, clientY) {\n var rect = canvas.getBoundingClientRect();\n var cssX = (clientX - rect.left) * (canvas.width / rect.width);\n var cssY = (clientY - rect.top) * (canvas.height / rect.height);\n return {\n x: (cssX - state.panX) / state.zoom,\n y: (cssY - state.panY) / state.zoom\n };\n }\n\n function clampPan() {\n if (state.zoom <= 1) {\n state.panX = 0;\n state.panY = 0;\n return;\n }\n var maxPanX = 0;\n var minPanX = canvas.width - canvas.width * state.zoom;\n var maxPanY = 0;\n var minPanY = canvas.height - canvas.height * state.zoom;\n if (state.panX > maxPanX) state.panX = maxPanX;\n if (state.panX < minPanX) state.panX = minPanX;\n if (state.panY > maxPanY) state.panY = maxPanY;\n if (state.panY < minPanY) state.panY = minPanY;\n }\n\n function zoomToCenter(newZoom) {\n newZoom = Math.max(MIN_ZOOM, Math.min(MAX_ZOOM, newZoom));\n var cx = canvas.width / 2;\n var cy = canvas.height / 2;\n state.panX = cx - (cx - state.panX) * (newZoom / state.zoom);\n state.panY = cy - (cy - state.panY) * (newZoom / state.zoom);\n state.zoom = newZoom;\n clampPan();\n render();\n }\n\n function resetZoom() {\n state.zoom = 1;\n state.panX = 0;\n state.panY = 0;\n render();\n }\n\n function resetToDefaults() {\n if (!state.image) return;\n\n state.showImage = true;\n toggleImageBtn.classList.add(\"active\");\n\n state.layers.masks = true;\n state.layers.boxes = true;\n state.layers.skeleton = true;\n state.layers.keypoints = true;\n\n state.thresholdMin = initialScoreThresholdMin;\n state.thresholdMax = initialScoreThresholdMax;\n state.keypointThreshold = initialKeypointThreshold;\n state.keypointRadius = initialKeypointRadius;\n state.connectionWidth = CONNECTION_WIDTH;\n state.maskAlpha = MASK_ALPHA;\n state.connectionAlpha = CONNECTION_ALPHA;\n state.bboxLineWidth = BBOX_LINE_WIDTH;\n\n for (var label in state.labelVisibility) {\n if (state.labelVisibility.hasOwnProperty(label)) {\n state.labelVisibility[label] = true;\n }\n }\n for (var i = 0; i < state.visibility.length; i++) {\n state.visibility[i] = true;\n }\n\n state.selectedIndex = -1;\n state.expandedIndex = -1;\n state.sortMode = getDefaultSortMode();\n state.zoom = 1;\n state.panX = 0;\n state.panY = 0;\n canvas.style.cursor = \"default\";\n\n render();\n renderControlPanel();\n }\n\n // \u2500\u2500 Rendering \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\n function isAnnotationVisible(i) {\n if (!state.visibility[i]) return false;\n var ann = state.annotations[i];\n if (ann.score != null && (ann.score < state.thresholdMin || ann.score > state.thresholdMax)) return false;\n if (ann.label && state.labelVisibility.hasOwnProperty(ann.label) && !state.labelVisibility[ann.label]) return false;\n return true;\n }\n\n function isKeypointVisible(kp) {\n if (kp.x == null || kp.y == null) return false;\n if (kp.confidence != null && kp.confidence < state.keypointThreshold) return false;\n return true;\n }\n\n function render() {\n if (!state.image) return;\n\n // Clear in screen space\n ctx.setTransform(1, 0, 0, 1, 0, 0);\n ctx.clearRect(0, 0, canvas.width, canvas.height);\n\n // Dark background visible when zoomed/panned\n if (state.zoom > 1) {\n ctx.fillStyle = \"#1a1a1a\";\n ctx.fillRect(0, 0, canvas.width, canvas.height);\n }\n\n // Apply zoom+pan transform\n ctx.setTransform(state.zoom, 0, 0, state.zoom, state.panX, state.panY);\n\n if (state.showImage) {\n ctx.drawImage(state.image, 0, 0, canvas.width, canvas.height);\n } else {\n ctx.fillStyle = \"#2a2a2a\";\n ctx.fillRect(0, 0, canvas.width, canvas.height);\n }\n\n var hasSelection = state.selectedIndex >= 0;\n\n // Draw order: masks (back) \u2192 bbox \u2192 connections \u2192 keypoints (front)\n for (var pass = 0; pass < 4; pass++) {\n for (var i = 0; i < state.annotations.length; i++) {\n if (!isAnnotationVisible(i)) continue;\n\n var ann = state.annotations[i];\n var dim = hasSelection && i !== state.selectedIndex;\n\n ctx.save();\n if (dim) ctx.globalAlpha = DIM_ALPHA;\n\n if (pass === 0 && state.layers.masks) drawMask(i);\n else if (pass === 1 && state.layers.boxes) drawBbox(ann);\n else if (pass === 2 && state.layers.skeleton) drawConnections(ann);\n else if (pass === 3 && state.layers.keypoints) drawKeypoints(ann);\n\n ctx.restore();\n }\n }\n\n // Draw zoom indicator in screen space\n ctx.setTransform(1, 0, 0, 1, 0, 0);\n if (state.zoom > 1) {\n var zoomText = Math.round(state.zoom * 100) + \"%\";\n ctx.font = \"bold 12px -apple-system, BlinkMacSystemFont, sans-serif\";\n var tm = ctx.measureText(zoomText);\n var px = canvas.width - tm.width - 12;\n var py = 8;\n ctx.fillStyle = \"rgba(0,0,0,0.5)\";\n ctx.beginPath();\n ctx.roundRect(px - 6, py - 2, tm.width + 12, 20, 4);\n ctx.fill();\n ctx.fillStyle = \"#fff\";\n ctx.textBaseline = \"top\";\n ctx.fillText(zoomText, px, py + 2);\n }\n }\n\n function drawMask(i) {\n var maskImg = state.maskImages[i];\n if (!maskImg) return;\n var baseAlpha = ctx.globalAlpha;\n ctx.globalAlpha = baseAlpha * state.maskAlpha;\n ctx.drawImage(maskImg, 0, 0, canvas.width, canvas.height);\n ctx.globalAlpha = baseAlpha;\n }\n\n function drawBbox(ann) {\n var bbox = ann.bbox;\n if (!bbox) return;\n\n var x = bbox.x * state.scale;\n var y = bbox.y * state.scale;\n var w = bbox.width * state.scale;\n var h = bbox.height * state.scale;\n var baseAlpha = ctx.globalAlpha;\n var iz = 1 / state.zoom;\n\n ctx.strokeStyle = ann.color;\n ctx.lineWidth = state.bboxLineWidth * iz;\n ctx.strokeRect(x, y, w, h);\n\n // Label + score text\n var labelText = ann.label || \"\";\n if (ann.score != null) {\n labelText += (labelText ? \" \" : \"\") + (ann.score * 100).toFixed(1) + \"%\";\n }\n if (labelText) {\n var fontSize = 11 * iz;\n ctx.font = \"bold \" + fontSize + \"px -apple-system, BlinkMacSystemFont, sans-serif\";\n var textMetrics = ctx.measureText(labelText);\n var textH = 16 * iz;\n var pad = 4 * iz;\n var bgW = textMetrics.width + pad * 2;\n var bgH = textH + pad;\n\n // Place label above bbox; if clipped, place inside top edge\n var labelAbove = y - bgH >= 0;\n var bgY = labelAbove ? y - bgH : y;\n var textY = labelAbove ? y - pad / 2 : y + bgH - pad / 2;\n\n // Semi-transparent background\n ctx.fillStyle = ann.color;\n ctx.globalAlpha = baseAlpha * 0.7;\n ctx.fillRect(x, bgY, bgW, bgH);\n\n // White text\n ctx.globalAlpha = baseAlpha;\n ctx.fillStyle = \"#ffffff\";\n ctx.textBaseline = \"bottom\";\n ctx.fillText(labelText, x + pad, textY);\n }\n }\n\n function drawConnections(ann) {\n var kps = ann.keypoints;\n var conns = ann.connections;\n if (!conns || conns.length === 0 || !kps || kps.length === 0) return;\n\n var baseAlpha = ctx.globalAlpha;\n ctx.strokeStyle = ann.color;\n ctx.lineWidth = state.connectionWidth / state.zoom;\n ctx.globalAlpha = baseAlpha * state.connectionAlpha;\n\n for (var i = 0; i < conns.length; i++) {\n var idxA = conns[i][0];\n var idxB = conns[i][1];\n if (idxA < 0 || idxA >= kps.length || idxB < 0 || idxB >= kps.length) continue;\n\n var a = kps[idxA];\n var b = kps[idxB];\n\n if (!isKeypointVisible(a) || !isKeypointVisible(b)) continue;\n\n ctx.beginPath();\n ctx.moveTo(a.x * state.scale, a.y * state.scale);\n ctx.lineTo(b.x * state.scale, b.y * state.scale);\n ctx.stroke();\n }\n\n ctx.globalAlpha = baseAlpha;\n }\n\n function drawKeypoints(ann) {\n var kps = ann.keypoints;\n if (!kps || kps.length === 0) return;\n\n var iz = 1 / state.zoom;\n for (var i = 0; i < kps.length; i++) {\n var kp = kps[i];\n if (!isKeypointVisible(kp)) continue;\n\n var cx = kp.x * state.scale;\n var cy = kp.y * state.scale;\n\n // White border\n ctx.beginPath();\n ctx.arc(cx, cy, (state.keypointRadius + 1) * iz, 0, 2 * Math.PI);\n ctx.fillStyle = \"#ffffff\";\n ctx.fill();\n\n // Colored fill\n ctx.beginPath();\n ctx.arc(cx, cy, state.keypointRadius * iz, 0, 2 * Math.PI);\n ctx.fillStyle = ann.color;\n ctx.fill();\n }\n }\n\n // \u2500\u2500 RLE Decode \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\n function createMaskCanvas(rle, colorHex) {\n var counts = rle.counts;\n var h = rle.size[0], w = rle.size[1];\n var r = parseInt(colorHex.slice(1, 3), 16);\n var g = parseInt(colorHex.slice(3, 5), 16);\n var b = parseInt(colorHex.slice(5, 7), 16);\n var offscreen = document.createElement(\"canvas\");\n offscreen.width = w;\n offscreen.height = h;\n var offCtx = offscreen.getContext(\"2d\");\n var imageData = offCtx.createImageData(w, h);\n var data = imageData.data;\n // RLE is column-major (COCO format), convert to row-major ImageData\n var pos = 0;\n for (var i = 0; i < counts.length; i++) {\n var c = counts[i];\n if (i % 2 === 1) {\n var end = pos + c;\n for (var j = pos; j < end; j++) {\n var row = j % h;\n var col = (j / h) | 0;\n var idx = (row * w + col) * 4;\n data[idx] = r;\n data[idx + 1] = g;\n data[idx + 2] = b;\n data[idx + 3] = 255;\n }\n }\n pos += c;\n }\n offCtx.putImageData(imageData, 0, 0);\n return offscreen;\n }\n\n // \u2500\u2500 Helpers \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\n function buildAnnotationSummary(ann) {\n var parts = [];\n if (ann.mask) {\n parts.push(\"mask\");\n }\n if (ann.bbox) {\n parts.push(\"bbox\");\n }\n var kps = ann.keypoints || [];\n if (kps.length > 0) {\n var validCount = 0;\n for (var j = 0; j < kps.length; j++) {\n if (isKeypointVisible(kps[j])) validCount++;\n }\n parts.push(validCount + \" pts\");\n }\n if (ann.score != null) {\n parts.push((ann.score * 100).toFixed(1) + \"%\");\n }\n return parts.join(\", \") || \"empty\";\n }\n\n function escapeHtml(text) {\n var div = document.createElement(\"div\");\n div.textContent = text;\n return div.innerHTML;\n }\n\n function getLabelColor(label) {\n // Return the color of the first annotation with this label\n for (var i = 0; i < state.annotations.length; i++) {\n if (state.annotations[i].label === label) {\n return state.annotations[i].color;\n }\n }\n return \"#888\";\n }\n\n function getDefaultSortMode() {\n var hasScores = false;\n var hasBboxes = false;\n for (var i = 0; i < state.annotations.length; i++) {\n if (state.annotations[i].score != null) hasScores = true;\n if (state.annotations[i].bbox) hasBboxes = true;\n if (hasScores && hasBboxes) break;\n }\n if (hasScores) return \"score-desc\";\n if (hasBboxes) return \"size-desc\";\n return \"none\";\n }\n\n function bboxArea(ann) {\n if (!ann.bbox) return null;\n return ann.bbox.width * ann.bbox.height;\n }\n\n function computeSortedIndices() {\n var n = state.annotations.length;\n var indices = [];\n for (var i = 0; i < n; i++) indices.push(i);\n\n var mode = state.sortMode;\n if (mode === \"score-desc\" || mode === \"score-asc\") {\n var dir = mode === \"score-desc\" ? -1 : 1;\n indices.sort(function (a, b) {\n var sa = state.annotations[a].score;\n var sb = state.annotations[b].score;\n var hasA = sa != null;\n var hasB = sb != null;\n if (hasA && hasB) return (sa - sb) * dir || a - b;\n if (hasA) return -1;\n if (hasB) return 1;\n return a - b;\n });\n } else if (mode === \"size-desc\" || mode === \"size-asc\") {\n var dir = mode === \"size-desc\" ? -1 : 1;\n indices.sort(function (a, b) {\n var aa = bboxArea(state.annotations[a]);\n var ab = bboxArea(state.annotations[b]);\n var hasA = aa != null;\n var hasB = ab != null;\n if (hasA && hasB) return (aa - ab) * dir || a - b;\n if (hasA) return -1;\n if (hasB) return 1;\n return a - b;\n });\n }\n\n // Stable partition: visible first, hidden last (only when some label filter is OFF)\n var anyLabelFiltered = false;\n var labels = Object.keys(state.labelVisibility);\n for (var li = 0; li < labels.length; li++) {\n if (!state.labelVisibility[labels[li]]) { anyLabelFiltered = true; break; }\n }\n if (anyLabelFiltered) {\n var visible = [];\n var hidden = [];\n for (var j = 0; j < indices.length; j++) {\n var idx = indices[j];\n var ann = state.annotations[idx];\n var labelHidden = ann.label && state.labelVisibility.hasOwnProperty(ann.label) && !state.labelVisibility[ann.label];\n if (labelHidden) hidden.push(idx);\n else visible.push(idx);\n }\n indices = visible.concat(hidden);\n }\n\n state.sortedIndices = indices;\n }\n\n function handleSortToggle(e) {\n var key = e.currentTarget.getAttribute(\"data-sort-key\");\n if (!key) return;\n // Toggle direction if same key is already active\n if (state.sortMode === key + \"-desc\") {\n state.sortMode = key + \"-asc\";\n } else if (state.sortMode === key + \"-asc\") {\n state.sortMode = key + \"-desc\";\n } else {\n state.sortMode = key + \"-desc\";\n }\n computeSortedIndices();\n renderControlPanel();\n }\n\n function updateHeaderStats() {\n if (!countEl) return;\n\n var total = state.annotations.length;\n var visibleCount = 0;\n var minScore = Infinity;\n var maxScore = -Infinity;\n var hasScores = false;\n\n for (var i = 0; i < state.annotations.length; i++) {\n if (!isAnnotationVisible(i)) continue;\n visibleCount++;\n var ann = state.annotations[i];\n if (ann.score != null) {\n hasScores = true;\n if (ann.score < minScore) minScore = ann.score;\n if (ann.score > maxScore) maxScore = ann.score;\n }\n }\n\n var text = visibleCount + \" / \" + total;\n if (hasScores && visibleCount > 0) {\n text += \" \\u00B7 \" + (minScore * 100).toFixed(0) + \"\\u2013\" + (maxScore * 100).toFixed(0) + \"%\";\n }\n countEl.textContent = text;\n }\n\n // \u2500\u2500 Control Panel \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\n function renderControlPanel() {\n if (state.annotations.length === 0) {\n controlPanel.classList.remove(\"visible\");\n annotationList.innerHTML = \"\";\n return;\n }\n\n computeSortedIndices();\n\n var html = \"\";\n\n // Detect which layer types are present in annotations\n var availableLayers = [];\n var hasMasks = false, hasBoxes = false, hasSkeleton = false, hasKeypoints = false;\n for (var i = 0; i < state.annotations.length; i++) {\n var ann = state.annotations[i];\n if (ann.mask) hasMasks = true;\n if (ann.bbox) hasBoxes = true;\n if (ann.connections && ann.connections.length > 0) hasSkeleton = true;\n if (ann.keypoints && ann.keypoints.length > 0) hasKeypoints = true;\n }\n if (hasMasks) availableLayers.push(\"masks\");\n if (hasBoxes) availableLayers.push(\"boxes\");\n if (hasSkeleton) availableLayers.push(\"skeleton\");\n if (hasKeypoints) availableLayers.push(\"keypoints\");\n\n var layerLabels = { masks: \"Masks\", boxes: \"Boxes\", skeleton: \"Skeleton\", keypoints: \"Keypoints\" };\n\n // Only show layer toggles when 2+ types are present\n if (availableLayers.length >= 2) {\n html += '
';\n for (var i = 0; i < availableLayers.length; i++) {\n var layer = availableLayers[i];\n html += '';\n }\n html += '
';\n }\n\n // Label filters with counts\n var labels = Object.keys(state.labelVisibility);\n if (labels.length > 0) {\n var labelTotalCounts = {};\n for (var i = 0; i < state.annotations.length; i++) {\n var lbl = state.annotations[i].label;\n if (lbl) labelTotalCounts[lbl] = (labelTotalCounts[lbl] || 0) + 1;\n }\n html += '
';\n html += 'Labels';\n for (var li = 0; li < labels.length; li++) {\n var lbl = labels[li];\n var lblActive = state.labelVisibility[lbl];\n var lblColor = getLabelColor(lbl);\n html += '';\n }\n html += '
';\n }\n\n // Sort controls\n var hasScoresForSort = false;\n var hasBboxesForSort = false;\n for (var si = 0; si < state.annotations.length; si++) {\n if (state.annotations[si].score != null) hasScoresForSort = true;\n if (state.annotations[si].bbox) hasBboxesForSort = true;\n }\n if (hasScoresForSort || hasBboxesForSort) {\n var sortButtons = [];\n if (hasScoresForSort) sortButtons.push(\"score\");\n if (hasBboxesForSort) sortButtons.push(\"size\");\n // Only show controls when 2+ sort options exist\n if (sortButtons.length >= 2) {\n var sortLabels = { score: \"Score\", size: \"Size\" };\n html += '
';\n html += 'Sort';\n for (var sbi = 0; sbi < sortButtons.length; sbi++) {\n var sk = sortButtons[sbi];\n var isDesc = state.sortMode === sk + \"-desc\";\n var isAsc = state.sortMode === sk + \"-asc\";\n var isActive = isDesc || isAsc;\n var arrow = isActive ? (isDesc ? \" \\u25BC\" : \" \\u25B2\") : \"\";\n html += '';\n }\n html += '
';\n }\n }\n\n // Dual-thumb score threshold slider\n html += '
';\n html += 'Score';\n html += '
';\n html += '';\n html += '';\n html += '
';\n html += '' + Math.round(state.thresholdMin * 100) + '%\\u2013' + Math.round(state.thresholdMax * 100) + '%';\n html += '
';\n\n // Keypoint threshold slider (only when annotations have keypoints)\n if (hasKeypoints) {\n html += '
';\n html += 'Keypoint ≥';\n html += '';\n html += '' + Math.round(state.keypointThreshold * 100) + '%';\n html += '
';\n }\n\n // Collapsible draw options section\n var drawOptionsHtml = '';\n if (hasMasks) {\n drawOptionsHtml += '
';\n drawOptionsHtml += 'Mask Opacity';\n drawOptionsHtml += '';\n drawOptionsHtml += '' + Math.round(state.maskAlpha * 100) + '%';\n drawOptionsHtml += '
';\n }\n if (hasKeypoints) {\n drawOptionsHtml += '
';\n drawOptionsHtml += 'Keypoint Size';\n drawOptionsHtml += '';\n drawOptionsHtml += '' + state.keypointRadius + '';\n drawOptionsHtml += '
';\n }\n if (hasSkeleton) {\n drawOptionsHtml += '
';\n drawOptionsHtml += 'Line Width';\n drawOptionsHtml += '';\n drawOptionsHtml += '' + state.connectionWidth + '';\n drawOptionsHtml += '
';\n drawOptionsHtml += '
';\n drawOptionsHtml += 'Line Opacity';\n drawOptionsHtml += '';\n drawOptionsHtml += '' + Math.round(state.connectionAlpha * 100) + '%';\n drawOptionsHtml += '
';\n }\n if (hasBoxes) {\n drawOptionsHtml += '
';\n drawOptionsHtml += 'Box Width';\n drawOptionsHtml += '';\n drawOptionsHtml += '' + state.bboxLineWidth + '';\n drawOptionsHtml += '
';\n }\n if (drawOptionsHtml) {\n html += '
';\n html += '';\n html += '
' + drawOptionsHtml + '
';\n html += '
';\n }\n\n // Select-all row + scrollable annotation rows container\n html += '
';\n\n var allChecked = true;\n var anyChecked = false;\n for (var i = 0; i < state.visibility.length; i++) {\n if (state.visibility[i]) anyChecked = true;\n else allChecked = false;\n }\n html += '
';\n html += '';\n html += 'All';\n html += '
';\n\n // Annotation rows (iterate in sorted/grouped order)\n var anyLabelFiltered = false;\n var labelKeys = Object.keys(state.labelVisibility);\n for (var li = 0; li < labelKeys.length; li++) {\n if (!state.labelVisibility[labelKeys[li]]) { anyLabelFiltered = true; break; }\n }\n var separatorInserted = false;\n\n for (var si = 0; si < state.sortedIndices.length; si++) {\n var i = state.sortedIndices[si];\n var ann = state.annotations[i];\n var visible = state.visibility[i];\n var selected = state.selectedIndex === i;\n var summary = buildAnnotationSummary(ann);\n var outsideRange = ann.score != null && (ann.score < state.thresholdMin || ann.score > state.thresholdMax);\n var labelHidden = ann.label && state.labelVisibility.hasOwnProperty(ann.label) && !state.labelVisibility[ann.label];\n\n // Insert separator before the first label-hidden item\n if (anyLabelFiltered && labelHidden && !separatorInserted) {\n html += '
Hidden
';\n separatorInserted = true;\n }\n\n var expanded = state.expandedIndex === i;\n\n html += '
';\n html += '';\n html += '';\n html += '' + escapeHtml(ann.label) + '';\n html += '' + escapeHtml(summary) + '';\n html += '';\n html += '
';\n\n // Detail panel (shown when expand button is clicked)\n html += '
';\n html += buildDetailHtml(ann);\n html += '
';\n }\n\n html += '
'; // close .annotation-rows\n\n annotationList.innerHTML = html;\n controlPanel.classList.add(\"visible\");\n updateHeaderStats();\n\n // Initialize dual-range track highlight\n var dualWrapper = annotationList.querySelector(\".dual-range-wrapper\");\n if (dualWrapper) updateDualRangeTrack(dualWrapper);\n\n // Bind layer toggle events\n var layerBtns = annotationList.querySelectorAll(\".layer-btn\");\n for (var i = 0; i < layerBtns.length; i++) {\n layerBtns[i].addEventListener(\"click\", handleLayerToggle);\n }\n\n // Bind label filter events (single-click: toggle, double-click: solo)\n var labelBtns = annotationList.querySelectorAll(\".label-filter-btn\");\n for (var i = 0; i < labelBtns.length; i++) {\n labelBtns[i].addEventListener(\"click\", handleLabelFilterClick);\n labelBtns[i].addEventListener(\"dblclick\", handleLabelFilterDblClick);\n }\n\n // Bind sort button events\n var sortBtns = annotationList.querySelectorAll(\".sort-btn\");\n for (var i = 0; i < sortBtns.length; i++) {\n sortBtns[i].addEventListener(\"click\", handleSortToggle);\n }\n\n // Bind dual threshold sliders\n var sliderMin = annotationList.querySelector(\".threshold-slider-min\");\n var sliderMax = annotationList.querySelector(\".threshold-slider-max\");\n if (sliderMin) sliderMin.addEventListener(\"input\", handleThresholdMinChange);\n if (sliderMax) sliderMax.addEventListener(\"input\", handleThresholdMaxChange);\n\n // Bind keypoint threshold slider\n var kpSlider = annotationList.querySelector(\".keypoint-threshold-slider\");\n if (kpSlider) {\n kpSlider.addEventListener(\"input\", handleKeypointThresholdChange);\n }\n\n // Bind draw options toggle\n var drawToggle = annotationList.querySelector(\".draw-options-toggle\");\n if (drawToggle) {\n drawToggle.addEventListener(\"click\", function () {\n var section = drawToggle.closest(\".draw-options\");\n section.classList.toggle(\"open\");\n state.drawOptionsOpen = section.classList.contains(\"open\");\n });\n }\n\n // Bind visual parameter sliders\n var maSlider = annotationList.querySelector(\".mask-alpha-slider\");\n if (maSlider) {\n maSlider.addEventListener(\"input\", handleMaskAlphaChange);\n }\n var krSlider = annotationList.querySelector(\".keypoint-radius-slider\");\n if (krSlider) {\n krSlider.addEventListener(\"input\", handleKeypointRadiusChange);\n }\n var cwSlider = annotationList.querySelector(\".connection-width-slider\");\n if (cwSlider) {\n cwSlider.addEventListener(\"input\", handleConnectionWidthChange);\n }\n var caSlider = annotationList.querySelector(\".connection-alpha-slider\");\n if (caSlider) {\n caSlider.addEventListener(\"input\", handleConnectionAlphaChange);\n }\n var blwSlider = annotationList.querySelector(\".bbox-line-width-slider\");\n if (blwSlider) {\n blwSlider.addEventListener(\"input\", handleBboxLineWidthChange);\n }\n\n // Initialize single slider track fills\n var singleSliders = annotationList.querySelectorAll('.threshold-row input[type=\"range\"]:not(.threshold-slider-min):not(.threshold-slider-max)');\n for (var i = 0; i < singleSliders.length; i++) {\n updateSliderTrack(singleSliders[i]);\n }\n\n // Bind select-all checkbox\n var selectAllCb = annotationList.querySelector(\".select-all-checkbox\");\n if (selectAllCb) {\n // Set indeterminate state: some checked but not all\n if (anyChecked && !allChecked) {\n selectAllCb.indeterminate = true;\n }\n selectAllCb.addEventListener(\"change\", function (e) {\n var newVal = e.target.checked;\n for (var i = 0; i < state.visibility.length; i++) {\n state.visibility[i] = newVal;\n }\n render();\n renderControlPanel();\n });\n }\n\n // Bind checkbox events\n var checkboxes = annotationList.querySelectorAll(\".ann-checkbox\");\n for (var i = 0; i < checkboxes.length; i++) {\n checkboxes[i].addEventListener(\"change\", handleCheckboxChange);\n checkboxes[i].addEventListener(\"click\", function (e) { e.stopPropagation(); });\n }\n\n // Bind expand button events\n var expandBtns = annotationList.querySelectorAll(\".ann-expand\");\n for (var i = 0; i < expandBtns.length; i++) {\n expandBtns[i].addEventListener(\"click\", handleExpandClick);\n }\n\n // Bind row click events\n var rows = annotationList.querySelectorAll(\".annotation-row\");\n for (var i = 0; i < rows.length; i++) {\n rows[i].addEventListener(\"click\", handleRowClick);\n }\n }\n\n function buildDetailHtml(ann) {\n var html = \"\";\n if (ann.bbox) {\n html += '
Bounding Box
';\n html += '';\n html += '';\n html += '';\n if (ann.score != null) {\n html += '';\n }\n html += '
Position:' + ann.bbox.x.toFixed(1) + ', ' + ann.bbox.y.toFixed(1) + '
Size:' + ann.bbox.width.toFixed(1) + ' × ' + ann.bbox.height.toFixed(1) + '
Score:' + (ann.score * 100).toFixed(1) + '%
';\n }\n var kps = ann.keypoints || [];\n if (kps.length > 0) {\n html += '
Keypoints
';\n html += '';\n for (var j = 0; j < kps.length; j++) {\n var kp = kps[j];\n var name = kp.name || (\"kp\" + j);\n var coords = (kp.x != null && kp.y != null) ? kp.x.toFixed(1) + ', ' + kp.y.toFixed(1) : \"missing\";\n var conf = kp.confidence != null ? (kp.confidence * 100).toFixed(1) + '%' : \"\";\n html += '';\n }\n html += '
' + escapeHtml(name) + '' + coords + '' + conf + '
';\n }\n return html || 'No details available';\n }\n\n // \u2500\u2500 Event Handlers \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\n function handleLayerToggle(e) {\n var layer = e.target.getAttribute(\"data-layer\");\n if (layer && state.layers.hasOwnProperty(layer)) {\n state.layers[layer] = !state.layers[layer];\n render();\n renderControlPanel();\n }\n }\n\n var labelClickTimer = null;\n\n function handleLabelFilterClick(e) {\n var label = e.currentTarget.getAttribute(\"data-label\");\n if (!label || !state.labelVisibility.hasOwnProperty(label)) return;\n if (labelClickTimer) clearTimeout(labelClickTimer);\n labelClickTimer = setTimeout(function () {\n labelClickTimer = null;\n var newVal = !state.labelVisibility[label];\n state.labelVisibility[label] = newVal;\n for (var i = 0; i < state.annotations.length; i++) {\n if (state.annotations[i].label === label) {\n state.visibility[i] = newVal;\n }\n }\n render();\n renderControlPanel();\n }, 200);\n }\n\n function handleLabelFilterDblClick(e) {\n e.preventDefault();\n if (labelClickTimer) {\n clearTimeout(labelClickTimer);\n labelClickTimer = null;\n }\n var label = e.currentTarget.getAttribute(\"data-label\");\n if (!label || !state.labelVisibility.hasOwnProperty(label)) return;\n\n // Check if this label is already solo (only this one is ON)\n var labels = Object.keys(state.labelVisibility);\n var onlyThisOn = labels.every(function (l) {\n return l === label ? state.labelVisibility[l] : !state.labelVisibility[l];\n });\n\n if (onlyThisOn) {\n // Unsolo: turn all labels ON\n for (var li = 0; li < labels.length; li++) {\n state.labelVisibility[labels[li]] = true;\n }\n for (var i = 0; i < state.annotations.length; i++) {\n state.visibility[i] = true;\n }\n } else {\n // Solo: turn only this label ON\n for (var li = 0; li < labels.length; li++) {\n state.labelVisibility[labels[li]] = (labels[li] === label);\n }\n for (var i = 0; i < state.annotations.length; i++) {\n state.visibility[i] = (state.annotations[i].label === label);\n }\n }\n render();\n renderControlPanel();\n }\n\n function updateThresholdUI() {\n render();\n var label = annotationList.querySelector(\".threshold-value\");\n if (label) label.textContent = Math.round(state.thresholdMin * 100) + '%\\u2013' + Math.round(state.thresholdMax * 100) + '%';\n // Update row opacity for outside-range items\n var rows = annotationList.querySelectorAll(\".annotation-row\");\n for (var i = 0; i < rows.length; i++) {\n var idx = parseInt(rows[i].getAttribute(\"data-index\"), 10);\n var ann = state.annotations[idx];\n var outsideRange = ann.score != null && (ann.score < state.thresholdMin || ann.score > state.thresholdMax);\n if (outsideRange) {\n rows[i].classList.add(\"below-threshold\");\n } else {\n rows[i].classList.remove(\"below-threshold\");\n }\n }\n // Update track highlight\n var wrapper = annotationList.querySelector(\".dual-range-wrapper\");\n if (wrapper) updateDualRangeTrack(wrapper);\n updateHeaderStats();\n }\n\n function handleThresholdMinChange(e) {\n var val = parseInt(e.target.value, 10) / 100;\n state.thresholdMin = Math.min(val, state.thresholdMax);\n e.target.value = Math.round(state.thresholdMin * 100);\n updateThresholdUI();\n }\n\n function handleThresholdMaxChange(e) {\n var val = parseInt(e.target.value, 10) / 100;\n state.thresholdMax = Math.max(val, state.thresholdMin);\n e.target.value = Math.round(state.thresholdMax * 100);\n updateThresholdUI();\n }\n\n function updateSliderTrack(slider) {\n var min = parseFloat(slider.min) || 0;\n var max = parseFloat(slider.max) || 100;\n var val = parseFloat(slider.value) || 0;\n var pct = ((val - min) / (max - min)) * 100;\n var t = 'calc(50% - 2px)';\n var b = 'calc(50% + 2px)';\n slider.style.background =\n 'linear-gradient(to bottom, transparent ' + t + ', var(--color-accent, #2196F3) ' + t + ', var(--color-accent, #2196F3) ' + b + ', transparent ' + b + ') 0 0 / ' + pct + '% 100% no-repeat, ' +\n 'linear-gradient(to bottom, transparent ' + t + ', var(--border-color-primary, #d0d0d0) ' + t + ', var(--border-color-primary, #d0d0d0) ' + b + ', transparent ' + b + ')';\n }\n\n function updateDualRangeTrack(wrapper) {\n var minVal = Math.round(state.thresholdMin * 100);\n var maxVal = Math.round(state.thresholdMax * 100);\n wrapper.style.background = 'linear-gradient(to right, var(--border-color-primary, #d0d0d0) ' + minVal + '%, var(--color-accent, #2196F3) ' + minVal + '%, var(--color-accent, #2196F3) ' + maxVal + '%, var(--border-color-primary, #d0d0d0) ' + maxVal + '%)';\n }\n\n function handleKeypointThresholdChange(e) {\n state.keypointThreshold = parseInt(e.target.value, 10) / 100;\n render();\n updateSliderTrack(e.target);\n // Update keypoint threshold label\n var label = annotationList.querySelector(\".keypoint-threshold-value\");\n if (label) label.textContent = Math.round(state.keypointThreshold * 100) + '%';\n updateHeaderStats();\n // Update annotation summaries\n var summaryEls = annotationList.querySelectorAll(\".ann-summary\");\n for (var i = 0; i < summaryEls.length; i++) {\n var row = summaryEls[i].closest(\".annotation-row\");\n if (row) {\n var idx = parseInt(row.getAttribute(\"data-index\"), 10);\n summaryEls[i].textContent = buildAnnotationSummary(state.annotations[idx]);\n }\n }\n }\n\n function handleKeypointRadiusChange(e) {\n state.keypointRadius = parseInt(e.target.value, 10);\n render();\n updateSliderTrack(e.target);\n var label = annotationList.querySelector(\".keypoint-radius-value\");\n if (label) label.textContent = state.keypointRadius;\n }\n\n function handleConnectionWidthChange(e) {\n state.connectionWidth = parseInt(e.target.value, 10);\n render();\n updateSliderTrack(e.target);\n var label = annotationList.querySelector(\".connection-width-value\");\n if (label) label.textContent = state.connectionWidth;\n }\n\n function handleMaskAlphaChange(e) {\n state.maskAlpha = parseInt(e.target.value, 10) / 100;\n render();\n updateSliderTrack(e.target);\n var label = annotationList.querySelector(\".mask-alpha-value\");\n if (label) label.textContent = Math.round(state.maskAlpha * 100) + '%';\n }\n\n function handleConnectionAlphaChange(e) {\n state.connectionAlpha = parseInt(e.target.value, 10) / 100;\n render();\n updateSliderTrack(e.target);\n var label = annotationList.querySelector(\".connection-alpha-value\");\n if (label) label.textContent = Math.round(state.connectionAlpha * 100) + '%';\n }\n\n function handleBboxLineWidthChange(e) {\n state.bboxLineWidth = parseInt(e.target.value, 10);\n render();\n updateSliderTrack(e.target);\n var label = annotationList.querySelector(\".bbox-line-width-value\");\n if (label) label.textContent = state.bboxLineWidth;\n }\n\n function handleExpandClick(e) {\n e.stopPropagation();\n var idx = parseInt(e.currentTarget.getAttribute(\"data-index\"), 10);\n if (state.expandedIndex === idx) {\n state.expandedIndex = -1;\n } else {\n state.expandedIndex = idx;\n }\n renderControlPanel();\n }\n\n function handleCheckboxChange(e) {\n var idx = parseInt(e.target.getAttribute(\"data-index\"), 10);\n state.visibility[idx] = e.target.checked;\n render();\n updateHeaderStats();\n }\n\n function handleRowClick(e) {\n var idx = parseInt(e.currentTarget.getAttribute(\"data-index\"), 10);\n if (state.selectedIndex === idx) {\n state.selectedIndex = -1;\n } else {\n state.selectedIndex = idx;\n }\n render();\n renderControlPanel();\n }\n\n // Toggle base image\n toggleImageBtn.addEventListener(\"click\", function () {\n state.showImage = !state.showImage;\n toggleImageBtn.classList.toggle(\"active\", state.showImage);\n render();\n });\n\n // Reset to defaults\n resetBtn.addEventListener(\"click\", resetToDefaults);\n\n // Maximize / minimize\n function toggleMaximize() {\n state.maximized = !state.maximized;\n container.classList.toggle(\"maximized\", state.maximized);\n\n if (state.maximized) {\n document.body.style.overflow = \"hidden\";\n } else {\n document.body.style.overflow = \"\";\n }\n\n if (state.image) {\n requestAnimationFrame(function () {\n fitCanvas();\n resetZoom();\n });\n }\n }\n\n maximizeBtn.addEventListener(\"click\", toggleMaximize);\n\n // Help dialog\n function toggleHelp() {\n helpOverlay.classList.toggle(\"visible\");\n }\n\n helpBtn.addEventListener(\"click\", toggleHelp);\n helpCloseBtn.addEventListener(\"click\", toggleHelp);\n helpOverlay.addEventListener(\"click\", function (e) {\n if (e.target === helpOverlay) toggleHelp();\n });\n\n // \u2500\u2500 Canvas Mouse Interaction (Pan + Selection) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\n canvas.addEventListener(\"mousedown\", function (e) {\n if (e.button !== 0) return;\n if (!state.image) return;\n\n state.isPanning = true;\n state.didDrag = false;\n state.panStartX = e.clientX;\n state.panStartY = e.clientY;\n state.panStartPanX = state.panX;\n state.panStartPanY = state.panY;\n\n if (state.zoom > 1) {\n canvas.style.cursor = \"grabbing\";\n }\n });\n\n window.addEventListener(\"mousemove\", function (e) {\n if (!state.image) return;\n\n if (state.isPanning) {\n var dx = e.clientX - state.panStartX;\n var dy = e.clientY - state.panStartY;\n\n if (!state.didDrag && (Math.abs(dx) > DRAG_THRESHOLD || Math.abs(dy) > DRAG_THRESHOLD)) {\n state.didDrag = true;\n }\n\n if (state.didDrag && state.zoom > 1) {\n var rect = canvas.getBoundingClientRect();\n var cssToCanvasX = canvas.width / rect.width;\n var cssToCanvasY = canvas.height / rect.height;\n state.panX = state.panStartPanX + dx * cssToCanvasX;\n state.panY = state.panStartPanY + dy * cssToCanvasY;\n clampPan();\n render();\n }\n return;\n }\n\n // Tooltip logic (only when not dragging)\n var rect = canvas.getBoundingClientRect();\n if (e.clientX < rect.left || e.clientX > rect.right || e.clientY < rect.top || e.clientY > rect.bottom) return;\n if (state.annotations.length === 0) return;\n\n var pt = clientToCanvas(e.clientX, e.clientY);\n var cx = pt.x;\n var cy = pt.y;\n\n // Update cursor\n var hitIndex = findHitAnnotationIndex(cx, cy);\n if (state.zoom > 1) {\n canvas.style.cursor = hitIndex >= 0 ? \"pointer\" : \"grab\";\n } else {\n canvas.style.cursor = hitIndex >= 0 ? \"pointer\" : \"default\";\n }\n\n var nearest = findNearestKeypoint(cx, cy);\n var tooltipText = \"\";\n if (nearest) {\n tooltipText = nearest.kp.name;\n if (nearest.kp.confidence != null) {\n tooltipText += \" (\" + (nearest.kp.confidence * 100).toFixed(1) + \"%)\";\n }\n } else {\n var bboxHit = findBboxAt(cx, cy);\n if (bboxHit) {\n tooltipText = bboxHit.label || \"\";\n if (bboxHit.score != null) {\n tooltipText += (tooltipText ? \" \" : \"\") + (bboxHit.score * 100).toFixed(1) + \"%\";\n }\n }\n }\n\n if (tooltipText) {\n tooltip.textContent = tooltipText;\n tooltip.classList.add(\"visible\");\n\n var containerRect = container.getBoundingClientRect();\n var tooltipX = e.clientX - containerRect.left + 12;\n var tooltipY = e.clientY - containerRect.top - 8;\n tooltip.style.left = tooltipX + \"px\";\n tooltip.style.top = tooltipY + \"px\";\n } else {\n tooltip.classList.remove(\"visible\");\n }\n });\n\n window.addEventListener(\"mouseup\", function (e) {\n if (!state.isPanning) return;\n state.isPanning = false;\n\n if (state.zoom > 1) {\n canvas.style.cursor = \"grab\";\n }\n\n if (!state.didDrag && state.image && state.annotations.length > 0) {\n var pt = clientToCanvas(e.clientX, e.clientY);\n var hits = findAllHitAnnotationIndices(pt.x, pt.y);\n if (e.shiftKey && hits.length > 0) {\n // Shift+click: hide the topmost hit\n var target = hits[0];\n state.visibility[target] = false;\n if (state.selectedIndex === target) state.selectedIndex = -1;\n } else if (hits.length === 0) {\n state.selectedIndex = -1;\n } else if (hits.length === 1) {\n // Single hit: toggle as before\n state.selectedIndex = state.selectedIndex === hits[0] ? -1 : hits[0];\n } else {\n // Multiple overlapping hits: cycle through them\n var curPos = hits.indexOf(state.selectedIndex);\n if (curPos < 0) {\n state.selectedIndex = hits[0];\n } else {\n state.selectedIndex = hits[(curPos + 1) % hits.length];\n }\n }\n render();\n renderControlPanel();\n }\n });\n\n function findHitAnnotationIndex(cx, cy) {\n var hits = findAllHitAnnotationIndices(cx, cy);\n return hits.length > 0 ? hits[0] : -1;\n }\n\n function findAllHitAnnotationIndices(cx, cy) {\n var hitR = HIT_RADIUS / state.zoom;\n var hitR2 = hitR * hitR;\n var result = [];\n var keypointHitSet = {};\n // Check keypoints first (more precise)\n for (var i = 0; i < state.annotations.length; i++) {\n if (!isAnnotationVisible(i)) continue;\n var kps = state.annotations[i].keypoints || [];\n for (var j = 0; j < kps.length; j++) {\n var kp = kps[j];\n if (!isKeypointVisible(kp)) continue;\n var dx = cx - kp.x * state.scale;\n var dy = cy - kp.y * state.scale;\n if (dx * dx + dy * dy < hitR2) {\n result.push(i);\n keypointHitSet[i] = true;\n break;\n }\n }\n }\n // Then check bboxes (topmost first)\n for (var i = state.annotations.length - 1; i >= 0; i--) {\n if (keypointHitSet[i]) continue;\n if (!isAnnotationVisible(i)) continue;\n var bbox = state.annotations[i].bbox;\n if (!bbox) continue;\n var bx = bbox.x * state.scale;\n var by = bbox.y * state.scale;\n if (cx >= bx && cx <= bx + bbox.width * state.scale && cy >= by && cy <= by + bbox.height * state.scale) {\n result.push(i);\n }\n }\n return result;\n }\n\n // \u2500\u2500 Tooltip (mouseleave) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\n canvas.addEventListener(\"mouseleave\", function () {\n tooltip.classList.remove(\"visible\");\n if (!state.isPanning) {\n canvas.style.cursor = state.zoom > 1 ? \"grab\" : \"default\";\n }\n });\n\n function findNearestKeypoint(cx, cy) {\n var best = null;\n var hitR = HIT_RADIUS / state.zoom;\n var bestDist = hitR * hitR;\n\n for (var i = 0; i < state.annotations.length; i++) {\n if (!isAnnotationVisible(i)) continue;\n var ann = state.annotations[i];\n var kps = ann.keypoints || [];\n for (var j = 0; j < kps.length; j++) {\n var kp = kps[j];\n if (!isKeypointVisible(kp)) continue;\n\n var kx = kp.x * state.scale;\n var ky = kp.y * state.scale;\n var dx = cx - kx;\n var dy = cy - ky;\n var dist = dx * dx + dy * dy;\n\n if (dist < bestDist) {\n bestDist = dist;\n best = { kp: kp, ann: ann };\n }\n }\n }\n return best;\n }\n\n function findBboxAt(cx, cy) {\n for (var i = state.annotations.length - 1; i >= 0; i--) {\n if (!isAnnotationVisible(i)) continue;\n var ann = state.annotations[i];\n var bbox = ann.bbox;\n if (!bbox) continue;\n\n var bx = bbox.x * state.scale;\n var by = bbox.y * state.scale;\n var bw = bbox.width * state.scale;\n var bh = bbox.height * state.scale;\n\n if (cx >= bx && cx <= bx + bw && cy >= by && cy <= by + bh) {\n return ann;\n }\n }\n return null;\n }\n\n // \u2500\u2500 Wheel Zoom \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\n canvas.addEventListener(\"wheel\", function (e) {\n if (!state.image) return;\n e.preventDefault();\n\n var delta = e.deltaY;\n if (e.deltaMode === 1) delta *= 16;\n else if (e.deltaMode === 2) delta *= 100;\n\n var newZoom = state.zoom * (1 - delta * ZOOM_SENSITIVITY);\n newZoom = Math.max(MIN_ZOOM, Math.min(MAX_ZOOM, newZoom));\n\n // Zoom toward cursor position\n var rect = canvas.getBoundingClientRect();\n var mx = (e.clientX - rect.left) * (canvas.width / rect.width);\n var my = (e.clientY - rect.top) * (canvas.height / rect.height);\n\n state.panX = mx - (mx - state.panX) * (newZoom / state.zoom);\n state.panY = my - (my - state.panY) * (newZoom / state.zoom);\n state.zoom = newZoom;\n clampPan();\n render();\n }, { passive: false });\n\n // \u2500\u2500 Double-click to Reset Zoom \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\n canvas.addEventListener(\"dblclick\", function (e) {\n if (!state.image) return;\n e.preventDefault();\n resetZoom();\n canvas.style.cursor = \"default\";\n });\n\n // \u2500\u2500 Touch Support (Pinch-Zoom + Pan) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\n var touchState = { lastDist: 0, lastCenterX: 0, lastCenterY: 0, touchCount: 0 };\n\n function getTouchDistance(t1, t2) {\n var dx = t1.clientX - t2.clientX;\n var dy = t1.clientY - t2.clientY;\n return Math.sqrt(dx * dx + dy * dy);\n }\n\n function getTouchCenter(t1, t2) {\n return { x: (t1.clientX + t2.clientX) / 2, y: (t1.clientY + t2.clientY) / 2 };\n }\n\n canvas.addEventListener(\"touchstart\", function (e) {\n if (!state.image) return;\n e.preventDefault();\n\n touchState.touchCount = e.touches.length;\n\n if (e.touches.length === 1) {\n state.isPanning = true;\n state.didDrag = false;\n state.panStartX = e.touches[0].clientX;\n state.panStartY = e.touches[0].clientY;\n state.panStartPanX = state.panX;\n state.panStartPanY = state.panY;\n } else if (e.touches.length === 2) {\n state.isPanning = false;\n touchState.lastDist = getTouchDistance(e.touches[0], e.touches[1]);\n var center = getTouchCenter(e.touches[0], e.touches[1]);\n touchState.lastCenterX = center.x;\n touchState.lastCenterY = center.y;\n }\n }, { passive: false });\n\n canvas.addEventListener(\"touchmove\", function (e) {\n if (!state.image) return;\n e.preventDefault();\n\n if (e.touches.length === 1 && state.isPanning) {\n var dx = e.touches[0].clientX - state.panStartX;\n var dy = e.touches[0].clientY - state.panStartY;\n\n if (!state.didDrag && (Math.abs(dx) > DRAG_THRESHOLD || Math.abs(dy) > DRAG_THRESHOLD)) {\n state.didDrag = true;\n }\n\n if (state.didDrag && state.zoom > 1) {\n var rect = canvas.getBoundingClientRect();\n state.panX = state.panStartPanX + dx * (canvas.width / rect.width);\n state.panY = state.panStartPanY + dy * (canvas.height / rect.height);\n clampPan();\n render();\n }\n } else if (e.touches.length === 2) {\n var dist = getTouchDistance(e.touches[0], e.touches[1]);\n var center = getTouchCenter(e.touches[0], e.touches[1]);\n\n if (touchState.lastDist > 0) {\n var scale = dist / touchState.lastDist;\n var newZoom = Math.max(MIN_ZOOM, Math.min(MAX_ZOOM, state.zoom * scale));\n\n var rect = canvas.getBoundingClientRect();\n var mx = (center.x - rect.left) * (canvas.width / rect.width);\n var my = (center.y - rect.top) * (canvas.height / rect.height);\n\n state.panX = mx - (mx - state.panX) * (newZoom / state.zoom);\n state.panY = my - (my - state.panY) * (newZoom / state.zoom);\n\n // Simultaneous pan\n var panDx = (center.x - touchState.lastCenterX) * (canvas.width / rect.width);\n var panDy = (center.y - touchState.lastCenterY) * (canvas.height / rect.height);\n state.panX += panDx;\n state.panY += panDy;\n\n state.zoom = newZoom;\n clampPan();\n render();\n }\n\n touchState.lastDist = dist;\n touchState.lastCenterX = center.x;\n touchState.lastCenterY = center.y;\n }\n }, { passive: false });\n\n canvas.addEventListener(\"touchend\", function (e) {\n if (!state.image) return;\n e.preventDefault();\n\n if (e.touches.length === 0) {\n if (!state.didDrag && touchState.touchCount === 1 && state.annotations.length > 0) {\n var touch = e.changedTouches[0];\n var pt = clientToCanvas(touch.clientX, touch.clientY);\n var hits = findAllHitAnnotationIndices(pt.x, pt.y);\n if (hits.length === 0) {\n state.selectedIndex = -1;\n } else if (hits.length === 1) {\n state.selectedIndex = state.selectedIndex === hits[0] ? -1 : hits[0];\n } else {\n var curPos = hits.indexOf(state.selectedIndex);\n if (curPos < 0) {\n state.selectedIndex = hits[0];\n } else {\n state.selectedIndex = hits[(curPos + 1) % hits.length];\n }\n }\n render();\n renderControlPanel();\n }\n state.isPanning = false;\n touchState.lastDist = 0;\n touchState.touchCount = 0;\n } else if (e.touches.length === 1) {\n // Transitioned from two fingers to one \u2014 restart single-finger pan\n state.isPanning = true;\n state.didDrag = false;\n state.panStartX = e.touches[0].clientX;\n state.panStartY = e.touches[0].clientY;\n state.panStartPanX = state.panX;\n state.panStartPanY = state.panY;\n touchState.lastDist = 0;\n }\n }, { passive: false });\n\n // \u2500\u2500 Keyboard Shortcuts \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\n element.setAttribute(\"tabindex\", \"0\");\n element.style.outline = \"none\";\n\n element.addEventListener(\"keydown\", function (e) {\n // Help dialog shortcuts (work even without image data)\n if (e.key === \"?\" && e.target.tagName !== \"INPUT\") {\n toggleHelp();\n e.preventDefault();\n return;\n }\n if (e.key === \"Escape\" && helpOverlay.classList.contains(\"visible\")) {\n toggleHelp();\n e.preventDefault();\n return;\n }\n\n if (!state.image) return;\n\n if (e.key === \"Escape\") {\n if (state.maximized) {\n toggleMaximize();\n e.preventDefault();\n } else if (state.selectedIndex >= 0) {\n state.selectedIndex = -1;\n render();\n renderControlPanel();\n e.preventDefault();\n }\n } else if (e.key === \"f\" || e.key === \"F\") {\n if (e.target.tagName === \"INPUT\") return;\n toggleMaximize();\n e.preventDefault();\n } else if (e.key === \"i\" || e.key === \"I\") {\n if (e.target.tagName === \"INPUT\") return;\n state.showImage = !state.showImage;\n toggleImageBtn.classList.toggle(\"active\", state.showImage);\n render();\n e.preventDefault();\n } else if (e.key === \"a\" || e.key === \"A\") {\n if (e.target.tagName === \"INPUT\") return;\n var anyVisible = false;\n for (var i = 0; i < state.visibility.length; i++) {\n if (state.visibility[i]) { anyVisible = true; break; }\n }\n var newVal = !anyVisible;\n for (var i = 0; i < state.visibility.length; i++) {\n state.visibility[i] = newVal;\n }\n render();\n renderControlPanel();\n e.preventDefault();\n } else if (e.key === \"h\" || e.key === \"H\") {\n if (e.target.tagName === \"INPUT\") return;\n if (state.selectedIndex >= 0) {\n var idx = state.selectedIndex;\n state.visibility[idx] = false;\n state.selectedIndex = -1;\n render();\n renderControlPanel();\n }\n e.preventDefault();\n } else if (e.key === \"r\" || e.key === \"R\") {\n if (e.target.tagName === \"INPUT\") return;\n resetToDefaults();\n e.preventDefault();\n } else if (e.key === \"+\" || e.key === \"=\") {\n if (e.target.tagName === \"INPUT\") return;\n zoomToCenter(state.zoom * 1.25);\n e.preventDefault();\n } else if (e.key === \"-\" || e.key === \"_\") {\n if (e.target.tagName === \"INPUT\") return;\n zoomToCenter(state.zoom / 1.25);\n e.preventDefault();\n } else if (e.key === \"0\") {\n if (e.target.tagName === \"INPUT\") return;\n resetZoom();\n canvas.style.cursor = \"default\";\n e.preventDefault();\n }\n });\n\n // \u2500\u2500 Window Resize \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\n var resizeTimer = null;\n window.addEventListener(\"resize\", function () {\n if (resizeTimer) clearTimeout(resizeTimer);\n resizeTimer = setTimeout(function () {\n if (state.image) {\n fitCanvas();\n if (!state.maximized) resetZoom();\n else render();\n }\n }, 150);\n });\n})();\n", @@ -17,10 +17,10 @@ { "id": "spin-wheel", "name": "Spin Wheel", - "description": "", - "author": "", + "description": "Spin the wheel to win a prize!", + "author": "ysharma", "tags": [], - "category": "display", + "category": "input", "html_template": "\n
\n
\n \n
\u25bc
\n \n \n
\n \n ${(() => {\n const segments = JSON.parse(segments_json || '[]');\n const cx = 200, cy = 200, r = 180;\n let html = '';\n \n segments.forEach((seg, i) => {\n const startRad = (seg.startAngle - 90) * Math.PI / 180;\n const endRad = (seg.endAngle - 90) * Math.PI / 180;\n const x1 = cx + r * Math.cos(startRad);\n const y1 = cy + r * Math.sin(startRad);\n const x2 = cx + r * Math.cos(endRad);\n const y2 = cy + r * Math.sin(endRad);\n const largeArc = seg.endAngle - seg.startAngle > 180 ? 1 : 0;\n \n const d = `M ${cx} ${cy} L ${x1} ${y1} A ${r} ${r} 0 ${largeArc} 1 ${x2} ${y2} Z`;\n html += ``;\n \n // Text label\n const midRad = (seg.midAngle - 90) * Math.PI / 180;\n const textR = r * 0.65;\n const tx = cx + textR * Math.cos(midRad);\n const ty = cy + textR * Math.sin(midRad);\n const rotation = seg.midAngle;\n html += `${seg.label}`;\n });\n \n // Center circle\n html += ``;\n html += `SPIN`;\n \n return html;\n })()}\n \n
\n \n \n
\n ${Array.from({length: 16}, (_, i) => `
`).join('')}\n
\n
\n \n \n
\n ${value ? `
\ud83c\udf89 You won: ${value}
` : '
Spin to win!
'}\n
\n \n \n \n \n \n
\n
\n", "css_template": "\n .wheel-container {\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;\n display: flex;\n flex-direction: column;\n align-items: center;\n padding: 24px;\n background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);\n border-radius: 24px;\n position: relative;\n overflow: hidden;\n min-height: 500px;\n }\n \n .wheel-wrapper {\n position: relative;\n width: 320px;\n height: 320px;\n }\n \n .wheel {\n width: 100%;\n height: 100%;\n transform: rotate(0deg);\n }\n \n .wheel-svg {\n width: 100%;\n height: 100%;\n filter: drop-shadow(0 10px 30px rgba(0,0,0,0.4));\n }\n \n .segment {\n cursor: pointer;\n transition: filter 0.2s;\n }\n \n .segment:hover {\n filter: brightness(1.1);\n }\n \n .center-btn {\n cursor: pointer;\n transition: all 0.2s;\n }\n \n .center-btn:hover {\n filter: brightness(1.2);\n }\n \n /* Pointer */\n .wheel-pointer {\n position: absolute;\n top: -10px;\n left: 50%;\n transform: translateX(-50%);\n font-size: 48px;\n color: #FFD700;\n z-index: 10;\n filter: drop-shadow(0 4px 6px rgba(0,0,0,0.5));\n animation: bounce 0.6s ease-in-out infinite;\n }\n \n @keyframes bounce {\n 0%, 100% { transform: translateX(-50%) translateY(0); }\n 50% { transform: translateX(-50%) translateY(8px); }\n }\n \n /* Decorative lights */\n .wheel-lights {\n position: absolute;\n top: 50%;\n left: 50%;\n width: 360px;\n height: 360px;\n transform: translate(-50%, -50%);\n pointer-events: none;\n }\n \n .light {\n position: absolute;\n width: 14px;\n height: 14px;\n background: #FFD700;\n border-radius: 50%;\n top: 50%;\n left: 50%;\n transform-origin: center;\n transform: rotate(calc(var(--i) * 22.5deg)) translateY(-180px);\n box-shadow: 0 0 10px #FFD700, 0 0 20px #FFD700;\n animation: blink 0.5s ease-in-out infinite alternate;\n animation-delay: calc(var(--i) * 0.08s);\n }\n \n @keyframes blink {\n 0% { opacity: 0.4; transform: rotate(calc(var(--i) * 22.5deg)) translateY(-180px) scale(0.8); }\n 100% { opacity: 1; transform: rotate(calc(var(--i) * 22.5deg)) translateY(-180px) scale(1); box-shadow: 0 0 15px #FFD700, 0 0 30px #FFD700; }\n }\n \n /* Spin Button */\n .spin-button {\n margin-top: 24px;\n padding: 18px 56px;\n font-size: 20px;\n font-weight: 700;\n color: white;\n background: linear-gradient(135deg, #FF6B6B 0%, #FF8E53 100%);\n border: none;\n border-radius: 50px;\n cursor: pointer;\n box-shadow: 0 8px 25px rgba(255, 107, 107, 0.4);\n transition: all 0.3s ease;\n text-transform: uppercase;\n letter-spacing: 2px;\n }\n \n .spin-button:hover:not(:disabled) {\n transform: translateY(-3px);\n box-shadow: 0 12px 35px rgba(255, 107, 107, 0.5);\n }\n \n .spin-button:active:not(:disabled) {\n transform: translateY(0);\n }\n \n .spin-button:disabled {\n background: linear-gradient(135deg, #666 0%, #888 100%);\n cursor: not-allowed;\n box-shadow: none;\n transform: none;\n }\n \n /* Result Display */\n .result-display {\n margin-top: 20px;\n padding: 16px 32px;\n background: rgba(255,255,255,0.1);\n border-radius: 12px;\n backdrop-filter: blur(10px);\n border: 1px solid rgba(255,255,255,0.1);\n opacity: 0.7;\n transform: scale(0.95);\n transition: all 0.5s ease;\n }\n \n .result-display.show {\n opacity: 1;\n transform: scale(1);\n background: rgba(255,215,0,0.15);\n border-color: rgba(255,215,0,0.3);\n }\n \n .result-text {\n font-size: 18px;\n color: #fff;\n text-align: center;\n }\n \n .result-text strong {\n color: #FFD700;\n font-size: 22px;\n display: block;\n margin-top: 4px;\n }\n \n /* Confetti */\n .confetti-container {\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n pointer-events: none;\n overflow: hidden;\n }\n \n .confetti {\n position: absolute;\n width: 10px;\n height: 10px;\n animation: fall 3s ease-out forwards;\n }\n \n @keyframes fall {\n 0% {\n transform: translateY(-100px) rotate(0deg);\n opacity: 1;\n }\n 100% {\n transform: translateY(600px) rotate(720deg);\n opacity: 0;\n }\n }\n \n /* Spinning state indicator */\n .wheel-container.spinning .wheel-pointer {\n animation: none;\n color: #fff;\n }\n \n .wheel-container.spinning .light {\n animation: rapid-blink 0.1s ease-in-out infinite alternate;\n }\n \n @keyframes rapid-blink {\n 0% { opacity: 0.3; }\n 100% { opacity: 1; }\n }\n", "js_on_load": "\n const container = element.querySelector('.wheel-container');\n const wheel = element.querySelector('#prize-wheel');\n const spinBtn = element.querySelector('#spin-btn');\n const confettiContainer = element.querySelector('#confetti');\n const resultDisplay = element.querySelector('#result-display');\n \n let isSpinning = false;\n \n // Initialize totalRotation from the rotation prop (persists across re-renders)\n let totalRotation = parseFloat(props.rotation) || 0;\n \n function getSegments() {\n try {\n return JSON.parse(props.segments_json || '[]');\n } catch (e) {\n return [];\n }\n }\n \n function createConfetti() {\n const colors = ['#FF6B6B', '#4ECDC4', '#45B7D1', '#FFEAA7', '#DDA0DD', '#FFD700', '#FF8E53', '#96CEB4'];\n for (let i = 0; i < 150; i++) {\n setTimeout(() => {\n const confetti = document.createElement('div');\n confetti.className = 'confetti';\n confetti.style.left = Math.random() * 100 + '%';\n confetti.style.background = colors[Math.floor(Math.random() * colors.length)];\n confetti.style.animationDelay = Math.random() * 0.3 + 's';\n confetti.style.animationDuration = (2 + Math.random() * 2) + 's';\n const size = 6 + Math.random() * 10;\n confetti.style.width = size + 'px';\n confetti.style.height = size + 'px';\n confetti.style.borderRadius = Math.random() > 0.5 ? '50%' : Math.random() > 0.5 ? '0' : '2px';\n confettiContainer.appendChild(confetti);\n \n setTimeout(() => confetti.remove(), 4000);\n }, i * 15);\n }\n }\n \n function spinWheel() {\n if (isSpinning) return;\n \n const segments = getSegments();\n if (segments.length === 0) return;\n \n isSpinning = true;\n container.classList.add('spinning');\n spinBtn.disabled = true;\n spinBtn.textContent = '\ud83c\udfb0 SPINNING...';\n \n // Pick a random winning segment\n const winningIndex = Math.floor(Math.random() * segments.length);\n const winningSegment = segments[winningIndex];\n \n // === ROTATION MATH ===\n // The pointer is fixed at TOP (0\u00b0). Segments are drawn with 0\u00b0 at top.\n // For pointer to hit segment, we need rotation where segment's midAngle aligns with top.\n \n // Add random offset within segment bounds\n const segmentSize = winningSegment.endAngle - winningSegment.startAngle;\n const randomOffset = (Math.random() - 0.5) * segmentSize * 0.7;\n \n // Target position (mod 360) where wheel should stop\n const targetMod = ((360 - winningSegment.midAngle + randomOffset) % 360 + 360) % 360;\n \n // Current wheel position (mod 360)\n const currentMod = ((totalRotation % 360) + 360) % 360;\n \n // Calculate additional rotation needed (always spin forward)\n let additionalRotation = targetMod - currentMod;\n if (additionalRotation <= 0) additionalRotation += 360;\n \n // Add extra full spins for dramatic effect (5-7 spins)\n const extraSpins = 5 + Math.floor(Math.random() * 3);\n const finalRotation = totalRotation + extraSpins * 360 + additionalRotation;\n \n // Apply rotation with CSS transition\n wheel.style.transition = 'transform 4s cubic-bezier(0.17, 0.67, 0.12, 0.99)';\n wheel.style.transform = `rotate(${finalRotation}deg)`;\n \n // Store FULL rotation for next spin\n totalRotation = finalRotation;\n \n // After spin completes, determine winner from ACTUAL landing position\n setTimeout(() => {\n isSpinning = false;\n container.classList.remove('spinning');\n spinBtn.disabled = false;\n spinBtn.textContent = '\ud83c\udfb0 SPIN AGAIN!';\n \n // Verify which segment the pointer actually landed on\n const landedRotation = ((totalRotation % 360) + 360) % 360;\n const pointerAngle = ((360 - landedRotation) % 360 + 360) % 360;\n \n // Find which segment contains this angle\n let actualWinner = winningSegment;\n for (const seg of segments) {\n if (pointerAngle >= seg.startAngle && pointerAngle < seg.endAngle) {\n actualWinner = seg;\n break;\n }\n }\n // Handle wrap-around edge case\n if (pointerAngle >= segments[segments.length - 1].endAngle || pointerAngle < segments[0].startAngle) {\n if (segments[0].startAngle === 0) {\n actualWinner = segments[0];\n }\n }\n \n // Persist rotation as prop so it survives re-renders\n props.rotation = totalRotation;\n \n // Update value - this automatically triggers 'change' event\n // Do NOT call trigger('change') separately or you'll get duplicate events!\n props.value = actualWinner.label;\n \n createConfetti();\n \n console.log('Spin complete:', {\n finalRotation: totalRotation,\n pointerAngle,\n winner: actualWinner.label\n });\n }, 4100);\n }\n \n // Button click\n spinBtn.addEventListener('click', spinWheel);\n \n // Click on wheel center\n element.addEventListener('click', (e) => {\n if (e.target && e.target.classList.contains('center-btn')) {\n spinWheel();\n }\n });\n \n // Keyboard support\n element.addEventListener('keydown', (e) => {\n if (e.key === 'Enter' || e.key === ' ') {\n e.preventDefault();\n spinWheel();\n }\n });\n",