Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 | import type { NumberLineState } from '../../types' import { numberToScreenX } from '../../numberLineTicks' export const NUM_LEVELS = 50 // Fibonacci numbers for the construction. // Using Fibonacci ratio instead of exact φ means the two innermost squares // are exactly equal, and the aspect ratio genuinely converges to φ as the // construction grows outward. const FIB: number[] = [1, 1] for (let i = 2; i <= NUM_LEVELS; i++) { FIB.push(FIB[i - 1] + FIB[i - 2]) } /** Starting rectangle ratio F(n+1)/F(n) — converges to φ */ export const RECT_RATIO = FIB[NUM_LEVELS] / FIB[NUM_LEVELS - 1] /** * Target viewport for the golden ratio demo. * Centers the Fibonacci rectangle with some padding. */ export function goldenRatioDemoViewport(_cssWidth: number, cssHeight: number) { const center = RECT_RATIO / 2 // Scale so the rectangle height (1 unit) fills ~40% of canvas height const ppu = cssHeight * 0.35 return { center, pixelsPerUnit: ppu } } // --- Subdivision computation --- export interface Subdivision { /** Square top-left in number-line coords (y=0 is axis) */ sx: number sy: number side: number /** Arc center in number-line coords */ arcCx: number arcCy: number /** Start angle of the 90° clockwise arc (after y-flip) */ arcStartAngle: number } /** * Compute the golden rectangle subdivisions using Fibonacci numbers. * * Starting rectangle: [0, F(n+1)/F(n)] × [-1, 0]. The two innermost * squares are exactly equal (both side 1/F(n)), and the aspect ratio * converges to φ as the construction grows outward. * * After computing the canonical subdivisions, a y-flip is applied so the * spiral opens downward (toward the axis) instead of upward. */ function computeSubdivisions(): Subdivision[] { const subs: Subdivision[] = [] let rx = 0 let ry = -1 let rw = RECT_RATIO let rh = 1 let dir = 0 for (let i = 0; i < NUM_LEVELS; i++) { // Compute side directly from Fibonacci to avoid catastrophic cancellation // in the iterative subtraction of rw/rh over 50 levels. const side = FIB[NUM_LEVELS - i] / FIB[NUM_LEVELS] let sx: number, sy: number let arcCx: number, arcCy: number let arcStart: number switch (dir) { case 0: // Cut from LEFT sx = rx sy = ry rx += side rw -= side arcCx = sx + side arcCy = sy arcStart = Math.PI break case 1: // Cut from BOTTOM sx = rx sy = ry + rh - side rh -= side arcCx = sx arcCy = sy arcStart = Math.PI / 2 break case 2: // Cut from RIGHT sx = rx + rw - side sy = ry rw -= side arcCx = sx arcCy = sy + side arcStart = 0 break case 3: // Cut from TOP sx = rx sy = ry ry += side rh -= side arcCx = sx + side arcCy = sy + side arcStart = Math.PI * 1.5 break default: throw new Error('unreachable') } subs.push({ sx, sy, side, arcCx, arcCy, arcStartAngle: arcStart }) dir = (dir + 1) % 4 } // Y-flip within the rectangle (mirror around y = -0.5). // This flips the spiral so it opens toward the axis instead of away from it. // y' = -1 - y, angles negate, arcs become clockwise. for (const sub of subs) { sub.sy = -1 - sub.sy - sub.side sub.arcCy = -1 - sub.arcCy sub.arcStartAngle = -sub.arcStartAngle } return subs } // Pre-compute once export const SUBDIVISIONS = computeSubdivisions() /** Every arc sweeps exactly 90° */ const ARC_SWEEP = Math.PI / 2 // --- Stage bounds for convergence animation --- interface StageBounds { minX: number maxX: number minY: number maxY: number } /** * STAGE_BOUNDS[k] = bounding box of SUBDIVISIONS[k..N-1]. * * Used to rescale the inside-out construction so that it always fits * exactly in [0, φ] on the number line. As k decreases (more squares * added), the aspect ratio converges to φ. */ function computeStageBounds(): StageBounds[] { const result: StageBounds[] = new Array(NUM_LEVELS) let minX = Infinity let maxX = -Infinity let minY = Infinity let maxY = -Infinity for (let k = NUM_LEVELS - 1; k >= 0; k--) { const sub = SUBDIVISIONS[k] minX = Math.min(minX, sub.sx) maxX = Math.max(maxX, sub.sx + sub.side) minY = Math.min(minY, sub.sy) maxY = Math.max(maxY, sub.sy + sub.side) result[k] = { minX, maxX, minY, maxY } } return result } const STAGE_BOUNDS = computeStageBounds() // --- Pre-computed frame snapshots --- // At each step completion, the bounding box is transformed to number-line // coordinates and frozen. These static frames show the convergence. type NLCorners = [[number, number], [number, number], [number, number], [number, number]] function computeFrameSnapshots(): NLCorners[] { const snapshots: NLCorners[] = [] for (let i = 0; i < NUM_LEVELS; i++) { const sub = SUBDIVISIONS[NUM_LEVELS - 1 - i] const bb = STAGE_BOUNDS[NUM_LEVELS - 1 - i] // Arm parameters at step i completion (stepT = 1) const angle = sub.arcStartAngle const tX = sub.arcCx + sub.side * Math.cos(angle) const tY = sub.arcCy + sub.side * Math.sin(angle) const rot = Math.PI - angle const cR = Math.cos(rot) const sR = Math.sin(rot) const sc = 1 / sub.side const toNL = (x: number, y: number): [number, number] => { const dx = x - tX const dy = y - tY return [(dx * cR - dy * sR) * sc, (dx * sR + dy * cR) * sc] } snapshots.push([ toNL(bb.minX, bb.minY), toNL(bb.maxX, bb.minY), toNL(bb.maxX, bb.maxY), toNL(bb.minX, bb.maxY), ]) } return snapshots } const FRAME_SNAPSHOTS = computeFrameSnapshots() /** Number of steps before a frame fully fades out */ const FRAME_FADE_STEPS = 4 // --- Sweep transform (shared with renderPhiExploreImage) --- export interface SweepTransform { effRotation: number effScale: number tipX: number tipY: number } /** * Compute the sweep transform for a given revealProgress. * * This maps subdivision coords to number-line coords via rotation + uniform * scale, anchoring the current compass arm tip at the NL origin (0,0) and * the pivot at (1,0). Exported so renderPhiExploreImage can reuse it. */ export function computeSweepTransform(revealProgress: number): SweepTransform { const sweepProgress = Math.min(1, revealProgress / SWEEP_PHASE) let animStep = NUM_LEVELS let stepT = 0 for (let i = 0; i < NUM_LEVELS; i++) { if (sweepProgress < STEP_TIMINGS[i].end) { animStep = i stepT = Math.max( 0, Math.min( 1, (sweepProgress - STEP_TIMINGS[i].start) / (STEP_TIMINGS[i].end - STEP_TIMINGS[i].start) ) ) break } } let armPivotX: number, armPivotY: number, armAngle: number, armSide: number if (animStep < NUM_LEVELS) { const currSub = SUBDIVISIONS[NUM_LEVELS - 1 - animStep] const prevSub = animStep > 0 ? SUBDIVISIONS[NUM_LEVELS - animStep] : currSub armPivotX = lerp(prevSub.arcCx, currSub.arcCx, stepT) armPivotY = lerp(prevSub.arcCy, currSub.arcCy, stepT) armSide = lerp(prevSub.side, currSub.side, stepT) const transitionAngle = currSub.arcStartAngle + ARC_SWEEP armAngle = transitionAngle - ARC_SWEEP * stepT } else { const sub = SUBDIVISIONS[0] armPivotX = sub.arcCx armPivotY = sub.arcCy armSide = sub.side armAngle = sub.arcStartAngle } const tipX = armPivotX + armSide * Math.cos(armAngle) const tipY = armPivotY + armSide * Math.sin(armAngle) const effRotation = Math.PI - armAngle const effScale = 1 / armSide return { effRotation, effScale, tipX, tipY } } // --- Animation timing --- /** Fraction of revealProgress for compass sweeps; remainder for rectangle fade. */ const SWEEP_PHASE = 1.0 /** * Sequential step timings with decay: earlier steps (small inner squares * that cause the most dramatic rotation) get progressively more scrubber * range, while later (calmer) steps pass quickly. * * Default decay factor r = 0.8 gives ~5 arcs at scrubber midpoint, * ~9 arcs at 75% (with log base 7 scrubber mapping). */ function computeStepTimings(count: number, decay: number): Array<{ start: number; end: number }> { const durations: number[] = [] let d = 1 for (let i = 0; i < count; i++) { durations.push(d) d *= decay } const total = durations.reduce((a, b) => a + b, 0) let cumulative = 0 return durations.map((dur) => { const start = cumulative / total cumulative += dur return { start, end: cumulative / total } }) } let currentDecay = 0.8 let STEP_TIMINGS = computeStepTimings(NUM_LEVELS, currentDecay) /** Update the step timing decay factor (debug tuning). Recomputes all timings. */ export function setStepTimingDecay(decay: number): void { currentDecay = Math.max(0.5, Math.min(0.999, decay)) STEP_TIMINGS = computeStepTimings(NUM_LEVELS, currentDecay) } export function getStepTimingDecay(): number { return currentDecay } /** Given a sweep progress (0-1), returns how many arcs are fully complete */ export function arcCountAtProgress(sweepProgress: number): number { if (sweepProgress <= 0) return 0 if (sweepProgress >= 1) return NUM_LEVELS for (let i = 0; i < NUM_LEVELS; i++) { if (sweepProgress < STEP_TIMINGS[i].end) return i } return NUM_LEVELS } function lerp(a: number, b: number, t: number): number { return a + (b - a) * t } // --- Convergence gap data for visual indicator --- const PHI = (1 + Math.sqrt(5)) / 2 /** * Pre-compute normalized convergence gaps from Fibonacci ratios. * At inside-out step i, the aspect ratio is F(i+1)/F(i) which * converges to φ. The gap is |F(i+1)/F(i) - φ|, normalized to [0,1]. */ const CONVERGENCE_GAPS: number[] = (() => { const gaps: number[] = [] for (let i = 0; i < NUM_LEVELS; i++) { // F(i+1)/F(i) is the aspect ratio at step i (FIB is 0-indexed: F(0)=1, F(1)=1, F(2)=2, ...) const ratio = FIB[i + 1] / FIB[i] gaps.push(Math.abs(ratio - PHI)) } const maxGap = gaps[0] || 1 return gaps.map((g) => g / maxGap) })() /** * Returns the convergence gap at a given revealProgress (0-1). * 1 = maximum gap (just started), 0 = converged to φ. * Interpolates smoothly between adjacent step gap values. */ export function convergenceGapAtProgress(revealProgress: number): number { if (revealProgress <= 0) return 1 if (revealProgress >= 1) return CONVERGENCE_GAPS[NUM_LEVELS - 1] ?? 0 const sweepProgress = Math.min(1, revealProgress / SWEEP_PHASE) // Find which step we're in for (let i = 0; i < NUM_LEVELS; i++) { if (sweepProgress < STEP_TIMINGS[i].end) { const stepT = Math.max( 0, Math.min( 1, (sweepProgress - STEP_TIMINGS[i].start) / (STEP_TIMINGS[i].end - STEP_TIMINGS[i].start) ) ) const currGap = CONVERGENCE_GAPS[i] ?? 0 const prevGap = i > 0 ? CONVERGENCE_GAPS[i - 1] : 1 return lerp(prevGap, currGap, stepT) } } return CONVERGENCE_GAPS[NUM_LEVELS - 1] ?? 0 } // --- Canvas rendering --- // Construction lines + division lines const COLOR_LIGHT = '#6d28d9' const COLOR_DARK = '#f59e0b' // Spiral arcs const SPIRAL_COLOR_LIGHT = '#a855f7' const SPIRAL_COLOR_DARK = '#fbbf24' // Frame snapshot colors — cycle through a palette for visual variety const FRAME_COLORS_LIGHT = ['#dc2626', '#ea580c', '#d97706', '#65a30d', '#0891b2', '#7c3aed'] const FRAME_COLORS_DARK = ['#f87171', '#fb923c', '#fbbf24', '#a3e635', '#22d3ee', '#c4b5fd'] // Flash glow const FLASH_COLOR_LIGHT = '#fff' const FLASH_COLOR_DARK = '#fff' /** * Render the golden ratio demo overlay on the canvas. * * The animation builds the golden rectangle from the inside out at * a fixed [0, φ] scale. The construction rotates so the currently- * drawn compass arm is always horizontal on the number line axis, * pinned at position 1. Arcs grow with each step as the aspect * ratio converges to φ. After all sweeps, the construction smoothly * unrotates to canonical orientation. * * @param revealProgress 0-1 for the full construction animation * @param opacity 0-1 overall overlay opacity for fade-in/fade-out */ export function renderGoldenRatioOverlay( ctx: CanvasRenderingContext2D, state: NumberLineState, cssWidth: number, cssHeight: number, isDark: boolean, revealProgress: number, opacity: number ): void { if (opacity <= 0) return const centerY = cssHeight / 2 const ppu = state.pixelsPerUnit const toX = (nlx: number) => numberToScreenX(nlx, state.center, ppu, cssWidth) const toY = (nly: number) => centerY + nly * ppu const color = isDark ? COLOR_DARK : COLOR_LIGHT const spiralColor = isDark ? SPIRAL_COLOR_DARK : SPIRAL_COLOR_LIGHT ctx.save() ctx.globalAlpha = opacity ctx.setLineDash([]) // --- Base line on axis [0, φ] --- ctx.beginPath() ctx.moveTo(toX(0), toY(0)) ctx.lineTo(toX(RECT_RATIO), toY(0)) ctx.strokeStyle = color ctx.lineWidth = 2 ctx.stroke() // --- Compass sweep phase: build inside → out with convergence --- // Draws SUBDIVISIONS[N-1] (smallest) first, then [N-2], ... (progressively // larger). The currently-drawn compass arm is kept horizontal on the axis. // Arcs grow with each step, and the aspect ratio converges to φ. // Reversed sweep (endAngle → startAngle) ensures arm continuity between // steps in inside-out order. const sweepProgress = Math.min(1, revealProgress / SWEEP_PHASE) // Find current animation step. // Step i (inside-out) reveals SUBDIVISIONS[N-1-i]. // Pivot, scale, and arc sweep all happen simultaneously. let animStep = NUM_LEVELS // all done let stepT = 0 // 0→1 progress within current step for (let i = 0; i < NUM_LEVELS; i++) { if (sweepProgress < STEP_TIMINGS[i].end) { animStep = i stepT = Math.max( 0, Math.min( 1, (sweepProgress - STEP_TIMINGS[i].start) / (STEP_TIMINGS[i].end - STEP_TIMINGS[i].start) ) ) break } } // --- Transform: arm always spans [0, 1] on the number line --- // Tip at 0, pivot at 1, arm horizontal. // Pivot, scale, and angle all interpolate together with stepT. let armPivotX: number, armPivotY: number, armAngle: number, armSide: number if (animStep < NUM_LEVELS) { const currSub = SUBDIVISIONS[NUM_LEVELS - 1 - animStep] const prevSub = animStep > 0 ? SUBDIVISIONS[NUM_LEVELS - animStep] : currSub // Everything transitions simultaneously armPivotX = lerp(prevSub.arcCx, currSub.arcCx, stepT) armPivotY = lerp(prevSub.arcCy, currSub.arcCy, stepT) armSide = lerp(prevSub.side, currSub.side, stepT) // Angle sweeps from end to start simultaneously const transitionAngle = currSub.arcStartAngle + ARC_SWEEP armAngle = transitionAngle - ARC_SWEEP * stepT } else { // All steps done — use last step's final arm position const sub = SUBDIVISIONS[0] armPivotX = sub.arcCx armPivotY = sub.arcCy armSide = sub.side armAngle = sub.arcStartAngle } // Arm tip (arc-drawing endpoint) in subdivision coords const tipX = armPivotX + armSide * Math.cos(armAngle) const tipY = armPivotY + armSide * Math.sin(armAngle) // Transform anchored at the TIP → origin (0, 0). Pivot lands at (1, 0). const effRotation = Math.PI - armAngle const cosR = Math.cos(effRotation) const sinR = Math.sin(effRotation) const effScale = 1 / armSide // Transform subdivision coords → number-line coords function subToNL(x: number, y: number): [number, number] { const dx = x - tipX const dy = y - tipY const rx = dx * cosR - dy * sinR const ry = dx * sinR + dy * cosR return [rx * effScale, ry * effScale] } function subToScreen(x: number, y: number): [number, number] { const [nlx, nly] = subToNL(x, y) return [toX(nlx), toY(nly)] } // Transform angle from subdivision space to screen space function xformAngle(angle: number): number { return angle + effRotation } // Screen radius for a subdivision side length function screenR(side: number): number { return side * effScale * ppu } // Draw completed arcs + division lines (from previous steps) for (let i = 0; i < animStep && i < NUM_LEVELS; i++) { const sub = SUBDIVISIONS[NUM_LEVELS - 1 - i] const [cx, cy] = subToScreen(sub.arcCx, sub.arcCy) const r = screenR(sub.side) const sEnd = xformAngle(sub.arcStartAngle + ARC_SWEEP) const sStart = xformAngle(sub.arcStartAngle) // Full spiral arc (reversed: end → start, anticlockwise) ctx.globalAlpha = opacity ctx.strokeStyle = spiralColor ctx.lineWidth = 2.5 ctx.beginPath() ctx.arc(cx, cy, r, sEnd, sStart, true) ctx.stroke() // Division line at endAngle const ex = cx + r * Math.cos(sEnd) const ey = cy + r * Math.sin(sEnd) ctx.strokeStyle = color ctx.lineWidth = 1 ctx.beginPath() ctx.moveTo(cx, cy) ctx.lineTo(ex, ey) ctx.stroke() } // Draw current sweeping arc + compass arm // Use interpolated arm parameters so the growing tip stays pinned at origin if (animStep < NUM_LEVELS && stepT > 0) { const [cx, cy] = subToScreen(armPivotX, armPivotY) const r = screenR(armSide) const currSub = SUBDIVISIONS[NUM_LEVELS - 1 - animStep] const sEnd = xformAngle(currSub.arcStartAngle + ARC_SWEEP) const sCur = xformAngle(armAngle) // Partial spiral arc (reversed: end → current, anticlockwise) ctx.globalAlpha = opacity ctx.strokeStyle = spiralColor ctx.lineWidth = 2.5 ctx.beginPath() ctx.arc(cx, cy, r, sEnd, sCur, true) ctx.stroke() // Division line at endAngle (boundary with previous arc's square) const dx = cx + r * Math.cos(sEnd) const dy = cy + r * Math.sin(sEnd) ctx.strokeStyle = color ctx.lineWidth = 1 ctx.beginPath() ctx.moveTo(cx, cy) ctx.lineTo(dx, dy) ctx.stroke() // Compass arm const ex = cx + r * Math.cos(sCur) const ey = cy + r * Math.sin(sCur) ctx.beginPath() ctx.moveTo(cx, cy) ctx.lineTo(ex, ey) ctx.stroke() } // --- Frame snapshots: static bounding boxes left behind at each step --- // Each completed step leaves a frozen frame in NL coordinates. // Newer frames are opaque; older ones progressively fade out. // The most recently completed frame "flashes" with a bright glow. const framePalette = isDark ? FRAME_COLORS_DARK : FRAME_COLORS_LIGHT const flashColor = isDark ? FLASH_COLOR_DARK : FLASH_COLOR_LIGHT for (let i = 0; i < animStep && i < NUM_LEVELS; i++) { const age = animStep - 1 - i // 0 = most recent, 1 = one back, ... if (age >= FRAME_FADE_STEPS) continue const frameColor = framePalette[i % framePalette.length] const corners = FRAME_SNAPSHOTS[i] // Base opacity fades with age const baseAlpha = 1 - age / FRAME_FADE_STEPS // Flash: dramatic glow on most recently completed frame const isFlashing = age === 0 && animStep < NUM_LEVELS const flashT = isFlashing ? Math.max(0, 1 - stepT * 2.5) : 0 // Identify the RIGHT edge (primary, convergence) and TOP edge (secondary) // by scoring all 4 edges in NL space independently. // Right edge = highest average x; Top edge = lowest average y (highest on screen). const edgeIndices: [number, number][] = [ [0, 1], [1, 2], [2, 3], [3, 0], ] let rightEdge = 0, topEdge = 0 let maxAvgX = -Infinity, minAvgY = Infinity for (let j = 0; j < 4; j++) { const [a, b] = edgeIndices[j] const avgX = (corners[a][0] + corners[b][0]) / 2 const avgY = (corners[a][1] + corners[b][1]) / 2 if (avgX > maxAvgX) { maxAvgX = avgX rightEdge = j } if (avgY < minAvgY) { minAvgY = avgY topEdge = j } } const [rFrom, rTo] = edgeIndices[rightEdge] const [tFrom, tTo] = edgeIndices[topEdge] // The other two edges (for non-flash context drawing) const otherEdges = edgeIndices.filter((_, j) => j !== rightEdge && j !== topEdge) // Draw glow pass first (wider, blurred, bright) if (flashT > 0) { ctx.shadowColor = flashColor ctx.shadowBlur = 20 * flashT // Right edge — thicker glow (primary convergence edge) ctx.globalAlpha = opacity * flashT * 0.8 ctx.strokeStyle = flashColor ctx.lineWidth = 8 ctx.beginPath() ctx.moveTo(toX(corners[rFrom][0]), toY(corners[rFrom][1])) ctx.lineTo(toX(corners[rTo][0]), toY(corners[rTo][1])) ctx.stroke() // Top edge — standard glow ctx.lineWidth = 4 ctx.beginPath() ctx.moveTo(toX(corners[tFrom][0]), toY(corners[tFrom][1])) ctx.lineTo(toX(corners[tTo][0]), toY(corners[tTo][1])) ctx.stroke() ctx.shadowColor = 'transparent' ctx.shadowBlur = 0 } // Draw the frame itself ctx.globalAlpha = opacity * Math.min(1, baseAlpha + flashT * 0.3) ctx.strokeStyle = frameColor if (isFlashing) { // Right edge — brighter ctx.lineWidth = lerp(2.5, 1.5, stepT) ctx.beginPath() ctx.moveTo(toX(corners[rFrom][0]), toY(corners[rFrom][1])) ctx.lineTo(toX(corners[rTo][0]), toY(corners[rTo][1])) ctx.stroke() // Top edge ctx.lineWidth = lerp(2, 1.5, stepT) ctx.beginPath() ctx.moveTo(toX(corners[tFrom][0]), toY(corners[tFrom][1])) ctx.lineTo(toX(corners[tTo][0]), toY(corners[tTo][1])) ctx.stroke() // Remaining two edges at normal width for context ctx.lineWidth = 1.5 for (const [oFrom, oTo] of otherEdges) { ctx.beginPath() ctx.moveTo(toX(corners[oFrom][0]), toY(corners[oFrom][1])) ctx.lineTo(toX(corners[oTo][0]), toY(corners[oTo][1])) ctx.stroke() } } else { ctx.lineWidth = 1.5 ctx.beginPath() ctx.moveTo(toX(corners[0][0]), toY(corners[0][1])) ctx.lineTo(toX(corners[1][0]), toY(corners[1][1])) ctx.lineTo(toX(corners[2][0]), toY(corners[2][1])) ctx.lineTo(toX(corners[3][0]), toY(corners[3][1])) ctx.closePath() ctx.stroke() } } ctx.globalAlpha = 1 ctx.restore() } |