video / src /mv /engine /scenesB.ts
Ethscriptions's picture
v6:街角开场;高速入隧道;愤怒表情;3D 车灯修正方向;远光随音乐;警灯;参照 Tesla Roadster 重做喷气飞越;终章靠树;天际线律动
c58db7d verified
Raw History Blame Contribute Delete
17.9 kB
import { BAR, BEAT, DROP2 } from './audio'
import {
figureBack,
fogBand,
glowBlob,
lantern,
lightCone,
neonSign,
neonText,
rain,
skyline,
speedLines,
splitText,
vGradient,
walker,
wetReflection,
} from './draw'
import { camera, resetCam, type SceneCtx } from './types'
import { HERO_H, bike, heroLean, heroPortrait, heroRide, heroSitPaper } from './hero'
import { FONT, PALETTE, clamp, easeInOutCubic, easeOutCubic, easeOutExpo, fbm, fract, hash, hash2, lerp, rgba, smoothstep } from './util'
const area = (s: SceneCtx) => (s.W * s.H) / (1920 * 1080)
// =====================================================================
// S7 — 暗场·心跳 (92 – 109.436s)
// =====================================================================
function ecg(ph: number): number {
const g = (c: number, w: number, a: number) => a * Math.exp(-Math.pow((ph - c) / w, 2))
return g(0.1, 0.025, 0.12) + g(0.2, 0.008, -0.18) + g(0.225, 0.01, 1) + g(0.25, 0.01, -0.35) + g(0.45, 0.04, 0.25)
}
export function sHeartbeat(s: SceneCtx) {
const { ctx, W, H, P, lt, u, t, dur } = s
const hbAt = (x: number) => (50 * x + (170 * x * x * x) / (3 * dur * dur)) / 60
const hb = hbAt(lt)
const hbPhase = fract(hb)
const beatGlow = Math.exp(-Math.max(0, hbPhase - 0.2) * 9) * (hbPhase > 0.2 ? 1 : 0)
// night sky, the moon shining down on him (replaces the street lamp)
vGradient(ctx, 0, 0, W, H, [
[0, '#03040c'],
[0.7, '#0a1024'],
[1, '#06060a'],
])
for (let i = 0; i < 140; i++) {
const x = hash(i * 1.37) * W
const y = hash(i * 2.71) * H * 0.6
ctx.fillStyle = '#dfe8ff'
ctx.globalAlpha = (0.3 + 0.7 * Math.abs(Math.sin(t * (0.5 + hash(i) * 2) + i))) * 0.7
const sz = u * (1.2 + 1.8 * hash(i * 7))
ctx.fillRect(x, y, sz, sz)
}
ctx.globalAlpha = 1
const lx = W / 2
const floorY = P ? H * 0.8 : H * 0.84
const on = 0.9 * (0.85 + 0.3 * beatGlow)
const mx = W * (P ? 0.7 : 0.74)
const my = H * 0.17
const mr = Math.min(W, H) * 0.085
glowBlob(ctx, mx, my, mr * 7, '#9fb8ff', 0.28)
glowBlob(ctx, mx, my, mr * 2.4, '#e8efff', 0.5)
const mg = ctx.createRadialGradient(mx - mr * 0.3, my - mr * 0.3, mr * 0.1, mx, my, mr)
mg.addColorStop(0, '#ffffff')
mg.addColorStop(0.7, '#e8eefc')
mg.addColorStop(1, '#b8c4e0')
ctx.fillStyle = mg
ctx.beginPath()
ctx.arc(mx, my, mr, 0, Math.PI * 2)
ctx.fill()
ctx.fillStyle = 'rgba(140,150,180,0.35)'
for (let i = 0; i < 6; i++) {
const a = hash(i * 4.1) * Math.PI * 2
const rr = mr * Math.sqrt(hash(i * 2.2)) * 0.7
ctx.beginPath()
ctx.arc(mx + Math.cos(a) * rr, my + Math.sin(a) * rr, mr * (0.08 + hash(i) * 0.12), 0, Math.PI * 2)
ctx.fill()
}
// distant rooftops on the horizon
ctx.fillStyle = '#05060c'
for (let i = 0; i < 16; i++) {
const bw = W / 16
const bh = H * (0.05 + 0.12 * hash(i * 3.3))
ctx.fillRect(i * bw, floorY - bh, bw + 1, bh)
}
ctx.fillStyle = '#07080c'
ctx.fillRect(0, floorY, W, H - floorY)
// moonbeam from the moon onto him
const dx = lx - mx
const dy = floorY - my
const len = Math.hypot(dx, dy)
const ang = Math.atan2(-dx, dy)
const spread = 0.17
lightCone(ctx, mx, my, len * 1.05, spread, ang, '#cfe0ff', 0.32 * on)
// pool of moonlight + the moon reflected in a puddle
ctx.save()
ctx.translate(lx, floorY)
ctx.scale(1, 0.18)
glowBlob(ctx, 0, 0, len * Math.tan(spread) * 1.6, '#cfe0ff', 0.55 * on)
ctx.restore()
ctx.save()
ctx.translate(mx * 0.6 + lx * 0.4, floorY + (H - floorY) * 0.5)
ctx.scale(1, 0.35)
glowBlob(ctx, 0, 0, mr * 1.6, '#e8efff', 0.35)
ctx.restore()
// ECG trace across the frame
const baseY = P ? H * 0.56 : H * 0.55
const span = 3.2
ctx.save()
ctx.strokeStyle = PALETTE.red
ctx.shadowColor = PALETTE.red
ctx.shadowBlur = u * 18
ctx.lineWidth = Math.max(2, u * 5)
ctx.lineJoin = 'round'
ctx.beginPath()
const steps = 220
for (let i = 0; i <= steps; i++) {
const x = (i / steps) * W
const tau = lt - (1 - i / steps) * span
const v = tau < 0 ? 0 : ecg(fract(hbAt(tau)))
const y = baseY - v * H * 0.16
if (i === 0) ctx.moveTo(x, y)
else ctx.lineTo(x, y)
}
ctx.globalAlpha = 0.85
ctx.stroke()
ctx.restore()
glowBlob(ctx, W - u * 4, baseY - ecg(hbPhase) * H * 0.16, u * 40, PALETTE.red, 0.9)
// rain glinting in the moonbeam
ctx.save()
const px = Math.cos(ang) * len * Math.tan(spread)
const py = Math.sin(ang) * len * Math.tan(spread)
ctx.beginPath()
ctx.moveTo(mx, my)
ctx.lineTo(lx + px * 1.05, floorY + py * 1.05)
ctx.lineTo(lx - px * 1.05, floorY - py * 1.05)
ctx.closePath()
ctx.clip()
rain(ctx, W, H, t, { count: Math.floor(260 * area(s)) + 60, speed: H * 1.5, len: u * 50, slant: 0.04, color: '#dfe8ff', alpha: 0.32 * on, width: Math.max(1, u * 1.4) })
ctx.restore()
// 阿飞 sits on the ground in front of his parked bike, reading the evening paper
// under the lamp … pulls out a lighter, sets the corner alight, lets it burn, drops it.
const fh = P ? H * 0.2 : H * 0.3
const hh = fh * 1.3
bike(ctx, lx + hh * 0.05, floorY - hh * 0.01, hh / HERO_H, { facing: -1, light: 0, parked: true })
const lighter = smoothstep(9.3, 10.5, lt)
const lit = lt > 10.25
const burn = lt < 10.6 ? 0 : lt < 13.8 ? lerp(0, 0.55, (lt - 10.6) / 3.2) : lt < 14.4 ? 0.55 : clamp(lerp(0.55, 1, (lt - 14.4) / 1.8), 0, 1)
const drop = smoothstep(13.8, 14.4, lt)
heroSitPaper(ctx, lx, floorY, hh, t, { lighter, lit, burn, drop })
if (burn > 0 && burn < 1) {
// warm firelight on him and the floor
ctx.save()
ctx.globalCompositeOperation = 'lighter'
glowBlob(ctx, lx, floorY - hh * (0.45 - drop * 0.4), hh * 1.2, '#ff7a2e', 0.35 * (0.85 + 0.15 * Math.sin(t * 13)))
ctx.restore()
}
policeCorner(s)
if (lt > 5.0 && lt < 9.3) {
const k = (lt - 5.0) / 4.3
ctx.fillStyle = '#050406'
ctx.fillRect(0, 0, W, H)
glowBlob(ctx, W * 0.85, H * 0.1, Math.max(W, H) * 0.5, '#9fb8ff', 0.25)
const fr = P ? W * 0.24 : H * 0.24
heroPortrait(ctx, W / 2, H * (P ? 0.46 : 0.48), fr * (1 + k * 0.06), { t, eyes: 0, tear: clamp(k * 1.3), rim: rgba('#cfe0ff', 0.8 * on) })
ctx.save()
ctx.strokeStyle = PALETTE.red
ctx.shadowColor = PALETTE.red
ctx.shadowBlur = u * 18
ctx.lineWidth = Math.max(2, u * 4)
ctx.globalAlpha = 0.7
ctx.beginPath()
for (let i = 0; i <= 160; i++) {
const x = (i / 160) * W
const tau = lt - (1 - i / 160) * span
const y = H * 0.9 - ecg(fract(hbAt(tau))) * H * 0.08
if (i === 0) ctx.moveTo(x, y)
else ctx.lineTo(x, y)
}
ctx.stroke()
ctx.restore()
}
// heartbeat red pulse vignette
const pulse = beatGlow * (0.15 + 0.35 * (lt / dur))
if (pulse > 0.01) {
const g = ctx.createRadialGradient(W / 2, H / 2, Math.min(W, H) * 0.3, W / 2, H / 2, Math.max(W, H) * 0.75)
g.addColorStop(0, 'rgba(155,21,48,0)')
g.addColorStop(1, `rgba(155,21,48,${pulse})`)
ctx.fillStyle = g
ctx.fillRect(0, 0, W, H)
}
// last 4 beats: 再 · 来 · 一 · 次
const bp = (t - DROP2) / BEAT
if (bp >= -4 && bp < 0) {
const words = ['再', '来', '一', '次']
const idx = Math.floor(bp + 4)
const ph = fract(bp)
const size = Math.min(W, H) * (0.42 + 0.12 * (1 - easeOutExpo(ph)))
splitText(ctx, words[idx], W / 2, H * 0.42, size, FONT.black, PALETTE.bone, u * 14 * (1 - ph))
}
}
// =====================================================================
// S9 — 终章·黑街之王 (127.64 – 144.3s)
// =====================================================================
function titlePoints(s: SceneCtx): { x: number; y: number }[] {
const key = `pts:${s.W}x${s.H}`
const cached = s.cache.get(key) as { x: number; y: number }[] | undefined
if (cached) return cached
const sc = 0.25
const c = document.createElement('canvas')
c.width = Math.max(4, Math.round(s.W * sc))
c.height = Math.max(4, Math.round(s.H * sc))
const g = c.getContext('2d')!
const L = titleLayout(s)
g.fillStyle = '#fff'
g.textAlign = 'center'
g.textBaseline = 'middle'
g.font = `${L.size * sc}px ${FONT.neon}`
for (const p of L.chars) g.fillText(p.c, p.x * sc, p.y * sc)
const img = g.getImageData(0, 0, c.width, c.height).data
const pts: { x: number; y: number }[] = []
const step = Math.max(1, Math.round(Math.min(c.width, c.height) / 140))
for (let y = 0; y < c.height; y += step) {
for (let x = 0; x < c.width; x += step) {
if (img[(y * c.width + x) * 4 + 3] > 128) pts.push({ x: x / sc, y: y / sc })
}
}
s.cache.set(key, pts)
return pts
}
function titleLayout(s: SceneCtx) {
const { W, H, P } = s
const size = P ? Math.min(W * 0.5, H * 0.22) : Math.min(W * 0.26, H * 0.42)
const cy = P ? H * 0.4 : H * 0.42
const chars = P
? [
{ c: '黑', x: W / 2, y: cy - size * 0.55 },
{ c: '街', x: W / 2, y: cy + size * 0.55 },
]
: [
{ c: '黑', x: W / 2 - size * 0.56, y: cy },
{ c: '街', x: W / 2 + size * 0.56, y: cy },
]
return { size, cy, chars }
}
export function sFinale(s: SceneCtx) {
const { ctx, W, H, P, lt, u, t, a } = s
if (t >= 144.22) {
ctx.fillStyle = '#000'
ctx.fillRect(0, 0, W, H)
return
}
// night hill: a big tree, a crescent moon, a train passing in the distance
vGradient(ctx, 0, 0, W, H, [
[0, '#04071a'],
[0.55, '#14204a'],
[0.8, '#3a1d4a'],
[1, '#120a18'],
])
// stars
for (let i = 0; i < 180; i++) {
const x = hash(i * 1.37) * W
const y = hash(i * 2.71) * H * 0.7
const tw = 0.35 + 0.65 * Math.abs(Math.sin(t * (0.6 + hash(i) * 2) + i))
ctx.fillStyle = i % 9 === 0 ? '#ffe6b0' : '#dfe8ff'
ctx.globalAlpha = tw * (0.5 + 0.5 * hash(i * 5))
const sz = u * (1.5 + 2 * hash(i * 7))
ctx.fillRect(x, y, sz, sz)
}
ctx.globalAlpha = 1
// crescent moon
{
const mx = P ? W * 0.76 : W * 0.82
const my = H * 0.16
const mr = Math.min(W, H) * 0.07
glowBlob(ctx, mx, my, mr * 4, '#ffe6b0', 0.25)
ctx.fillStyle = '#fff1c8'
ctx.beginPath()
ctx.arc(mx, my, mr, 0, Math.PI * 2)
ctx.fill()
ctx.fillStyle = '#0a1030'
ctx.beginPath()
ctx.arc(mx + mr * 0.45, my - mr * 0.2, mr * 0.92, 0, Math.PI * 2)
ctx.fill()
}
const horizon = P ? H * 0.8 : H * 0.78
// far hills
ctx.fillStyle = '#0c0f24'
ctx.beginPath()
ctx.moveTo(0, horizon)
for (let i = 0; i <= 30; i++) ctx.lineTo((i / 30) * W, horizon - H * (0.06 + 0.06 * fbm(i * 0.4, 11)))
ctx.lineTo(W, H)
ctx.lineTo(0, H)
ctx.fill()
// the train (red & yellow carriages, warm windows)
{
const ty = horizon - H * 0.035
const cw = u * (P ? 120 : 150)
const chh = cw * 0.32
const head = W * 1.15 - lt * W * 0.09
for (let c = 0; c < 9; c++) {
const x = head + c * cw * 1.04
if (x > W + cw || x + cw < -cw) continue
ctx.fillStyle = c === 0 ? '#c4142f' : c % 2 ? '#d9a12a' : '#b8202c'
ctx.fillRect(x, ty - chh, cw, chh)
ctx.fillStyle = '#2a0a0a'
ctx.fillRect(x, ty - chh * 0.25, cw, chh * 0.12)
ctx.fillStyle = '#ffe6a0'
for (let w = 0; w < 6; w++) ctx.fillRect(x + cw * (0.08 + w * 0.15), ty - chh * 0.78, cw * 0.09, chh * 0.32)
if (c === 0) glowBlob(ctx, x, ty - chh * 0.5, cw * 0.5, '#fff1c8', 0.9)
}
ctx.fillStyle = '#05060f'
ctx.fillRect(0, ty, W, u * 4)
}
// one police car far away on the horizon road — red / blue strobe bouncing off the haze
{
ctx.save()
ctx.globalCompositeOperation = 'lighter'
const py = horizon - H * 0.045
const px = W * 0.7 + Math.sin(lt * 0.3) * W * 0.01
const ph = Math.floor(t * 7) % 2 === 0
const pr = Math.min(W, H) * 0.012
glowBlob(ctx, px - pr, py, pr * 6, '#ff1a3a', ph ? 0.95 : 0.15)
glowBlob(ctx, px + pr, py, pr * 6, '#2a6aff', ph ? 0.15 : 0.95)
glowBlob(ctx, px - pr, py, pr * 1.2, '#ffffff', ph ? 0.9 : 0)
glowBlob(ctx, px + pr, py, pr * 1.2, '#ffffff', ph ? 0 : 0.9)
glowBlob(ctx, px, horizon - H * 0.1, Math.max(W, H) * 0.25, ph ? '#ff1a3a' : '#2a6aff', 0.1)
ctx.restore()
}
// near hill
ctx.fillStyle = '#05060c'
ctx.beginPath()
ctx.moveTo(0, horizon + H * 0.02)
ctx.quadraticCurveTo(W * 0.3, horizon - H * 0.06, W * 0.6, horizon + H * 0.01)
ctx.quadraticCurveTo(W * 0.8, horizon + H * 0.04, W, horizon + H * 0.02)
ctx.lineTo(W, H)
ctx.lineTo(0, H)
ctx.fill()
// the big tree
{
const tx = P ? W * 0.2 : W * 0.17
const base = horizon - H * 0.02
const th = P ? H * 0.42 : H * 0.6
ctx.fillStyle = '#04050a'
ctx.beginPath()
ctx.moveTo(tx - th * 0.05, base)
ctx.quadraticCurveTo(tx - th * 0.02, base - th * 0.4, tx - th * 0.1, base - th * 0.62)
ctx.lineTo(tx + th * 0.08, base - th * 0.62)
ctx.quadraticCurveTo(tx + th * 0.03, base - th * 0.4, tx + th * 0.07, base)
ctx.fill()
ctx.fillStyle = '#060a12'
ctx.beginPath()
for (let i = 0; i < 16; i++) {
const ang = (i / 16) * Math.PI * 2
const rr = th * (0.26 + 0.1 * hash(i * 2.3))
const px = tx + Math.cos(ang) * th * 0.32 * (0.7 + 0.3 * hash(i))
const py = base - th * 0.78 + Math.sin(ang) * th * 0.18 + Math.sin(t * 0.8 + i) * u * 2
ctx.moveTo(px + rr, py)
ctx.arc(px, py, rr, 0, Math.PI * 2)
}
ctx.fill()
// fireflies
ctx.fillStyle = '#d8ff9a'
for (let i = 0; i < 18; i++) {
const px = tx + Math.sin(t * 0.5 + i * 2.1) * th * 0.45
const py = base - th * (0.25 + 0.5 * hash(i * 3.3)) + Math.cos(t * 0.7 + i) * u * 20
ctx.globalAlpha = 0.4 + 0.6 * Math.abs(Math.sin(t * 2 + i))
ctx.fillRect(px, py, u * 3, u * 3)
}
ctx.globalAlpha = 1
// his bike parked beside the tree; 阿飞 leans against the trunk, watching the train
const bs = P ? W * 0.3 : H * 0.21
bike(ctx, tx + th * (P ? 0.5 : 0.4), base + u * 2, bs, { facing: -1, light: 0.35, parked: true })
heroLean(ctx, tx + th * 0.075, base + u * 2, bs * HERO_H, t, { facing: 1, rim: rgba('#9ab8ff', 0.6) })
}
// neon bursts on kicks
ctx.save()
ctx.globalCompositeOperation = 'lighter'
const b = a.beat
for (let k = 0; k < 3; k++) {
const bi = b - k
const age = a.beatPos - bi
if (age < 0 || age > 2.5) continue
const bx = W * (0.15 + 0.7 * hash(bi * 1.3))
const byy = H * (0.15 + 0.3 * hash(bi * 2.1))
const col = [PALETTE.red, PALETTE.cyan, PALETTE.sodium, PALETTE.violet][((bi % 4) + 4) % 4]
const R = Math.min(W, H) * 0.22 * easeOutExpo(age / 1.2)
ctx.fillStyle = rgba(col, clamp(1 - age / 2.5))
for (let p = 0; p < 36; p++) {
const ang = (p / 36) * Math.PI * 2 + hash(bi)
const rr = R * (0.85 + 0.15 * hash2(p, bi))
ctx.fillRect(bx + Math.cos(ang) * rr, byy + Math.sin(ang) * rr + age * age * u * 20, u * 4, u * 4)
}
}
ctx.restore()
// particles converge into the title
const pts = titlePoints(s)
const L = titleLayout(s)
const conv = clamp((lt - 5) / 4.5)
ctx.save()
ctx.globalCompositeOperation = 'lighter'
const n = pts.length
for (let i = 0; i < n; i++) {
const p = pts[i]
const d = hash(i * 0.37)
const k = easeInOutCubic(clamp((conv - d * 0.4) / 0.6))
if (k <= 0 && lt < 4) continue
const ang = hash(i * 1.9) * Math.PI * 2 + lt * 0.4
const rad = Math.max(W, H) * (0.3 + 0.6 * hash(i * 2.9))
const sx = W / 2 + Math.cos(ang) * rad
const sy = H / 2 + Math.sin(ang) * rad * 0.6
const jit = a.kick * u * 6 * (hash2(i, a.beat) - 0.5)
const x = lerp(sx, p.x, k) + jit
const y = lerp(sy, p.y, k) + jit
ctx.fillStyle = i % 5 === 0 ? PALETTE.cyan : i % 7 === 0 ? PALETTE.sodium : PALETTE.red
ctx.globalAlpha = clamp((lt - 3.5) / 1.5) * (1 - smoothstep(9.5, 11, lt) * 0.7)
const sz = u * (3 + 3 * hash(i))
ctx.fillRect(x - sz / 2, y - sz / 2, sz, sz)
}
ctx.restore()
// solid neon title
const titleOn = smoothstep(9.2, 10.2, lt)
if (titleOn > 0) {
ctx.fillStyle = `rgba(3,4,10,${0.35 * titleOn})`
ctx.fillRect(0, 0, W, H)
const fl = a.drop && hash(a.beat * 3.1) > 0.8 && a.beatPhase < 0.1 ? 0.3 : 1
glowBlob(ctx, W / 2, L.cy, L.size * 2.4, PALETTE.red, 0.3 * titleOn)
for (const c of L.chars) neonText(ctx, c.c, c.x, c.y, L.size, PALETTE.red, titleOn * fl, { tube: true, blur: 0.45 })
}
// credits
const cr = smoothstep(11, 12, lt)
if (cr > 0) {
ctx.save()
ctx.globalAlpha = cr
ctx.textAlign = 'center'
ctx.fillStyle = PALETTE.bone
const fs = P ? W * 0.05 : H * 0.045
ctx.font = `${fs}px ${FONT.black}`
const y0 = P ? H * 0.72 : H * 0.8
ctx.fillText('陈雅雯 · 黑街', W / 2, y0)
ctx.restore()
}
rain(ctx, W, H, t, { count: Math.floor(200 * area(s)) + 40, speed: H * 1.6, len: u * 60, slant: 0.1, color: '#a9c2d6', alpha: 0.2, width: Math.max(1, u * 1.3) })
void camera
void fbm
}
/** a police car parked just out of frame, lower-right: rotating beacon washing the scene */
function policeCorner(s: SceneCtx) {
const { ctx, W, H, t, u } = s
const ox = W * 1.01
const oy = H * 0.97
const red = Math.floor(t * 4) % 2 === 0
ctx.save()
ctx.globalCompositeOperation = 'lighter'
glowBlob(ctx, ox, oy, W * 0.5, red ? '#ff1a3a' : '#2a5aff', 0.3)
// two rotating beams sweeping across the frame
for (let k = 0; k < 2; k++) {
const ang = t * 4.2 + k * Math.PI
const dir = Math.PI + Math.sin(ang) * 0.9 - 0.35
const col = k ? '#2a5aff' : '#ff1a3a'
const L = Math.max(W, H) * 1.2
const g = ctx.createRadialGradient(ox, oy, 0, ox, oy, L)
g.addColorStop(0, rgba(col, 0.28 * Math.max(0, Math.cos(ang))))
g.addColorStop(1, rgba(col, 0))
ctx.fillStyle = g
ctx.beginPath()
ctx.moveTo(ox, oy)
ctx.lineTo(ox + Math.cos(dir - 0.12) * L, oy + Math.sin(dir - 0.12) * L)
ctx.lineTo(ox + Math.cos(dir + 0.12) * L, oy + Math.sin(dir + 0.12) * L)
ctx.closePath()
ctx.fill()
}
glowBlob(ctx, W * 0.985, H * 0.955, u * 40, red ? '#ff4060' : '#5a8aff', 0.9)
ctx.restore()
}