All files / web/src/lib homepage-previews.ts

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

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
/**
 * Homepage preview target registry.
 *
 * Each target describes how to produce a preview image for an exploration card.
 * Canvas targets render client-side via an offscreen canvas; AI targets delegate
 * to the existing image generation pipeline.
 */

// ---------------------------------------------------------------------------
// Euclid imports
// ---------------------------------------------------------------------------
import { buildFinalState } from '@/components/toys/euclid/render/buildFinalStates'
import { renderConstruction } from '@/components/toys/euclid/render/renderConstruction'
import {
  getAllPoints,
  getAllCircles,
  getPoint,
  getRadius,
} from '@/components/toys/euclid/engine/constructionState'
import { PROP_REGISTRY } from '@/components/toys/euclid/propositions/registry'
import type { ConstructionState, EuclidViewportState } from '@/components/toys/euclid/types'

// ---------------------------------------------------------------------------
// Number line imports
// ---------------------------------------------------------------------------
import {
  renderPiOverlay,
  piDemoViewport,
} from '@/components/toys/number-line/constants/demos/piDemo'

// ---------------------------------------------------------------------------
// Coordinate plane imports
// ---------------------------------------------------------------------------
import { renderCoordinatePlane } from '@/components/toys/coordinate-plane/renderCoordinatePlane'

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

export interface CanvasPreviewTarget {
  id: string
  type: 'canvas'
  label: string
  width: number
  height: number
  /** Self-contained recipe: build state, compute viewport, render to ctx. */
  render: (ctx: CanvasRenderingContext2D, w: number, h: number) => void
}

export interface AIPreviewTarget {
  id: string
  type: 'ai'
  label: string
  prompt: string
  width: number
  height: number
}

export type PreviewTarget = CanvasPreviewTarget | AIPreviewTarget

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

const PADDING_FRACTION = 0.15

/** Compute a viewport that frames all Euclid geometry into the given pixel dims. */
function computeFitViewport(state: ConstructionState, w: number, h: number): EuclidViewportState {
  let minX = Infinity
  let maxX = -Infinity
  let minY = Infinity
  let maxY = -Infinity

  for (const pt of getAllPoints(state)) {
    minX = Math.min(minX, pt.x)
    maxX = Math.max(maxX, pt.x)
    minY = Math.min(minY, pt.y)
    maxY = Math.max(maxY, pt.y)
  }

  for (const circle of getAllCircles(state)) {
    const center = getPoint(state, circle.centerId)
    if (!center) continue
    const r = getRadius(state, circle.id)
    minX = Math.min(minX, center.x - r)
    maxX = Math.max(maxX, center.x + r)
    minY = Math.min(minY, center.y - r)
    maxY = Math.max(maxY, center.y + r)
  }

  const rangeX = maxX - minX
  const rangeY = maxY - minY
  const padX = rangeX * PADDING_FRACTION
  const padY = rangeY * PADDING_FRACTION
  minX -= padX
  maxX += padX
  minY -= padY
  maxY += padY

  const cx = (minX + maxX) / 2
  const cy = (minY + maxY) / 2
  const scaleX = w / (maxX - minX)
  const scaleY = h / (maxY - minY)
  const pixelsPerUnit = Math.min(scaleX, scaleY)

  return { center: { x: cx, y: cy }, pixelsPerUnit }
}

// ---------------------------------------------------------------------------
// Canvas targets
// ---------------------------------------------------------------------------

const euclidProp1: CanvasPreviewTarget = {
  id: 'euclid-prop1',
  type: 'canvas',
  label: "Euclid's Elements",
  width: 800,
  height: 500,
  render(ctx, w, h) {
    const result = buildFinalState(1)
    if (!result) return
    const prop = PROP_REGISTRY[1]
    if (!prop) return
    const viewport = computeFitViewport(result.state, w, h)
    const idle = { tag: 'idle' as const }

    renderConstruction(
      ctx,
      result.state,
      viewport,
      w,
      h,
      idle, // compassPhase
      idle, // straightedgePhase
      null, // pointerWorld
      null, // snappedPointId
      [], // candidates
      0, // nextColorIndex
      null, // candidateFilter
      true, // isComplete
      prop.resultSegments,
      undefined, // hiddenElementIds
      false // transparentBg (white bg for preview)
    )
  },
}

const numberlinePi: CanvasPreviewTarget = {
  id: 'numberline-pi',
  type: 'canvas',
  label: 'Constant Demos',
  width: 800,
  height: 500,
  render(ctx, w, h) {
    const vp = piDemoViewport(w, h)
    const state = { center: vp.center, pixelsPerUnit: vp.pixelsPerUnit }
    renderPiOverlay(ctx, state, w, h, true, 0.6, 1)
  },
}

const coordinatePlane: CanvasPreviewTarget = {
  id: 'coordinate-plane',
  type: 'canvas',
  label: 'Coordinate Plane',
  width: 800,
  height: 500,
  render(ctx, w, h) {
    const state = { center: { x: 0, y: 0 }, pixelsPerUnit: { x: 50, y: 50 } }
    renderCoordinatePlane(ctx, state, w, h, true)
  },
}

// ---------------------------------------------------------------------------
// AI targets
// ---------------------------------------------------------------------------

const numberlineCall: AIPreviewTarget = {
  id: 'numberline-call',
  type: 'ai',
  label: 'Talk to a Number',
  width: 800,
  height: 500,
  prompt:
    'A whimsical illustration of a phone call interface floating above a number line. ' +
    'A child is talking on the phone to the number 7, which has a friendly face and is waving. ' +
    'The number line stretches horizontally with tick marks and numbers. ' +
    'Warm, playful art style with a dark background. Educational math app UI.',
}

const diceTray: AIPreviewTarget = {
  id: 'dice-tray',
  type: 'ai',
  label: 'Dice Tray',
  width: 800,
  height: 500,
  prompt:
    'Colorful 3D dice mid-roll on a dark felt surface, viewed from slightly above. ' +
    'Multiple dice in bright primary colors (red, blue, green, yellow) with white pips, ' +
    'some still spinning. Dramatic lighting with soft shadows. ' +
    'Clean, modern illustration style suitable for a math education app.',
}

// ---------------------------------------------------------------------------
// Registry
// ---------------------------------------------------------------------------

export const PREVIEW_TARGETS: PreviewTarget[] = [
  euclidProp1,
  numberlinePi,
  numberlineCall,
  coordinatePlane,
  diceTray,
]

export const CANVAS_TARGETS = PREVIEW_TARGETS.filter(
  (t): t is CanvasPreviewTarget => t.type === 'canvas'
)

export const AI_TARGETS = PREVIEW_TARGETS.filter((t): t is AIPreviewTarget => t.type === 'ai')

/** Look up a target by id. */
export function getPreviewTarget(id: string): PreviewTarget | undefined {
  return PREVIEW_TARGETS.find((t) => t.id === id)
}