All files / web/src/arcade-games/know-your-world/features/magnifier ZoomLinesOverlay.tsx

0% Statements 0/335
0% Branches 0/1
0% Functions 0/1
0% Lines 0/335

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
/**
 * Zoom Lines Overlay Component
 *
 * Renders decorative lines connecting the indicator box (on main map)
 * to the magnifier window, creating a visual "pop out" effect.
 *
 * Features:
 * - Bezier curves with gentle outward bow
 * - Gradient fading toward magnifier
 * - Animated dash pattern
 * - Glow effect for premium feel
 * - Visibility culling for lines that would pass through rectangles
 *
 * Extracted from MapRenderer to improve maintainability.
 */

'use client'

import type { RefObject } from 'react'
import {
  getAdjustedMagnifiedDimensions,
  getMagnifierDimensions,
} from '../../utils/magnifierDimensions'
import { getRenderedViewport } from '../labels'

// ============================================================================
// Types
// ============================================================================

interface Point {
  x: number
  y: number
}

interface SafeZoneMargins {
  top: number
  right: number
  bottom: number
  left: number
}

interface ParsedViewBox {
  x: number
  y: number
  width: number
  height: number
}

export interface ZoomLinesOverlayProps {
  /** Ref to SVG element */
  svgRef: RefObject<SVGSVGElement>
  /** Ref to container element */
  containerRef: RefObject<HTMLDivElement>
  /** Cursor position in screen coordinates */
  cursorPosition: Point
  /** Parsed viewBox dimensions */
  parsedViewBox: ParsedViewBox
  /** Safe zone margins */
  safeZoneMargins: SafeZoneMargins
  /** Target magnifier top position */
  targetTop: number
  /** Target magnifier left position */
  targetLeft: number
  /** Target magnifier opacity */
  targetOpacity: number
  /** Current zoom level */
  currentZoom: number
  /** High zoom threshold for styling */
  highZoomThreshold: number
  /** Whether dark mode is active */
  isDark: boolean
}

// ============================================================================
// Helper Functions
// ============================================================================

/**
 * Check if a line segment passes through a rectangle (excluding endpoints)
 */
function linePassesThroughRect(
  from: Point,
  to: Point,
  rectLeft: number,
  rectTop: number,
  rectRight: number,
  rectBottom: number
): boolean {
  // Sample points along the line (excluding endpoints)
  for (let t = 0.1; t <= 0.9; t += 0.1) {
    const px = from.x + (to.x - from.x) * t
    const py = from.y + (to.y - from.y) * t
    if (px > rectLeft && px < rectRight && py > rectTop && py < rectBottom) {
      return true
    }
  }
  return false
}

/**
 * Create a bezier path with elegant curve between two points
 */
function createBezierPath(from: Point, to: Point): string {
  const dx = to.x - from.x
  const dy = to.y - from.y
  const dist = Math.sqrt(dx * dx + dy * dy)

  // Perpendicular offset creates gentle outward bow
  const bowAmount = dist * 0.06
  const perpX = (-dy / dist) * bowAmount
  const perpY = (dx / dist) * bowAmount

  const midX = (from.x + to.x) / 2 + perpX
  const midY = (from.y + to.y) / 2 + perpY

  // Quadratic bezier for smooth curve
  return `M ${from.x} ${from.y} Q ${midX} ${midY}, ${to.x} ${to.y}`
}

// ============================================================================
// Component
// ============================================================================

export function ZoomLinesOverlay({
  svgRef,
  containerRef,
  cursorPosition,
  parsedViewBox,
  safeZoneMargins,
  targetTop,
  targetLeft,
  targetOpacity,
  currentZoom,
  highZoomThreshold,
  isDark,
}: ZoomLinesOverlayProps) {
  // Need both refs to render
  if (!svgRef.current || !containerRef.current) {
    return null
  }

  const containerRect = containerRef.current.getBoundingClientRect()
  const svgRect = svgRef.current.getBoundingClientRect()

  // Calculate leftover rectangle dimensions (area not covered by UI elements)
  const leftoverWidth = containerRect.width - safeZoneMargins.left - safeZoneMargins.right
  const leftoverHeight = containerRect.height - safeZoneMargins.top - safeZoneMargins.bottom

  // Get magnifier dimensions based on leftover rectangle (responsive to its aspect ratio)
  const { width: magnifierWidth, height: magnifierHeight } = getMagnifierDimensions(
    leftoverWidth,
    leftoverHeight
  )

  // Magnifier position
  const magTop = targetTop
  const magLeft = targetLeft

  // Calculate indicator box position in screen coordinates
  const { x: viewBoxX, y: viewBoxY, width: viewBoxWidth, height: viewBoxHeight } = parsedViewBox

  // Use adjusted dimensions to match magnifier aspect ratio
  const { width: indicatorWidth, height: indicatorHeight } = getAdjustedMagnifiedDimensions(
    viewBoxWidth,
    viewBoxHeight,
    currentZoom,
    leftoverWidth,
    leftoverHeight
  )

  // Convert cursor to SVG coordinates (accounting for preserveAspectRatio)
  const viewport = getRenderedViewport(svgRect, viewBoxX, viewBoxY, viewBoxWidth, viewBoxHeight)
  const svgOffsetX = svgRect.left - containerRect.left + viewport.letterboxX
  const svgOffsetY = svgRect.top - containerRect.top + viewport.letterboxY

  const cursorSvgX = (cursorPosition.x - svgOffsetX) / viewport.scale + viewBoxX
  const cursorSvgY = (cursorPosition.y - svgOffsetY) / viewport.scale + viewBoxY

  // Indicator box in SVG coordinates
  const indSvgLeft = cursorSvgX - indicatorWidth / 2
  const indSvgTop = cursorSvgY - indicatorHeight / 2
  const indSvgRight = indSvgLeft + indicatorWidth
  const indSvgBottom = indSvgTop + indicatorHeight

  // Convert indicator corners to screen coordinates
  const svgToScreen = (svgX: number, svgY: number): Point => ({
    x: (svgX - viewBoxX) * viewport.scale + svgOffsetX,
    y: (svgY - viewBoxY) * viewport.scale + svgOffsetY,
  })

  const indTL = svgToScreen(indSvgLeft, indSvgTop)
  const indTR = svgToScreen(indSvgRight, indSvgTop)
  const indBL = svgToScreen(indSvgLeft, indSvgBottom)
  const indBR = svgToScreen(indSvgRight, indSvgBottom)

  // Magnifier corners in screen coordinates
  const magTL = { x: magLeft, y: magTop }
  const magTR = { x: magLeft + magnifierWidth, y: magTop }
  const magBL = { x: magLeft, y: magTop + magnifierHeight }
  const magBR = { x: magLeft + magnifierWidth, y: magTop + magnifierHeight }

  // Define the corner pairs with identifiers
  const cornerPairs = [
    { from: indTL, to: magTL, corner: indTL },
    { from: indTR, to: magTR, corner: indTR },
    { from: indBL, to: magBL, corner: indBL },
    { from: indBR, to: magBR, corner: indBR },
  ]

  // Filter out lines that pass through either rectangle
  const visibleCornerPairs = cornerPairs.filter(({ from, to }) => {
    // Check if line passes through magnifier
    const passesThroughMag = linePassesThroughRect(
      from,
      to,
      magLeft,
      magTop,
      magLeft + magnifierWidth,
      magTop + magnifierHeight
    )
    // Check if line passes through indicator
    const passesThroughInd = linePassesThroughRect(from, to, indTL.x, indTL.y, indBR.x, indBR.y)
    return !passesThroughMag && !passesThroughInd
  })

  const paths = visibleCornerPairs.map(({ from, to }) => createBezierPath(from, to))
  const visibleCorners = visibleCornerPairs.map(({ corner }) => corner)

  // Color based on zoom level (matches magnifier border)
  const isHighZoom = currentZoom > highZoomThreshold
  const lineColor = isHighZoom
    ? isDark
      ? '#fbbf24'
      : '#f59e0b' // gold
    : isDark
      ? '#60a5fa'
      : '#3b82f6' // blue
  const glowColor = isHighZoom ? 'rgba(251, 191, 36, 0.6)' : 'rgba(96, 165, 250, 0.6)'

  return (
    <svg
      data-element="zoom-lines"
      style={{
        position: 'absolute',
        top: 0,
        left: 0,
        width: '100%',
        height: '100%',
        pointerEvents: 'none',
        zIndex: 99, // Just below magnifier (100)
        overflow: 'visible',
      }}
    >
      <defs>
        {/* Gradient for lines - fades toward magnifier */}
        <linearGradient id="zoom-line-gradient" x1="0%" y1="0%" x2="100%" y2="0%">
          <stop offset="0%" stopColor={lineColor} stopOpacity="0.8" />
          <stop offset="40%" stopColor={lineColor} stopOpacity="0.5" />
          <stop offset="100%" stopColor={lineColor} stopOpacity="0.2" />
        </linearGradient>

        {/* Glow filter for premium effect */}
        <filter id="zoom-line-glow" x="-50%" y="-50%" width="200%" height="200%">
          <feGaussianBlur in="SourceGraphic" stdDeviation="3" result="blur" />
          <feMerge>
            <feMergeNode in="blur" />
            <feMergeNode in="SourceGraphic" />
          </feMerge>
        </filter>

        {/* Animated dash pattern */}
        <pattern id="dash-pattern" patternUnits="userSpaceOnUse" width="12" height="1">
          <rect width="8" height="1" fill={lineColor} opacity="0.6" />
        </pattern>
      </defs>

      {/* Glow layer (underneath) */}
      <g filter="url(#zoom-line-glow)" opacity={0.4}>
        {paths.map((d, i) => (
          <path
            key={`glow-${i}`}
            d={d}
            fill="none"
            stroke={glowColor}
            strokeWidth="6"
            strokeLinecap="round"
          />
        ))}
      </g>

      {/* Main lines with gradient */}
      <g opacity={targetOpacity}>
        {paths.map((d, i) => (
          <path
            key={`line-${i}`}
            d={d}
            fill="none"
            stroke="url(#zoom-line-gradient)"
            strokeWidth="2"
            strokeLinecap="round"
            style={{
              // Subtle animation for the lines
              strokeDasharray: '8 4',
              strokeDashoffset: '0',
              animation: 'zoom-line-flow 1s linear infinite',
            }}
          />
        ))}
      </g>

      {/* Corner dots on indicator for visible lines only */}
      <g opacity={targetOpacity * 0.8}>
        {visibleCorners.map((corner, i) => (
          <circle
            key={`corner-${i}`}
            cx={corner.x}
            cy={corner.y}
            r="3"
            fill={lineColor}
            opacity="0.7"
          />
        ))}
      </g>

      <style>
        {`
          @keyframes zoom-line-flow {
            from { stroke-dashoffset: 12; }
            to { stroke-dashoffset: 0; }
          }
        `}
      </style>
    </svg>
  )
}