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 | import type { ConstructionState, EuclidViewportState, CompassPhase, StraightedgePhase, IntersectionCandidate, } from '../types' import type { PostCompletionAction } from '../engine/replayConstruction' import { BYRNE } from '../types' import { getAllPoints, getAllCircles, getAllSegments, getPoint, getRadius, } from '../engine/constructionState' import { isCandidateBeyondPoint } from '../engine/intersections' import { worldToScreen2D } from '../../shared/coordinateConversions' export const BG_COLOR = '#FAFAF0' const POINT_RADIUS = 5 const SNAP_RING_RADIUS = 8 const LABEL_FONT = '14px system-ui, sans-serif' const LABEL_OFFSET_X = 10 const LABEL_OFFSET_Y = -10 const CANDIDATE_RADIUS = 4 const CANDIDATE_COLOR_ACTIVE = 'rgba(120, 120, 120, 0.5)' const CANDIDATE_COLOR_DIM = 'rgba(120, 120, 120, 0.15)' const RESULT_COLOR = '#10b981' // matches completion banner green function toScreen(wx: number, wy: number, viewport: EuclidViewportState, w: number, h: number) { return worldToScreen2D( wx, wy, viewport.center.x, viewport.center.y, viewport.pixelsPerUnit, viewport.pixelsPerUnit, w, h ) } export function renderConstruction( ctx: CanvasRenderingContext2D, state: ConstructionState, viewport: EuclidViewportState, w: number, h: number, compassPhase: CompassPhase, straightedgePhase: StraightedgePhase, pointerWorld: { x: number; y: number } | null, snappedPointId: string | null, candidates: IntersectionCandidate[], nextColorIndex: number, candidateFilter?: { ofA: string; ofB: string; beyondId?: string } | null, isComplete?: boolean, resultSegments?: Array<{ fromId: string; toId: string }>, hiddenElementIds?: Set<string>, transparentBg?: boolean | 'preserve', draggablePointIds?: string[], postCompletionActions?: PostCompletionAction[] ) { const ppu = viewport.pixelsPerUnit // 1. Background if (transparentBg === 'preserve') { // Caller has already prepared the bg (e.g. with a constraint field) — // leave whatever's on the canvas alone. } else if (transparentBg) { ctx.clearRect(0, 0, w, h) } else { ctx.fillStyle = BG_COLOR ctx.fillRect(0, 0, w, h) } // 2. Completed circles for (const circle of getAllCircles(state)) { if (hiddenElementIds?.has(circle.id)) continue const center = getPoint(state, circle.centerId) if (!center) continue const r = getRadius(state, circle.id) if (r <= 0) continue const sc = toScreen(center.x, center.y, viewport, w, h) const sr = r * ppu ctx.beginPath() ctx.arc(sc.x, sc.y, sr, 0, Math.PI * 2) ctx.strokeStyle = circle.color ctx.lineWidth = 2 ctx.stroke() } // 3. Completed segments for (const seg of getAllSegments(state)) { if (hiddenElementIds?.has(seg.id)) continue const from = getPoint(state, seg.fromId) const to = getPoint(state, seg.toId) if (!from || !to) continue const sf = toScreen(from.x, from.y, viewport, w, h) const st = toScreen(to.x, to.y, viewport, w, h) ctx.beginPath() ctx.moveTo(sf.x, sf.y) ctx.lineTo(st.x, st.y) ctx.strokeStyle = seg.color ctx.lineWidth = 2 ctx.stroke() } // 4. Intersection candidates — active ones prominent, others dimmed for (const c of candidates) { const sc = toScreen(c.x, c.y, viewport, w, h) let color = CANDIDATE_COLOR_ACTIVE if (isComplete) { color = CANDIDATE_COLOR_DIM } else if (candidateFilter && candidateFilter.ofA && candidateFilter.ofB) { const matchesElements = (c.ofA === candidateFilter.ofA && c.ofB === candidateFilter.ofB) || (c.ofA === candidateFilter.ofB && c.ofB === candidateFilter.ofA) let isPreferred = true if (matchesElements && candidateFilter.beyondId) { isPreferred = isCandidateBeyondPoint(c, candidateFilter.beyondId, c.ofA, c.ofB, state) } else if (matchesElements && !candidateFilter.beyondId) { // When multiple candidates match with no beyondId (e.g. circle-circle), // only highlight the one with the highest Y (matches tutorial arrow convention) const hasHigherMatch = candidates.some( (other) => other !== c && ((other.ofA === candidateFilter.ofA && other.ofB === candidateFilter.ofB) || (other.ofA === candidateFilter.ofB && other.ofB === candidateFilter.ofA)) && other.y > c.y ) if (hasHigherMatch) isPreferred = false } color = matchesElements && isPreferred ? CANDIDATE_COLOR_ACTIVE : CANDIDATE_COLOR_DIM } ctx.beginPath() ctx.arc(sc.x, sc.y, CANDIDATE_RADIUS, 0, Math.PI * 2) ctx.strokeStyle = color ctx.lineWidth = 1.5 ctx.stroke() } // 5. (Active tool previews moved to renderToolOverlay.ts) // 6. Marked points — filled circles with labels for (const pt of getAllPoints(state)) { if (hiddenElementIds?.has(pt.id)) continue const sp = toScreen(pt.x, pt.y, viewport, w, h) ctx.beginPath() ctx.arc(sp.x, sp.y, POINT_RADIUS, 0, Math.PI * 2) ctx.fillStyle = pt.color || BYRNE.given ctx.fill() // Label ctx.font = LABEL_FONT ctx.fillStyle = pt.color || BYRNE.given ctx.textAlign = 'left' ctx.textBaseline = 'bottom' ctx.fillText(pt.label, sp.x + LABEL_OFFSET_X, sp.y + LABEL_OFFSET_Y) } // 6b. Draggable point ripple rings (post-completion invitation to interact) if (isComplete && draggablePointIds && draggablePointIds.length > 0) { const time = performance.now() / 1000 const RIPPLE_PERIOD = 2.5 // seconds per full ripple cycle const RIPPLE_COUNT = 2 // concurrent expanding rings per point const RIPPLE_MAX_RADIUS = 22 // max ring radius (screen px) const STAGGER_PER_POINT = 0.3 // seconds offset between points // Build a lookup for extend actions so we can draw rail lines const extendActions = new Map<string, { baseId: string; throughId: string }>() if (postCompletionActions) { for (const a of postCompletionActions) { if (a.type === 'extend') extendActions.set(a.pointId, { baseId: a.baseId, throughId: a.throughId }) } } for (let ptIdx = 0; ptIdx < draggablePointIds.length; ptIdx++) { const ptId = draggablePointIds[ptIdx] const pt = getPoint(state, ptId) if (!pt) continue if (hiddenElementIds?.has(ptId)) continue const sp = toScreen(pt.x, pt.y, viewport, w, h) const extendInfo = extendActions.get(ptId) const isExtendPt = pt.origin === 'extend' && extendInfo != null if (isExtendPt) { // ── Rail line: dashed line along the ray through the extend point ── const basePt = getPoint(state, extendInfo.baseId) const throughPt = getPoint(state, extendInfo.throughId) if (basePt && throughPt) { const dx = throughPt.x - basePt.x const dy = throughPt.y - basePt.y const len = Math.sqrt(dx * dx + dy * dy) if (len > 0.001) { // Draw rail from throughPt to well past the extend point const throughSp = toScreen(throughPt.x, throughPt.y, viewport, w, h) const railLength = 60 // px beyond the extend point // Use screen-space direction (world→screen may flip Y) const sdx = sp.x - throughSp.x const sdy = sp.y - throughSp.y const slen = Math.sqrt(sdx * sdx + sdy * sdy) if (slen > 1) { const snx = sdx / slen const sny = sdy / slen ctx.save() ctx.setLineDash([5, 4]) ctx.beginPath() ctx.moveTo(throughSp.x, throughSp.y) ctx.lineTo(sp.x + snx * railLength, sp.y + sny * railLength) ctx.strokeStyle = 'rgba(225, 87, 89, 0.3)' // Byrne red, subtle ctx.lineWidth = 1.5 ctx.stroke() ctx.setLineDash([]) ctx.restore() // Small arrowhead at the far end of the rail const arrowX = sp.x + snx * railLength const arrowY = sp.y + sny * railLength const arrowSize = 5 ctx.beginPath() ctx.moveTo(arrowX, arrowY) ctx.lineTo( arrowX - snx * arrowSize + sny * arrowSize * 0.5, arrowY - sny * arrowSize - snx * arrowSize * 0.5 ) ctx.moveTo(arrowX, arrowY) ctx.lineTo( arrowX - snx * arrowSize - sny * arrowSize * 0.5, arrowY - sny * arrowSize + snx * arrowSize * 0.5 ) ctx.strokeStyle = 'rgba(225, 87, 89, 0.35)' ctx.lineWidth = 1.5 ctx.stroke() } } } // ── Extend point ripples: Byrne red instead of blue ── for (let ring = 0; ring < RIPPLE_COUNT; ring++) { const offset = ring / RIPPLE_COUNT + (ptIdx * STAGGER_PER_POINT) / RIPPLE_PERIOD const t = (time / RIPPLE_PERIOD + offset) % 1 const radius = POINT_RADIUS + t * RIPPLE_MAX_RADIUS const alpha = 0.5 * (1 - t) ctx.beginPath() ctx.arc(sp.x, sp.y, radius, 0, Math.PI * 2) ctx.strokeStyle = `rgba(225, 87, 89, ${alpha})` // Byrne red: #E15759 ctx.lineWidth = 2 ctx.stroke() } } else { // ── Standard draggable point: blue ripple rings ── for (let ring = 0; ring < RIPPLE_COUNT; ring++) { const offset = ring / RIPPLE_COUNT + (ptIdx * STAGGER_PER_POINT) / RIPPLE_PERIOD const t = (time / RIPPLE_PERIOD + offset) % 1 const radius = POINT_RADIUS + t * RIPPLE_MAX_RADIUS const alpha = 0.5 * (1 - t) ctx.beginPath() ctx.arc(sp.x, sp.y, radius, 0, Math.PI * 2) ctx.strokeStyle = `rgba(78, 121, 167, ${alpha})` // Byrne blue: #4E79A7 ctx.lineWidth = 2 ctx.stroke() } } } } // 7. Snap highlight — larger ring around nearest point if (snappedPointId) { const pt = getPoint(state, snappedPointId) if (pt) { const sp = toScreen(pt.x, pt.y, viewport, w, h) ctx.beginPath() ctx.arc(sp.x, sp.y, SNAP_RING_RADIUS, 0, Math.PI * 2) ctx.strokeStyle = pt.color || BYRNE.given ctx.lineWidth = 2 ctx.stroke() } } // 8. Result highlight — glowing segments on completion if (isComplete && resultSegments) { for (const seg of resultSegments) { const from = getPoint(state, seg.fromId) const to = getPoint(state, seg.toId) if (!from || !to) continue const sf = toScreen(from.x, from.y, viewport, w, h) const st = toScreen(to.x, to.y, viewport, w, h) // Glow layer ctx.beginPath() ctx.moveTo(sf.x, sf.y) ctx.lineTo(st.x, st.y) ctx.strokeStyle = 'rgba(16, 185, 129, 0.25)' ctx.lineWidth = 10 ctx.lineCap = 'round' ctx.stroke() // Core line ctx.beginPath() ctx.moveTo(sf.x, sf.y) ctx.lineTo(st.x, st.y) ctx.strokeStyle = RESULT_COLOR ctx.lineWidth = 3.5 ctx.lineCap = 'round' ctx.stroke() } // Highlight the result endpoints for (const seg of resultSegments) { for (const ptId of [seg.fromId, seg.toId]) { const pt = getPoint(state, ptId) if (!pt) continue const sp = toScreen(pt.x, pt.y, viewport, w, h) // Glow ring ctx.beginPath() ctx.arc(sp.x, sp.y, POINT_RADIUS + 5, 0, Math.PI * 2) ctx.strokeStyle = 'rgba(16, 185, 129, 0.3)' ctx.lineWidth = 3 ctx.stroke() // Point fill ctx.beginPath() ctx.arc(sp.x, sp.y, POINT_RADIUS + 1, 0, Math.PI * 2) ctx.fillStyle = RESULT_COLOR ctx.fill() } } } } /** * Render a "drag the points" invitation text on the canvas post-completion. * Fades in after a delay, lingers, then fades to a subtle level. * * @param completionTime - performance.now() when completion first happened * @returns true if still animating (needs next frame) */ export function renderDragInvitation( ctx: CanvasRenderingContext2D, w: number, h: number, completionTime: number ): boolean { const now = performance.now() const elapsed = now - completionTime // Timeline: 1.5s delay → 0.6s fade in → 3s hold → 1s fade to residual const DELAY = 1500 const FADE_IN = 600 const HOLD = 3000 const FADE_OUT = 1000 const RESIDUAL_ALPHA = 0.15 // stays faintly visible forever if (elapsed < DELAY) return true // still waiting let alpha: number const t = elapsed - DELAY if (t < FADE_IN) { // Fading in alpha = (t / FADE_IN) * 0.85 } else if (t < FADE_IN + HOLD) { // Holding alpha = 0.85 } else if (t < FADE_IN + HOLD + FADE_OUT) { // Fading to residual const ft = (t - FADE_IN - HOLD) / FADE_OUT alpha = 0.85 - (0.85 - RESIDUAL_ALPHA) * ft } else { alpha = RESIDUAL_ALPHA } ctx.save() ctx.globalAlpha = alpha ctx.font = '600 15px system-ui, sans-serif' ctx.textAlign = 'center' ctx.textBaseline = 'bottom' ctx.fillStyle = '#4E79A7' ctx.fillText('Drag the points!', w / 2, h - 80) ctx.restore() return t < FADE_IN + HOLD + FADE_OUT // still animating? } |