Spaces:
Running
Running
File size: 20,747 Bytes
0daf81d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 | // Frame renderer for the "这个用英语怎么说?" Live Photo card. Pure Canvas 2D, deterministic per frame.
import { FONT } from './fonts'
import { getStyle, type CardStyle } from './styles'
export const W = 1080
export const H = 1440
export const FPS = 30
const CW = 920
const PILL_SPACE = 82
export interface CardSpec {
image: CanvasImageSource | null
imgW: number
imgH: number
crop: { zoom: number; x: number; y: number } // zoom ≥ 1; x/y = centre of the 3:4 window (0..1)
en: string
zh: string
ipa: string
pos: string
question: string // use {zh} as the highlighted keyword
pillQ: string
pillA: string
showPill: boolean
styleId: string
accent: string
cardY: number // card centre, 0..1 of frame height
motion: boolean
grain: boolean
confetti: boolean
sweep: boolean
hold: number // seconds the question stays before the reveal
compact: boolean // fit everything into 3 s (WeChat Moments only accepts ~3 s Live Photos, like native iPhone ones)
}
export const MAX_COMPACT = 3.0
export interface Timeline {
rev: number
grow: number
type0: number
dt: number
done: number
marker: number
ipa: number
en1: number
mean: number
en2: number | null
sweep: number
end: number
nf: number
dur: number
coverFrame: number
}
export function makeTimeline(spec: CardSpec, d1: number, d2: number | null): Timeline {
const letters = [...spec.en].filter((c) => c !== ' ').length || 1
if (spec.compact) {
const rev = clamp(spec.hold, 0.25, 0.6)
const type0 = rev + 0.15
const dt = clamp(0.4 / letters, 0.035, 0.07)
const done = type0 + (letters - 1) * dt + 0.1
const en1 = done + 0.1
const mean = en1 + d1 * 0.5
const en2 = d2 != null ? en1 + d1 + 0.1 : null
const end = Math.min(MAX_COMPACT, d2 != null ? MAX_COMPACT : Math.max(en1 + d1 + 0.25, mean + 0.6))
const nf = Math.min(Math.round(MAX_COMPACT * FPS), Math.ceil(end * FPS))
return {
rev, grow: 0.35, type0, dt, done, marker: done + 0.03, ipa: done + 0.08, en1, mean, en2, sweep: done + 0.25, end, nf,
dur: nf / FPS, coverFrame: Math.max(0, Math.min(6, Math.floor(rev * FPS) - 3)),
}
}
const rev = Math.max(0.3, spec.hold)
const type0 = rev + 0.23
const dt = clamp(0.55 / letters, 0.05, 0.085)
const done = type0 + (letters - 1) * dt + 0.14
const en1 = done + 0.24
const mean = en1 + d1 * 0.55
const en2 = d2 != null ? en1 + d1 + 0.3 : null
const end = (en2 != null ? en2 + (d2 as number) : Math.max(en1 + d1, mean + 0.5)) + 0.5
const nf = Math.ceil(end * FPS)
return {
rev, grow: 0.45, type0, dt, done, marker: done + 0.05, ipa: done + 0.18, en1, mean, en2, sweep: done + 0.35, end, nf,
dur: nf / FPS, coverFrame: Math.max(0, Math.min(6, Math.floor(rev * FPS) - 3)),
}
}
// ---------- math ----------
function clamp(x: number, a = 0, b = 1) {
return Math.max(a, Math.min(b, x))
}
const prog = (t: number, t0: number, d: number) => (d > 0 ? clamp((t - t0) / d) : t >= t0 ? 1 : 0)
const easeOutCubic = (x: number) => 1 - (1 - x) ** 3
const easeInOut = (x: number) => (x < 0.5 ? 4 * x ** 3 : 1 - (-2 * x + 2) ** 3 / 2)
const easeOutBack = (x: number, s = 1.9) => 1 + (s + 1) * (x - 1) ** 3 + s * (x - 1) ** 2
const lerp = (a: number, b: number, x: number) => a + (b - a) * x
function rrect(ctx: CanvasRenderingContext2D, x: number, y: number, w: number, h: number, r: number) {
r = Math.max(0, Math.min(r, w / 2, h / 2))
ctx.beginPath()
ctx.moveTo(x + r, y)
ctx.arcTo(x + w, y, x + w, y + h, r)
ctx.arcTo(x + w, y + h, x, y + h, r)
ctx.arcTo(x, y + h, x, y, r)
ctx.arcTo(x, y, x + w, y, r)
ctx.closePath()
}
function seeded(seed: number) {
let s = seed >>> 0
return () => {
s = (s * 1664525 + 1013904223) >>> 0
return s / 4294967296
}
}
const CONFETTI = ['#ff3b5c', '#ffd63d', '#3ddc97', '#4d96ff', '#ff8a3d', '#b57cff']
interface Particle {
x: number; y: number; vx: number; vy: number; rot: number; vr: number; kind: number; col: string; sz: number; delay: number
}
function canvas(w: number, h: number) {
const c = document.createElement('canvas')
c.width = w
c.height = h
return c
}
export class CardRenderer {
st: CardStyle
T: Timeline
env: Float32Array
wsize = 124
hsize = 84
wordX0 = 0
wordW = 0
finalCx: number[] = []
qParts: { text: string; key: boolean }[] = []
po: number
CHQ: number
CHA: number
particles: Particle[]
grain: HTMLCanvasElement[] = []
tmpA = canvas(8, 8)
tmpB = canvas(8, 8)
filterOK: boolean
constructor(public spec: CardSpec, T: Timeline, env: Float32Array) {
this.st = getStyle(spec.styleId)
this.T = T
this.env = env
this.po = spec.showPill ? 0 : PILL_SPACE
this.CHQ = 252 - this.po
this.CHA = (spec.ipa || spec.zh || spec.pos ? 452 : 290) - this.po
const m = canvas(4, 4).getContext('2d')!
this.filterOK = typeof (m as CanvasRenderingContext2D & { filter?: string }).filter === 'string'
// word layout
let size = 124
m.font = FONT.word(size)
while (m.measureText(spec.en).width > CW - 120 && size > 40) {
size -= 2
m.font = FONT.word(size)
}
this.wsize = size
this.wordW = m.measureText(spec.en).width
this.wordX0 = (CW - this.wordW) / 2
this.finalCx = [...spec.en].map((c, i) => this.wordX0 + m.measureText(spec.en.slice(0, i)).width + m.measureText(c).width / 2)
// question layout
const q = spec.question || '「{zh}」用英语怎么说?'
const idx = q.indexOf('{zh}')
this.qParts = idx < 0 ? [{ text: q, key: false }] : [
{ text: q.slice(0, idx), key: false }, { text: spec.zh, key: true }, { text: q.slice(idx + 4), key: false },
].filter((p) => p.text)
// brackets around the keyword read as part of it
for (let i = 0; i < this.qParts.length; i++) {
const p = this.qParts[i]
if (p.key && i > 0 && this.qParts[i - 1].text.endsWith('「')) {
this.qParts[i - 1].text = this.qParts[i - 1].text.slice(0, -1)
p.text = '「' + p.text
}
if (p.key && i + 1 < this.qParts.length && this.qParts[i + 1].text.startsWith('」')) {
this.qParts[i + 1].text = this.qParts[i + 1].text.slice(1)
p.text = p.text + '」'
}
}
this.qParts = this.qParts.filter((p) => p.text)
let hs = 84
m.font = FONT.head(hs)
while (m.measureText(this.qParts.map((p) => p.text).join('')).width > CW - 110 && hs > 36) {
hs -= 2
m.font = FONT.head(hs)
}
this.hsize = hs
// particles
const r = seeded(spec.en.length * 977 + 13)
this.particles = Array.from({ length: 46 }, (_, k) => {
const ang = -Math.PI * (0.05 + r() * 0.9)
const sp = 650 + r() * 850
return {
x: (r() - 0.5) * 520, y: (r() - 0.5) * 60, vx: Math.cos(ang) * sp * 0.9, vy: Math.sin(ang) * sp,
rot: r() * 360, vr: (r() - 0.5) * 1440, kind: Math.floor(r() * 3), col: CONFETTI[k % CONFETTI.length], sz: 9 + r() * 8, delay: r() * 0.08,
}
})
if (spec.grain) {
const gr = seeded(7)
for (let g = 0; g < 3; g++) {
const c = canvas(540, 720)
const x = c.getContext('2d')!
const id = x.createImageData(540, 720)
for (let i = 0; i < id.data.length; i += 4) {
const v = 128 + (gr() + gr() + gr() - 1.5) * 34
id.data[i] = id.data[i + 1] = id.data[i + 2] = v
id.data[i + 3] = 255
}
x.putImageData(id, 0, 0)
this.grain.push(c)
}
}
}
cardH(t: number) {
return lerp(this.CHQ, this.CHA, easeInOut(prog(t, this.T.rev, this.T.grow)))
}
// ----- photo with handheld drift -----
drawPhoto(ctx: CanvasRenderingContext2D, t: number) {
const s = this.spec
ctx.fillStyle = '#1a1a1f'
ctx.fillRect(0, 0, W, H)
if (!s.image || !s.imgW) return
const { imgW, imgH } = s
let bw = Math.min(imgW, (imgH * 3) / 4)
let bh = (bw * 4) / 3
bw /= s.crop.zoom
bh /= s.crop.zoom
let cx = clamp(s.crop.x) * imgW
let cy = clamp(s.crop.y) * imgH
let vw = bw
let vh = bh
if (s.motion) {
const z = 1.02 * (1 + 0.05 * easeInOut(clamp(t / Math.max(this.T.dur, 0.1))))
vw = bw / z
vh = bh / z
const k = bw / W
cx += (5 * Math.sin(2 * Math.PI * 0.31 * t) + 2.2 * Math.sin(2 * Math.PI * 0.87 * t + 1.1)) * k
cy += (4 * Math.sin(2 * Math.PI * 0.23 * t + 0.4) - 4 * Math.sin(0.4) + 1.8 * Math.sin(2 * Math.PI * 0.71 * t)) * k
}
cx = clamp(cx, vw / 2, imgW - vw / 2)
cy = clamp(cy, vh / 2, imgH - vh / 2)
ctx.imageSmoothingEnabled = true
ctx.imageSmoothingQuality = 'high'
ctx.drawImage(s.image, cx - vw / 2, cy - vh / 2, vw, vh, 0, 0, W, H)
}
// ----- frosted glass: blur the photo region under the card -----
blurRegion(src: HTMLCanvasElement, x: number, y: number, w: number, h: number) {
x = Math.max(0, Math.floor(x)); y = Math.max(0, Math.floor(y))
w = Math.min(W - x, Math.ceil(w)); h = Math.min(H - y, Math.ceil(h))
if (this.filterOK) {
const sw = Math.max(1, Math.round(w / 3)), sh = Math.max(1, Math.round(h / 3))
this.tmpA.width = sw; this.tmpA.height = sh
const a = this.tmpA.getContext('2d')!
a.filter = 'blur(8px)'
a.drawImage(src, x - 30, y - 30, w + 60, h + 60, -10, -10, sw + 20, sh + 20)
return { c: this.tmpA, x, y, w, h }
}
const sw = Math.max(1, Math.round(w / 12)), sh = Math.max(1, Math.round(h / 12))
this.tmpA.width = sw; this.tmpA.height = sh
const a = this.tmpA.getContext('2d')!
a.imageSmoothingQuality = 'high'
a.drawImage(src, x, y, w, h, 0, 0, sw, sh)
const mw = sw * 3, mh = sh * 3
this.tmpB.width = mw; this.tmpB.height = mh
const b = this.tmpB.getContext('2d')!
b.imageSmoothingQuality = 'high'
b.drawImage(this.tmpA, 0, 0, mw, mh)
return { c: this.tmpB, x, y, w, h }
}
frame(ctx: CanvasRenderingContext2D, fidx: number) {
const t = fidx / FPS
const T = this.T
const st = this.st
this.drawPhoto(ctx, t)
const bump0 = Math.sin(Math.PI * prog(t, T.rev, 0.32)) * 0.02 + Math.sin(Math.PI * prog(t, T.done, 0.3)) * 0.024
const ccy0 = clamp(this.spec.cardY, 0.12, 0.88) * H
// sample the photo for the frosted glass before anything is drawn over it
const region = st.card === 'glass'
? this.blurRegion(ctx.canvas, W / 2 - (CW * (1 + bump0) + 80) / 2, ccy0 - (this.cardH(t) * (1 + bump0) + 80) / 2, CW * (1 + bump0) + 80, this.cardH(t) * (1 + bump0) + 80)
: null
const bump = Math.sin(Math.PI * prog(t, T.rev, 0.32)) * 0.02 + Math.sin(Math.PI * prog(t, T.done, 0.3)) * 0.024
const sc = 1 + bump
const CH = this.cardH(t)
const ccx = W / 2
const ccy = clamp(this.spec.cardY, 0.12, 0.88) * H
const rot = (st.rotate * Math.PI) / 180
const setCard = () => {
ctx.setTransform(1, 0, 0, 1, 0, 0)
ctx.translate(ccx, ccy)
ctx.rotate(rot)
ctx.scale(sc, sc)
ctx.translate(-CW / 2, -CH / 2)
}
// shadow
ctx.save()
setCard()
ctx.shadowColor = `rgba(10,10,20,${st.shadow})`
ctx.shadowBlur = 68
ctx.shadowOffsetY = 22 / sc
rrect(ctx, 0, 0, CW, CH, st.radius)
ctx.fillStyle = st.card === 'solid' ? st.fill : 'rgba(128,128,128,1)'
ctx.fill()
ctx.restore()
// glass / solid body
ctx.save()
setCard()
rrect(ctx, 0, 0, CW, CH, st.radius)
ctx.clip()
if (region) {
ctx.setTransform(1, 0, 0, 1, 0, 0)
ctx.imageSmoothingQuality = 'high'
ctx.drawImage(region.c, region.x, region.y, region.w, region.h)
setCard()
ctx.fillStyle = st.fill
ctx.fillRect(-4, -4, CW + 8, CH + 8)
} else {
ctx.fillStyle = st.fill
ctx.fillRect(-4, -4, CW + 8, CH + 8)
}
this.content(ctx, t, fidx, CH)
if (this.spec.sweep) this.drawSweep(ctx, t, CH)
ctx.restore()
if (st.rim) {
ctx.save()
setCard()
rrect(ctx, 1, 1, CW - 2, CH - 2, st.radius)
ctx.strokeStyle = st.rim
ctx.lineWidth = 2
ctx.stroke()
ctx.restore()
}
ctx.setTransform(1, 0, 0, 1, 0, 0)
if (this.spec.confetti) this.drawConfetti(ctx, t, ccy - (CH * sc) / 2 + (186 - this.po) * sc)
if (this.grain.length) {
ctx.save()
ctx.globalCompositeOperation = 'overlay'
ctx.globalAlpha = 0.09
ctx.imageSmoothingEnabled = false
ctx.drawImage(this.grain[fidx % 3], 0, 0, W, H)
ctx.restore()
}
}
drawSweep(ctx: CanvasRenderingContext2D, t: number, CH: number) {
const swp = prog(t, this.T.sweep, 0.55)
if (swp <= 0 || swp >= 1) return
const bx = lerp(-260, CW + 300, easeInOut(swp))
ctx.save()
ctx.translate(bx, CH / 2)
ctx.rotate((18 * Math.PI) / 180)
const g = ctx.createLinearGradient(-130, 0, 130, 0)
g.addColorStop(0, 'rgba(255,255,255,0)')
g.addColorStop(0.5, 'rgba(255,255,255,0.45)')
g.addColorStop(1, 'rgba(255,255,255,0)')
ctx.fillStyle = g
ctx.fillRect(-130, -CH * 1.2, 260, CH * 2.4)
ctx.restore()
}
drawPill(ctx: CanvasRenderingContext2D, t: number) {
const T = this.T
const st = this.st
const p = easeInOut(prog(t, T.rev + 0.05, T.grow * 0.7))
const y = 38, h = 54
ctx.font = FONT.sc(700, 28)
ctx.textBaseline = 'middle'
ctx.textAlign = 'left'
if (p < 1 && this.spec.pillQ) {
const txt = this.spec.pillQ
const w = 48 + ctx.measureText(txt).width + 26
const x = (CW - w) / 2
ctx.globalAlpha = 1 - p
rrect(ctx, x, y, w, h, h / 2)
ctx.fillStyle = st.pillQ.bg
ctx.fill()
ctx.fillStyle = this.spec.accent
ctx.beginPath()
ctx.arc(x + 27, y + h / 2, 7, 0, Math.PI * 2)
ctx.fill()
if (Math.floor(t * 2.2) % 2 === 0 || t >= T.rev) {
ctx.globalAlpha = (1 - p) * 0.35
ctx.strokeStyle = this.spec.accent
ctx.lineWidth = 3
ctx.beginPath()
ctx.arc(x + 27, y + h / 2, 11, 0, Math.PI * 2)
ctx.stroke()
ctx.globalAlpha = 1 - p
}
ctx.fillStyle = st.pillQ.fg
ctx.fillText(txt, x + 48, y + h / 2 + 1)
ctx.globalAlpha = 1
}
if (p > 0 && this.spec.pillA) {
const txt = this.spec.pillA
const w = 54 + ctx.measureText(txt).width + 26
const s = 0.85 + 0.15 * easeOutBack(p)
ctx.save()
ctx.globalAlpha = p
ctx.translate(CW / 2, y + h / 2)
ctx.scale(s, s)
ctx.translate(-w / 2, -h / 2)
rrect(ctx, 0, 0, w, h, h / 2)
ctx.fillStyle = st.pillA.bg
ctx.fill()
ctx.strokeStyle = st.pillA.fg
ctx.lineWidth = 5
ctx.lineCap = 'round'
ctx.lineJoin = 'round'
ctx.beginPath()
ctx.moveTo(21, h / 2 + 1)
ctx.lineTo(27, h / 2 + 8)
ctx.lineTo(40, h / 2 - 8)
ctx.stroke()
ctx.fillStyle = st.pillA.fg
ctx.fillText(txt, 54, h / 2 + 1)
ctx.restore()
}
}
content(ctx: CanvasRenderingContext2D, t: number, fidx: number, _CH: number) {
const T = this.T
const st = this.st
const s = this.spec
const po = this.po
if (s.showPill) this.drawPill(ctx, t)
// question
const hp = easeInOut(prog(t, T.rev, T.grow * 0.62))
if (hp < 1) {
ctx.font = FONT.head(this.hsize)
ctx.textBaseline = 'middle'
ctx.textAlign = 'left'
const tw = ctx.measureText(this.qParts.map((p) => p.text).join('')).width
let x = (CW - tw) / 2
const y = 176 - po - 34 * hp
ctx.globalAlpha = 1 - hp
for (const part of this.qParts) {
ctx.fillStyle = part.key ? s.accent : st.ink
ctx.fillText(part.text, x, y)
x += ctx.measureText(part.text).width
}
ctx.globalAlpha = 1
}
// word
const rowY = 186 - po
const mk = easeOutCubic(prog(t, T.marker, 0.32))
if (mk > 0) {
const yb = rowY + this.wsize * 0.22
rrect(ctx, this.wordX0 - 14, yb - this.wsize * 0.26, (this.wordW + 28) * mk, this.wsize * 0.32, 12)
ctx.fillStyle = st.marker
ctx.fill()
}
ctx.textAlign = 'center'
ctx.textBaseline = 'alphabetic'
ctx.fillStyle = st.ink
let k = 0
;[...s.en].forEach((c, i) => {
if (c === ' ') return
const tOn = T.type0 + k * T.dt
const appear = prog(t, tOn, 0.16)
const kk = k
k++
if (appear <= 0) return
const pop = easeOutBack(appear, 2.6)
const size = this.wsize * (0.45 + 0.55 * pop)
const wave = Math.sin(clamp((t - T.done - kk * 0.03) / 0.4) * Math.PI) * 14
const base = rowY + this.wsize * 0.3 - wave - 26 * (1 - easeOutCubic(appear))
ctx.globalAlpha = Math.min(1, appear * 1.8)
ctx.font = FONT.word(size)
ctx.fillText(c, this.finalCx[i], base)
})
ctx.globalAlpha = 1
// IPA row with speaker icon
const ip = easeOutCubic(prog(t, T.ipa, 0.32))
if (ip > 0 && this.CHA > 300 - po) {
ctx.font = FONT.ipa(46)
ctx.textAlign = 'left'
ctx.textBaseline = 'middle'
const iw = s.ipa ? ctx.measureText(s.ipa).width : 0
const icon = 62
const x0 = (CW - (icon + (s.ipa ? 20 + iw : 0))) / 2
const y = 306 - po + 26 * (1 - ip)
const env = this.env[Math.min(fidx, this.env.length - 1)] ?? 0
const icx = x0 + icon / 2
ctx.globalAlpha = ip
if (env > 0.05) {
ctx.globalAlpha = 0.43 * env * ip
ctx.strokeStyle = s.accent
ctx.lineWidth = 3
ctx.beginPath()
ctx.arc(icx, y, icon / 2 + 6 + 16 * env, 0, Math.PI * 2)
ctx.stroke()
ctx.globalAlpha = ip
}
ctx.fillStyle = s.accent
ctx.beginPath()
ctx.arc(icx, y, (icon / 2) * (1 + 0.1 * env), 0, Math.PI * 2)
ctx.fill()
const bx = icx - 13
ctx.fillStyle = '#ffffff'
ctx.beginPath()
ctx.moveTo(bx, y - 6); ctx.lineTo(bx + 7, y - 6); ctx.lineTo(bx + 16, y - 14); ctx.lineTo(bx + 16, y + 14)
ctx.lineTo(bx + 7, y + 6); ctx.lineTo(bx, y + 6); ctx.closePath()
ctx.fill()
ctx.strokeStyle = '#ffffff'
ctx.lineWidth = 3
ctx.lineCap = 'butt'
;[9, 16].forEach((rad, k2) => {
ctx.globalAlpha = ip * (0.55 + 0.45 * clamp(env * 2.2 - k2 * 0.5))
ctx.beginPath()
ctx.arc(bx + 15, y, rad, (-48 * Math.PI) / 180, (48 * Math.PI) / 180)
ctx.stroke()
})
ctx.globalAlpha = ip
if (s.ipa) {
ctx.fillStyle = st.sub
ctx.fillText(s.ipa, x0 + icon + 20, y + 5)
}
ctx.globalAlpha = 1
}
// meaning row
const mp = easeOutCubic(prog(t, T.mean, 0.3))
if (mp > 0 && (s.zh || s.pos)) {
ctx.globalAlpha = mp
ctx.textBaseline = 'middle'
ctx.font = FONT.wordSemi(28)
const tagw = s.pos ? ctx.measureText(s.pos).width + 26 : 0
ctx.font = FONT.sc(700, 38)
const zw = ctx.measureText(s.zh).width
const x0 = (CW - (tagw + (s.pos ? 16 : 0) + zw)) / 2
const y = 394 - po + 20 * (1 - mp)
if (s.pos) {
rrect(ctx, x0, y - 21, tagw, 42, 10)
ctx.fillStyle = st.tag.bg
ctx.fill()
ctx.font = FONT.wordSemi(28)
ctx.textAlign = 'center'
ctx.fillStyle = st.tag.fg
ctx.fillText(s.pos, x0 + tagw / 2, y + 2)
}
ctx.font = FONT.sc(700, 38)
ctx.textAlign = 'left'
ctx.fillStyle = st.ink
ctx.fillText(s.zh, x0 + tagw + (s.pos ? 16 : 0), y + 1)
ctx.globalAlpha = 1
}
}
drawConfetti(ctx: CanvasRenderingContext2D, t: number, oy: number) {
const ct = t - (this.T.done + 0.04)
if (ct <= 0 || ct > 1.6) return
const ox = W / 2
for (const p of this.particles) {
const tt = ct - p.delay
if (tt <= 0) continue
const drag = (1 - Math.exp(-2.6 * tt)) / 2.6
const px = ox + p.x + p.vx * drag
const py = oy + p.y + p.vy * drag + 0.5 * 1500 * tt * tt * 0.55
const life = clamp(1 - (tt - 0.75) / 0.6)
if (life <= 0) continue
const s = p.sz
const rot = ((p.rot + p.vr * tt) * Math.PI) / 180
ctx.globalAlpha = life
ctx.fillStyle = p.col
ctx.save()
ctx.translate(px, py)
ctx.rotate(rot)
if (p.kind === 0) {
const flip = Math.abs(Math.cos(rot * 1.7)) * 0.8 + 0.2
ctx.fillRect(-s * 0.4 * flip, -s, s * 0.8 * flip, s * 2)
} else if (p.kind === 1) {
ctx.beginPath()
ctx.arc(0, 0, s * 0.5, 0, Math.PI * 2)
ctx.fill()
} else {
if (p.col === CONFETTI[1]) ctx.fillStyle = '#ffffff'
ctx.beginPath()
for (let k = 0; k < 8; k++) {
const rr = k % 2 === 0 ? s * 0.95 : s * 0.28
const a = (k * Math.PI) / 4
if (k === 0) ctx.moveTo(rr * Math.cos(a), rr * Math.sin(a))
else ctx.lineTo(rr * Math.cos(a), rr * Math.sin(a))
}
ctx.closePath()
ctx.fill()
}
ctx.restore()
}
ctx.globalAlpha = 1
}
}
|