video / src /mv /engine /draw.ts
Ethscriptions's picture
v7:街景楼群不跳;修正58秒光效;夜市服装与正面摊主;Drop2去货车/加方向盘/正后方起飞/去HUD/灰白冷气/Roadster比例;音频留1dB余量
624ddb0 verified
Raw History Blame Contribute Delete
21.2 kB
import { FONT, PALETTE, clamp, fract, hash, hash2, hash3, lerp, rgba } from './util'
type C2D = CanvasRenderingContext2D
// ---------------------------------------------------------------- sky / fog
export function vGradient(ctx: C2D, x: number, y: number, w: number, h: number, stops: [number, string][]) {
const g = ctx.createLinearGradient(0, y, 0, y + h)
for (const [o, c] of stops) g.addColorStop(o, c)
ctx.fillStyle = g
ctx.fillRect(x, y, w, h)
}
export function glowBlob(ctx: C2D, x: number, y: number, r: number, color: string, alpha: number) {
if (alpha <= 0.002 || r <= 0) return
const g = ctx.createRadialGradient(x, y, 0, x, y, r)
g.addColorStop(0, rgba(color, alpha))
g.addColorStop(0.4, rgba(color, alpha * 0.35))
g.addColorStop(1, rgba(color, 0))
ctx.fillStyle = g
ctx.fillRect(x - r, y - r, r * 2, r * 2)
}
export function fogBand(ctx: C2D, W: number, y: number, h: number, color: string, alpha: number) {
const g = ctx.createLinearGradient(0, y - h, 0, y + h)
g.addColorStop(0, rgba(color, 0))
g.addColorStop(0.5, rgba(color, alpha))
g.addColorStop(1, rgba(color, 0))
ctx.fillStyle = g
ctx.fillRect(0, y - h, W, h * 2)
}
// ---------------------------------------------------------------- buildings
/** audio levels for this frame — distant skylines bounce to the music */
export const PULSE = { low: 0, mid: 0, high: 0, kick: 0 }
export interface SkylineOpts {
W: number
seed: number
baseY: number
scroll: number
cell: number
minH: number
maxH: number
color: string
t: number
windows?: boolean
density?: number
winAlpha?: number
/** per-building height multiplier (equalizer etc.) */
hMod?: (k: number) => number
/** per-building window brightness boost */
litMod?: (k: number, row: number, rows: number) => number
roof?: boolean
rimColor?: string
/** do not bounce with the music */
still?: boolean
}
export function skyline(ctx: C2D, o: SkylineOpts) {
const k0 = Math.floor(o.scroll / o.cell) - 1
const k1 = k0 + Math.ceil(o.W / o.cell) + 3
const blds: { x: number; w: number; top: number; h: number; k: number }[] = []
ctx.fillStyle = o.color
for (let k = k0; k <= k1; k++) {
const cellX = k * o.cell - o.scroll
const w = o.cell * (0.6 + 0.4 * hash(k * 7.13 + o.seed))
const x = cellX + (o.cell - w) * hash(k * 3.31 + o.seed)
let h = lerp(o.minH, o.maxH, Math.pow(hash(k * 1.71 + o.seed * 3.1), 1.4))
if (o.hMod) h *= o.hMod(k)
else if (!o.still) {
const kk = ((k % 3) + 3) % 3
const band = kk === 0 ? PULSE.low : kk === 1 ? PULSE.mid : PULSE.high
h *= 1 + (0.22 * band + 0.16 * PULSE.kick) * (0.4 + 0.6 * hash(k * 2.9 + o.seed))
}
const top = o.baseY - h
ctx.fillRect(x, top, w, h + 4)
if (o.roof !== false) {
const r = hash(k * 9.7 + o.seed)
if (r > 0.55) {
// stepped crown
ctx.fillRect(x + w * 0.18, top - h * 0.05, w * 0.64, h * 0.05 + 1)
}
if (r > 0.72) {
// antenna
ctx.fillRect(x + w * (0.3 + 0.4 * hash(k + o.seed)), top - h * 0.18, Math.max(1, w * 0.025), h * 0.18)
}
if (r < 0.2) {
// water tank
const tw = w * 0.22
ctx.fillRect(x + w * 0.6, top - tw * 0.9, tw, tw * 0.9)
}
}
blds.push({ x, w, top, h, k })
}
if (o.rimColor) {
ctx.fillStyle = o.rimColor
for (const b of blds) ctx.fillRect(b.x, b.top, Math.max(1, b.w * 0.03), b.h)
}
if (o.windows === false) return
const dens = o.density ?? 0.3
const wa = o.winAlpha ?? 0.8
const sp = o.cell * 0.1
const ww = sp * 0.42
const wh = sp * 0.55
const flickT = Math.floor(o.t * 6)
for (let pass = 0; pass < 2; pass++) {
ctx.fillStyle = pass === 0 ? PALETTE.sodium : PALETTE.cyan
for (const b of blds) {
const cols = Math.max(1, Math.floor((b.w - sp * 0.6) / sp))
const rows = Math.max(1, Math.floor((b.h - sp) / (sp * 1.25)))
const ox = b.x + (b.w - cols * sp) / 2 + (sp - ww) / 2
for (let r = 0; r < rows; r++) {
const y = b.top + sp * 0.8 + r * sp * 1.25
for (let c = 0; c < cols; c++) {
const hv = hash3(b.k, r, c + o.seed)
let lit = hv < dens
const boost = o.litMod ? o.litMod(b.k, r, rows) : 0
if (!lit && boost <= 0) continue
const col = hash3(b.k + 3, r, c) > 0.82 ? 1 : 0
if (col !== pass) continue
if (hv < dens * 0.08) lit = hash3(b.k, r, flickT + c) > 0.3
const a = clamp((lit ? wa * (0.55 + 0.45 * hash3(c, r, b.k)) : 0) + boost)
if (a <= 0.02) continue
ctx.globalAlpha = a
ctx.fillRect(ox + c * sp, y, ww, wh)
}
}
}
}
ctx.globalAlpha = 1
}
// ---------------------------------------------------------------- rain
export interface RainOpts {
count: number
speed: number
len: number
slant: number
color: string
alpha: number
width: number
seed?: number
}
export function rain(ctx: C2D, W: number, H: number, t: number, o: RainOpts) {
const seed = o.seed ?? 0
ctx.strokeStyle = rgba(o.color, o.alpha)
ctx.lineWidth = o.width
ctx.lineCap = 'round'
ctx.beginPath()
const span = W + H * Math.abs(o.slant)
for (let i = 0; i < o.count; i++) {
const s = 0.6 + 0.8 * hash(i * 1.7 + seed)
const y = fract(hash(i * 1.31 + seed) + (t * o.speed * s) / H) * (H + o.len * 2) - o.len
const x = hash(i * 2.17 + seed) * span - (o.slant > 0 ? H * o.slant : 0) + y * o.slant
const l = o.len * s
ctx.moveTo(x, y)
ctx.lineTo(x + l * o.slant, y + l)
}
ctx.stroke()
}
export function splashes(ctx: C2D, W: number, y0: number, y1: number, t: number, count: number, color: string, alpha: number, u: number) {
if (!(y1 > y0 + 1)) return
ctx.strokeStyle = rgba(color, alpha)
ctx.lineWidth = Math.max(1, u * 1.2)
ctx.beginPath()
for (let i = 0; i < count; i++) {
const period = 0.5 + hash(i * 3.1) * 0.6
const ph = fract(t / period + hash(i * 5.3))
const cyc = Math.floor(t / period + hash(i * 5.3))
const x = hash2(i, cyc) * W
const y = lerp(y0, y1, hash2(i + 9, cyc))
const r = Math.max(0.01, ph * u * (8 + 10 * hash(i)) * (0.4 + (y - y0) / Math.max(1, y1 - y0)))
ctx.moveTo(x + r, y)
ctx.ellipse(x, y, r, r * 0.28, 0, 0, Math.PI * 2)
}
ctx.globalAlpha = 1
ctx.stroke()
}
// ---------------------------------------------------------------- figures
export interface WalkerOpts {
run?: number
color?: string
rim?: string
rimSide?: number
facing?: number
coat?: number
}
/** Procedural side-view hooded figure. phase: 0..1 per full stride (2 steps). */
export function walker(ctx: C2D, x: number, y: number, h: number, phase: number, o: WalkerOpts = {}) {
const run = o.run ?? 0
const facing = o.facing ?? 1
const col = o.color ?? '#040507'
const drawBody = (color: string, farColor: string, dx: number) => {
const P = Math.PI * 2
const bob = Math.abs(Math.sin(phase * P * 2)) * h * (0.012 + run * 0.02)
const lean = 0.06 + run * 0.28
const hipX = x + dx
const hipY = y - h * 0.47 - bob + run * h * 0.03
const shX = hipX + Math.sin(lean) * h * 0.31 * facing
const shY = hipY - Math.cos(lean) * h * 0.31
const L1 = h * 0.25
const L2 = h * 0.245
const leg = (ph: number, c: string) => {
const s = Math.sin(ph * P)
const th = s * (0.42 + run * 0.45)
const knee = Math.max(0, Math.cos(ph * P)) * (0.35 + run * 1.1) + 0.06
const kx = hipX + Math.sin(th) * L1 * facing
const ky = hipY + Math.cos(th) * L1
const sa = th - knee
const fx = kx + Math.sin(sa) * L2 * facing
const fy = ky + Math.cos(sa) * L2
ctx.strokeStyle = c
ctx.lineWidth = h * 0.072
ctx.beginPath()
ctx.moveTo(hipX, hipY)
ctx.lineTo(kx, ky)
ctx.lineTo(fx, fy)
ctx.stroke()
// shoe
ctx.lineWidth = h * 0.05
ctx.beginPath()
ctx.moveTo(fx - h * 0.01 * facing, fy)
ctx.lineTo(fx + h * 0.06 * facing, fy + h * 0.004)
ctx.stroke()
}
const arm = (ph: number, c: string) => {
const s = Math.sin(ph * P)
const a = -s * (0.45 + run * 0.7)
const elbow = 0.25 + run * 1.2
const ex = shX + Math.sin(a) * h * 0.17 * facing
const ey = shY + Math.cos(a) * h * 0.17
const fa = a - elbow * -1
const hx = ex + Math.sin(a + elbow) * h * 0.155 * facing
const hy = ey + Math.cos(a + elbow) * h * 0.155
void fa
ctx.strokeStyle = c
ctx.lineWidth = h * 0.058
ctx.beginPath()
ctx.moveTo(shX, shY + h * 0.02)
ctx.lineTo(ex, ey)
ctx.lineTo(hx, hy)
ctx.stroke()
}
ctx.lineCap = 'round'
ctx.lineJoin = 'round'
leg(phase + 0.5, farColor)
arm(phase, farColor)
// torso (hoodie)
ctx.fillStyle = color
ctx.beginPath()
const tw = h * 0.085
ctx.moveTo(hipX - tw * facing, hipY + h * 0.02)
ctx.quadraticCurveTo(hipX - tw * 1.25 * facing, (hipY + shY) / 2, shX - tw * 1.1 * facing, shY)
ctx.lineTo(shX + tw * 0.9 * facing, shY + h * 0.01)
ctx.quadraticCurveTo(hipX + tw * 1.25 * facing, (hipY + shY) / 2, hipX + tw * 0.95 * facing, hipY + h * 0.03)
ctx.closePath()
ctx.fill()
// coat tail flutter
const coat = o.coat ?? 0.6
if (coat > 0) {
const fl = Math.sin(phase * P * 2) * 0.5 + 0.5
ctx.beginPath()
ctx.moveTo(hipX - tw * facing, hipY - h * 0.02)
ctx.lineTo(hipX - (tw + h * (0.05 + 0.06 * run + 0.02 * fl) * coat) * facing, hipY + h * 0.1)
ctx.lineTo(hipX + tw * 0.6 * facing, hipY + h * 0.06)
ctx.closePath()
ctx.fill()
}
// head + hood
const hx = shX + h * 0.02 * facing
const hy = shY - h * 0.085
ctx.beginPath()
ctx.arc(hx, hy, h * 0.072, 0, Math.PI * 2)
ctx.fill()
ctx.beginPath()
ctx.moveTo(hx + h * 0.06 * facing, hy - h * 0.05)
ctx.quadraticCurveTo(hx - h * 0.02 * facing, hy - h * 0.11, hx - h * 0.1 * facing, hy - h * 0.02)
ctx.lineTo(hx - h * 0.075 * facing, hy + h * 0.07)
ctx.lineTo(hx, hy + h * 0.06)
ctx.closePath()
ctx.fill()
leg(phase, color)
arm(phase + 0.5, color)
}
if (o.rim) {
const side = o.rimSide ?? -1
drawBody(o.rim, o.rim, side * h * 0.014)
}
drawBody(col, '#0d0f14', 0)
}
/** Rear-view hooded figure (standing / slight sway). */
export function figureBack(ctx: C2D, x: number, y: number, h: number, sway = 0, o: { color?: string; rim?: string } = {}) {
const draw = (c: string, dx: number) => {
ctx.fillStyle = c
const sw = sway * h * 0.02
// legs
ctx.fillRect(x - h * 0.075 + dx, y - h * 0.46, h * 0.065, h * 0.46)
ctx.fillRect(x + h * 0.012 + dx, y - h * 0.46, h * 0.065, h * 0.46)
// body
ctx.beginPath()
ctx.moveTo(x - h * 0.13 + dx + sw, y - h * 0.8)
ctx.quadraticCurveTo(x - h * 0.17 + dx, y - h * 0.6, x - h * 0.12 + dx, y - h * 0.4)
ctx.lineTo(x + h * 0.12 + dx, y - h * 0.4)
ctx.quadraticCurveTo(x + h * 0.17 + dx, y - h * 0.6, x + h * 0.13 + dx + sw, y - h * 0.8)
ctx.closePath()
ctx.fill()
// arms
ctx.fillRect(x - h * 0.18 + dx + sw, y - h * 0.78, h * 0.055, h * 0.36)
ctx.fillRect(x + h * 0.125 + dx + sw, y - h * 0.78, h * 0.055, h * 0.36)
// hood
ctx.beginPath()
ctx.ellipse(x + dx + sw * 1.3, y - h * 0.88, h * 0.085, h * 0.1, 0, 0, Math.PI * 2)
ctx.fill()
}
if (o.rim) draw(o.rim, -h * 0.012)
draw(o.color ?? '#030405', 0)
}
// ---------------------------------------------------------------- neon
export interface NeonOpts {
font?: string
align?: CanvasTextAlign
baseline?: CanvasTextBaseline
tube?: boolean
core?: string
blur?: number
weight?: string
}
export function neonText(ctx: C2D, text: string, x: number, y: number, size: number, color: string, on: number, o: NeonOpts = {}) {
ctx.save()
ctx.font = `${o.weight ?? ''} ${size}px ${o.font ?? FONT.neon}`
ctx.textAlign = o.align ?? 'center'
ctx.textBaseline = o.baseline ?? 'middle'
const tube = o.tube ?? false
if (on < 0.98) {
// unlit glass tube
ctx.globalAlpha = 1
if (tube) {
ctx.strokeStyle = '#2a1d22'
ctx.lineWidth = size * 0.045
ctx.strokeText(text, x, y)
} else {
ctx.fillStyle = '#1d1519'
ctx.fillText(text, x, y)
}
}
if (on > 0.02) {
ctx.globalAlpha = clamp(on)
ctx.shadowColor = color
ctx.shadowBlur = size * (o.blur ?? 0.3)
if (tube) {
ctx.strokeStyle = color
ctx.lineWidth = size * 0.05
ctx.strokeText(text, x, y)
ctx.shadowBlur = size * 0.08
ctx.strokeStyle = o.core ?? '#fff3f5'
ctx.lineWidth = size * 0.018
ctx.strokeText(text, x, y)
} else {
ctx.fillStyle = color
ctx.fillText(text, x, y)
ctx.shadowBlur = size * 0.06
ctx.globalAlpha = clamp(on) * 0.75
ctx.fillStyle = o.core ?? '#fff3f5'
ctx.fillText(text, x, y)
}
}
ctx.restore()
}
export function neonSign(
ctx: C2D,
x: number,
y: number,
w: number,
h: number,
text: string,
color: string,
on: number,
vertical: boolean,
) {
ctx.save()
ctx.fillStyle = 'rgba(6,6,10,0.92)'
ctx.fillRect(x, y, w, h)
ctx.lineWidth = Math.max(1, Math.min(w, h) * 0.05)
ctx.strokeStyle = on > 0.3 ? color : '#221a1e'
if (on > 0.3) {
ctx.shadowColor = color
ctx.shadowBlur = Math.min(w, h) * 0.5 * on
}
ctx.globalAlpha = on > 0.3 ? on : 1
const inset = Math.min(w, h) * 0.08
ctx.strokeRect(x + inset, y + inset, w - inset * 2, h - inset * 2)
ctx.restore()
const chars = Array.from(text)
if (vertical) {
const cs = Math.min(w * 0.66, (h * 0.86) / chars.length)
const total = cs * chars.length
chars.forEach((c, i) => neonText(ctx, c, x + w / 2, y + (h - total) / 2 + cs * (i + 0.5), cs * 0.92, color, on, { blur: 0.25 }))
} else {
const cs = Math.min(h * 0.62, (w * 0.86) / chars.length)
neonText(ctx, text, x + w / 2, y + h / 2, cs, color, on, { blur: 0.25 })
}
}
// ---------------------------------------------------------------- light
export function lightCone(ctx: C2D, x: number, y: number, len: number, spread: number, angle: number, color: string, alpha: number) {
if (alpha <= 0.003) return
ctx.save()
ctx.translate(x, y)
ctx.rotate(angle)
const g = ctx.createLinearGradient(0, 0, 0, len)
g.addColorStop(0, rgba(color, alpha))
g.addColorStop(0.6, rgba(color, alpha * 0.25))
g.addColorStop(1, rgba(color, 0))
ctx.fillStyle = g
ctx.beginPath()
ctx.moveTo(-len * 0.01, 0)
ctx.lineTo(len * 0.01, 0)
ctx.lineTo(Math.tan(spread) * len, len)
ctx.lineTo(-Math.tan(spread) * len, len)
ctx.closePath()
ctx.fill()
ctx.restore()
}
export function streetLamp(ctx: C2D, x: number, groundY: number, h: number, on: number, color = PALETTE.sodium) {
ctx.fillStyle = '#07080b'
ctx.fillRect(x - h * 0.012, groundY - h, h * 0.024, h)
ctx.fillRect(x - h * 0.012, groundY - h, h * 0.16, h * 0.018)
const lx = x + h * 0.14
const ly = groundY - h + h * 0.03
ctx.fillRect(lx - h * 0.035, ly - h * 0.02, h * 0.07, h * 0.025)
lightCone(ctx, lx, ly, h * 1.05, 0.42, 0, color, 0.22 * on)
glowBlob(ctx, lx, ly, h * 0.12, color, 0.9 * on)
// pool of light on ground
ctx.save()
ctx.globalAlpha = 0.35 * on
const g = ctx.createRadialGradient(lx, groundY, 0, lx, groundY, h * 0.5)
g.addColorStop(0, rgba(color, 0.5))
g.addColorStop(1, rgba(color, 0))
ctx.fillStyle = g
ctx.scale(1, 0.25)
ctx.fillRect(lx - h * 0.5, (groundY - h * 0.5) * 4, h, h * 4)
ctx.restore()
}
// ---------------------------------------------------------------- effects
export function wetReflection(
ctx: C2D,
scratch: HTMLCanvasElement,
W: number,
H: number,
groundY: number,
t: number,
alpha: number,
ripple: number,
) {
const s = scratch.getContext('2d')!
s.setTransform(1, 0, 0, 1, 0, 0)
s.globalAlpha = 1
s.globalCompositeOperation = 'copy'
s.drawImage(ctx.canvas, 0, 0)
s.globalCompositeOperation = 'source-over'
ctx.save()
ctx.setTransform(1, 0, 0, 1, 0, 0)
const regionH = H - groundY
const strips = Math.max(12, Math.floor(regionH / Math.max(2, H / 260)))
const bh = regionH / strips
ctx.globalAlpha = alpha
for (let i = 0; i < strips; i++) {
const dy = i * bh
const srcY = groundY - dy - bh * 1.02
if (srcY < 0) break
const k = i / strips
const off = Math.sin(i * 0.83 + t * 3.1) * ripple * (0.3 + k) + Math.sin(i * 2.1 - t * 5) * ripple * 0.4 * k
ctx.save()
ctx.translate(off, groundY + dy + bh)
ctx.scale(1, -1)
ctx.drawImage(scratch, 0, srcY, W, bh * 1.02, 0, 0, W, bh * 1.02)
ctx.restore()
}
ctx.restore()
// darken / tint the wet ground
vGradient(ctx, 0, groundY, W, regionH, [
[0, 'rgba(4,6,10,0.25)'],
[1, 'rgba(4,6,10,0.85)'],
])
}
export function speedLines(ctx: C2D, cx: number, cy: number, R: number, t: number, count: number, color: string, alpha: number, inner = 0.35) {
ctx.fillStyle = rgba(color, alpha)
ctx.beginPath()
const fr = Math.floor(t * 18)
for (let i = 0; i < count; i++) {
if (hash2(i, fr) < 0.35) continue
const a = hash(i * 1.37) * Math.PI * 2
const wdt = 0.004 + 0.012 * hash(i * 4.1 + fr)
const r0 = R * (inner + 0.4 * hash2(i, fr + 1))
const r1 = R * 1.6
ctx.moveTo(cx + Math.cos(a) * r0, cy + Math.sin(a) * r0)
ctx.lineTo(cx + Math.cos(a - wdt) * r1, cy + Math.sin(a - wdt) * r1)
ctx.lineTo(cx + Math.cos(a + wdt) * r1, cy + Math.sin(a + wdt) * r1)
ctx.closePath()
}
ctx.fill()
}
/** Bottom subtitle with typewriter reveal. */
export function caption(ctx: C2D, W: number, H: number, text: string, lt: number, dur: number, portrait: boolean) {
const chars = Array.from(text)
const size = portrait ? Math.min(W * 0.06, H * 0.034) : H * 0.048
const y = portrait ? H * 0.8 : H * 0.875
const reveal = clamp(lt / Math.min(0.9, dur * 0.3))
const fade = clamp((dur - lt) / 0.35)
const n = Math.ceil(chars.length * reveal)
ctx.save()
ctx.font = `${size}px ${FONT.black}`
ctx.textBaseline = 'middle'
ctx.textAlign = 'left'
const spacing = size * 0.12
const widths = chars.map((c) => ctx.measureText(c).width)
const total = widths.reduce((s, w) => s + w + spacing, -spacing)
let x = (W - total) / 2
// accent bar
ctx.globalAlpha = fade
ctx.fillStyle = PALETTE.red
ctx.fillRect(x - size * 0.55, y - size * 0.45, size * 0.12, size * 0.9)
ctx.lineJoin = 'round'
for (let i = 0; i < n; i++) {
const c = chars[i]
const fresh = clamp((reveal * chars.length - i) / 1.5)
ctx.globalAlpha = fade * fresh
ctx.strokeStyle = 'rgba(0,0,0,0.85)'
ctx.lineWidth = size * 0.16
ctx.strokeText(c, x, y + (1 - fresh) * size * 0.25)
ctx.fillStyle = i === n - 1 && reveal < 1 ? PALETTE.sodium : PALETTE.bone
ctx.fillText(c, x, y + (1 - fresh) * size * 0.25)
x += widths[i] + spacing
}
ctx.restore()
}
/** Big poster text with manual RGB split. */
export function splitText(ctx: C2D, text: string, x: number, y: number, size: number, font: string, color: string, split: number, align: CanvasTextAlign = 'center') {
ctx.save()
ctx.font = `${size}px ${font}`
ctx.textAlign = align
ctx.textBaseline = 'middle'
if (split > 0.5) {
ctx.globalCompositeOperation = 'lighter'
ctx.fillStyle = 'rgba(255,40,80,0.85)'
ctx.fillText(text, x - split, y)
ctx.fillStyle = 'rgba(40,220,255,0.85)'
ctx.fillText(text, x + split, y)
ctx.globalCompositeOperation = 'source-over'
}
ctx.fillStyle = color
ctx.fillText(text, x, y)
ctx.restore()
}
export function marquee(ctx: C2D, text: string, y: number, W: number, size: number, offset: number, color: string, font = FONT.latin) {
ctx.save()
ctx.font = `${size}px ${font}`
ctx.textBaseline = 'middle'
ctx.textAlign = 'left'
ctx.fillStyle = color
const w = ctx.measureText(text).width + size
let x = -fract(offset / w) * w
while (x < W) {
ctx.fillText(text, x, y)
x += w
}
ctx.restore()
}
export function halftone(ctx: C2D, W: number, H: number, cell: number, color: string, f: (x: number, y: number) => number) {
ctx.fillStyle = color
ctx.beginPath()
for (let y = cell / 2; y < H; y += cell) {
for (let x = cell / 2; x < W; x += cell) {
const r = f(x / W, y / H) * cell * 0.5
if (r < 0.4) continue
ctx.moveTo(x + r, y)
ctx.arc(x, y, r, 0, Math.PI * 2)
}
}
ctx.fill()
}
export function lantern(ctx: C2D, x: number, y: number, r: number, swing: number, on: number) {
ctx.save()
ctx.translate(x, y)
ctx.rotate(swing)
ctx.strokeStyle = '#1a0d0a'
ctx.lineWidth = Math.max(1, r * 0.05)
ctx.beginPath()
ctx.moveTo(0, -r * 1.6)
ctx.lineTo(0, -r * 0.9)
ctx.stroke()
glowBlob(ctx, 0, 0, r * 3.2, PALETTE.amber, 0.35 * on)
const g = ctx.createRadialGradient(-r * 0.25, -r * 0.2, r * 0.1, 0, 0, r)
g.addColorStop(0, '#ffd59a')
g.addColorStop(0.45, '#ff4a2e')
g.addColorStop(1, '#7a0f12')
ctx.fillStyle = g
ctx.beginPath()
ctx.ellipse(0, 0, r, r * 0.85, 0, 0, Math.PI * 2)
ctx.fill()
ctx.fillStyle = '#2a0b07'
ctx.fillRect(-r * 0.4, -r * 0.98, r * 0.8, r * 0.18)
ctx.fillRect(-r * 0.4, r * 0.8, r * 0.8, r * 0.18)
// tassel
ctx.fillStyle = '#c4142f'
ctx.fillRect(-r * 0.06, r * 0.98, r * 0.12, r * 0.5)
ctx.restore()
}