401 lines
14 KiB
Vue
401 lines
14 KiB
Vue
<script setup lang="ts">
|
|
// The title screen's background: an endless circuit board seen from above,
|
|
// drifting slowly by. Parts are placed one after the other, wires draw
|
|
// themselves between them, then signals run along them and lamps light up,
|
|
// like a factory building itself. With reduced motion, it is a still board.
|
|
|
|
const canvas = ref<HTMLCanvasElement>()
|
|
|
|
const CELL = 22
|
|
const CW = 30 // chunk size in cells
|
|
const CH = 18
|
|
|
|
type Kind = 'nand' | 'chip' | 'lamp' | 'switch' | 'hex'
|
|
interface Part {
|
|
kind: Kind
|
|
x: number
|
|
y: number
|
|
w: number
|
|
h: number
|
|
label?: string
|
|
ins: [number, number][]
|
|
outs: [number, number][]
|
|
born: number
|
|
}
|
|
interface Route {
|
|
pts: [number, number][]
|
|
len: number
|
|
born: number
|
|
/** Seconds between signals, and the offset of the first. */
|
|
every: number
|
|
phase: number
|
|
to?: Part
|
|
/** Several lines side by side: a cable. */
|
|
lanes: number
|
|
}
|
|
interface Chunk {
|
|
parts: Part[]
|
|
routes: Route[]
|
|
start: number
|
|
}
|
|
|
|
/** Same chunk, same circuit: a small seeded random generator. */
|
|
function rng(seed: number) {
|
|
let a = seed >>> 0
|
|
return () => {
|
|
a = (a + 0x6d2b79f5) >>> 0
|
|
let t = a
|
|
t = Math.imul(t ^ (t >>> 15), t | 1)
|
|
t ^= t + Math.imul(t ^ (t >>> 7), t | 61)
|
|
return ((t ^ (t >>> 14)) >>> 0) / 4294967296
|
|
}
|
|
}
|
|
|
|
const LABELS = ['ADD', 'ALU', 'REG', 'MUX', 'CNT', 'RAM', 'DEC', 'XOR', 'CPU', 'ROM', 'PC', 'ACC', 'SR', 'D-FF', 'CMP', 'BUS']
|
|
|
|
/** `warm`: made when the menu opens, so the board starts partly built. */
|
|
function makeChunk(cx: number, cy: number, now: number, warm: boolean): Chunk {
|
|
const r = rng(cx * 73856093 ^ cy * 19349663 ^ 0x5bd1e995)
|
|
const parts: Part[] = []
|
|
const free = (x: number, y: number, w: number, h: number) =>
|
|
x >= 1 && y >= 1 && x + w <= CW - 1 && y + h <= CH - 2 && !parts.some(p => x < p.x + p.w + 2 && x + w + 2 > p.x && y < p.y + p.h + 1 && y + h + 1 > p.y)
|
|
const count = 5 + Math.floor(r() * 5)
|
|
for (let i = 0; i < count * 6 && parts.length < count; i++) {
|
|
const roll = r()
|
|
const kind: Kind = roll < 0.3 ? 'nand' : roll < 0.6 ? 'chip' : roll < 0.75 ? 'lamp' : roll < 0.9 ? 'switch' : 'hex'
|
|
const [w, h] = kind === 'nand' ? [3, 2] : kind === 'chip' ? [4 + Math.floor(r() * 3), 3 + Math.floor(r() * 4)] : kind === 'lamp' ? [2, 1] : kind === 'switch' ? [2, 1] : [2, 3]
|
|
const x = Math.floor(r() * (CW - w))
|
|
const y = Math.floor(r() * (CH - h))
|
|
if (!free(x, y, w, h)) continue
|
|
const ins: [number, number][] = []
|
|
const outs: [number, number][] = []
|
|
if (kind === 'nand') {
|
|
ins.push([x, y + 0.5], [x, y + 1.5])
|
|
outs.push([x + w, y + 1])
|
|
} else if (kind === 'chip') {
|
|
for (let k = 0; k < h; k++) ins.push([x, y + 0.5 + k])
|
|
for (let k = 0; k < Math.max(1, h - 1); k++) outs.push([x + w, y + 0.5 + k])
|
|
} else if (kind === 'lamp') ins.push([x, y + 0.5])
|
|
else if (kind === 'switch') outs.push([x + w, y + 0.5])
|
|
else for (let k = 0; k < 3; k++) ins.push([x, y + 0.5 + k])
|
|
parts.push({ kind, x, y, w, h, ins, outs, label: kind === 'chip' ? LABELS[Math.floor(r() * LABELS.length)] : undefined, born: 0 })
|
|
}
|
|
// Built left to right, one part every second or two, as you watch.
|
|
parts.sort((a, b) => a.x - b.x)
|
|
const start = now + r() * 2 - (warm ? r() * 18 : 0)
|
|
let at = start
|
|
for (const p of parts) {
|
|
p.born = at
|
|
at += 1 + r() * 1.6
|
|
}
|
|
|
|
const routes: Route[] = []
|
|
const used = new Set<string>()
|
|
for (const p of parts) {
|
|
for (const o of p.outs) {
|
|
const targets = parts.filter(q => q !== p && q.x > o[0] + 1 && q.ins.length)
|
|
if (!targets.length) continue
|
|
const q = targets[Math.floor(r() * targets.length)]!
|
|
const free = q.ins.filter(i => !used.has(`${i[0]},${i[1]}`))
|
|
const i = free[Math.floor(r() * free.length)]
|
|
if (!i) continue
|
|
used.add(`${i[0]},${i[1]}`)
|
|
const mid = Math.round((o[0] + i[0]) / 2) + (r() < 0.5 ? 0 : 1)
|
|
const pts: [number, number][] = [o, [mid, o[1]], [mid, i[1]], i]
|
|
const len = pts.reduce((s, pt, k) => (k ? s + Math.abs(pt[0] - pts[k - 1]![0]) + Math.abs(pt[1] - pts[k - 1]![1]) : 0), 0)
|
|
routes.push({ pts, len, born: Math.max(p.born, q.born) + 0.2, every: 2 + r() * 3, phase: r() * 3, to: q, lanes: 1 })
|
|
}
|
|
}
|
|
// A cable runs through every chunk of some rows, so long buses cross the board.
|
|
const rowRand = rng(cy * 2654435761)
|
|
if (rowRand() < 0.6) {
|
|
const yy = CH - 1 + 0.5
|
|
const pts: [number, number][] = [
|
|
[0, yy],
|
|
[CW, yy],
|
|
]
|
|
routes.push({ pts, len: CW, born: start + 0.3, every: 3 + rowRand() * 2, phase: rowRand() * 3, lanes: 4 })
|
|
}
|
|
return { parts, routes, start }
|
|
}
|
|
|
|
let raf = 0
|
|
let alive = true
|
|
|
|
onMounted(() => {
|
|
const c = canvas.value!
|
|
const ctx = c.getContext('2d')!
|
|
const still = window.matchMedia('(prefers-reduced-motion: reduce)').matches
|
|
const chunks = new Map<string, Chunk>()
|
|
const t0 = performance.now()
|
|
let cam = { x: 0, y: 0 }
|
|
let first = true
|
|
|
|
function resize() {
|
|
const dpr = Math.min(2, window.devicePixelRatio || 1)
|
|
c.width = c.clientWidth * dpr
|
|
c.height = c.clientHeight * dpr
|
|
ctx.setTransform(dpr, 0, 0, dpr, 0, 0)
|
|
// A still board is drawn once, and again when the window changes size.
|
|
if (still && !first) requestAnimationFrame(frame)
|
|
}
|
|
resize()
|
|
window.addEventListener('resize', resize)
|
|
onBeforeUnmount(() => window.removeEventListener('resize', resize))
|
|
|
|
/** The point a share `f` (0..1) along a route. */
|
|
function along(route: Route, f: number): [number, number] {
|
|
let left = f * route.len
|
|
for (let k = 1; k < route.pts.length; k++) {
|
|
const a = route.pts[k - 1]!
|
|
const b = route.pts[k]!
|
|
const seg = Math.abs(b[0] - a[0]) + Math.abs(b[1] - a[1])
|
|
if (left <= seg) {
|
|
const t = seg ? left / seg : 0
|
|
return [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t]
|
|
}
|
|
left -= seg
|
|
}
|
|
return route.pts[route.pts.length - 1]!
|
|
}
|
|
|
|
function pathTo(route: Route, f: number, ox: number, oy: number, off: number) {
|
|
ctx.beginPath()
|
|
let left = f * route.len
|
|
const p0 = route.pts[0]!
|
|
ctx.moveTo(ox + p0[0] * CELL, oy + (p0[1] + off) * CELL)
|
|
for (let k = 1; k < route.pts.length && left > 0; k++) {
|
|
const a = route.pts[k - 1]!
|
|
const b = route.pts[k]!
|
|
const seg = Math.abs(b[0] - a[0]) + Math.abs(b[1] - a[1])
|
|
const t = seg ? Math.min(1, left / seg) : 1
|
|
ctx.lineTo(ox + (a[0] + (b[0] - a[0]) * t) * CELL, oy + (a[1] + (b[1] - a[1]) * t + off) * CELL)
|
|
left -= seg
|
|
}
|
|
ctx.stroke()
|
|
}
|
|
|
|
function drawPart(p: Part, ox: number, oy: number, k: number, lit: boolean) {
|
|
const x = ox + p.x * CELL
|
|
const y = oy + p.y * CELL
|
|
const w = p.w * CELL
|
|
const h = p.h * CELL
|
|
ctx.save()
|
|
ctx.globalAlpha = k
|
|
ctx.translate(x + w / 2, y + h / 2)
|
|
ctx.scale(0.85 + 0.15 * k, 0.85 + 0.15 * k)
|
|
ctx.translate(-w / 2, -h / 2)
|
|
ctx.lineWidth = 1.4
|
|
ctx.strokeStyle = '#3c4859'
|
|
ctx.fillStyle = '#141b24'
|
|
if (p.kind === 'nand') {
|
|
ctx.beginPath()
|
|
ctx.moveTo(0, 0)
|
|
ctx.lineTo(w * 0.45, 0)
|
|
ctx.arc(w * 0.45, h / 2, h / 2, -Math.PI / 2, Math.PI / 2)
|
|
ctx.lineTo(0, h)
|
|
ctx.closePath()
|
|
ctx.fill()
|
|
ctx.stroke()
|
|
ctx.beginPath()
|
|
ctx.arc(w * 0.45 + h / 2 + 4, h / 2, 3.5, 0, Math.PI * 2)
|
|
ctx.stroke()
|
|
} else if (p.kind === 'lamp') {
|
|
ctx.beginPath()
|
|
ctx.arc(w * 0.6, h / 2, CELL * 0.45, 0, Math.PI * 2)
|
|
ctx.fillStyle = lit ? '#ffd34d' : '#1b222c'
|
|
if (lit) {
|
|
ctx.shadowColor = 'rgba(255, 211, 77, 0.8)'
|
|
ctx.shadowBlur = 14
|
|
}
|
|
ctx.fill()
|
|
ctx.shadowBlur = 0
|
|
ctx.stroke()
|
|
} else if (p.kind === 'switch') {
|
|
ctx.beginPath()
|
|
ctx.roundRect(0, h * 0.15, w * 0.85, h * 0.7, h * 0.35)
|
|
ctx.fill()
|
|
ctx.stroke()
|
|
ctx.beginPath()
|
|
ctx.arc(w * 0.62, h / 2, h * 0.24, 0, Math.PI * 2)
|
|
ctx.fillStyle = '#9fb0c4'
|
|
ctx.fill()
|
|
} else {
|
|
ctx.beginPath()
|
|
ctx.roundRect(0, 0, w, h, 3)
|
|
ctx.fill()
|
|
ctx.stroke()
|
|
ctx.fillStyle = p.kind === 'hex' ? (lit ? '#ff5c5c' : '#3a2226') : '#56647a'
|
|
if (p.kind === 'hex') {
|
|
ctx.fillRect(w * 0.3, h * 0.2, w * 0.4, 2.5)
|
|
ctx.fillRect(w * 0.3, h * 0.5, w * 0.4, 2.5)
|
|
ctx.fillRect(w * 0.3, h * 0.78, w * 0.4, 2.5)
|
|
ctx.fillRect(w * 0.27, h * 0.22, 2.5, h * 0.28)
|
|
ctx.fillRect(w * 0.7, h * 0.52, 2.5, h * 0.28)
|
|
} else if (p.label) {
|
|
ctx.font = '600 12px Inter, sans-serif'
|
|
ctx.textAlign = 'center'
|
|
ctx.textBaseline = 'middle'
|
|
ctx.fillText(p.label, w / 2, h / 2)
|
|
}
|
|
}
|
|
// Pins
|
|
ctx.fillStyle = '#4c586b'
|
|
for (const [px, py] of [...p.ins, ...p.outs]) {
|
|
ctx.beginPath()
|
|
ctx.arc((px - p.x) * CELL, (py - p.y) * CELL, 2.2, 0, Math.PI * 2)
|
|
ctx.fill()
|
|
}
|
|
ctx.restore()
|
|
}
|
|
|
|
/** The chunk's builder: a small bot that flies to where the next part goes. */
|
|
function drawBuilder(ch: Chunk, ox: number, oy: number, now: number) {
|
|
const ps = ch.parts
|
|
const i = ps.findIndex(p => p.born > now)
|
|
if (i < 0 || !ps.length) return
|
|
const to = ps[i]!
|
|
const from = ps[i - 1]
|
|
const c = (p: Part): [number, number] => [ox + (p.x + p.w / 2) * CELL, oy + (p.y + p.h / 2) * CELL]
|
|
const [tx, ty] = c(to)
|
|
let x = tx
|
|
let y = ty
|
|
let fade = 1
|
|
if (from) {
|
|
const [fx, fy] = c(from)
|
|
const span = to.born - from.born
|
|
const t = Math.min(1, Math.max(0, (now - from.born) / span) * 1.4)
|
|
const e = t < 0.5 ? 2 * t * t : 1 - (-2 * t + 2) ** 2 / 2
|
|
x = fx + (tx - fx) * e
|
|
y = fy + (ty - fy) * e - Math.sin(e * Math.PI) * 18
|
|
} else fade = Math.max(0, 1 - (to.born - now) / 2)
|
|
// It hovers over the spot, then the part appears.
|
|
const bob = Math.sin(now * 6) * 1.5
|
|
ctx.save()
|
|
ctx.globalAlpha = fade
|
|
ctx.fillStyle = 'rgba(76, 194, 255, 0.12)'
|
|
ctx.beginPath()
|
|
ctx.ellipse(x, y + 12, 9, 3, 0, 0, Math.PI * 2)
|
|
ctx.fill()
|
|
ctx.shadowColor = 'rgba(76, 194, 255, 0.9)'
|
|
ctx.shadowBlur = 12
|
|
ctx.fillStyle = '#4cc2ff'
|
|
ctx.beginPath()
|
|
ctx.arc(x, y + bob, 3.5, 0, Math.PI * 2)
|
|
ctx.fill()
|
|
ctx.shadowBlur = 0
|
|
ctx.strokeStyle = 'rgba(76, 194, 255, 0.5)'
|
|
ctx.lineWidth = 1
|
|
ctx.beginPath()
|
|
ctx.arc(x, y + bob, 7, now * 4, now * 4 + Math.PI * 1.2)
|
|
ctx.stroke()
|
|
ctx.restore()
|
|
}
|
|
|
|
function frame(nowMs: number) {
|
|
if (!alive) return
|
|
const now = still ? 1e6 : (nowMs - t0) / 1000
|
|
const W = c.clientWidth
|
|
const H = c.clientHeight
|
|
// Drift down and to the right, slowly.
|
|
cam = { x: now * 18, y: now * 6 }
|
|
if (still) cam = { x: 0, y: 0 }
|
|
ctx.fillStyle = '#0b0f14'
|
|
ctx.fillRect(0, 0, W, H)
|
|
// Grid dots
|
|
ctx.fillStyle = '#1a222d'
|
|
const gx = -(cam.x % CELL)
|
|
const gy = -(cam.y % CELL)
|
|
for (let x = gx; x < W; x += CELL) for (let y = gy; y < H; y += CELL) ctx.fillRect(x, y, 1.2, 1.2)
|
|
|
|
const cwPx = CW * CELL
|
|
const chPx = CH * CELL
|
|
// Only chunks on screen: new ones are built where you can see it.
|
|
const x0 = Math.floor(cam.x / cwPx)
|
|
const y0 = Math.floor(cam.y / chPx)
|
|
const x1 = Math.floor((cam.x + W) / cwPx)
|
|
const y1 = Math.floor((cam.y + H) / chPx)
|
|
const seen = new Set<string>()
|
|
for (let cx = x0; cx <= x1; cx++) {
|
|
for (let cy = y0; cy <= y1; cy++) {
|
|
const key = `${cx},${cy}`
|
|
seen.add(key)
|
|
let ch = chunks.get(key)
|
|
if (!ch) {
|
|
ch = makeChunk(cx, cy, still ? now - 60 : now, first)
|
|
chunks.set(key, ch)
|
|
}
|
|
const ox = cx * cwPx - cam.x
|
|
const oy = cy * chPx - cam.y
|
|
// Wires: drawn out as they are laid, then signals run along them.
|
|
for (const r of ch.routes) {
|
|
const built = Math.min(1, Math.max(0, (now - r.born) / Math.max(0.6, r.len / 7)))
|
|
if (built <= 0) continue
|
|
for (let lane = 0; lane < r.lanes; lane++) {
|
|
const off = (lane - (r.lanes - 1) / 2) * 0.16
|
|
ctx.strokeStyle = r.lanes > 1 ? '#253040' : '#2a3442'
|
|
ctx.lineWidth = r.lanes > 1 ? 1.3 : 1.8
|
|
pathTo(r, built, ox, oy, off)
|
|
}
|
|
if (built < 1) {
|
|
// The tip of a wire being laid.
|
|
const [tx, ty] = along(r, built)
|
|
ctx.fillStyle = '#4cc2ff'
|
|
ctx.beginPath()
|
|
ctx.arc(ox + tx * CELL, oy + ty * CELL, 2.4, 0, Math.PI * 2)
|
|
ctx.fill()
|
|
continue
|
|
}
|
|
const cycle = (now - r.born + r.phase) % r.every
|
|
const travel = r.len / 9
|
|
if (cycle < travel) {
|
|
const f = cycle / travel
|
|
ctx.strokeStyle = 'rgba(61, 220, 132, 0.55)'
|
|
ctx.lineWidth = r.lanes > 1 ? 1.4 : 2
|
|
for (let lane = 0; lane < r.lanes; lane++) pathTo(r, f, ox, oy, (lane - (r.lanes - 1) / 2) * 0.16)
|
|
const [px, py] = along(r, f)
|
|
ctx.fillStyle = '#3ddc84'
|
|
ctx.shadowColor = 'rgba(61, 220, 132, 0.9)'
|
|
ctx.shadowBlur = 10
|
|
ctx.beginPath()
|
|
ctx.arc(ox + px * CELL, oy + py * CELL, 2.8, 0, Math.PI * 2)
|
|
ctx.fill()
|
|
ctx.shadowBlur = 0
|
|
}
|
|
}
|
|
for (const p of ch.parts) {
|
|
const k = Math.min(1, Math.max(0, (now - p.born) / 0.4))
|
|
if (k <= 0) continue
|
|
// A lamp or display lights for a moment after a signal reaches it.
|
|
const lit = ch.routes.some(r => r.to === p && now > r.born && (now - r.born + r.phase) % r.every < r.len / 9 + 0.9 && (now - r.born + r.phase) % r.every > r.len / 9)
|
|
drawPart(p, ox, oy, k, lit)
|
|
// A ring where a part was just put down.
|
|
const age = now - p.born
|
|
if (age < 0.7 && !still) {
|
|
const g = age / 0.7
|
|
ctx.strokeStyle = `rgba(76, 194, 255, ${0.6 * (1 - g)})`
|
|
ctx.lineWidth = 1.5
|
|
const pad = 4 + g * 10
|
|
ctx.strokeRect(ox + p.x * CELL - pad, oy + p.y * CELL - pad, p.w * CELL + pad * 2, p.h * CELL + pad * 2)
|
|
}
|
|
}
|
|
if (!still) drawBuilder(ch, ox, oy, now)
|
|
}
|
|
}
|
|
for (const key of chunks.keys()) if (!seen.has(key)) chunks.delete(key)
|
|
first = false
|
|
if (!still) raf = requestAnimationFrame(frame)
|
|
}
|
|
raf = requestAnimationFrame(frame)
|
|
})
|
|
|
|
onBeforeUnmount(() => {
|
|
alive = false
|
|
cancelAnimationFrame(raf)
|
|
})
|
|
</script>
|
|
|
|
<template>
|
|
<canvas ref="canvas" class="menu-bg" aria-hidden="true" />
|
|
</template>
|