Spaces:
Paused
Paused
Download src/mv/engine/hero.ts from Ethscriptions/video: direct link, hf CLI and curl.
- Browser
- Download file 77.3 kB
-
https://huggingface.co/spaces/Ethscriptions/video/resolve/main/src/mv/engine/hero.ts
- Command line
-
hf download hf://spaces/Ethscriptions/video/src/mv/engine/hero.ts
-
curl -L -o hero.ts https://huggingface.co/spaces/Ethscriptions/video/resolve/main/src/mv/engine/hero.ts
77.3 kB
// 主角「阿飞」v3 — redrawn from the protagonist of the reference footage:
// a young man with a big mop of curly black hair, a deep-blue long-sleeve
// sweater, grey trousers, white-soled sneakers, and his black classic
// motorcycle (round headlight, two round mirrors, red tail light).
//
// v3: everything is built on ONE rig.
// • The bike exposes anchor points (seat / grip / peg) in its own units.
// • The rider's limbs are solved with two-bone IK onto those anchors, so
// hands are always on the grips, feet on the pegs, hips on the seat.
// • Rider and bike share one scale: standing height h = HERO_H * bike scale s.
// • Draw order is explicit (far limbs → bike → body → near limbs), so
// nothing pokes through the bike.
import { glowBlob, lightCone } from './draw'
import { clamp, fract, hash, rgba } from './util'
type C2D = CanvasRenderingContext2D
interface V {
x: number
y: number
}
export const HERO = {
skin: '#ecd0a8',
skinShade: '#c49f7c',
blush: '#e89a86',
hair: '#121411',
hairHi: '#44596f',
line: '#08090c',
sweater: '#2f5072',
sweaterShade: '#223a54',
sweaterHi: '#4a74a0',
rib: '#264262',
pants: '#8d9298',
pantsShade: '#666b72',
shoe: '#1d1d21',
sole: '#e4ded2',
bike: '#15171c',
bikeShade: '#0b0c10',
bikeHi: '#56606f',
chrome: '#cfd6de',
chromeShade: '#7e8894',
seat: '#221c19',
tire: '#0a0a0c',
}
/** active palette (lets the same rig draw other characters, e.g. the stall auntie) */
let C: typeof HERO = HERO
export function withPalette(p: Partial<typeof HERO>, fn: () => void) {
const prev = C
C = { ...HERO, ...p }
try {
fn()
} finally {
C = prev
}
}
export const AUNTIE: Partial<typeof HERO> = {
sweater: '#8a3a3a',
sweaterShade: '#6a2a2c',
sweaterHi: '#b0585a',
rib: '#5a2426',
pants: '#2f3440',
pantsShade: '#22262f',
hair: '#3a3a3c',
hairHi: '#6a6a70',
skin: '#e2c09a',
}
export const RIVAL: Partial<typeof HERO> = {
sweater: '#17181c',
sweaterShade: '#0e0f12',
sweaterHi: '#3a3c44',
rib: '#25262c',
}
/** standing height of the hero in bike units (bike wheelbase = 1) */
export const HERO_H = 1.4
/** total height of rider + bike in bike units (for layout) */
export const RIDE_TOP = 1.5
// ------------------------------------------------------------------ geometry helpers
const add = (a: V, b: V): V => ({ x: a.x + b.x, y: a.y + b.y })
const lerpV = (a: V, b: V, k: number): V => ({ x: a.x + (b.x - a.x) * k, y: a.y + (b.y - a.y) * k })
/** Two-bone IK. bend: -1 / +1 picks which side the joint goes. Returns joint + end. */
export function ik(a: V, target: V, l1: number, l2: number, bend: number): { j: V; e: V } {
const dx = target.x - a.x
const dy = target.y - a.y
let d = Math.hypot(dx, dy)
const ang = Math.atan2(dy, dx)
d = clamp(d, Math.abs(l1 - l2) + 1e-4, l1 + l2 - 1e-4)
const cosA = (l1 * l1 + d * d - l2 * l2) / (2 * l1 * d)
const A = Math.acos(clamp(cosA, -1, 1))
const ja = ang + bend * A
const j = { x: a.x + Math.cos(ja) * l1, y: a.y + Math.sin(ja) * l1 }
const ea = Math.atan2(target.y - j.y, target.x - j.x)
return { j, e: { x: j.x + Math.cos(ea) * l2, y: j.y + Math.sin(ea) * l2 } }
}
/** tapered capsule between two points (limbs) */
function capsulePath(ctx: C2D, a: V, b: V, ra: number, rb: number) {
const ang = Math.atan2(b.y - a.y, b.x - a.x)
const px = Math.cos(ang + Math.PI / 2)
const py = Math.sin(ang + Math.PI / 2)
ctx.beginPath()
ctx.moveTo(a.x + px * ra, a.y + py * ra)
ctx.lineTo(b.x + px * rb, b.y + py * rb)
ctx.arc(b.x, b.y, rb, ang + Math.PI / 2, ang - Math.PI / 2, true)
ctx.lineTo(a.x - px * ra, a.y - py * ra)
ctx.arc(a.x, a.y, ra, ang - Math.PI / 2, ang + Math.PI / 2, true)
ctx.closePath()
}
export function ink(ctx: C2D, fill: string, lw: number) {
ctx.fillStyle = fill
ctx.fill()
if (lw > 0) {
ctx.strokeStyle = C.line
ctx.lineWidth = lw
ctx.lineJoin = 'round'
ctx.stroke()
}
}
export function capsule(ctx: C2D, a: V, b: V, ra: number, rb: number, fill: string, lw: number) {
capsulePath(ctx, a, b, ra, rb)
ink(ctx, fill, lw)
}
export function circle(ctx: C2D, c: V, r: number, fill: string, lw: number) {
ctx.beginPath()
ctx.arc(c.x, c.y, r, 0, Math.PI * 2)
ink(ctx, fill, lw)
}
// ------------------------------------------------------------------ hair
/** Curly mop. local: centre (cx,cy), radius r, facing +1/-1, or front view. */
function curls(ctx: C2D, cx: number, cy: number, r: number, facing: number, wind: number, t: number, mode: 'side' | 'front' | 'back') {
const pts: { x: number; y: number; r: number }[] = []
const n = mode === 'back' ? 30 : 32
for (let i = 0; i < n; i++) {
let a: number
if (mode === 'front') a = Math.PI * 0.9 + (i / (n - 1)) * Math.PI * 1.2
else if (mode === 'back') a = (i / n) * Math.PI * 2
else a = -Math.PI / 2 - facing * (-0.85 + (i / (n - 1)) * 3.05) // forehead → crown → nape
if (mode === 'back' && Math.sin(a) > 0.82) continue
const k = 1.02 + 0.14 * hash(i * 3.7)
const wob = Math.sin(t * 9 + i * 1.7) * wind * 0.05
const blow = mode === 'side' ? -facing * wind * r * (0.2 + 0.4 * (1 - Math.abs(Math.sin(a)))) : mode === 'back' ? 0 : 0
const lift = mode === 'back' ? -wind * r * 0.18 * (1 - Math.sin(a)) : 0
pts.push({
x: cx + Math.cos(a) * r * k + blow + wob * r,
y: cy + Math.sin(a) * r * k * (mode === 'front' ? 0.95 : 1) + wob * r * 0.5 + lift,
r: r * (0.34 + 0.16 * hash(i * 1.3)),
})
}
const inner = 10
for (let i = 0; i < inner; i++) {
let a: number
if (mode === 'front') a = Math.PI * 1.05 + (i / (inner - 1)) * Math.PI * 0.9
else if (mode === 'back') a = (i / inner) * Math.PI * 2
else a = -Math.PI / 2 - facing * (-0.3 + (i / (inner - 1)) * 1.9)
pts.push({ x: cx + Math.cos(a) * r * 0.6 - (mode === 'side' ? facing * wind * r * 0.2 : 0), y: cy + Math.sin(a) * r * 0.6 - r * 0.05, r: r * (0.32 + 0.12 * hash(i * 7.9)) })
}
const crown = () => {
if (mode === 'front') {
ctx.moveTo(cx + r * 0.95, cy - r * 0.5)
ctx.ellipse(cx, cy - r * 0.5, r * 0.95, r * 0.5, 0, 0, Math.PI * 2)
} else if (mode === 'back') {
ctx.moveTo(cx + r, cy)
ctx.ellipse(cx, cy - r * 0.05, r * 1.0, r * 0.95, 0, 0, Math.PI * 2)
} else {
const ccx = cx - facing * r * (0.22 + wind * 0.12)
ctx.moveTo(ccx + r * 0.8, cy - r * 0.3)
ctx.ellipse(ccx, cy - r * 0.3, r * 0.8, r * 0.62, 0, 0, Math.PI * 2)
const bx = cx - facing * r * (0.45 + wind * 0.15)
ctx.moveTo(bx + r * 0.62, cy + r * 0.1)
ctx.ellipse(bx, cy + r * 0.1, r * 0.62, r * 0.8, 0, 0, Math.PI * 2)
}
}
// ink outline: same shapes, slightly larger, in line colour
const lw = Math.max(1, r * 0.07)
ctx.fillStyle = C.line
ctx.beginPath()
for (const p of pts) {
ctx.moveTo(p.x + p.r + lw, p.y)
ctx.arc(p.x, p.y, p.r + lw, 0, Math.PI * 2)
}
ctx.fill()
ctx.fillStyle = C.hair
ctx.beginPath()
for (const p of pts) {
ctx.moveTo(p.x + p.r, p.y)
ctx.arc(p.x, p.y, p.r, 0, Math.PI * 2)
}
crown()
ctx.fill()
// curl texture: highlight crescent on each curl (light from top-front)
ctx.strokeStyle = C.hairHi
ctx.lineWidth = Math.max(1, r * 0.075)
ctx.lineCap = 'round'
ctx.beginPath()
for (const p of pts) {
const rr = p.r * 0.6
const a0 = mode === 'side' ? (facing > 0 ? -2.3 : -2.6) : -2.6
ctx.moveTo(p.x + rr * Math.cos(a0), p.y + rr * Math.sin(a0))
ctx.arc(p.x, p.y, rr, a0, a0 + 2.0)
}
ctx.stroke()
}
// ------------------------------------------------------------------ heads
export interface HeadOpts {
wind?: number
t?: number
eyes?: 'closed' | 'open'
smile?: boolean
rim?: string
tilt?: number
scarf?: string
hair?: HairStyle
mood?: 'calm' | 'angry'
}
export type HairStyle = 'curly' | 'short' | 'long' | 'ponytail' | 'bun' | 'bald' | 'cap' | 'hat'
/** Side-view head. (cx,cy) = head centre, r = head radius. facing 1 = looking right. */
export function heroHead(ctx: C2D, cx: number, cy: number, r: number, facing: number, o: HeadOpts = {}) {
const t = o.t ?? 0
const wind = o.wind ?? 0
const lw = Math.max(1, r * 0.06)
ctx.save()
ctx.translate(cx, cy)
ctx.rotate((o.tilt ?? 0) * facing)
ctx.scale(facing, 1)
// face + profile (local facing +x)
ctx.beginPath()
ctx.moveTo(-r * 0.55, -r * 0.7)
ctx.quadraticCurveTo(r * 0.55, -r * 1.0, r * 0.8, -r * 0.25)
ctx.quadraticCurveTo(r * 0.86, -r * 0.08, r * 0.98, r * 0.12) // nose
ctx.quadraticCurveTo(r * 0.92, r * 0.26, r * 0.8, r * 0.28)
ctx.lineTo(r * 0.78, r * 0.5)
ctx.quadraticCurveTo(r * 0.72, r * 0.86, r * 0.32, r * 0.9) // chin / jaw
ctx.quadraticCurveTo(-r * 0.2, r * 0.9, -r * 0.5, r * 0.55)
ctx.quadraticCurveTo(-r * 0.85, r * 0.1, -r * 0.55, -r * 0.7)
ctx.closePath()
ink(ctx, C.skin, lw)
// jaw shade
ctx.fillStyle = rgba(C.skinShade, 0.45)
ctx.beginPath()
ctx.moveTo(-r * 0.5, r * 0.55)
ctx.quadraticCurveTo(-r * 0.1, r * 0.92, r * 0.32, r * 0.9)
ctx.quadraticCurveTo(r * 0.0, r * 0.7, -r * 0.3, r * 0.3)
ctx.closePath()
ctx.fill()
// ear
ctx.beginPath()
ctx.ellipse(-r * 0.12, r * 0.14, r * 0.15, r * 0.21, 0, 0, Math.PI * 2)
ink(ctx, C.skinShade, lw * 0.8)
// eye
ctx.strokeStyle = '#21160f'
ctx.fillStyle = '#21160f'
ctx.lineWidth = Math.max(1, r * 0.075)
ctx.lineCap = 'round'
const ex = r * 0.5
const ey = r * 0.02
const angry = o.mood === 'angry'
ctx.beginPath()
if (angry) {
// narrowed, glaring eye under a heavy lid
ctx.ellipse(ex + r * 0.02, ey + r * 0.01, r * 0.08, r * 0.05, 0, 0, Math.PI * 2)
ctx.fill()
ctx.lineWidth = Math.max(1, r * 0.06)
ctx.beginPath()
ctx.moveTo(ex - r * 0.12, ey - r * 0.08)
ctx.lineTo(ex + r * 0.14, ey - r * 0.02)
ctx.stroke()
} else if ((o.eyes ?? 'closed') === 'closed') {
ctx.arc(ex, ey - r * 0.06, r * 0.15, Math.PI * 0.18, Math.PI * 0.82)
ctx.stroke()
} else {
ctx.ellipse(ex, ey, r * 0.075, r * 0.11, 0, 0, Math.PI * 2)
ctx.fill()
ctx.fillStyle = '#ffffff'
ctx.beginPath()
ctx.arc(ex + r * 0.025, ey - r * 0.04, r * 0.028, 0, Math.PI * 2)
ctx.fill()
}
// thick brow (angry: pulled down hard toward the nose)
ctx.lineWidth = Math.max(1.5, r * (angry ? 0.13 : 0.1))
ctx.beginPath()
if (angry) {
ctx.moveTo(ex - r * 0.2, ey - r * 0.3)
ctx.lineTo(ex + r * 0.2, ey - r * 0.12)
} else {
ctx.moveTo(ex - r * 0.17, ey - r * 0.24)
ctx.lineTo(ex + r * 0.15, ey - r * 0.28)
}
ctx.stroke()
if (angry) {
// nose-bridge crease
ctx.lineWidth = Math.max(1, r * 0.04)
ctx.beginPath()
ctx.moveTo(r * 0.72, -r * 0.16)
ctx.lineTo(r * 0.8, -r * 0.06)
ctx.stroke()
}
// mouth
ctx.lineWidth = Math.max(1, r * 0.06)
ctx.beginPath()
if (angry) {
// gritted teeth, mouth pulled down
ctx.fillStyle = '#f4f0e8'
ctx.fillRect(r * 0.5, r * 0.5, r * 0.24, r * 0.09)
ctx.strokeRect(r * 0.5, r * 0.5, r * 0.24, r * 0.09)
ctx.beginPath()
ctx.moveTo(r * 0.5, r * 0.545)
ctx.lineTo(r * 0.74, r * 0.545)
ctx.moveTo(r * 0.47, r * 0.62)
ctx.lineTo(r * 0.52, r * 0.6)
ctx.stroke()
} else if (o.smile) ctx.arc(r * 0.6, r * 0.5, r * 0.11, 0.15, Math.PI * 0.75)
else {
ctx.moveTo(r * 0.55, r * 0.56)
ctx.lineTo(r * 0.7, r * 0.54)
}
ctx.stroke()
// blush
ctx.fillStyle = rgba(C.blush, 0.22)
ctx.beginPath()
ctx.ellipse(r * 0.38, r * 0.32, r * 0.14, r * 0.08, 0, 0, Math.PI * 2)
ctx.fill()
if (o.scarf) {
// headscarf over the hair, knot at the nape
ctx.beginPath()
ctx.moveTo(r * 0.55, -r * 0.55)
ctx.quadraticCurveTo(r * 0.1, -r * 1.25, -r * 0.75, -r * 0.55)
ctx.quadraticCurveTo(-r * 0.95, r * 0.1, -r * 0.55, r * 0.45)
ctx.lineTo(-r * 0.3, r * 0.1)
ctx.quadraticCurveTo(r * 0.05, -r * 0.6, r * 0.55, -r * 0.55)
ctx.closePath()
ink(ctx, o.scarf, lw)
ctx.beginPath()
ctx.moveTo(-r * 0.7, r * 0.25)
ctx.lineTo(-r * 1.05, r * 0.55)
ctx.lineTo(-r * 0.75, r * 0.6)
ctx.closePath()
ink(ctx, o.scarf, lw)
} else if (o.hair && o.hair !== 'curly') sideHair(ctx, r, o.hair, lw, t)
else curls(ctx, -r * 0.05, -r * 0.14, r, 1, wind, t, 'side')
if (o.rim) {
ctx.strokeStyle = o.rim
ctx.lineWidth = Math.max(1, r * 0.08)
ctx.beginPath()
ctx.arc(-r * 0.05, -r * 0.2, r * 1.2, Math.PI * 1.05, Math.PI * 1.5)
ctx.stroke()
}
ctx.restore()
}
// ------------------------------------------------------------------ body pieces (side view)
export interface Pose {
hip: V
sh: V
head: V
headR: number
kneeN: V
footN: V
kneeF: V
footF: V
elbowN: V
handN: V
elbowF: V
handF: V
}
export function torso(ctx: C2D, hip: V, sh: V, h: number, f: number, lw: number) {
// sweater body: a rounded slab between hip and shoulder
const ux = sh.x - hip.x
const uy = sh.y - hip.y
const L = Math.hypot(ux, uy) || 1
const nx = -uy / L
const ny = ux / L
// n points to the "front" when facing +x? make sure:
const s = nx * f > 0 ? 1 : -1
const fx = nx * s
const fy = ny * s
const wF = h * 0.11
const wB = h * 0.105
const p0 = { x: hip.x + fx * wF * 0.9 + (ux / L) * -h * 0.03, y: hip.y + fy * wF * 0.9 + (uy / L) * -h * 0.03 }
const p1 = { x: sh.x + fx * wF * 0.95, y: sh.y + fy * wF * 0.95 }
const p2 = { x: sh.x - fx * wB, y: sh.y - fy * wB }
const p3 = { x: hip.x - fx * wB + (ux / L) * -h * 0.03, y: hip.y - fy * wB + (uy / L) * -h * 0.03 }
ctx.beginPath()
ctx.moveTo(p0.x, p0.y)
ctx.quadraticCurveTo((p0.x + p1.x) / 2 + fx * h * 0.02, (p0.y + p1.y) / 2 + fy * h * 0.02, p1.x, p1.y)
ctx.quadraticCurveTo(sh.x + (ux / L) * h * 0.05, sh.y + (uy / L) * h * 0.05, p2.x, p2.y)
ctx.quadraticCurveTo((p2.x + p3.x) / 2 - fx * h * 0.025, (p2.y + p3.y) / 2 - fy * h * 0.025, p3.x, p3.y)
ctx.closePath()
ink(ctx, C.sweater, lw)
// back shading
ctx.fillStyle = rgba(C.sweaterShade, 0.9)
ctx.beginPath()
ctx.moveTo(p3.x, p3.y)
ctx.quadraticCurveTo((p2.x + p3.x) / 2 - fx * h * 0.025, (p2.y + p3.y) / 2 - fy * h * 0.025, p2.x, p2.y)
ctx.lineTo(sh.x - fx * wB * 0.35, sh.y - fy * wB * 0.35)
ctx.lineTo(hip.x - fx * wB * 0.3, hip.y - fy * wB * 0.3)
ctx.closePath()
ctx.fill()
// knit texture lines
ctx.strokeStyle = rgba(C.sweaterHi, 0.35)
ctx.lineWidth = Math.max(1, h * 0.004)
ctx.beginPath()
for (let k = 1; k < 4; k++) {
const q = k / 4
const a = lerpV(p0, p1, q)
const b = lerpV(p3, p2, q)
ctx.moveTo(a.x - fx * h * 0.02, a.y - fy * h * 0.02)
ctx.lineTo(lerpV(a, b, 0.55).x, lerpV(a, b, 0.55).y)
}
ctx.stroke()
// ribbed hem
capsule(ctx, { x: p3.x + (ux / L) * h * 0.02, y: p3.y + (uy / L) * h * 0.02 }, { x: p0.x + (ux / L) * h * 0.02, y: p0.y + (uy / L) * h * 0.02 }, h * 0.018, h * 0.018, C.rib, lw)
}
export function legPiece(ctx: C2D, hip: V, knee: V, foot: V, h: number, f: number, near: boolean, lw: number) {
const col = near ? C.pants : C.pantsShade
capsule(ctx, hip, knee, h * 0.052, h * 0.045, col, lw)
capsule(ctx, knee, foot, h * 0.045, h * 0.037, col, lw)
// knee crease
ctx.strokeStyle = rgba(C.line, 0.35)
ctx.lineWidth = Math.max(1, h * 0.004)
ctx.beginPath()
ctx.moveTo(knee.x - h * 0.02 * f, knee.y - h * 0.01)
ctx.lineTo(knee.x + h * 0.01 * f, knee.y + h * 0.015)
ctx.stroke()
shoe(ctx, foot, h, f, lw)
}
export function shoe(ctx: C2D, foot: V, h: number, f: number, lw: number) {
ctx.beginPath()
ctx.moveTo(foot.x - h * 0.035 * f, foot.y - h * 0.02)
ctx.quadraticCurveTo(foot.x + h * 0.02 * f, foot.y - h * 0.045, foot.x + h * 0.075 * f, foot.y - h * 0.008)
ctx.lineTo(foot.x + h * 0.08 * f, foot.y + h * 0.012)
ctx.lineTo(foot.x - h * 0.04 * f, foot.y + h * 0.012)
ctx.closePath()
ink(ctx, C.shoe, lw)
ctx.fillStyle = C.sole
ctx.fillRect(Math.min(foot.x - h * 0.04 * f, foot.x + h * 0.08 * f), foot.y + h * 0.003, h * 0.12, h * 0.012)
}
export function armPiece(ctx: C2D, sh: V, elbow: V, hand: V, h: number, near: boolean, lw: number) {
const col = near ? C.sweater : C.sweaterShade
capsule(ctx, sh, elbow, h * 0.04, h * 0.035, col, lw)
capsule(ctx, elbow, hand, h * 0.035, h * 0.031, col, lw)
// ribbed cuff
const c = lerpV(elbow, hand, 0.86)
capsule(ctx, lerpV(elbow, hand, 0.78), c, h * 0.033, h * 0.033, C.rib, lw)
circle(ctx, hand, h * 0.03, C.skin, lw)
}
function drawFar(ctx: C2D, p: Pose, h: number, f: number, lw: number) {
legPiece(ctx, p.hip, p.kneeF, p.footF, h, f, false, lw)
armPiece(ctx, p.sh, p.elbowF, p.handF, h, false, lw)
}
function drawNear(ctx: C2D, p: Pose, h: number, f: number, lw: number, o: HeadOpts) {
// neck
capsule(ctx, p.sh, { x: p.head.x - f * p.headR * 0.1, y: p.head.y + p.headR * 0.6 }, h * 0.034, h * 0.032, C.skinShade, lw)
torso(ctx, p.hip, p.sh, h, f, lw)
legPiece(ctx, p.hip, p.kneeN, p.footN, h, f, true, lw)
heroHead(ctx, p.head.x, p.head.y, p.headR, f, o)
armPiece(ctx, p.sh, p.elbowN, p.handN, h, true, lw)
}
const LEG1 = 0.21
const LEG2 = 0.205
const ARM1 = 0.15
const ARM2 = 0.14
const TORSO = 0.25
function headFor(sh: V, h: number, f: number, lean: number): { head: V; r: number } {
const r = h * 0.14
return { head: { x: sh.x + f * (h * 0.03 + Math.sin(lean) * h * 0.05), y: sh.y - h * 0.035 - r * 0.95 }, r }
}
// ------------------------------------------------------------------ walking / standing
export interface HeroWalkOpts {
facing?: number
run?: number
rim?: string
t?: number
eyes?: 'closed' | 'open'
scarf?: string
/** world-space hand targets (e.g. bike grips) */
gripN?: V
gripF?: V
stand?: boolean
}
export function walkPose(x: number, y: number, h: number, phase: number, o: HeroWalkOpts): Pose {
const f = o.facing ?? 1
const run = o.run ?? 0
const P = Math.PI * 2
const bob = Math.abs(Math.cos(phase * P)) * h * (0.012 + run * 0.02)
const hip = { x, y: y - h * 0.4 + bob * 0.6 - h * 0.012 }
const lean = 0.04 + run * 0.25
const sh = { x: hip.x + f * Math.sin(lean) * h * TORSO, y: hip.y - Math.cos(lean) * h * TORSO }
const stride = h * (0.13 + run * 0.08)
const foot = (ph: number): V => {
const s = Math.sin(ph * P)
const lift = Math.max(0, Math.cos(ph * P)) * h * (0.045 + run * 0.06)
return { x: x + f * s * stride, y: y - lift }
}
const fN = o.stand ? { x: x + f * h * 0.05, y } : foot(phase)
const fF = o.stand ? { x: x - f * h * 0.04, y } : foot(phase + 0.5)
const legN = ik(hip, fN, h * LEG1, h * LEG2, -f)
const legF = ik(hip, fF, h * LEG1, h * LEG2, -f)
const handSwing = (ph: number): V => {
const s = Math.sin(ph * P)
return { x: sh.x + f * s * h * (0.1 + run * 0.08), y: sh.y + h * (0.25 - run * 0.08) }
}
const tN = o.gripN ?? handSwing(phase + 0.5)
const tF = o.gripF ?? handSwing(phase)
const armN = ik(sh, tN, h * ARM1, h * ARM2, f)
const armF = ik(sh, tF, h * ARM1, h * ARM2, f)
const hd = headFor(sh, h, f, lean)
return {
hip,
sh,
head: hd.head,
headR: hd.r,
kneeN: legN.j,
footN: legN.e,
kneeF: legF.j,
footF: legF.e,
elbowN: armN.j,
handN: armN.e,
elbowF: armF.j,
handF: armF.e,
}
}
/** Side-view walking hero. (x,y) = feet on ground, h = total height. */
export function heroWalk(ctx: C2D, x: number, y: number, h: number, phase: number, o: HeroWalkOpts = {}): Pose {
const f = o.facing ?? 1
const lw = Math.max(1, h * 0.007)
const p = walkPose(x, y, h, phase, o)
contactShadow(ctx, x, y, h * 0.2)
drawFar(ctx, p, h, f, lw)
drawNear(ctx, p, h, f, lw, { t: o.t, wind: (o.run ?? 0) * 0.6 + 0.1, eyes: o.eyes ?? 'closed', rim: o.rim, scarf: o.scarf })
return p
}
function contactShadow(ctx: C2D, x: number, y: number, w: number) {
ctx.fillStyle = 'rgba(0,0,0,0.45)'
ctx.beginPath()
ctx.ellipse(x, y + w * 0.03, w, w * 0.12, 0, 0, Math.PI * 2)
ctx.fill()
}
// ------------------------------------------------------------------ the motorcycle
export interface BikeOpts {
facing?: number
light?: number
spin?: number
t?: number
beam?: boolean
/** show the side stand (parked) */
parked?: boolean
}
/** anchor points of the bike, local units (wheelbase = 1, ground y = 0, facing +x) */
const ANCHOR = {
seat: { x: -0.2, y: -0.6 },
grip: { x: 0.27, y: -0.84 },
peg: { x: -0.1, y: -0.3 },
R: 0.23,
}
export function bikeAnchors(x: number, y: number, s: number, facing = 1) {
const m = (p: V): V => ({ x: x + p.x * s * facing, y: y + p.y * s })
return { seat: m(ANCHOR.seat), grip: m(ANCHOR.grip), peg: m(ANCHOR.peg) }
}
export function bike(ctx: C2D, x: number, y: number, s: number, o: BikeOpts = {}) {
ctx.save()
ctx.translate(x, y)
ctx.scale(o.facing ?? 1, 1)
bikeLocal(ctx, s, o)
ctx.restore()
}
function wheel(ctx: C2D, cx: number, cy: number, R: number, s: number, spin: number, front: boolean, lw: number) {
// tyre
ctx.beginPath()
ctx.arc(cx, cy, R, 0, Math.PI * 2)
ink(ctx, C.tire, lw)
ctx.strokeStyle = 'rgba(140,150,165,0.35)'
ctx.lineWidth = Math.max(1, s * 0.008)
ctx.beginPath()
ctx.arc(cx, cy, R * 0.92, Math.PI * 1.05, Math.PI * 1.55)
ctx.stroke()
// rim
ctx.strokeStyle = C.chrome
ctx.lineWidth = Math.max(1, s * 0.022)
ctx.beginPath()
ctx.arc(cx, cy, R * 0.7, 0, Math.PI * 2)
ctx.stroke()
ctx.fillStyle = '#121418'
ctx.beginPath()
ctx.arc(cx, cy, R * 0.66, 0, Math.PI * 2)
ctx.fill()
// spokes
ctx.strokeStyle = rgba(C.chrome, 0.85)
ctx.lineWidth = Math.max(1, s * 0.005)
ctx.beginPath()
for (let k = 0; k < 16; k++) {
const a = (k / 16) * Math.PI * 2 + spin
const b = a + (k % 2 ? 0.35 : -0.35)
ctx.moveTo(cx + Math.cos(a) * R * 0.1, cy + Math.sin(a) * R * 0.1)
ctx.lineTo(cx + Math.cos(b) * R * 0.66, cy + Math.sin(b) * R * 0.66)
}
ctx.stroke()
if (front) {
// brake disc
ctx.strokeStyle = rgba(C.chromeShade, 0.9)
ctx.lineWidth = Math.max(1, s * 0.03)
ctx.beginPath()
ctx.arc(cx, cy, R * 0.33, 0, Math.PI * 2)
ctx.stroke()
}
circle(ctx, { x: cx, y: cy }, R * 0.13, C.chrome, lw)
circle(ctx, { x: cx, y: cy }, R * 0.05, C.chromeShade, 0)
}
/** bike drawn at origin facing +x (ground at y = 0) */
function bikeLocal(ctx: C2D, s: number, o: BikeOpts) {
const R = s * ANCHOR.R
const rx = -s * 0.5
const fx = s * 0.5
const wy = -R
const light = o.light ?? 1
const lw = Math.max(1, s * 0.008)
const X = (dx: number) => dx * s
const Y = (dy: number) => -dy * s
const spin = o.spin ?? 0
ctx.lineCap = 'round'
ctx.lineJoin = 'round'
// ground shadow
ctx.fillStyle = 'rgba(0,0,0,0.5)'
ctx.beginPath()
ctx.ellipse(0, s * 0.01, s * 0.78, s * 0.05, 0, 0, Math.PI * 2)
ctx.fill()
if (o.beam && light > 0) lightCone(ctx, X(0.55), Y(0.72), s * 3.2, 0.17, -Math.PI / 2, '#fff1c8', 0.16 * light)
// side stand
if (o.parked) capsule(ctx, { x: X(-0.06), y: Y(0.28) }, { x: X(-0.16), y: Y(0.005) }, s * 0.012, s * 0.01, C.bikeShade, lw)
wheel(ctx, rx, wy, R, s, spin, false, lw)
wheel(ctx, fx, wy, R, s, spin, true, lw)
// swing arm
capsule(ctx, { x: X(-0.1), y: Y(0.3) }, { x: rx, y: wy }, s * 0.03, s * 0.022, C.bike, lw)
// chain
ctx.strokeStyle = '#3a3f48'
ctx.lineWidth = Math.max(1, s * 0.008)
ctx.beginPath()
ctx.moveTo(X(-0.06), Y(0.33))
ctx.lineTo(rx, wy - R * 0.32)
ctx.moveTo(X(-0.06), Y(0.27))
ctx.lineTo(rx, wy + R * 0.32)
ctx.stroke()
circle(ctx, { x: rx, y: wy }, R * 0.32, 'rgba(0,0,0,0)', lw)
// rear shock (spring)
{
const a = { x: X(-0.36), y: Y(0.58) }
const b = { x: rx + R * 0.15, y: wy - R * 0.1 }
capsule(ctx, a, b, s * 0.016, s * 0.016, C.chromeShade, lw)
ctx.strokeStyle = '#d8b04a'
ctx.lineWidth = Math.max(1, s * 0.009)
ctx.beginPath()
for (let k = 0; k <= 9; k++) {
const p = lerpV(a, b, 0.12 + (k / 9) * 0.7)
const dx = (b.y - a.y) / Math.hypot(b.x - a.x, b.y - a.y)
const dy = -(b.x - a.x) / Math.hypot(b.x - a.x, b.y - a.y)
const side = k % 2 ? 1 : -1
const q = { x: p.x + dx * s * 0.026 * side, y: p.y + dy * s * 0.026 * side }
if (k === 0) ctx.moveTo(q.x, q.y)
else ctx.lineTo(q.x, q.y)
}
ctx.stroke()
}
// frame tubes
capsule(ctx, { x: X(0.36), y: Y(0.78) }, { x: X(0.12), y: Y(0.24) }, s * 0.024, s * 0.024, C.bike, lw)
capsule(ctx, { x: X(0.36), y: Y(0.78) }, { x: X(-0.44), y: Y(0.6) }, s * 0.022, s * 0.022, C.bike, lw)
// engine: crankcase + finned cylinder
ctx.beginPath()
ctx.moveTo(X(-0.16), Y(0.44))
ctx.lineTo(X(0.12), Y(0.46))
ctx.quadraticCurveTo(X(0.22), Y(0.4), X(0.2), Y(0.24))
ctx.quadraticCurveTo(X(0.05), Y(0.15), X(-0.12), Y(0.2))
ctx.quadraticCurveTo(X(-0.2), Y(0.3), X(-0.16), Y(0.44))
ctx.closePath()
ink(ctx, '#1b1e24', lw)
ctx.save()
ctx.translate(X(0.1), Y(0.52))
ctx.rotate(0.3)
ctx.beginPath()
ctx.rect(-s * 0.075, -s * 0.11, s * 0.15, s * 0.17)
ink(ctx, '#2a2f37', lw)
ctx.strokeStyle = C.chrome
ctx.lineWidth = Math.max(1, s * 0.009)
ctx.beginPath()
for (let k = 0; k < 6; k++) {
const yy = -s * 0.095 + k * s * 0.028
ctx.moveTo(-s * 0.085, yy)
ctx.lineTo(s * 0.085, yy)
}
ctx.stroke()
ctx.restore()
circle(ctx, { x: X(0.0), y: Y(0.3) }, s * 0.075, C.chromeShade, lw)
circle(ctx, { x: X(0.0), y: Y(0.3) }, s * 0.05, C.chrome, 0)
// exhaust: header + muffler
ctx.strokeStyle = C.line
ctx.lineWidth = s * 0.05
ctx.beginPath()
ctx.moveTo(X(0.2), Y(0.5))
ctx.quadraticCurveTo(X(0.28), Y(0.22), X(0.05), Y(0.16))
ctx.lineTo(X(-0.25), Y(0.2))
ctx.stroke()
ctx.strokeStyle = C.chrome
ctx.lineWidth = s * 0.034
ctx.stroke()
capsule(ctx, { x: X(-0.22), y: Y(0.2) }, { x: X(-0.64), y: Y(0.3) }, s * 0.04, s * 0.045, C.chrome, lw)
ctx.strokeStyle = rgba('#ffffff', 0.55)
ctx.lineWidth = Math.max(1, s * 0.008)
ctx.beginPath()
ctx.moveTo(X(-0.26), Y(0.225))
ctx.lineTo(X(-0.6), Y(0.32))
ctx.stroke()
circle(ctx, { x: X(-0.64), y: Y(0.3) }, s * 0.03, '#2a2f37', lw)
// side cover under the seat
ctx.beginPath()
ctx.moveTo(X(-0.38), Y(0.58))
ctx.lineTo(X(-0.12), Y(0.6))
ctx.lineTo(X(-0.14), Y(0.42))
ctx.lineTo(X(-0.34), Y(0.44))
ctx.closePath()
ink(ctx, C.bike, lw)
circle(ctx, { x: X(-0.25), y: Y(0.51) }, s * 0.025, C.chromeShade, lw)
// rear fender + tail light + plate
ctx.beginPath()
ctx.moveTo(X(-0.4), Y(0.6))
ctx.quadraticCurveTo(X(-0.62), Y(0.62), X(-0.72), Y(0.48))
ctx.lineTo(X(-0.68), Y(0.46))
ctx.quadraticCurveTo(X(-0.6), Y(0.56), X(-0.4), Y(0.55))
ctx.closePath()
ink(ctx, C.bike, lw)
ctx.beginPath()
ctx.rect(X(-0.72), Y(0.46), s * 0.06, s * 0.08)
ink(ctx, '#e8e2d6', lw)
ctx.beginPath()
ctx.ellipse(X(-0.69), Y(0.565), s * 0.035, s * 0.022, 0, 0, Math.PI * 2)
ink(ctx, '#ff3a5c', lw)
glowBlob(ctx, X(-0.7), Y(0.565), s * 0.13, '#ff2d55', 0.85 * light)
// seat
ctx.beginPath()
ctx.moveTo(X(-0.46), Y(0.6))
ctx.quadraticCurveTo(X(-0.44), Y(0.67), X(-0.36), Y(0.66))
ctx.lineTo(X(-0.04), Y(0.64))
ctx.quadraticCurveTo(X(0.02), Y(0.62), X(0.0), Y(0.58))
ctx.lineTo(X(-0.44), Y(0.57))
ctx.closePath()
ink(ctx, C.seat, lw)
ctx.strokeStyle = 'rgba(200,180,150,0.25)'
ctx.lineWidth = Math.max(1, s * 0.004)
ctx.setLineDash([s * 0.012, s * 0.01])
ctx.beginPath()
ctx.moveTo(X(-0.4), Y(0.62))
ctx.lineTo(X(-0.04), Y(0.605))
ctx.stroke()
ctx.setLineDash([])
// fuel tank — glossy teardrop with chrome badge and knee pad
ctx.beginPath()
ctx.moveTo(X(-0.04), Y(0.6))
ctx.quadraticCurveTo(X(0.02), Y(0.77), X(0.2), Y(0.77))
ctx.quadraticCurveTo(X(0.36), Y(0.76), X(0.38), Y(0.66))
ctx.quadraticCurveTo(X(0.36), Y(0.58), X(0.22), Y(0.56))
ctx.quadraticCurveTo(X(0.06), Y(0.54), X(-0.04), Y(0.6))
ctx.closePath()
ink(ctx, C.bike, lw)
ctx.strokeStyle = rgba(C.bikeHi, 0.9)
ctx.lineWidth = Math.max(1, s * 0.014)
ctx.beginPath()
ctx.moveTo(X(0.04), Y(0.72))
ctx.quadraticCurveTo(X(0.18), Y(0.765), X(0.3), Y(0.73))
ctx.stroke()
ctx.beginPath()
ctx.ellipse(X(0.2), Y(0.66), s * 0.045, s * 0.03, 0, 0, Math.PI * 2)
ink(ctx, C.chrome, lw)
ctx.fillStyle = '#c4142f'
ctx.beginPath()
ctx.ellipse(X(0.2), Y(0.66), s * 0.026, s * 0.015, 0, 0, Math.PI * 2)
ctx.fill()
ctx.beginPath()
ctx.ellipse(X(0.04), Y(0.6), s * 0.05, s * 0.03, -0.2, 0, Math.PI * 2)
ink(ctx, '#202228', lw)
// front fork + fender
capsule(ctx, { x: X(0.37), y: Y(0.8) }, { x: fx, y: wy }, s * 0.022, s * 0.02, C.chrome, lw)
capsule(ctx, { x: X(0.44), y: Y(0.4) }, { x: X(0.49), y: Y(0.27) }, s * 0.03, s * 0.028, '#2a2e36', lw)
ctx.strokeStyle = C.line
ctx.lineWidth = s * 0.05
ctx.beginPath()
ctx.arc(fx, wy, R * 1.12, Math.PI * 1.08, Math.PI * 1.72)
ctx.stroke()
ctx.strokeStyle = C.bike
ctx.lineWidth = s * 0.034
ctx.stroke()
// handlebar + grips + lever
capsule(ctx, { x: X(0.37), y: Y(0.82) }, { x: X(0.31), y: Y(0.86) }, s * 0.011, s * 0.011, C.chrome, lw)
capsule(ctx, { x: X(0.31), y: Y(0.86) }, { x: X(0.24), y: Y(0.84) }, s * 0.018, s * 0.018, '#0e0f12', lw)
ctx.strokeStyle = C.chromeShade
ctx.lineWidth = Math.max(1, s * 0.008)
ctx.beginPath()
ctx.moveTo(X(0.31), Y(0.86))
ctx.lineTo(X(0.22), Y(0.81))
ctx.stroke()
// speedo
circle(ctx, { x: X(0.38), y: Y(0.86) }, s * 0.03, '#1a1c22', lw)
circle(ctx, { x: X(0.38), y: Y(0.86) }, s * 0.018, '#d9e2c9', 0)
// mirror
ctx.strokeStyle = C.chromeShade
ctx.lineWidth = Math.max(1, s * 0.01)
ctx.beginPath()
ctx.moveTo(X(0.32), Y(0.86))
ctx.quadraticCurveTo(X(0.29), Y(0.95), X(0.31), Y(1.0))
ctx.stroke()
ctx.beginPath()
ctx.ellipse(X(0.31), Y(1.03), s * 0.04, s * 0.032, 0, 0, Math.PI * 2)
ink(ctx, C.chrome, lw)
ctx.fillStyle = '#6d8296'
ctx.beginPath()
ctx.ellipse(X(0.31), Y(1.03), s * 0.026, s * 0.02, 0, 0, Math.PI * 2)
ctx.fill()
// headlight: chrome bucket, glass, amber indicator
ctx.beginPath()
ctx.ellipse(X(0.46), Y(0.72), s * 0.07, s * 0.08, 0, 0, Math.PI * 2)
ink(ctx, C.chrome, lw)
ctx.fillStyle = light > 0.1 ? '#fff6d8' : '#5a626c'
ctx.beginPath()
ctx.ellipse(X(0.5), Y(0.72), s * 0.035, s * 0.065, 0, 0, Math.PI * 2)
ctx.fill()
ctx.beginPath()
ctx.ellipse(X(0.4), Y(0.79), s * 0.02, s * 0.014, 0, 0, Math.PI * 2)
ink(ctx, '#ffa63a', lw)
if (light > 0.1) glowBlob(ctx, X(0.52), Y(0.72), s * 0.26, '#fff1c8', 0.9 * light)
// foot peg
capsule(ctx, { x: X(-0.13), y: Y(0.3) }, { x: X(-0.06), y: Y(0.3) }, s * 0.012, s * 0.012, C.chromeShade, lw)
}
// ------------------------------------------------------------------ pushing the bike
/** Hero walking beside (in front of) his bike, both hands on the bars. (x,y) = bike ground point. */
export function heroPush(ctx: C2D, x: number, y: number, s: number, phase: number, o: { facing?: number; t?: number; rim?: string; spin?: number; light?: number; beam?: boolean; eyes?: 'closed' | 'open' } = {}) {
const f = o.facing ?? 1
const h = HERO_H * s
const A = bikeAnchors(x, y, s, f)
const lw = Math.max(1, h * 0.007)
const px = x - f * s * 0.1
const p = walkPose(px, y, h, phase, { facing: f, gripN: A.grip, gripF: { x: A.grip.x - f * s * 0.02, y: A.grip.y - s * 0.01 } })
// far arm (to the far grip) is behind the bike
armPiece(ctx, p.sh, p.elbowF, p.handF, h, false, lw)
bike(ctx, x, y, s, { facing: f, spin: o.spin, light: o.light, beam: o.beam })
// body in front of the bike
contactShadow(ctx, px, y, h * 0.18)
legPiece(ctx, p.hip, p.kneeF, p.footF, h, f, false, lw)
drawNear(ctx, p, h, f, lw, { t: o.t, wind: 0.1, eyes: o.eyes ?? 'closed', rim: o.rim })
}
// ------------------------------------------------------------------ riding (side)
export interface RideOpts {
facing?: number
wind?: number
t?: number
spin?: number
light?: number
beam?: boolean
smile?: boolean
eyes?: 'closed' | 'open'
rim?: string
/** 0 = sitting upright (parked), 1 = tucked in (racing) */
tuck?: number
}
/** Hero riding his bike, side view. (x,y) = ground under bike centre, s = bike scale. */
export function ridePose(x: number, y: number, s: number, o: RideOpts = {}): Pose {
const f = o.facing ?? 1
const t = o.t ?? 0
const h = HERO_H * s
const A = bikeAnchors(x, y, s, f)
const bounce = Math.sin(t * 13) * s * 0.004 * (o.tuck ?? 0.5)
const hip = { x: A.seat.x, y: A.seat.y - h * 0.035 + bounce }
// choose torso lean so the hands reach the grips with a relaxed elbow
const reach = (ARM1 + ARM2) * h * (0.9 - (o.tuck ?? 0.5) * 0.1)
let best = 0.3
let err = 1e9
for (let l = 0.05; l <= 1.2; l += 0.02) {
const sh = { x: hip.x + f * Math.sin(l) * h * TORSO, y: hip.y - Math.cos(l) * h * TORSO }
const e = Math.abs(Math.hypot(A.grip.x - sh.x, A.grip.y - sh.y) - reach)
if (e < err) {
err = e
best = l
}
}
const lean = best
const sh = { x: hip.x + f * Math.sin(lean) * h * TORSO, y: hip.y - Math.cos(lean) * h * TORSO }
const legN = ik(hip, A.peg, h * LEG1, h * LEG2, -f)
const pegF = { x: A.peg.x - f * s * 0.02, y: A.peg.y - s * 0.01 }
const legF = ik(hip, pegF, h * LEG1, h * LEG2, -f)
const gripF = { x: A.grip.x - f * s * 0.02, y: A.grip.y - s * 0.012 }
const armN = ik(sh, A.grip, h * ARM1, h * ARM2, f)
const armF = ik(sh, gripF, h * ARM1, h * ARM2, f)
const hd = headFor(sh, h, f, lean)
const p: Pose = {
hip,
sh,
head: hd.head,
headR: hd.r,
kneeN: legN.j,
footN: legN.e,
kneeF: legF.j,
footF: legF.e,
elbowN: armN.j,
handN: armN.e,
elbowF: armF.j,
handF: armF.e,
}
return p
}
export function heroRide(ctx: C2D, x: number, y: number, s: number, o: RideOpts = {}) {
const f = o.facing ?? 1
const t = o.t ?? 0
const h = HERO_H * s
const lw = Math.max(1, h * 0.007)
const p = ridePose(x, y, s, o)
const hip = p.hip
// far limbs behind the bike
drawFar(ctx, p, h, f, lw)
bike(ctx, x, y, s, { facing: f, light: o.light, spin: o.spin, beam: o.beam })
// seat contact: the trousers sit on top of the seat
capsule(ctx, { x: hip.x - f * h * 0.05, y: hip.y + h * 0.01 }, { x: hip.x + f * h * 0.03, y: hip.y + h * 0.015 }, h * 0.05, h * 0.05, C.pants, lw)
drawNear(ctx, p, h, f, lw, { t, wind: o.wind ?? 0.6, smile: o.smile, eyes: o.eyes ?? 'open', rim: o.rim })
}
// ------------------------------------------------------------------ back view (standing)
export function heroBack(ctx: C2D, x: number, y: number, h: number, sway = 0, o: { rim?: string; t?: number; wind?: number } = {}) {
const lw = Math.max(1, h * 0.007)
const sw = sway * h * 0.02
const t = o.t ?? 0
contactShadow(ctx, x, y, h * 0.2)
// legs
capsule(ctx, { x: x - h * 0.05, y: y - h * 0.4 }, { x: x - h * 0.055, y: y - h * 0.03 }, h * 0.048, h * 0.038, C.pantsShade, lw)
capsule(ctx, { x: x + h * 0.05, y: y - h * 0.4 }, { x: x + h * 0.055, y: y - h * 0.03 }, h * 0.048, h * 0.038, C.pants, lw)
for (const sd of [-1, 1]) {
ctx.beginPath()
ctx.ellipse(x + sd * h * 0.055, y - h * 0.015, h * 0.045, h * 0.025, 0, 0, Math.PI * 2)
ink(ctx, C.shoe, lw)
}
// arms
for (const sd of [-1, 1]) {
capsule(ctx, { x: x + sd * h * 0.13 + sw, y: y - h * 0.63 }, { x: x + sd * h * 0.155, y: y - h * 0.4 }, h * 0.038, h * 0.032, C.sweaterShade, lw)
circle(ctx, { x: x + sd * h * 0.157, y: y - h * 0.375 }, h * 0.028, C.skin, lw)
}
// sweater
ctx.beginPath()
ctx.moveTo(x - h * 0.14 + sw, y - h * 0.67)
ctx.quadraticCurveTo(x - h * 0.16, y - h * 0.52, x - h * 0.12, y - h * 0.37)
ctx.lineTo(x + h * 0.12, y - h * 0.37)
ctx.quadraticCurveTo(x + h * 0.16, y - h * 0.52, x + h * 0.14 + sw, y - h * 0.67)
ctx.quadraticCurveTo(x + sw, y - h * 0.71, x - h * 0.14 + sw, y - h * 0.67)
ctx.closePath()
ink(ctx, C.sweater, lw)
ctx.fillStyle = rgba(C.sweaterShade, 0.8)
ctx.fillRect(x - h * 0.005, y - h * 0.66, h * 0.01, h * 0.28)
capsule(ctx, { x: x - h * 0.12, y: y - h * 0.385 }, { x: x + h * 0.12, y: y - h * 0.385 }, h * 0.018, h * 0.018, C.rib, lw)
// neck + head from behind
const r = h * 0.14
const hx = x + sw * 1.2
const hy = y - h * 0.7 - r * 0.9
capsule(ctx, { x: hx, y: y - h * 0.67 }, { x: hx, y: hy + r * 0.5 }, h * 0.026, h * 0.026, C.skinShade, lw)
for (const sd of [-1, 1]) {
ctx.beginPath()
ctx.ellipse(hx + sd * r * 0.92, hy + r * 0.15, r * 0.14, r * 0.2, 0, 0, Math.PI * 2)
ink(ctx, C.skinShade, lw)
}
curls(ctx, hx, hy, r, 1, o.wind ?? 0, t, 'back')
if (o.rim) {
ctx.strokeStyle = o.rim
ctx.lineWidth = Math.max(1, h * 0.008)
ctx.beginPath()
ctx.arc(hx, hy - r * 0.1, r * 1.22, Math.PI * 1.1, Math.PI * 1.9)
ctx.stroke()
}
}
// ------------------------------------------------------------------ riding (rear view)
/** Rear view of hero on the bike. (x,y) = ground under rear tyre, s = overall scale (≈ height of rider+bike). */
export function heroRideRear(ctx: C2D, x: number, y: number, s: number, lean: number, t: number, wind = 0.8) {
const lw = Math.max(1, s * 0.006)
ctx.save()
ctx.translate(x, y)
ctx.rotate(lean)
ctx.lineCap = 'round'
ctx.lineJoin = 'round'
// --- far: handlebar, mirrors, headlight glow ahead
glowBlob(ctx, 0, -s * 0.5, s * 0.5, '#fff1c8', 0.12)
capsule(ctx, { x: -s * 0.3, y: -s * 0.6 }, { x: s * 0.3, y: -s * 0.6 }, s * 0.01, s * 0.01, C.chrome, lw)
for (const sd of [-1, 1]) {
ctx.strokeStyle = C.chromeShade
ctx.lineWidth = Math.max(1, s * 0.008)
ctx.beginPath()
ctx.moveTo(sd * s * 0.25, -s * 0.6)
ctx.lineTo(sd * s * 0.3, -s * 0.71)
ctx.stroke()
ctx.beginPath()
ctx.ellipse(sd * s * 0.305, -s * 0.735, s * 0.034, s * 0.027, 0, 0, Math.PI * 2)
ink(ctx, C.chrome, lw)
ctx.fillStyle = '#6d8296'
ctx.beginPath()
ctx.ellipse(sd * s * 0.305, -s * 0.735, s * 0.022, s * 0.017, 0, 0, Math.PI * 2)
ctx.fill()
}
// tank glimpsed between the knees
ctx.beginPath()
ctx.ellipse(0, -s * 0.47, s * 0.09, s * 0.06, 0, 0, Math.PI * 2)
ink(ctx, C.bike, lw)
// --- rider: arms reach forward to the grips
const h = s * 0.95
for (const sd of [-1, 1]) {
const shp = { x: sd * h * 0.13, y: -s * 0.78 }
const grip = { x: sd * s * 0.27, y: -s * 0.6 }
const a = ik(shp, grip, h * 0.15, h * 0.14, sd > 0 ? -1 : 1)
capsule(ctx, shp, a.j, h * 0.04, h * 0.035, C.sweaterShade, lw)
capsule(ctx, a.j, a.e, h * 0.035, h * 0.031, C.sweaterShade, lw)
circle(ctx, a.e, h * 0.03, C.skinShade, lw)
}
// back
ctx.beginPath()
ctx.moveTo(-h * 0.16, -s * 0.47)
ctx.quadraticCurveTo(-h * 0.19, -s * 0.66, -h * 0.13, -s * 0.8)
ctx.quadraticCurveTo(0, -s * 0.84, h * 0.13, -s * 0.8)
ctx.quadraticCurveTo(h * 0.19, -s * 0.66, h * 0.16, -s * 0.47)
ctx.closePath()
ink(ctx, C.sweater, lw)
ctx.fillStyle = rgba(C.sweaterShade, 0.85)
ctx.fillRect(-h * 0.006, -s * 0.79, h * 0.012, s * 0.3)
capsule(ctx, { x: -h * 0.155, y: -s * 0.475 }, { x: h * 0.155, y: -s * 0.475 }, h * 0.018, h * 0.018, C.rib, lw)
// neck + hair
const r = h * 0.14
capsule(ctx, { x: 0, y: -s * 0.8 }, { x: 0, y: -s * 0.86 }, h * 0.026, h * 0.026, C.skinShade, lw)
curls(ctx, 0, -s * 0.84 - r * 0.85, r, 1, wind, t, 'back')
// legs out to the pegs (both sides)
for (const sd of [-1, 1]) {
const hipp = { x: sd * h * 0.08, y: -s * 0.44 }
const peg = { x: sd * s * 0.17, y: -s * 0.2 }
const k = ik(hipp, peg, h * 0.2, h * 0.19, sd > 0 ? -1 : 1)
capsule(ctx, hipp, k.j, h * 0.05, h * 0.044, sd > 0 ? C.pants : C.pantsShade, lw)
capsule(ctx, k.j, k.e, h * 0.044, h * 0.036, sd > 0 ? C.pants : C.pantsShade, lw)
ctx.beginPath()
ctx.ellipse(k.e.x + sd * h * 0.01, k.e.y + h * 0.01, h * 0.04, h * 0.03, 0, 0, Math.PI * 2)
ink(ctx, C.shoe, lw)
}
// --- near: seat edge, twin shocks, mufflers, rear tyre, fender, tail light, plate
ctx.beginPath()
ctx.ellipse(0, -s * 0.43, h * 0.13, s * 0.032, 0, 0, Math.PI * 2)
ink(ctx, C.seat, lw)
for (const sd of [-1, 1]) {
// rear shock absorbers with yellow springs
const a = { x: sd * s * 0.11, y: -s * 0.42 }
const b = { x: sd * s * 0.1, y: -s * 0.17 }
capsule(ctx, a, b, s * 0.014, s * 0.014, C.chromeShade, lw)
ctx.strokeStyle = '#d8b04a'
ctx.lineWidth = Math.max(1, s * 0.008)
ctx.beginPath()
for (let k = 0; k <= 8; k++) {
const p = lerpV(a, b, 0.1 + (k / 8) * 0.75)
ctx[k ? 'lineTo' : 'moveTo'](p.x + (k % 2 ? 1 : -1) * s * 0.022, p.y)
}
ctx.stroke()
// muffler on each side
capsule(ctx, { x: sd * s * 0.17, y: -s * 0.16 }, { x: sd * s * 0.19, y: -s * 0.25 }, s * 0.036, s * 0.042, C.chrome, lw)
circle(ctx, { x: sd * s * 0.19, y: -s * 0.25 }, s * 0.024, '#2a2f37', lw)
}
// fat rear tyre with tread
ctx.beginPath()
ctx.ellipse(0, -s * 0.15, s * 0.085, s * 0.16, 0, 0, Math.PI * 2)
ink(ctx, C.tire, lw)
ctx.strokeStyle = 'rgba(140,150,165,0.4)'
ctx.lineWidth = Math.max(1, s * 0.006)
ctx.beginPath()
for (let k = 0; k < 6; k++) {
const yy = -s * 0.27 + k * s * 0.045 + ((t * 40) % 1) * s * 0.045
ctx.moveTo(-s * 0.05, yy)
ctx.lineTo(s * 0.05, yy)
}
ctx.stroke()
ctx.beginPath()
ctx.moveTo(-s * 0.09, -s * 0.24)
ctx.quadraticCurveTo(0, -s * 0.31, s * 0.09, -s * 0.24)
ctx.lineTo(s * 0.08, -s * 0.36)
ctx.lineTo(-s * 0.08, -s * 0.36)
ctx.closePath()
ink(ctx, C.bike, lw)
ctx.beginPath()
ctx.rect(-s * 0.05, -s * 0.27, s * 0.1, s * 0.05)
ink(ctx, '#e8e2d6', lw)
ctx.beginPath()
ctx.ellipse(0, -s * 0.335, s * 0.045, s * 0.022, 0, 0, Math.PI * 2)
ink(ctx, '#ff3a5c', lw)
glowBlob(ctx, 0, -s * 0.335, s * 0.16, '#ff2d55', 0.95)
for (const sd of [-1, 1]) {
ctx.beginPath()
ctx.ellipse(sd * s * 0.1, -s * 0.33, s * 0.016, s * 0.012, 0, 0, Math.PI * 2)
ink(ctx, '#ffa63a', lw)
}
ctx.restore()
}
// ------------------------------------------------------------------ sitting with the letter
/** Hero sitting on the ground, knees up, holding a letter. h = standing height. */
export function heroSit(ctx: C2D, x: number, y: number, h: number, t: number, o: { facing?: number; letter?: number; fire?: number } = {}) {
const f = o.facing ?? 1
const lw = Math.max(1, h * 0.007)
const hip = { x, y: y - h * 0.07 }
const lean = 0.35
const sh = { x: hip.x + f * Math.sin(lean) * h * TORSO, y: hip.y - Math.cos(lean) * h * TORSO }
const footN = { x: x + f * h * 0.3, y: y - h * 0.012 }
const footF = { x: x + f * h * 0.26, y: y - h * 0.02 }
const legN = ik(hip, footN, h * LEG1, h * LEG2, -f)
const legF = ik(hip, footF, h * LEG1, h * LEG2, -f)
const handPt = { x: legN.j.x + f * h * 0.06, y: legN.j.y - h * 0.08 }
const armN = ik(sh, handPt, h * ARM1, h * ARM2, f)
const armF = ik(sh, { x: handPt.x - f * h * 0.02, y: handPt.y + h * 0.01 }, h * ARM1, h * ARM2, f)
const r = h * 0.14
const head = { x: sh.x + f * h * 0.07, y: sh.y - h * 0.02 - r * 0.85 }
const p: Pose = { hip, sh, head, headR: r, kneeN: legN.j, footN: legN.e, kneeF: legF.j, footF: legF.e, elbowN: armN.j, handN: armN.e, elbowF: armF.j, handF: armF.e }
contactShadow(ctx, x + f * h * 0.12, y, h * 0.26)
drawFar(ctx, p, h, f, lw)
// letter between the hands
const letter = o.letter ?? 1
const lp = { x: armN.e.x + f * h * 0.01, y: armN.e.y - h * 0.02 }
if (letter > 0) {
ctx.save()
ctx.translate(lp.x, lp.y)
ctx.rotate(-0.2 * f)
ctx.globalAlpha = clamp(letter * 1.5)
const lwid = h * 0.15
const lh = h * 0.19 * letter
ctx.beginPath()
ctx.rect(-lwid / 2, -lh, lwid, lh)
ink(ctx, '#f1e6cf', lw)
ctx.fillStyle = 'rgba(80,60,40,0.45)'
for (let k = 0; k < 5; k++) if (lh * (0.18 + k * 0.15) < lh) ctx.fillRect(-lwid * 0.38, -lh + lh * (0.18 + k * 0.15), lwid * 0.76 * (k === 4 ? 0.5 : 1), Math.max(1, h * 0.005))
ctx.restore()
}
drawNear(ctx, p, h, f, lw, { t, eyes: 'closed', wind: 0.05, tilt: 0.25 })
const fire = o.fire ?? 0
if (fire > 0) flame(ctx, lp.x, lp.y - h * 0.19 * letter, h * 0.15 * fire, t)
}
export function flame(ctx: C2D, x: number, y: number, size: number, t: number) {
glowBlob(ctx, x, y - size * 0.3, size * 3.2, '#ff8a3d', 0.55)
const layers: [string, number][] = [
['#ff4a1c', 1],
['#ff9a2e', 0.72],
['#ffe08a', 0.42],
]
for (const [c, k] of layers) {
ctx.fillStyle = c
ctx.beginPath()
const w = size * 0.45 * k
const hh = size * 1.3 * k * (0.9 + 0.15 * Math.sin(t * 17 + k * 5))
const sway = Math.sin(t * 11 + k * 3) * size * 0.12
ctx.moveTo(x - w, y)
ctx.quadraticCurveTo(x - w * 1.1, y - hh * 0.5, x + sway, y - hh)
ctx.quadraticCurveTo(x + w * 1.1, y - hh * 0.5, x + w, y)
ctx.quadraticCurveTo(x, y + w * 0.6, x - w, y)
ctx.fill()
}
ctx.fillStyle = '#ffcf7a'
for (let i = 0; i < 10; i++) {
const life = (t * (0.6 + hash(i) * 0.5) + hash(i * 3.3)) % 1
ctx.globalAlpha = 1 - life
ctx.fillRect(x + Math.sin(i * 2.1 + t * 3) * size * 0.6 * life, y - size * (0.6 + life * 2.4), Math.max(1, size * 0.05), Math.max(1, size * 0.05))
}
ctx.globalAlpha = 1
}
// ------------------------------------------------------------------ front head + portrait
export interface PortraitOpts {
t: number
eyes?: number // 0 closed .. 1 open
tear?: number
wind?: number
rim?: string
smile?: boolean
tilt?: number
hair?: FrontHair
glasses?: boolean
mustache?: boolean
hatColor?: string
}
export type FrontHair = 'curly' | 'short' | 'long' | 'bun' | 'bald' | 'cap' | 'scarf' | 'chef'
/** front-view hair / headwear for other characters */
function frontHair(ctx: C2D, cx: number, cy: number, r: number, o: PortraitOpts, lw: number, layer: 'back' | 'front') {
const st = o.hair ?? 'curly'
const hc = o.hatColor ?? '#c4142f'
if (layer === 'back') {
if (st === 'long') {
ctx.beginPath()
ctx.moveTo(cx - r * 1.05, cy - r * 0.4)
ctx.quadraticCurveTo(cx - r * 1.25, cy + r * 0.9, cx - r * 0.95, cy + r * 1.5)
ctx.lineTo(cx + r * 0.95, cy + r * 1.5)
ctx.quadraticCurveTo(cx + r * 1.25, cy + r * 0.9, cx + r * 1.05, cy - r * 0.4)
ctx.closePath()
ink(ctx, C.hair, lw)
}
return
}
const capTop = (col: string) => {
ctx.beginPath()
ctx.moveTo(cx - r * 0.98, cy - r * 0.05)
ctx.quadraticCurveTo(cx - r * 1.05, cy - r * 1.12, cx, cy - r * 1.12)
ctx.quadraticCurveTo(cx + r * 1.05, cy - r * 1.12, cx + r * 0.98, cy - r * 0.05)
ctx.quadraticCurveTo(cx + r * 0.7, cy - r * 0.62, cx, cy - r * 0.6)
ctx.quadraticCurveTo(cx - r * 0.7, cy - r * 0.62, cx - r * 0.98, cy - r * 0.05)
ctx.closePath()
ink(ctx, col, lw)
}
switch (st) {
case 'short':
capTop(C.hair)
break
case 'long':
capTop(C.hair)
ctx.fillStyle = C.hair
ctx.fillRect(cx - r * 0.75, cy - r * 0.62, r * 1.5, r * 0.22)
break
case 'bun':
capTop(C.hair)
ctx.beginPath()
ctx.arc(cx, cy - r * 1.2, r * 0.36, 0, Math.PI * 2)
ink(ctx, C.hair, lw)
break
case 'bald':
for (const sd of [-1, 1]) {
ctx.beginPath()
ctx.ellipse(cx + sd * r * 0.88, cy - r * 0.1, r * 0.14, r * 0.3, 0, 0, Math.PI * 2)
ink(ctx, C.hair, lw)
}
break
case 'cap':
capTop(hc)
ctx.beginPath()
ctx.ellipse(cx, cy - r * 0.52, r * 1.05, r * 0.2, 0, 0, Math.PI * 2)
ink(ctx, hc, lw)
break
case 'scarf':
capTop(hc)
ctx.beginPath()
ctx.moveTo(cx - r * 0.98, cy - r * 0.05)
ctx.quadraticCurveTo(cx - r * 1.1, cy + r * 0.6, cx - r * 0.7, cy + r * 0.85)
ctx.lineTo(cx - r * 0.6, cy + r * 0.2)
ctx.closePath()
ink(ctx, hc, lw)
ctx.beginPath()
ctx.moveTo(cx + r * 0.98, cy - r * 0.05)
ctx.quadraticCurveTo(cx + r * 1.1, cy + r * 0.6, cx + r * 0.7, cy + r * 0.85)
ctx.lineTo(cx + r * 0.6, cy + r * 0.2)
ctx.closePath()
ink(ctx, hc, lw)
break
case 'chef':
capTop('#f4f2ee')
ctx.beginPath()
ctx.moveTo(cx - r * 0.8, cy - r * 0.8)
ctx.lineTo(cx - r * 0.75, cy - r * 1.9)
ctx.quadraticCurveTo(cx, cy - r * 2.3, cx + r * 0.75, cy - r * 1.9)
ctx.lineTo(cx + r * 0.8, cy - r * 0.8)
ctx.closePath()
ink(ctx, '#f4f2ee', lw)
break
}
}
/** Front-facing head. (cx,cy) = face centre, r = face radius. */
export function frontHead(ctx: C2D, cx: number, cy: number, r: number, o: PortraitOpts) {
const t = o.t
const lw = Math.max(1, r * 0.035)
ctx.save()
ctx.translate(cx, cy)
ctx.rotate(o.tilt ?? 0)
ctx.translate(-cx, -cy)
const curly = (o.hair ?? 'curly') === 'curly'
// back hair volume
if (curly) {
ctx.fillStyle = C.line
ctx.beginPath()
ctx.ellipse(cx, cy - r * 0.15, r * 1.34, r * 1.22, 0, 0, Math.PI * 2)
ctx.fill()
ctx.fillStyle = C.hair
ctx.beginPath()
ctx.ellipse(cx, cy - r * 0.15, r * 1.3, r * 1.18, 0, 0, Math.PI * 2)
ctx.fill()
} else frontHair(ctx, cx, cy, r, o, lw, 'back')
for (const sd of [-1, 1]) {
ctx.beginPath()
ctx.ellipse(cx + sd * r * 0.95, cy + r * 0.12, r * 0.15, r * 0.24, 0, 0, Math.PI * 2)
ink(ctx, C.skinShade, lw)
}
// face (square-ish jaw)
ctx.beginPath()
ctx.moveTo(cx - r * 0.94, cy - r * 0.2)
ctx.lineTo(cx - r * 0.9, cy + r * 0.5)
ctx.quadraticCurveTo(cx - r * 0.6, cy + r * 0.95, cx, cy + r * 1.0)
ctx.quadraticCurveTo(cx + r * 0.6, cy + r * 0.95, cx + r * 0.9, cy + r * 0.5)
ctx.lineTo(cx + r * 0.94, cy - r * 0.2)
ctx.quadraticCurveTo(cx, cy - r * 1.05, cx - r * 0.94, cy - r * 0.2)
ctx.closePath()
ink(ctx, C.skin, lw)
ctx.fillStyle = rgba(C.skinShade, 0.35)
ctx.beginPath()
ctx.ellipse(cx - r * 0.6, cy + r * 0.5, r * 0.3, r * 0.35, 0.3, 0, Math.PI * 2)
ctx.fill()
ctx.fillStyle = rgba(C.blush, 0.16)
ctx.beginPath()
ctx.ellipse(cx - r * 0.5, cy + r * 0.35, r * 0.2, r * 0.1, 0, 0, Math.PI * 2)
ctx.ellipse(cx + r * 0.5, cy + r * 0.35, r * 0.2, r * 0.1, 0, 0, Math.PI * 2)
ctx.fill()
// eyes + thick brows
const open = o.eyes ?? 0
ctx.strokeStyle = '#21160f'
ctx.fillStyle = '#21160f'
ctx.lineCap = 'round'
ctx.lineWidth = Math.max(1, r * 0.06)
for (const sd of [-1, 1]) {
const ex = cx + sd * r * 0.38
const ey = cy + r * 0.08
ctx.beginPath()
if (open < 0.3) {
ctx.arc(ex, ey - r * 0.06, r * 0.17, Math.PI * 0.15, Math.PI * 0.85)
ctx.stroke()
} else {
ctx.ellipse(ex, ey, r * 0.1, r * 0.13 * open, 0, 0, Math.PI * 2)
ctx.fill()
ctx.fillStyle = '#ffffff'
ctx.beginPath()
ctx.arc(ex + r * 0.035, ey - r * 0.04, r * 0.03, 0, Math.PI * 2)
ctx.fill()
ctx.fillStyle = '#21160f'
}
ctx.save()
ctx.lineWidth = Math.max(1.5, r * 0.1)
ctx.beginPath()
ctx.moveTo(ex - sd * r * 0.18, ey - r * 0.3)
ctx.lineTo(ex + sd * r * 0.17, ey - r * 0.25)
ctx.stroke()
ctx.restore()
}
// nose + mouth
ctx.strokeStyle = rgba(C.skinShade, 0.95)
ctx.lineWidth = Math.max(1, r * 0.05)
ctx.beginPath()
ctx.moveTo(cx + r * 0.02, cy + r * 0.25)
ctx.lineTo(cx - r * 0.05, cy + r * 0.42)
ctx.lineTo(cx + r * 0.05, cy + r * 0.44)
ctx.stroke()
ctx.strokeStyle = '#6a3530'
ctx.beginPath()
if (o.smile) ctx.arc(cx, cy + r * 0.56, r * 0.14, 0.25, Math.PI - 0.25)
else {
ctx.moveTo(cx - r * 0.13, cy + r * 0.66)
ctx.quadraticCurveTo(cx, cy + r * 0.6, cx + r * 0.13, cy + r * 0.66)
}
ctx.stroke()
const tear = o.tear ?? 0
if (tear > 0) {
const ty = cy + r * 0.2 + tear * r * 0.55
ctx.fillStyle = 'rgba(190,230,255,0.9)'
ctx.beginPath()
ctx.ellipse(cx - r * 0.42, ty, r * 0.05, r * 0.08, 0, 0, Math.PI * 2)
ctx.fill()
ctx.strokeStyle = 'rgba(190,230,255,0.45)'
ctx.lineWidth = Math.max(1, r * 0.03)
ctx.beginPath()
ctx.moveTo(cx - r * 0.42, cy + r * 0.18)
ctx.lineTo(cx - r * 0.42, ty)
ctx.stroke()
}
if (o.glasses) {
ctx.strokeStyle = '#1a1a1e'
ctx.lineWidth = Math.max(1, r * 0.05)
ctx.beginPath()
ctx.arc(cx - r * 0.38, cy + r * 0.08, r * 0.2, 0, Math.PI * 2)
ctx.moveTo(cx + r * 0.58, cy + r * 0.08)
ctx.arc(cx + r * 0.38, cy + r * 0.08, r * 0.2, 0, Math.PI * 2)
ctx.moveTo(cx - r * 0.18, cy + r * 0.06)
ctx.lineTo(cx + r * 0.18, cy + r * 0.06)
ctx.stroke()
}
if (o.mustache) {
ctx.fillStyle = C.hair
ctx.beginPath()
ctx.ellipse(cx - r * 0.12, cy + r * 0.52, r * 0.16, r * 0.06, 0.2, 0, Math.PI * 2)
ctx.ellipse(cx + r * 0.12, cy + r * 0.52, r * 0.16, r * 0.06, -0.2, 0, Math.PI * 2)
ctx.fill()
}
if (!curly) {
frontHair(ctx, cx, cy, r, o, lw, 'front')
ctx.restore()
return
}
// curly fringe + crown
curls(ctx, cx, cy - r * 0.38, r * 1.1, 1, o.wind ?? 0, t, 'front')
const fr: { x: number; y: number; rr: number }[] = []
for (let i = 0; i < 7; i++) fr.push({ x: cx + (i - 3) * r * 0.26, y: cy - r * 0.66 + Math.abs(i - 3) * r * 0.06 + Math.sin(t * 2 + i) * r * 0.01, rr: r * (0.2 + 0.05 * hash(i * 9.1)) })
ctx.fillStyle = C.line
ctx.beginPath()
for (const p of fr) {
ctx.moveTo(p.x + p.rr + lw, p.y)
ctx.arc(p.x, p.y, p.rr + lw, 0, Math.PI * 2)
}
ctx.fill()
ctx.fillStyle = C.hair
ctx.beginPath()
for (const p of fr) {
ctx.moveTo(p.x + p.rr, p.y)
ctx.arc(p.x, p.y, p.rr, 0, Math.PI * 2)
}
ctx.fill()
ctx.strokeStyle = C.hairHi
ctx.lineWidth = Math.max(1, r * 0.05)
ctx.beginPath()
for (const p of fr) {
ctx.moveTo(p.x + p.rr * 0.6 * Math.cos(-2.6), p.y + p.rr * 0.6 * Math.sin(-2.6))
ctx.arc(p.x, p.y, p.rr * 0.6, -2.6, -0.6)
}
ctx.stroke()
if (o.rim) {
ctx.strokeStyle = o.rim
ctx.lineWidth = Math.max(1, r * 0.05)
ctx.beginPath()
ctx.arc(cx, cy - r * 0.2, r * 1.45, Math.PI * 1.08, Math.PI * 1.55)
ctx.stroke()
}
ctx.restore()
}
/** Front-facing bust. (cx,cy) = face centre, size = face radius. */
export function heroPortrait(ctx: C2D, cx: number, cy: number, size: number, o: PortraitOpts) {
const r = size
const lw = Math.max(1, r * 0.035)
ctx.beginPath()
ctx.moveTo(cx - r * 2.5, cy + r * 3.3)
ctx.quadraticCurveTo(cx - r * 2.4, cy + r * 1.5, cx - r * 0.55, cy + r * 1.28)
ctx.lineTo(cx + r * 0.55, cy + r * 1.28)
ctx.quadraticCurveTo(cx + r * 2.4, cy + r * 1.5, cx + r * 2.5, cy + r * 3.3)
ctx.closePath()
ink(ctx, C.sweater, lw)
ctx.fillStyle = C.sweaterShade
ctx.beginPath()
ctx.moveTo(cx - r * 2.5, cy + r * 3.3)
ctx.quadraticCurveTo(cx - r * 2.4, cy + r * 1.5, cx - r * 1.3, cy + r * 1.34)
ctx.quadraticCurveTo(cx - r * 1.7, cy + r * 2.2, cx - r * 1.6, cy + r * 3.3)
ctx.closePath()
ctx.fill()
ctx.strokeStyle = rgba(C.sweaterHi, 0.35)
ctx.lineWidth = Math.max(1, r * 0.03)
ctx.beginPath()
for (let k = -3; k <= 3; k++) {
ctx.moveTo(cx + k * r * 0.45, cy + r * 1.75)
ctx.lineTo(cx + k * r * 0.5, cy + r * 3.2)
}
ctx.stroke()
ctx.beginPath()
ctx.rect(cx - r * 0.34, cy + r * 0.65, r * 0.68, r * 0.72)
ink(ctx, C.skinShade, lw)
ctx.beginPath()
ctx.ellipse(cx, cy + r * 1.34, r * 0.6, r * 0.2, 0, 0, Math.PI)
ctx.strokeStyle = C.line
ctx.lineWidth = r * 0.2
ctx.stroke()
ctx.strokeStyle = C.rib
ctx.lineWidth = r * 0.15
ctx.stroke()
frontHead(ctx, cx, cy, r, o)
}
// ------------------------------------------------------------------ front-view body (sitting)
export function frontTorso(ctx: C2D, cx: number, hipY: number, shY: number, h: number, lw: number) {
const sw = h * 0.16
const ww = h * 0.13
ctx.beginPath()
ctx.moveTo(cx - ww, hipY)
ctx.quadraticCurveTo(cx - sw * 1.05, (hipY + shY) / 2, cx - sw, shY + h * 0.02)
ctx.quadraticCurveTo(cx - sw * 0.6, shY - h * 0.02, cx, shY - h * 0.015)
ctx.quadraticCurveTo(cx + sw * 0.6, shY - h * 0.02, cx + sw, shY + h * 0.02)
ctx.quadraticCurveTo(cx + sw * 1.05, (hipY + shY) / 2, cx + ww, hipY)
ctx.closePath()
ink(ctx, C.sweater, lw)
ctx.fillStyle = rgba(C.sweaterShade, 0.85)
ctx.beginPath()
ctx.moveTo(cx - ww, hipY)
ctx.quadraticCurveTo(cx - sw * 1.05, (hipY + shY) / 2, cx - sw, shY + h * 0.02)
ctx.lineTo(cx - sw * 0.55, shY + h * 0.03)
ctx.quadraticCurveTo(cx - ww * 0.8, (hipY + shY) / 2, cx - ww * 0.55, hipY)
ctx.closePath()
ctx.fill()
ctx.strokeStyle = rgba(C.sweaterHi, 0.3)
ctx.lineWidth = Math.max(1, h * 0.004)
ctx.beginPath()
for (let k = -2; k <= 2; k++) {
ctx.moveTo(cx + k * h * 0.05, shY + h * 0.05)
ctx.lineTo(cx + k * h * 0.045, hipY - h * 0.03)
}
ctx.stroke()
capsule(ctx, { x: cx - ww, y: hipY - h * 0.01 }, { x: cx + ww, y: hipY - h * 0.01 }, h * 0.018, h * 0.018, C.rib, lw)
// crew collar + neck
capsule(ctx, { x: cx, y: shY }, { x: cx, y: shY - h * 0.05 }, h * 0.034, h * 0.032, C.skinShade, lw)
ctx.beginPath()
ctx.ellipse(cx, shY + h * 0.005, h * 0.05, h * 0.02, 0, 0, Math.PI)
ctx.strokeStyle = C.rib
ctx.lineWidth = h * 0.016
ctx.stroke()
}
function frontShoe(ctx: C2D, p: V, h: number, lw: number) {
ctx.beginPath()
ctx.ellipse(p.x, p.y - h * 0.018, h * 0.045, h * 0.03, 0, 0, Math.PI * 2)
ink(ctx, C.shoe, lw)
ctx.fillStyle = C.sole
ctx.fillRect(p.x - h * 0.045, p.y - h * 0.008, h * 0.09, h * 0.01)
}
/**
* Hero sitting side-saddle on his parked bike, body turned to the camera.
* (x,y) = bike ground point, s = bike scale.
*/
export function heroSideSit(ctx: C2D, x: number, y: number, s: number, o: { facing?: number; t?: number; light?: number; beam?: boolean; look?: number; rim?: string } = {}) {
const f = o.facing ?? 1
const t = o.t ?? 0
const h = HERO_H * s
const lw = Math.max(1, h * 0.007)
const A = bikeAnchors(x, y, s, f)
bike(ctx, x, y, s, { facing: f, light: o.light, beam: o.beam, parked: true })
const cx = A.seat.x + f * s * 0.04
const hipY = A.seat.y - h * 0.02
const breathe = Math.sin(t * 1.6) * h * 0.004
const shY = hipY - h * TORSO + breathe
// legs dangle on the near side, feet resting at foot-peg height
const footY = A.peg.y + s * 0.02
const swing = Math.sin(t * 1.3) * h * 0.012
const hipL = { x: cx - h * 0.07, y: hipY }
const hipR = { x: cx + h * 0.07, y: hipY }
const kneeL = { x: cx - h * 0.09, y: hipY + h * 0.06 }
const kneeR = { x: cx + h * 0.1, y: hipY + h * 0.06 }
const footL = { x: cx - h * 0.1 + swing, y: footY }
const footR = { x: cx + h * 0.12, y: footY - h * 0.01 }
frontTorso(ctx, cx, hipY, shY, h, lw)
for (const [hp, kn, ft] of [
[hipL, kneeL, footL],
[hipR, kneeR, footR],
] as [V, V, V][]) {
capsule(ctx, hp, kn, h * 0.062, h * 0.056, C.pants, lw)
capsule(ctx, kn, ft, h * 0.048, h * 0.04, C.pants, lw)
frontShoe(ctx, ft, h, lw)
}
// arms resting on the knees, hands loosely together
const hands = { x: cx + h * 0.005, y: kneeL.y - h * 0.02 }
for (const sd of [-1, 1]) {
const shp = { x: cx + sd * h * 0.15, y: shY + h * 0.03 }
const a = ik(shp, { x: hands.x + sd * h * 0.03, y: hands.y }, h * ARM1, h * ARM2, sd)
capsule(ctx, shp, a.j, h * 0.04, h * 0.035, sd < 0 ? C.sweaterShade : C.sweater, lw)
capsule(ctx, a.j, a.e, h * 0.035, h * 0.031, sd < 0 ? C.sweaterShade : C.sweater, lw)
circle(ctx, a.e, h * 0.03, C.skin, lw)
}
const r = h * 0.14
frontHead(ctx, cx, shY - h * 0.05 - r * 0.85, r, { t, eyes: 0.8, tilt: (o.look ?? 0) * 0.15, rim: o.rim, wind: 0.1 })
}
// ------------------------------------------------------------------ sitting on the ground with a newspaper (front view)
export interface PaperState {
/** 0..1 left hand bringing the lighter to the corner */
lighter?: number
/** lighter flame visible */
lit?: boolean
/** 0..1 burn progress (from the bottom-left corner) */
burn?: number
/** 0 held … 1 lying on the ground */
drop?: number
}
function paperSheet(ctx: C2D, w: number, hh: number, burn: number, t: number) {
// local: (0,0) top-left, (w,hh) bottom-right
const lw = Math.max(1, w * 0.008)
ctx.save()
// unburned region: py < px + 1 - 2p (normalised)
if (burn > 0) {
const p = burn
ctx.beginPath()
ctx.moveTo(-0.5 * w, (0.5 - 2 * p) * hh)
ctx.lineTo(1.5 * w, (2.5 - 2 * p) * hh)
ctx.lineTo(1.5 * w, -1 * hh)
ctx.lineTo(-0.5 * w, -1 * hh)
ctx.closePath()
ctx.clip()
}
ctx.beginPath()
ctx.rect(0, 0, w, hh)
ink(ctx, '#e9e3d3', lw)
// fold line + print
ctx.strokeStyle = 'rgba(90,80,60,0.35)'
ctx.lineWidth = lw
ctx.beginPath()
ctx.moveTo(w / 2, 0)
ctx.lineTo(w / 2, hh)
ctx.stroke()
ctx.fillStyle = '#1d1b18'
ctx.font = `${hh * 0.16}px "MV Black", sans-serif`
ctx.textBaseline = 'top'
ctx.fillText('黑街晚报', w * 0.06, hh * 0.05)
ctx.fillRect(w * 0.06, hh * 0.25, w * 0.38, hh * 0.012)
ctx.fillStyle = 'rgba(40,36,30,0.55)'
for (let c = 0; c < 2; c++)
for (let k = 0; k < 9; k++) ctx.fillRect(w * (0.06 + c * 0.2), hh * (0.31 + k * 0.07), w * (0.16 - (k === 8 ? 0.06 : 0)), Math.max(1, hh * 0.022))
ctx.fillStyle = 'rgba(40,36,30,0.75)'
ctx.fillRect(w * 0.55, hh * 0.08, w * 0.38, hh * 0.4)
ctx.fillStyle = 'rgba(200,190,170,0.6)'
ctx.beginPath()
ctx.arc(w * 0.74, hh * 0.3, hh * 0.1, 0, Math.PI * 2)
ctx.fill()
ctx.fillStyle = 'rgba(40,36,30,0.55)'
for (let k = 0; k < 5; k++) ctx.fillRect(w * 0.55, hh * (0.56 + k * 0.08), w * 0.38, Math.max(1, hh * 0.022))
ctx.restore()
if (burn > 0 && burn < 1) {
// charred band + flames along the burn front
const p = burn
ctx.save()
ctx.beginPath()
ctx.rect(0, 0, w, hh)
ctx.clip()
ctx.strokeStyle = 'rgba(30,18,8,0.9)'
ctx.lineWidth = hh * 0.06
ctx.beginPath()
ctx.moveTo(-0.5 * w, (0.5 - 2 * p) * hh - hh * 0.03)
ctx.lineTo(1.5 * w, (2.5 - 2 * p) * hh - hh * 0.03)
ctx.stroke()
ctx.strokeStyle = 'rgba(255,140,40,0.9)'
ctx.lineWidth = hh * 0.02
ctx.stroke()
ctx.restore()
for (let k = 0; k <= 8; k++) {
const px = k / 8
const py = px + 1 - 2 * p
if (py < 0 || py > 1) continue
flame(ctx, px * w, py * hh, hh * (0.12 + 0.08 * hash(k * 3.1 + Math.floor(t * 6))), t + k)
}
}
}
/** Hero sits on the ground facing camera, knees up, a newspaper in front of his face. h = standing height. */
export function heroSitPaper(ctx: C2D, x: number, y: number, h: number, t: number, st: PaperState = {}) {
const lw = Math.max(1, h * 0.007)
const hipY = y - h * 0.07
const shY = hipY - h * TORSO
const r = h * 0.14
contactShadow(ctx, x, y, h * 0.28)
// torso + head (behind the paper)
frontTorso(ctx, x, hipY, shY, h, lw)
const headY = shY - h * 0.05 - r * 0.85
const reveal = clamp(((st.burn ?? 0) - 0.25) / 0.3) + (st.drop ?? 0)
frontHead(ctx, x, headY, r, { t, eyes: reveal > 0.5 ? 0.9 : 0, wind: 0.05, tilt: -0.05 })
// knees up (in front of the torso)
for (const sd of [-1, 1]) {
const hp = { x: x + sd * h * 0.07, y: hipY }
const kn = { x: x + sd * h * 0.12, y: y - h * 0.3 }
const ft = { x: x + sd * h * 0.11, y }
capsule(ctx, hp, kn, h * 0.06, h * 0.05, sd < 0 ? C.pantsShade : C.pants, lw)
capsule(ctx, ft, kn, h * 0.042, h * 0.048, sd < 0 ? C.pantsShade : C.pants, lw)
frontShoe(ctx, ft, h, lw)
}
// newspaper geometry
const pw = h * 0.46
const ph = h * 0.32
const drop = st.drop ?? 0
const burn = st.burn ?? 0
const held = { x: x - pw / 2, y: headY - ph * 0.45 }
const ground = { x: x - pw * 0.15, y: y + h * 0.04 }
const px0 = held.x + (ground.x - held.x) * drop
const py0 = held.y + (ground.y - held.y) * drop * drop
const sy = 1 - drop * 0.72 // lying flat → foreshortened
// hands: right hand holds the top-right edge; left hand holds the left edge or the lighter
const rightHand = drop < 0.15 ? { x: px0 + pw, y: py0 + ph * 0.35 * sy } : { x: x + h * 0.14, y: y - h * 0.34 }
const leftHold = { x: px0, y: py0 + ph * 0.5 * sy }
const lighterPos = { x: px0 + pw * 0.02, y: py0 + ph * 1.04 }
const restL = { x: x - h * 0.14, y: y - h * 0.34 }
const lk = st.lighter ?? 0
let leftHand: V
if (burn > 0.3 || drop > 0) leftHand = restL
else if (lk <= 0) leftHand = leftHold
else if (lk < 0.5) leftHand = lerpV(leftHold, { x: x - h * 0.2, y: y - h * 0.25 }, lk * 2) // reach into the pocket
else leftHand = lerpV({ x: x - h * 0.2, y: y - h * 0.25 }, lighterPos, (lk - 0.5) * 2)
// arms (drawn before the paper when holding it up)
const arms = () => {
for (const [sd, hand] of [
[-1, leftHand],
[1, rightHand],
] as [number, V][]) {
const shp = { x: x + sd * h * 0.15, y: shY + h * 0.03 }
const a = ik(shp, hand, h * ARM1, h * ARM2, sd)
capsule(ctx, shp, a.j, h * 0.04, h * 0.035, sd < 0 ? C.sweaterShade : C.sweater, lw)
capsule(ctx, a.j, a.e, h * 0.035, h * 0.031, sd < 0 ? C.sweaterShade : C.sweater, lw)
circle(ctx, a.e, h * 0.03, C.skin, lw)
}
}
if (burn < 1) {
ctx.save()
ctx.translate(px0, py0)
ctx.scale(1, sy)
ctx.rotate(-0.04 + drop * 0.25)
paperSheet(ctx, pw, ph, burn, t)
ctx.restore()
} else {
// ashes + embers on the ground
ctx.fillStyle = 'rgba(30,26,22,0.85)'
ctx.beginPath()
ctx.ellipse(ground.x + pw * 0.45, ground.y + ph * 0.12, pw * 0.4, ph * 0.08, 0, 0, Math.PI * 2)
ctx.fill()
ctx.fillStyle = '#ff8a3d'
for (let k = 0; k < 14; k++) {
ctx.globalAlpha = 0.4 + 0.6 * Math.abs(Math.sin(t * 3 + k))
ctx.fillRect(ground.x + pw * (0.1 + 0.7 * hash(k)), ground.y + ph * (0.06 + 0.12 * hash(k * 2)), h * 0.008, h * 0.008)
}
ctx.globalAlpha = 1
}
arms()
// the lighter
if (lk > 0.5 && burn <= 0.3 && drop === 0) {
ctx.save()
ctx.translate(leftHand.x, leftHand.y)
ctx.beginPath()
ctx.rect(-h * 0.012, -h * 0.05, h * 0.024, h * 0.045)
ink(ctx, '#c4142f', lw)
ctx.beginPath()
ctx.rect(-h * 0.012, -h * 0.062, h * 0.024, h * 0.014)
ink(ctx, C.chrome, lw)
ctx.restore()
if (st.lit) flame(ctx, leftHand.x, leftHand.y - h * 0.062, h * 0.03, t)
}
// smoke / ash flakes drifting up while burning
if (burn > 0) {
for (let k = 0; k < 16; k++) {
const life = fract(t * (0.3 + hash(k) * 0.3) + hash(k * 7))
ctx.fillStyle = k % 3 ? `rgba(60,55,50,${0.5 * (1 - life)})` : `rgba(255,150,60,${0.8 * (1 - life)})`
ctx.fillRect(px0 + pw * hash(k * 3.3) + Math.sin(t + k) * h * 0.03, py0 - life * h * 0.5, h * 0.012, h * 0.008)
}
}
}
// ------------------------------------------------------------------ other people
/** side-view hair / headwear (local: facing +x, head centre 0,0, radius r) */
function sideHair(ctx: C2D, r: number, style: HairStyle, lw: number, t: number) {
const cap = () => {
ctx.beginPath()
ctx.moveTo(r * 0.62, -r * 0.5)
ctx.quadraticCurveTo(r * 0.3, -r * 1.12, -r * 0.4, -r * 0.95)
ctx.quadraticCurveTo(-r * 0.95, -r * 0.6, -r * 0.82, r * 0.1)
ctx.quadraticCurveTo(-r * 0.6, r * 0.45, -r * 0.35, r * 0.4)
ctx.quadraticCurveTo(-r * 0.3, -r * 0.1, r * 0.1, -r * 0.48)
ctx.closePath()
ink(ctx, C.hair, lw)
}
if (style === 'bald') {
ctx.beginPath()
ctx.ellipse(-r * 0.45, r * 0.05, r * 0.25, r * 0.3, 0, 0, Math.PI * 2)
ink(ctx, C.hair, lw)
return
}
if (style === 'long') {
ctx.beginPath()
ctx.moveTo(-r * 0.2, -r * 0.6)
ctx.quadraticCurveTo(-r * 1.15, -r * 0.2, -r * 0.95, r * 1.55)
ctx.lineTo(-r * 0.25, r * 1.45 + Math.sin(t * 2) * r * 0.05)
ctx.quadraticCurveTo(-r * 0.35, r * 0.6, -r * 0.2, r * 0.2)
ctx.closePath()
ink(ctx, C.hair, lw)
}
if (style === 'ponytail') {
ctx.beginPath()
ctx.moveTo(-r * 0.75, -r * 0.35)
ctx.quadraticCurveTo(-r * 1.6, -r * 0.1 + Math.sin(t * 6) * r * 0.1, -r * 1.25, r * 0.95)
ctx.quadraticCurveTo(-r * 1.0, r * 0.2, -r * 0.7, -r * 0.05)
ctx.closePath()
ink(ctx, C.hair, lw)
}
if (style === 'bun') {
ctx.beginPath()
ctx.arc(-r * 0.75, -r * 0.75, r * 0.38, 0, Math.PI * 2)
ink(ctx, C.hair, lw)
}
cap()
if (style === 'cap') {
ctx.beginPath()
ctx.moveTo(-r * 0.85, -r * 0.2)
ctx.quadraticCurveTo(-r * 0.7, -r * 1.2, r * 0.45, -r * 0.85)
ctx.lineTo(r * 1.25, -r * 0.62)
ctx.lineTo(r * 1.2, -r * 0.5)
ctx.lineTo(-r * 0.85, -r * 0.2)
ctx.closePath()
ink(ctx, C.rib, lw)
}
if (style === 'hat') {
ctx.beginPath()
ctx.moveTo(-r * 1.3, -r * 0.5)
ctx.lineTo(r * 1.25, -r * 0.62)
ctx.lineTo(r * 0.6, -r * 0.82)
ctx.quadraticCurveTo(0, -r * 1.5, -r * 0.7, -r * 0.75)
ctx.closePath()
ink(ctx, C.rib, lw)
}
}
export interface PersonStyle {
palette: Partial<typeof HERO>
hair: HairStyle
skirt?: string
/** colour of the legs under a skirt (skin or tights) */
legs?: string
shortSleeves?: boolean
scale?: number
stoop?: number
carry?: 'none' | 'skewer' | 'cup' | 'phone' | 'bag' | 'cane'
}
/** a passer-by: same rig, different body, clothes, hair and props */
export function personWalk(ctx: C2D, x: number, y: number, h0: number, phase: number, st: PersonStyle, t: number, facing = 1) {
withPalette(st.palette, () => {
const h = h0 * (st.scale ?? 1)
const f = facing
const lw = Math.max(1, h * 0.007)
const slow = st.stoop ? 0.6 : 1
const p = walkPose(x, y, h, phase * slow, { facing: f })
if (st.stoop) {
const k = st.stoop
p.sh = { x: p.sh.x + f * h * 0.08 * k, y: p.sh.y + h * 0.04 * k }
p.head = { x: p.head.x + f * h * 0.11 * k, y: p.head.y + h * 0.06 * k }
}
// props in the near hand
const holdUp = st.carry === 'skewer' || st.carry === 'cup' || st.carry === 'phone'
if (holdUp) {
const target = { x: p.sh.x + f * h * 0.12, y: p.sh.y + h * 0.04 }
const a = ik(p.sh, target, h * ARM1, h * ARM2, f)
p.elbowN = a.j
p.handN = a.e
}
if (st.carry === 'cane') {
const target = { x: p.sh.x + f * h * 0.12, y: p.sh.y + h * 0.27 }
const a = ik(p.sh, target, h * ARM1, h * ARM2, f)
p.elbowN = a.j
p.handN = a.e
}
contactShadow(ctx, x, y, h * 0.2)
const arm = (near: boolean) => {
const e = near ? p.elbowN : p.elbowF
const hd = near ? p.handN : p.handF
const col = near ? C.sweater : C.sweaterShade
if (st.shortSleeves) {
const mid = { x: p.sh.x + (e.x - p.sh.x) * 0.6, y: p.sh.y + (e.y - p.sh.y) * 0.6 }
capsule(ctx, p.sh, mid, h * 0.045, h * 0.042, col, lw)
capsule(ctx, mid, e, h * 0.03, h * 0.028, C.skin, lw)
capsule(ctx, e, hd, h * 0.028, h * 0.025, C.skin, lw)
circle(ctx, hd, h * 0.028, C.skin, lw)
} else armPiece(ctx, p.sh, e, hd, h, near, lw)
}
if (st.skirt) {
// legs show only below the hem; the skirt covers the thighs
const legCol = st.legs ?? C.skin
const shin = (kn: V, ft: V, near: boolean) => {
capsule(ctx, kn, ft, h * 0.036, h * 0.03, near ? legCol : rgba(legCol, 0.85), lw)
shoe(ctx, ft, h, f, lw)
}
shin(p.kneeF, p.footF, false)
arm(false)
const hemY = Math.max(p.kneeN.y, p.kneeF.y) + h * 0.015
const xs = [p.kneeN.x, p.kneeF.x]
const hemL = Math.min(...xs) - h * 0.07
const hemR = Math.max(...xs) + h * 0.07
const sway = Math.sin(phase * Math.PI * 4) * h * 0.012
ctx.beginPath()
ctx.moveTo(p.hip.x - h * 0.085, p.hip.y - h * 0.05)
ctx.lineTo(p.hip.x + h * 0.085, p.hip.y - h * 0.05)
ctx.quadraticCurveTo(hemR + sway, (p.hip.y + hemY) / 2, hemR + sway, hemY)
ctx.lineTo(hemL + sway, hemY + h * 0.008)
ctx.quadraticCurveTo(hemL + sway, (p.hip.y + hemY) / 2, p.hip.x - h * 0.085, p.hip.y - h * 0.05)
ctx.closePath()
ink(ctx, st.skirt, lw)
ctx.strokeStyle = rgba(C.line, 0.3)
ctx.beginPath()
ctx.moveTo(p.hip.x, p.hip.y)
ctx.lineTo(p.hip.x + sway, hemY)
ctx.stroke()
// torso, head, near shin, near arm
capsule(ctx, p.sh, { x: p.head.x - f * p.headR * 0.1, y: p.head.y + p.headR * 0.6 }, h * 0.03, h * 0.028, C.skinShade, lw)
torso(ctx, p.hip, p.sh, h, f, lw)
shin(p.kneeN, p.footN, true)
heroHead(ctx, p.head.x, p.head.y, p.headR, f, { t, eyes: 'open', hair: st.hair, wind: 0 })
arm(true)
} else {
legPiece(ctx, p.hip, p.kneeF, p.footF, h, f, false, lw)
arm(false)
capsule(ctx, p.sh, { x: p.head.x - f * p.headR * 0.1, y: p.head.y + p.headR * 0.6 }, h * 0.032, h * 0.03, C.skinShade, lw)
torso(ctx, p.hip, p.sh, h, f, lw)
legPiece(ctx, p.hip, p.kneeN, p.footN, h, f, true, lw)
heroHead(ctx, p.head.x, p.head.y, p.headR, f, { t, eyes: 'open', hair: st.hair, wind: 0 })
arm(true)
}
const hand = p.handN
if (st.carry === 'skewer') {
ctx.strokeStyle = '#d8b07a'
ctx.lineWidth = Math.max(1, h * 0.006)
ctx.beginPath()
ctx.moveTo(hand.x, hand.y)
ctx.lineTo(hand.x + f * h * 0.02, hand.y - h * 0.16)
ctx.stroke()
ctx.fillStyle = '#8a3a1a'
for (let k = 0; k < 4; k++) ctx.fillRect(hand.x + f * h * 0.004 * k - h * 0.012, hand.y - h * (0.06 + k * 0.026), h * 0.024, h * 0.02)
} else if (st.carry === 'cup') {
ctx.beginPath()
ctx.moveTo(hand.x - h * 0.022, hand.y - h * 0.07)
ctx.lineTo(hand.x + h * 0.022, hand.y - h * 0.07)
ctx.lineTo(hand.x + h * 0.017, hand.y + h * 0.01)
ctx.lineTo(hand.x - h * 0.017, hand.y + h * 0.01)
ctx.closePath()
ink(ctx, '#f0e0c8', lw)
ctx.fillStyle = '#2a1a10'
for (let k = 0; k < 4; k++) ctx.fillRect(hand.x - h * 0.012 + k * h * 0.007, hand.y - h * 0.005, h * 0.005, h * 0.005)
ctx.strokeStyle = '#ff5fa2'
ctx.lineWidth = Math.max(1, h * 0.005)
ctx.beginPath()
ctx.moveTo(hand.x, hand.y - h * 0.07)
ctx.lineTo(hand.x + f * h * 0.01, hand.y - h * 0.11)
ctx.stroke()
} else if (st.carry === 'phone') {
ctx.fillStyle = '#cfe8ff'
ctx.fillRect(hand.x - h * 0.012, hand.y - h * 0.05, h * 0.024, h * 0.045)
glowBlob(ctx, hand.x, hand.y - h * 0.03, h * 0.12, '#7fc8ff', 0.5)
} else if (st.carry === 'bag') {
ctx.beginPath()
ctx.roundRect(hand.x - h * 0.035, hand.y + h * 0.01, h * 0.07, h * 0.09, h * 0.01)
ink(ctx, '#e8e2d6', lw)
} else if (st.carry === 'cane') {
ctx.strokeStyle = '#4a2e1a'
ctx.lineWidth = Math.max(1, h * 0.01)
ctx.beginPath()
ctx.moveTo(hand.x, hand.y)
ctx.lineTo(hand.x + f * h * 0.04, y)
ctx.stroke()
}
})
}
/** standing with his back against a tree trunk, arms folded, one ankle crossed, looking up. (x,y) feet/trunk contact */
export function heroLean(ctx: C2D, x: number, y: number, h: number, t: number, o: { facing?: number; rim?: string } = {}) {
const f = o.facing ?? 1
const lw = Math.max(1, h * 0.007)
const breathe = Math.sin(t * 1.4) * h * 0.003
const hip = { x: x + f * h * 0.1, y: y - h * 0.39 }
const lean = -0.2 // back against the trunk
const sh = { x: hip.x + f * Math.sin(lean) * h * TORSO, y: hip.y - Math.cos(lean) * h * TORSO + breathe }
const footN = { x: x + f * h * 0.3, y }
const footF = { x: x + f * h * 0.24, y: y - h * 0.005 }
const legN = ik(hip, footN, h * LEG1, h * LEG2, -f)
const legF = ik(hip, footF, h * LEG1, h * LEG2, -f)
// folded arms: both hands meet in front of the chest
const chest = { x: sh.x + f * h * 0.08, y: sh.y + h * 0.1 }
const armN = ik(sh, { x: chest.x + f * h * 0.03, y: chest.y }, h * ARM1, h * ARM2, f)
const armF = ik(sh, { x: chest.x - f * h * 0.01, y: chest.y - h * 0.02 }, h * ARM1, h * ARM2, f)
const r = h * 0.14
const head = { x: sh.x + f * h * 0.02, y: sh.y - h * 0.035 - r * 0.95 }
const p: Pose = { hip, sh, head, headR: r, kneeN: legN.j, footN: legN.e, kneeF: legF.j, footF: legF.e, elbowN: armN.j, handN: armN.e, elbowF: armF.j, handF: armF.e }
contactShadow(ctx, x + f * h * 0.18, y, h * 0.2)
drawFar(ctx, p, h, f, lw)
drawNear(ctx, p, h, f, lw, { t, eyes: 'open', smile: true, wind: 0.15, tilt: -0.18, rim: o.rim })
}
export interface VendorStyle {
palette: Partial<typeof HERO>
hair: FrontHair
hatColor?: string
apron?: string
glasses?: boolean
mustache?: boolean
smile?: boolean
scale?: number
}
/** a stall-keeper facing the camera, working behind the counter. (x, counterY) = centre on the counter top */
export function vendorFront(ctx: C2D, x: number, counterY: number, h0: number, st: VendorStyle, t: number, seed = 0) {
withPalette(st.palette, () => {
const h = h0 * (st.scale ?? 1)
const lw = Math.max(1, h * 0.007)
const hipY = counterY + h * 0.08
const shY = hipY - h * TORSO
frontTorso(ctx, x, hipY, shY, h, lw)
if (st.apron) {
ctx.beginPath()
ctx.moveTo(x - h * 0.1, shY + h * 0.08)
ctx.lineTo(x + h * 0.1, shY + h * 0.08)
ctx.lineTo(x + h * 0.12, hipY + h * 0.05)
ctx.lineTo(x - h * 0.12, hipY + h * 0.05)
ctx.closePath()
ink(ctx, st.apron, lw)
ctx.strokeStyle = st.apron
ctx.lineWidth = h * 0.012
ctx.beginPath()
ctx.moveTo(x - h * 0.08, shY + h * 0.08)
ctx.lineTo(x - h * 0.04, shY - h * 0.01)
ctx.moveTo(x + h * 0.08, shY + h * 0.08)
ctx.lineTo(x + h * 0.04, shY - h * 0.01)
ctx.stroke()
}
// working arms: elbows out, hands busy on the counter
for (const sd of [-1, 1]) {
const work = Math.sin(t * (3 + (seed % 3)) + sd + seed) * h * 0.025
const shp = { x: x + sd * h * 0.15, y: shY + h * 0.03 }
const hand = { x: x + sd * h * 0.09, y: counterY - h * 0.01 + (sd > 0 ? work : -work * 0.5) }
const a = ik(shp, hand, h * ARM1, h * ARM2, sd)
capsule(ctx, shp, a.j, h * 0.04, h * 0.035, sd < 0 ? C.sweaterShade : C.sweater, lw)
capsule(ctx, a.j, a.e, h * 0.035, h * 0.031, C.skin, lw)
circle(ctx, a.e, h * 0.03, C.skin, lw)
}
const r = h * 0.14
frontHead(ctx, x, shY - h * 0.05 - r * 0.85, r, { t, eyes: 1, smile: st.smile, hair: st.hair, hatColor: st.hatColor, glasses: st.glasses, mustache: st.mustache, tilt: Math.sin(t * 0.7 + seed) * 0.05 })
})
}
// 主角「阿飞」v3 — redrawn from the protagonist of the reference footage:
// a young man with a big mop of curly black hair, a deep-blue long-sleeve
// sweater, grey trousers, white-soled sneakers, and his black classic
// motorcycle (round headlight, two round mirrors, red tail light).
//
// v3: everything is built on ONE rig.
// • The bike exposes anchor points (seat / grip / peg) in its own units.
// • The rider's limbs are solved with two-bone IK onto those anchors, so
// hands are always on the grips, feet on the pegs, hips on the seat.
// • Rider and bike share one scale: standing height h = HERO_H * bike scale s.
// • Draw order is explicit (far limbs → bike → body → near limbs), so
// nothing pokes through the bike.
import { glowBlob, lightCone } from './draw'
import { clamp, fract, hash, rgba } from './util'
type C2D = CanvasRenderingContext2D
interface V {
x: number
y: number
}
export const HERO = {
skin: '#ecd0a8',
skinShade: '#c49f7c',
blush: '#e89a86',
hair: '#121411',
hairHi: '#44596f',
line: '#08090c',
sweater: '#2f5072',
sweaterShade: '#223a54',
sweaterHi: '#4a74a0',
rib: '#264262',
pants: '#8d9298',
pantsShade: '#666b72',
shoe: '#1d1d21',
sole: '#e4ded2',
bike: '#15171c',
bikeShade: '#0b0c10',
bikeHi: '#56606f',
chrome: '#cfd6de',
chromeShade: '#7e8894',
seat: '#221c19',
tire: '#0a0a0c',
}
/** active palette (lets the same rig draw other characters, e.g. the stall auntie) */
let C: typeof HERO = HERO
export function withPalette(p: Partial<typeof HERO>, fn: () => void) {
const prev = C
C = { ...HERO, ...p }
try {
fn()
} finally {
C = prev
}
}
export const AUNTIE: Partial<typeof HERO> = {
sweater: '#8a3a3a',
sweaterShade: '#6a2a2c',
sweaterHi: '#b0585a',
rib: '#5a2426',
pants: '#2f3440',
pantsShade: '#22262f',
hair: '#3a3a3c',
hairHi: '#6a6a70',
skin: '#e2c09a',
}
export const RIVAL: Partial<typeof HERO> = {
sweater: '#17181c',
sweaterShade: '#0e0f12',
sweaterHi: '#3a3c44',
rib: '#25262c',
}
/** standing height of the hero in bike units (bike wheelbase = 1) */
export const HERO_H = 1.4
/** total height of rider + bike in bike units (for layout) */
export const RIDE_TOP = 1.5
// ------------------------------------------------------------------ geometry helpers
const add = (a: V, b: V): V => ({ x: a.x + b.x, y: a.y + b.y })
const lerpV = (a: V, b: V, k: number): V => ({ x: a.x + (b.x - a.x) * k, y: a.y + (b.y - a.y) * k })
/** Two-bone IK. bend: -1 / +1 picks which side the joint goes. Returns joint + end. */
export function ik(a: V, target: V, l1: number, l2: number, bend: number): { j: V; e: V } {
const dx = target.x - a.x
const dy = target.y - a.y
let d = Math.hypot(dx, dy)
const ang = Math.atan2(dy, dx)
d = clamp(d, Math.abs(l1 - l2) + 1e-4, l1 + l2 - 1e-4)
const cosA = (l1 * l1 + d * d - l2 * l2) / (2 * l1 * d)
const A = Math.acos(clamp(cosA, -1, 1))
const ja = ang + bend * A
const j = { x: a.x + Math.cos(ja) * l1, y: a.y + Math.sin(ja) * l1 }
const ea = Math.atan2(target.y - j.y, target.x - j.x)
return { j, e: { x: j.x + Math.cos(ea) * l2, y: j.y + Math.sin(ea) * l2 } }
}
/** tapered capsule between two points (limbs) */
function capsulePath(ctx: C2D, a: V, b: V, ra: number, rb: number) {