import { ImageAsset, resources, SpriteFrame, Texture2D } from 'cc'; import { POSTER_FRAMES, PosterFrameDef } from '../core/AtelierService'; import { HOOP_CENTER, HOOP_HEIGHT, HOOP_RADIUS, HOOP_WIDTH, MOTIFS, NEST } from '../core/LevelData'; import { dyeRemainingUses, isClipUnlocked, isPondBridged, isStrandClipLocked, isStrandExposed, isTargetLocked } from '../core/RouteEngine'; import { AntState, ButtonAnchorDef, DyeVatDef, GameState, GateDef, HintState, LevelDef, OldClothClipDef, Point2D, PondDef, RoughZoneDef, RouteState, SectionDef, SpoolDef, TargetColor, UnspoolConfig, UnspoolRuntimeState, YarnColor, } from '../core/Types'; declare const wx: any; export const UNSPOOL_CLOTH_WIDTH = 560; export const UNSPOOL_CLOTH_HEIGHT = 136; const GOLD = ['#9f501b', '#d59328', '#f6bb4f', '#ffe698']; const HONEY = ['#9a4b1d', '#cb7724', '#ed9b39', '#ffd283']; const LEAF = ['#394a31', '#7d8b50', '#a8aa68', '#d0cd8a']; const ROSE = ['#8d3f59', '#c36a80', '#e797a5', '#ffdbcf']; const SKY = ['#32556b', '#5688a3', '#84b3cc', '#cbe4f2']; const CORAL = ['#8f3b26', '#ce6645', '#eb936d', '#ffd4bd']; const LAVENDER = ['#564373', '#8a72ab', '#b49ed1', '#e4d8f5']; const CREAM = ['#8c7d65', '#c8bba1', '#e8dfcd', '#fffbf0']; const COCOA = ['#4f301e', '#86573b', '#b5835f', '#dfc0a3']; const WHITE = ['#8a806f', '#cec7b8', '#efeae0', '#ffffff']; const BROWN = ['#4d2d22', '#875639', '#b37f52', '#d7aa74']; const SEEDS = ['#3e2a25', '#654235', '#936047', '#b7805a']; const RINGS: Record = { gold: '#98621e', green: '#516441', rose: '#934b65', sky: '#3c6b86', coral: '#a1482c', lavender: '#6a538b', cream: '#87775c', cocoa: '#6c442c', white: '#7b7363', }; const YARN_PALETTES: Record = { gold: GOLD, green: LEAF, rose: ROSE, sky: SKY, coral: CORAL, lavender: LAVENDER, cream: CREAM, cocoa: COCOA, white: WHITE, }; const ROUTE_COLORS: Record = { gold: '#dd9b35', green: '#81915c', rose: '#bd6381', sky: '#5c92b0', coral: '#d66d4a', lavender: '#9078b3', cream: '#cfc3a8', cocoa: '#966444', white: '#ddd7c7', }; const SERIF_FONT = '"Songti SC", "Noto Serif CJK SC", SimSun, serif'; const CX = HOOP_CENTER.x; const CY = HOOP_CENTER.y; const SIZE = HOOP_RADIUS; const TAU = Math.PI * 2; const BALL_SPRITE_SIZE = 72; const BALL_SPRITE_HALF = BALL_SPRITE_SIZE / 2; interface OffscreenLayer { canvas: any; ctx: CanvasRenderingContext2D; w: number; h: number; } interface OverlayEntry { canvas: any; x: number; y: number; w: number; h: number; } interface BakedArtwork { colorCanvas: any; sketchCanvas: any; subjectBox: { x: number; y: number; w: number; h: number }; } function createPlatformCanvas(width: number, height: number): any { if (typeof wx !== 'undefined' && typeof wx.createCanvas === 'function') { const c = wx.createCanvas(); c.width = width; c.height = height; return c; } const c = document.createElement('canvas'); c.width = width; c.height = height; return c; } function createLayer(w = HOOP_WIDTH, h = HOOP_HEIGHT, scale = 2): OffscreenLayer { const canvas = createPlatformCanvas(Math.ceil(w * scale), Math.ceil(h * scale)); const ctx = canvas.getContext('2d') as CanvasRenderingContext2D; ctx.scale(scale, scale); return { canvas, ctx, w, h }; } function rng(seed: number): () => number { return () => { seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0; return seed / 4294967296; }; } function ellipse(c: CanvasRenderingContext2D, x: number, y: number, rx: number, ry: number, angle = 0): void { c.beginPath(); c.ellipse(x, y, rx, ry, angle, 0, TAU); } function roundRectPath(c: CanvasRenderingContext2D, x: number, y: number, w: number, h: number, r: number): void { const radius = Math.min(r, w / 2, h / 2); c.beginPath(); c.moveTo(x + radius, y); c.arcTo(x + w, y, x + w, y + h, radius); c.arcTo(x + w, y + h, x, y + h, radius); c.arcTo(x, y + h, x, y, radius); c.arcTo(x, y, x + w, y, radius); c.closePath(); } function line(c: CanvasRenderingContext2D, x: number, y: number, tx: number, ty: number): void { c.beginPath(); c.moveTo(x, y); c.lineTo(tx, ty); c.stroke(); } function twine(c: CanvasRenderingContext2D, x: number, y: number, tx: number, ty: number, colors: string[], width: number, tint = 0): void { c.lineCap = 'round'; c.strokeStyle = colors[0]; c.lineWidth = width + 0.7; line(c, x, y + 0.6, tx, ty + 0.6); c.strokeStyle = colors[tint > 0.55 ? 2 : 1]; c.lineWidth = width; line(c, x, y, tx, ty); c.strokeStyle = colors[3]; c.lineWidth = Math.max(0.52, width * 0.23); line(c, x * 0.87 + tx * 0.13, y * 0.87 + ty * 0.13 - 0.65, tx * 0.88 + x * 0.12, ty * 0.88 + y * 0.12 - 0.65); } function mark(c: CanvasRenderingContext2D, color: YarnColor, x: number, y: number, r: number): void { c.beginPath(); if (color === 'green') { c.moveTo(x, y - r); c.quadraticCurveTo(x + r * 0.95, y, x, y + r); c.quadraticCurveTo(x - r * 0.95, y, x, y - r); } else if (color === 'sky') { // 4-point sparkle for (let i = 0; i < 8; i++) { const a = (i * Math.PI) / 4 - Math.PI / 2; const rad = i % 2 === 0 ? r : r * 0.42; const px = x + Math.cos(a) * rad; const py = y + Math.sin(a) * rad; if (i === 0) c.moveTo(px, py); else c.lineTo(px, py); } } else if (color === 'coral') { // cute heart c.moveTo(x, y + r * 0.82); c.bezierCurveTo(x - r * 1.15, y + r * 0.1, x - r * 0.95, y - r * 0.9, x, y - r * 0.25); c.bezierCurveTo(x + r * 0.95, y - r * 0.9, x + r * 1.15, y + r * 0.1, x, y + r * 0.82); } else if (color === 'cocoa') { // diamond c.moveTo(x, y - r); c.lineTo(x + r * 0.82, y); c.lineTo(x, y + r); c.lineTo(x - r * 0.82, y); } else if (color === 'cream') { // soft circle c.arc(x, y, r * 0.78, 0, TAU); } else { // 5-petal floret (gold / rose / lavender / white) const petals = color === 'lavender' ? 6 : 5; for (let i = 0; i <= 60; i++) { const a = (i / 60) * TAU; const t = r * (0.74 + 0.25 * Math.cos(a * petals)); const px = x + Math.cos(a) * t; const py = y + Math.sin(a) * t; if (i === 0) c.moveTo(px, py); else c.lineTo(px, py); } } c.closePath(); c.fill(); } function createLinenPattern(targetCtx: CanvasRenderingContext2D): CanvasPattern | null { const dim = 96; const tile = createPlatformCanvas(dim, dim); const tc = tile.getContext('2d') as CanvasRenderingContext2D; const rand = rng(349921); for (let i = 0; i < 420; i++) { const x = rand() * dim; const y = rand() * dim; const len = 1.8 + rand() * 5.8; tc.fillStyle = i % 3 === 0 ? '#a6834e24' : '#fffdf833'; if (i % 2) tc.fillRect(x, y, len, 0.6); else tc.fillRect(x, y, 0.6, len); } return targetCtx.createPattern(tile, 'repeat'); } function drawHoop(c: CanvasRenderingContext2D): void { const rand = rng(349921); c.save(); c.shadowColor = '#30301b9a'; c.shadowOffsetY = 16; c.shadowBlur = 23; ellipse(c, CX, CY, 275, 275); c.fillStyle = '#a87037'; c.fill(); c.restore(); const wood = c.createLinearGradient(72, 75, 515, 547); for (const [stop, color] of [[0, '#e4b476'], [0.22, '#d59c59'], [0.53, '#a76735'], [0.7, '#c78a4e'], [1, '#71512e']] as [number, string][]) { wood.addColorStop(stop, color); } ellipse(c, CX, CY, 275, 275); c.fillStyle = wood; c.fill(); c.lineWidth = 2.6; c.strokeStyle = '#644420'; c.stroke(); for (let i = 0; i < 31; i++) { c.beginPath(); const a0 = rand() * TAU; const r = 258 + rand() * 16; c.arc(CX, CY, r, a0, a0 + 0.25 + rand() * 4.6); c.strokeStyle = i % 2 ? '#fed79252' : '#6e3d233d'; c.lineWidth = 0.45 + rand() * 0.8; c.stroke(); } ellipse(c, CX, CY, 272, 272); c.strokeStyle = '#ffe3a473'; c.lineWidth = 1.45; c.stroke(); ellipse(c, CX, CY, 259, 259); c.strokeStyle = '#634225'; c.lineWidth = 3; c.stroke(); c.save(); ellipse(c, CX, CY, SIZE, SIZE); c.clip(); const cloth = c.createRadialGradient(265, 209, 24, CX, CY, 326); cloth.addColorStop(0, '#fff3d6'); cloth.addColorStop(0.65, '#f5e4c1'); cloth.addColorStop(1, '#cead78'); c.fillStyle = cloth; c.fillRect(0, 0, HOOP_WIDTH, HOOP_HEIGHT); const linen = createLinenPattern(c); if (linen) { c.fillStyle = linen; c.fillRect(0, 0, HOOP_WIDTH, HOOP_HEIGHT); } ellipse(c, CX, CY, 247, 247); c.setLineDash([2, 5]); c.strokeStyle = '#b0925f87'; c.lineWidth = 1; c.stroke(); c.restore(); c.save(); c.translate(CX, 26); c.shadowColor = '#48362269'; c.shadowBlur = 6; c.shadowOffsetY = 3; c.fillStyle = '#6b472a'; c.fillRect(-23, -11, 11, 22); c.fillRect(12, -11, 11, 22); c.shadowColor = 'transparent'; const metal = c.createLinearGradient(0, -10, 0, 8); metal.addColorStop(0, '#f5d385'); metal.addColorStop(0.54, '#b2833f'); metal.addColorStop(1, '#77572d'); c.fillStyle = metal; roundRectPath(c, -32, -6, 65, 12, 4); c.fill(); roundRectPath(c, 5, -10, 16, 20, 3); c.fill(); c.strokeStyle = '#ffe7ab97'; c.lineWidth = 1.2; for (let x = -26; x < 4; x += 4) line(c, x, -3, x + 3, 3); c.restore(); } function drawPetal(c: CanvasRenderingContext2D, angle: number, variation: number, stitched: boolean, offset: number): void { const colors = variation % 3 ? GOLD : HONEY; const reach = 105 + ((variation * 11) % 21); c.save(); c.translate(300, 205); c.rotate(angle); c.beginPath(); c.moveTo(34, -5); c.bezierCurveTo(57, -29, reach - 13, -23, reach, 0); c.bezierCurveTo(reach - 13, 23, 57, 29, 34, 5); c.closePath(); if (stitched) { c.fillStyle = variation % 3 ? '#c98631' : '#ba702b'; c.fill(); c.clip(); const rand = rng(607 + variation * 5); for (let y = -25; y <= 25; y += 2.7) { for (let x = 35; x < reach + 13; x += 19) { const bend = Math.sin(y * 0.17) * 1.6; twine(c, x + rand() * 4 + offset, y, x + offset + 22, y + bend + rand() * 2 - 1, colors, 2.65, rand()); } } } else { c.fillStyle = '#e4b8551b'; c.fill(); c.strokeStyle = '#a277438e'; c.lineWidth = 1.35; c.setLineDash([3.5, 4.3]); c.stroke(); } c.restore(); } function drawLeafShape(c: CanvasRenderingContext2D, side: number, done: boolean): void { c.save(); c.translate(side < 0 ? 305 : 315, side < 0 ? 375 : 346); c.scale(side, 1); c.beginPath(); c.moveTo(0, 0); c.bezierCurveTo(25, -42, 37, -56, 87, -54); c.bezierCurveTo(78, -24, 54, 5, 0, 0); if (done) { c.fillStyle = '#5e7349'; c.fill(); c.save(); c.clip(); const rand = rng(843); for (let x = -30; x <= 92; x += 3.6) { twine(c, x, 8, x + 41, -62, LEAF, 3.4, rand()); } twine(c, 3, -2, 80, -50, LEAF, 2.65, 0.8); c.restore(); } else { c.fillStyle = '#78855420'; c.fill(); c.strokeStyle = '#63734f9c'; c.lineWidth = 1.6; c.setLineDash([4, 5]); c.stroke(); } c.restore(); } function drawFlowerBase(c: CanvasRenderingContext2D): void { const rand = rng(49); const stemX = (y: number) => { const t = (y - 231) / 206; return 301 + 18 * Math.sin(t * Math.PI * 1.2) + 19 * t; }; c.save(); c.beginPath(); c.moveTo(301, 231); for (let y = 233; y <= 437; y += 2) c.lineTo(stemX(y), y); c.strokeStyle = '#4d6240'; c.lineWidth = 14; c.lineCap = 'round'; c.stroke(); for (let y = 238; y < 436; y += 2.9) { const x = stemX(y); twine(c, x - 5.4, y + 1.6, x + 5.4, y - 1.3, LEAF, 3, rand()); } for (const i of [5, 6, 7]) drawPetal(c, (i * TAU) / 12 - Math.PI / 2, i, true, 0); c.restore(); } function drawFlowerHeart(c: CanvasRenderingContext2D): void { const rand = rng(49); c.save(); ellipse(c, 300, 205, 48, 47); c.fillStyle = '#6e4a32'; c.fill(); c.strokeStyle = '#9b6432'; c.lineWidth = 4.5; c.stroke(); for (let i = 0; i < 365; i++) { const a = 2.39996 * i; const r = 2.27 * Math.sqrt(i); const x = 300 + Math.cos(a) * r; const y = 205 + Math.sin(a) * r; twine(c, x, y, x + 1.8 + rand(), y - 1.6, rand() > 0.28 ? BROWN : SEEDS, 1.65 + rand(), rand()); } c.restore(); } function drawLotusPetal(c: CanvasRenderingContext2D, angle: number, reach: number, width: number, stitched: boolean, shade: string): void { c.save(); c.translate(0, 23); c.rotate(angle); c.beginPath(); c.moveTo(0, 4); c.bezierCurveTo(-width, -reach * 0.24, -width * 0.88, -reach * 0.62, 0, -reach); c.bezierCurveTo(width * 0.88, -reach * 0.66, width, -reach * 0.22, 0, 4); c.closePath(); if (stitched) { c.fillStyle = shade; c.fill(); c.clip(); const rand = rng(732 + reach); for (let x = -width; x <= width; x += 3.1) { for (let y = 4; y >= -reach; y -= 17) { twine( c, x * (1 + (y / reach) * 0.6), y, x * (1 + ((y - 19) / reach) * 0.6), y - 19, ROSE, 2.55, rand(), ); } } } else { c.fillStyle = '#cd879626'; c.fill(); c.strokeStyle = '#985b6c9e'; c.lineWidth = 1.35; c.setLineDash([3.4, 4]); c.stroke(); } c.restore(); } function drawLotusPad(c: CanvasRenderingContext2D, x: number, y: number, scale: number, angle: number, stitched = true): void { c.save(); c.translate(x, y); c.rotate(angle); c.scale(scale, scale); c.beginPath(); c.moveTo(1, 0); c.lineTo(59, -12); c.bezierCurveTo(43, -42, -45, -40, -64, -12); c.bezierCurveTo(-83, 19, -32, 40, 13, 33); c.bezierCurveTo(36, 30, 52, 21, 59, 11); c.closePath(); if (stitched) { c.fillStyle = '#57765b'; c.fill(); c.strokeStyle = '#3e6048'; c.lineWidth = 1.8; c.stroke(); c.clip(); const rand = rng(146); for (let px = -104; px < 80; px += 3.7) { for (let py = -36; py <= 36; py += 16) { twine(c, px + py * 0.6, py, px + (py + 18) * 0.6, py + 18, LEAF, 2.7, rand()); } } for (const [tx, ty] of [[-58, -9], [-35, -27], [13, -31], [-42, 21], [3, 28], [39, 19]]) { twine(c, 1, 0, tx, ty, LEAF, 1.6, 0.7); } } else { c.fillStyle = '#6f876026'; c.fill(); c.strokeStyle = '#536c48a6'; c.lineWidth = 1.4; c.setLineDash([3.5, 4]); c.stroke(); } c.restore(); } function drawLotusSection(c: CanvasRenderingContext2D, section: SectionDef, stitched: boolean): void { if (section.lotus === 'pad-left') { drawLotusPad(c, 206, 319, 0.82, 2.88, stitched); return; } if (section.lotus === 'pad-right') { drawLotusPad(c, 377, 304, 1.04, -0.26, stitched); return; } c.save(); c.translate(section.x, section.y); const petals: [number, number, number][] = section.lotus === 'bud' ? [[-0.36, 68, 25], [0.38, 67, 24], [0.04, 85, 27]] : [[-1.08, 78, 25], [1.08, 78, 25], [-0.52, 86, 26], [0.55, 84, 25], [-0.05, 97, 24], [-0.79, 58, 25], [0.82, 58, 25], [0, 62, 26]]; petals.forEach(([angle, reach, width], index) => { drawLotusPetal(c, angle, reach, width, stitched, index % 2 ? '#b65974' : '#a95670'); }); c.restore(); } function drawLotusBase(c: CanvasRenderingContext2D): void { c.save(); for (const [x, y, endX, endY] of [[221, 236, 276, 408], [369, 215, 337, 396]]) { const stem = () => { c.beginPath(); c.moveTo(x, y); c.bezierCurveTo(x + 12, y + 68, endX - 19, endY - 31, endX, endY); }; c.lineCap = 'round'; stem(); c.strokeStyle = '#45674a'; c.lineWidth = 8; c.stroke(); stem(); c.strokeStyle = '#98a36e'; c.lineWidth = 4; c.stroke(); c.setLineDash([1, 4]); stem(); c.strokeStyle = '#d6d9a1'; c.lineWidth = 5; c.stroke(); c.setLineDash([]); } c.lineWidth = 1.8; c.strokeStyle = '#759186aa'; c.setLineDash([6, 3]); for (const [x, y, width] of [[254, 352, 25], [394, 350, 28], [272, 408, 43], [330, 433, 28], [168, 287, 16]]) { c.beginPath(); c.moveTo(x - width, y); c.quadraticCurveTo(x, y + 5, x + width, y - 1); c.stroke(); } c.restore(); } function drawSakuraBase(c: CanvasRenderingContext2D): void { c.save(); c.lineCap = 'round'; // 主樱枝 const mainBranch = () => { c.beginPath(); c.moveTo(96, 304); c.bezierCurveTo(185, 294, 298, 284, 378, 240); c.bezierCurveTo(424, 214, 464, 186, 500, 174); }; mainBranch(); c.strokeStyle = '#5a3a2d'; c.lineWidth = 13; c.stroke(); mainBranch(); c.strokeStyle = '#8b6049'; c.lineWidth = 7.5; c.stroke(); c.setLineDash([2, 6]); mainBranch(); c.strokeStyle = '#c89b79'; c.lineWidth = 3.2; c.stroke(); c.setLineDash([]); // 分枝 const twigs: [number, number, number, number, number, number][] = [ [226, 284, 255, 215, 304, 158], [166, 296, 178, 268, 190, 244], [356, 252, 372, 274, 396, 294], [394, 228, 412, 216, 432, 212], ]; for (const [x0, y0, cx1, cy1, x1, y1] of twigs) { c.beginPath(); c.moveTo(x0, y0); c.quadraticCurveTo(cx1, cy1, x1, y1); c.strokeStyle = '#5e3d2f'; c.lineWidth = 6.5; c.stroke(); c.strokeStyle = '#976951'; c.lineWidth = 3.2; c.stroke(); } // 飘落的小樱花瓣点缀 for (const [px, py, ang] of [[148, 190, 0.5], [248, 128, -0.4], [442, 288, 0.8], [462, 146, -0.7]]) { c.save(); c.translate(px, py); c.rotate(ang); ellipse(c, 0, 0, 7, 3.6); c.fillStyle = '#e7a4b388'; c.fill(); c.restore(); } c.restore(); } function drawSakuraBlossom(c: CanvasRenderingContext2D, x: number, y: number, r: number, stitched: boolean, seed: number): void { const rand = rng(seed); c.save(); c.translate(x, y); for (let i = 0; i < 5; i++) { const a = (i * TAU) / 5 - Math.PI / 2; c.save(); c.rotate(a); c.beginPath(); c.moveTo(0, -4); c.bezierCurveTo(-r * 0.56, -r * 0.42, -r * 0.52, -r * 0.98, -r * 0.1, -r); c.lineTo(0, -r * 0.84); c.lineTo(r * 0.1, -r); c.bezierCurveTo(r * 0.52, -r * 0.98, r * 0.56, -r * 0.42, 0, -4); c.closePath(); if (stitched) { c.fillStyle = i % 2 ? '#d9899c' : '#e69dad'; c.fill(); c.strokeStyle = '#9a4d64'; c.lineWidth = 1.4; c.stroke(); c.clip(); for (let t = -r * 0.48; t <= r * 0.48; t += 4.2) { twine(c, t * 0.35, -6, t, -r * 0.96, ROSE, 2.8, rand()); } } else { c.fillStyle = '#e49aa924'; c.fill(); c.strokeStyle = '#9b586aa0'; c.lineWidth = 1.3; c.setLineDash([3.2, 3.8]); c.stroke(); } c.restore(); } // 花蕊 ellipse(c, 0, 0, stitched ? 9 : 7, stitched ? 9 : 7); c.fillStyle = stitched ? '#e8b34a' : '#d6a45244'; c.fill(); if (stitched) { c.strokeStyle = '#9a5f1f'; c.lineWidth = 1.2; c.stroke(); } c.restore(); } function drawSakuraBird(c: CanvasRenderingContext2D, x: number, y: number, stitched: boolean): void { const rand = rng(309); c.save(); c.translate(x, y); // 鸟身轮廓 c.beginPath(); c.moveTo(-42, 16); c.quadraticCurveTo(-24, -18, 6, -24); c.bezierCurveTo(26, -28, 40, -14, 34, 4); c.bezierCurveTo(28, 22, -4, 24, -22, 16); c.lineTo(-48, 24); c.closePath(); if (stitched) { c.fillStyle = '#d89a35'; c.fill(); c.strokeStyle = '#8b4e1b'; c.lineWidth = 1.8; c.stroke(); c.save(); c.clip(); for (let px = -46; px <= 38; px += 4.5) { twine(c, px, -26, px - 10, 26, GOLD, 2.9, rand()); } c.restore(); // 翅膀 c.beginPath(); c.ellipse(-6, 2, 20, 10, -0.22, 0, TAU); c.fillStyle = '#b97122'; c.fill(); c.strokeStyle = '#ffe59c'; c.lineWidth = 1.3; c.stroke(); // 嘴与眼睛 c.beginPath(); c.moveTo(32, -8); c.lineTo(44, -4); c.lineTo(32, 0); c.closePath(); c.fillStyle = '#a14b21'; c.fill(); ellipse(c, 22, -10, 3.2, 3.2); c.fillStyle = '#33221a'; c.fill(); ellipse(c, 23, -11, 1.1, 1.1); c.fillStyle = '#fff9e8'; c.fill(); } else { c.fillStyle = '#dfa43e26'; c.fill(); c.strokeStyle = '#9a6522a0'; c.lineWidth = 1.4; c.setLineDash([3.5, 4]); c.stroke(); } c.restore(); } function drawSakuraLeaves(c: CanvasRenderingContext2D, x: number, y: number, stitched: boolean): void { const rand = rng(417); c.save(); c.translate(x, y); for (const [ox, oy, ang, len] of [[-12, -4, 0.55, 42], [14, 6, 1.12, 38]] as [number, number, number, number][]) { c.save(); c.translate(ox, oy); c.rotate(ang); c.beginPath(); c.moveTo(0, 0); c.quadraticCurveTo(len * 0.45, -18, len, 0); c.quadraticCurveTo(len * 0.45, 18, 0, 0); c.closePath(); if (stitched) { c.fillStyle = '#6c7c48'; c.fill(); c.strokeStyle = '#3f5032'; c.lineWidth = 1.4; c.stroke(); c.clip(); for (let px = 2; px < len; px += 4.5) { twine(c, px, -16, px + 6, 16, LEAF, 2.4, rand()); } twine(c, 2, 0, len - 4, 0, LEAF, 1.5, 0.8); } else { c.fillStyle = '#6f875026'; c.fill(); c.strokeStyle = '#536c42a0'; c.lineWidth = 1.3; c.setLineDash([3.2, 3.8]); c.stroke(); } c.restore(); } c.restore(); } function drawSakuraSection(c: CanvasRenderingContext2D, section: SectionDef, stitched: boolean): void { if (section.sakura === 'bird') { drawSakuraBird(c, section.x, section.y, stitched); return; } if (section.sakura === 'leaf') { drawSakuraLeaves(c, section.x, section.y, stitched); return; } const r = section.sakura === 'top' ? 42 : 38; const seed = section.sakura === 'left' ? 511 : section.sakura === 'top' ? 523 : 541; drawSakuraBlossom(c, section.x, section.y, r, stitched, seed); } // ========================================================= // 第四章绣画:百花绣屏(8 处独立针位,适合 6~8 球高难组合解谜) // ========================================================= function drawGardenBase(c: CanvasRenderingContext2D): void { c.save(); c.lineCap = 'round'; c.lineJoin = 'round'; // 对称花藤底绣 const vines: [number, number, number, number, number, number][] = [ [300, 344, 240, 290, 176, 174], [300, 344, 360, 290, 424, 174], [300, 344, 298, 250, 300, 124], [255, 296, 206, 304, 168, 294], [345, 296, 394, 304, 432, 294], [176, 174, 232, 134, 300, 124], [424, 174, 368, 134, 300, 124], ]; for (const [x0, y0, cx1, cy1, x1, y1] of vines) { c.beginPath(); c.moveTo(x0, y0); c.quadraticCurveTo(cx1, cy1, x1, y1); c.strokeStyle = '#53624090'; c.lineWidth = 5.5; c.stroke(); c.strokeStyle = '#8b9963b8'; c.lineWidth = 2.6; c.stroke(); } // 细小装饰芽点 for (const [bx, by] of [[248, 152], [352, 152], [196, 248], [404, 248], [268, 286], [332, 286]]) { ellipse(c, bx, by, 4.5, 4.5); c.fillStyle = '#c89b5a66'; c.fill(); } c.restore(); } function drawGardenGoldFloret(c: CanvasRenderingContext2D, x: number, y: number, r: number, stitched: boolean, seed: number): void { const rand = rng(seed); c.save(); c.translate(x, y); const petals = 8; for (let i = 0; i < petals; i++) { const a = (i * TAU) / petals; c.save(); c.rotate(a); c.beginPath(); c.moveTo(0, -5); c.quadraticCurveTo(-r * 0.34, -r * 0.56, 0, -r); c.quadraticCurveTo(r * 0.34, -r * 0.56, 0, -5); c.closePath(); if (stitched) { c.fillStyle = i % 2 ? '#e2a538' : '#f1be54'; c.fill(); c.strokeStyle = '#995c1e'; c.lineWidth = 1.2; c.stroke(); c.clip(); for (let t = -r * 0.28; t <= r * 0.28; t += 3.8) { twine(c, t * 0.3, -6, t, -r * 0.94, GOLD, 2.5, rand()); } } else { c.fillStyle = '#dfa43e22'; c.fill(); c.strokeStyle = '#9b682295'; c.lineWidth = 1.25; c.setLineDash([3, 3.5]); c.stroke(); } c.restore(); } ellipse(c, 0, 0, stitched ? 9.5 : 7, stitched ? 9.5 : 7); c.fillStyle = stitched ? '#7c4825' : '#94613444'; c.fill(); if (stitched) { c.strokeStyle = '#ffe59c'; c.lineWidth = 1.2; c.stroke(); } c.restore(); } function drawGardenRosePeony(c: CanvasRenderingContext2D, x: number, y: number, r: number, stitched: boolean, seed: number): void { const rand = rng(seed); c.save(); c.translate(x, y); for (let i = 0; i < 6; i++) { const a = (i * TAU) / 6 - Math.PI / 6; c.save(); c.rotate(a); c.beginPath(); c.moveTo(0, -4); c.bezierCurveTo(-r * 0.52, -r * 0.4, -r * 0.48, -r * 0.95, 0, -r); c.bezierCurveTo(r * 0.48, -r * 0.95, r * 0.52, -r * 0.4, 0, -4); c.closePath(); if (stitched) { c.fillStyle = i % 2 ? '#cc7187' : '#e0899d'; c.fill(); c.strokeStyle = '#8b4059'; c.lineWidth = 1.3; c.stroke(); c.clip(); for (let t = -r * 0.42; t <= r * 0.42; t += 4) { twine(c, t * 0.32, -5, t, -r * 0.94, ROSE, 2.7, rand()); } } else { c.fillStyle = '#d9889a22'; c.fill(); c.strokeStyle = '#974f6395'; c.lineWidth = 1.25; c.setLineDash([3, 3.5]); c.stroke(); } c.restore(); } ellipse(c, 0, 0, stitched ? 10 : 7.5, stitched ? 10 : 7.5); c.fillStyle = stitched ? '#f1c25a' : '#d6a45244'; c.fill(); if (stitched) { c.strokeStyle = '#9a5f1f'; c.lineWidth = 1.2; c.stroke(); } c.restore(); } function drawGardenLeafCluster(c: CanvasRenderingContext2D, x: number, y: number, flip: number, stitched: boolean): void { const rand = rng(flip > 0 ? 733 : 719); c.save(); c.translate(x, y); c.scale(flip, 1); for (const [ox, oy, ang, len] of [[-18, 4, -0.32, 40], [-8, -6, 0.25, 36]] as [number, number, number, number][]) { c.save(); c.translate(ox, oy); c.rotate(ang); c.beginPath(); c.moveTo(0, 0); c.quadraticCurveTo(len * 0.48, -16, len, 0); c.quadraticCurveTo(len * 0.48, 16, 0, 0); c.closePath(); if (stitched) { c.fillStyle = '#687a46'; c.fill(); c.strokeStyle = '#3c4e2e'; c.lineWidth = 1.35; c.stroke(); c.clip(); for (let px = 2; px < len; px += 4.2) { twine(c, px, -14, px + 5, 14, LEAF, 2.4, rand()); } twine(c, 2, 0, len - 3, 0, LEAF, 1.4, 0.8); } else { c.fillStyle = '#6d844c26'; c.fill(); c.strokeStyle = '#4e673ea0'; c.lineWidth = 1.3; c.setLineDash([3, 3.6]); c.stroke(); } c.restore(); } c.restore(); } function drawGardenSection(c: CanvasRenderingContext2D, section: SectionDef, stitched: boolean): void { const seed = (section.x * 7 + section.y * 13) | 0; if (section.garden === 'gold-floret') { drawGardenGoldFloret(c, section.x, section.y, 33, stitched, seed); } else if (section.garden === 'rose-peony') { drawGardenRosePeony(c, section.x, section.y, 36, stitched, seed); } else if (section.garden === 'leaf-l') { drawGardenLeafCluster(c, section.x, section.y, -1, stitched); } else { drawGardenLeafCluster(c, section.x, section.y, 1, stitched); } } function drawSurround(c: CanvasRenderingContext2D): void { const rand = rng(166); c.save(); for (let i = 0; i < 220; i++) { const a = rand() * TAU; const r = 140 + rand() * 95; const x = CX + Math.cos(a) * r; const y = CY + Math.sin(a) * r; if (x > 119 && x < 480 && y > 111 && y < 548) continue; const len = 1 + rand() * 3; c.strokeStyle = i % 3 ? '#917b4930' : '#fff8cdaf'; c.lineWidth = 1; line(c, x, y, x + len, y - 1.8); } c.restore(); } function drawPond(c: CanvasRenderingContext2D, pond: PondDef, bridged = false): void { c.save(); c.translate(pond.x, pond.y); if (bridged) { ellipse(c, 0, 0, pond.rx, pond.ry); c.fillStyle = '#89a36f38'; c.fill(); c.strokeStyle = '#53723eaa'; c.lineWidth = 1.8; c.setLineDash([5, 4]); c.stroke(); c.setLineDash([]); c.fillStyle = '#3f5a2d'; c.font = 'bold 12px sans-serif'; c.textAlign = 'center'; c.textBaseline = 'middle'; c.fillText('已搭桥', 0, 0); c.restore(); return; } c.scale(pond.rx / 73, pond.ry / 30); const water = c.createLinearGradient(0, -23, 0, 24); water.addColorStop(0, '#9aaea0'); water.addColorStop(1, '#cbd0b0'); ellipse(c, 0, 0, 73, 30); c.fillStyle = water; c.fill(); c.strokeStyle = pond.bridgeSectionId ? '#a1742c' : '#667b66'; c.lineWidth = pond.bridgeSectionId ? 2 : 1.4; c.stroke(); c.setLineDash([2, 4]); ellipse(c, 0, 0, 68, 25); c.strokeStyle = '#edf0cef0'; c.lineWidth = 1.4; c.stroke(); c.setLineDash([]); for (const [x, y, r] of [[-36, 5, 13], [-3, -10, 18], [33, 7, 15]]) { c.beginPath(); c.ellipse(x, y, r, 3, -0.11, 0.2, 2.9); c.strokeStyle = '#e5edca'; c.lineWidth = 1.3; c.stroke(); } c.restore(); if (pond.bridgeSectionId) { c.save(); c.fillStyle = '#564222'; c.font = 'bold 12px sans-serif'; c.textAlign = 'center'; c.textBaseline = 'middle'; c.fillText('绣此搭桥', pond.x, pond.y + pond.ry * 0.45); c.restore(); } } function drawRoughZone(c: CanvasRenderingContext2D, zone: RoughZoneDef): void { c.save(); c.translate(zone.x, zone.y); ellipse(c, 0, 0, zone.rx, zone.ry); c.fillStyle = '#c5ae8066'; c.fill(); c.save(); c.clip(); c.strokeStyle = '#8d6c3d55'; c.lineWidth = 1.4; const span = Math.max(zone.rx, zone.ry) + 12; for (let d = -span * 2; d <= span * 2; d += 9) { line(c, d, -span, d + span, span); line(c, d, span, d + span, -span); } c.restore(); ellipse(c, 0, 0, zone.rx, zone.ry); c.strokeStyle = '#8b6838aa'; c.lineWidth = 1.8; c.setLineDash([4, 4]); c.stroke(); c.setLineDash([]); c.fillStyle = '#6d4c24'; c.font = 'bold 13px sans-serif'; c.textAlign = 'center'; c.textBaseline = 'middle'; c.fillText(`粗布 ×${(zone.factor ?? 1.8).toFixed(1)}`, 0, 0); c.restore(); } function drawSpool(c: CanvasRenderingContext2D, spool: SpoolDef, time: number): void { c.save(); c.translate(spool.x, spool.y); const pulse = 1 + Math.sin(time / 320) * 0.04; c.scale(pulse, pulse); ellipse(c, 0, 14, 18, 6); c.fillStyle = '#4c381f30'; c.fill(); // 木轴上下端 roundRectPath(c, -15, -16, 30, 7, 3); c.fillStyle = '#a96e3b'; c.fill(); c.strokeStyle = '#633c1e'; c.lineWidth = 1.2; c.stroke(); roundRectPath(c, -15, 9, 30, 7, 3); c.fillStyle = '#975e2f'; c.fill(); c.stroke(); // 绕线体 roundRectPath(c, -11, -9, 22, 18, 3); c.fillStyle = '#f6dc9b'; c.fill(); c.strokeStyle = '#b47f30'; c.lineWidth = 1.2; c.stroke(); for (let y = -5; y <= 6; y += 3.5) { c.strokeStyle = '#d49d3b'; c.lineWidth = 1.2; line(c, -10, y, 10, y - 1); } // 补给标签 c.fillStyle = '#5c4119'; c.font = 'bold 12px sans-serif'; c.textAlign = 'center'; c.fillText(`+${(spool.bonus / 100).toFixed(1)}`, 0, 27); c.restore(); } function drawButtonAnchor(c: CanvasRenderingContext2D, button: ButtonAnchorDef, threaded: boolean, time: number): void { c.save(); c.translate(button.x, button.y); ellipse(c, 0, 4, 20, 18); c.fillStyle = '#3d2c1932'; c.fill(); if (!threaded) { const r = 24 + Math.sin(time / 280) * 1.5; ellipse(c, 0, 0, r, r); c.strokeStyle = '#9b6a2c99'; c.lineWidth = 1.6; c.setLineDash([3, 4]); c.stroke(); c.setLineDash([]); } ellipse(c, 0, 0, 18, 18); c.fillStyle = threaded ? '#d9e3c4' : '#f4e4c2'; c.fill(); c.strokeStyle = threaded ? '#4f6d3c' : '#8b5f32'; c.lineWidth = 2.2; c.stroke(); ellipse(c, 0, 0, 13, 13); c.strokeStyle = threaded ? '#6e8c5788' : '#b88b5588'; c.lineWidth = 1.2; c.stroke(); const holes: [number, number][] = [[-4.5, -4.5], [4.5, -4.5], [-4.5, 4.5], [4.5, 4.5]]; for (const [hx, hy] of holes) { ellipse(c, hx, hy, 2.2, 2.2); c.fillStyle = '#5e4024'; c.fill(); } if (threaded) { c.strokeStyle = '#4e6c3a'; c.lineWidth = 2.2; line(c, -4.5, -4.5, 4.5, 4.5); line(c, 4.5, -4.5, -4.5, 4.5); } c.restore(); } function drawDyeVat(c: CanvasRenderingContext2D, dye: DyeVatDef, time: number, remaining = Infinity): void { const dry = remaining <= 0; c.save(); c.translate(dye.x, dye.y); ellipse(c, 0, 7, 26, 17); c.fillStyle = '#382a1a30'; c.fill(); // 陶碗外沿 ellipse(c, 0, 0, 24, 22); c.fillStyle = dry ? '#cfc6b6' : '#d9ccb2'; c.fill(); c.strokeStyle = dye.override && !dry ? '#9c3e26' : '#735b3d'; c.lineWidth = dye.override && !dry ? 2.5 : 2; c.stroke(); // 碗内染液 const pal = YARN_PALETTES[dye.color]; ellipse(c, 0, 0, 18.5, 16.5); c.fillStyle = dry ? '#b8b0a2' : pal[1]; c.fill(); if (!dry) { c.beginPath(); c.arc(0, 0, 9 + Math.sin(time / 360) * 1.4, -0.5, 2.2); c.strokeStyle = pal[3]; c.lineWidth = 1.8; c.stroke(); } c.fillStyle = dry ? '#5d564b' : '#fff9ea'; c.font = 'bold 13px sans-serif'; c.textAlign = 'center'; c.textBaseline = 'middle'; const label = dry ? '空' : (dye.uses !== undefined ? `染×${remaining}` : (dye.override ? '覆染' : '染')); c.fillText(label, 0, 0); c.restore(); } function drawGate(c: CanvasRenderingContext2D, gate: GateDef, passed: boolean, time: number): void { c.save(); c.translate(gate.x, gate.y); c.rotate(gate.angle); const pulse = passed ? 0 : Math.sin(time / 240) * 1.6; // 针眼金属环(垂直于行进方向) ellipse(c, 0, 0, 9, 22); c.fillStyle = passed ? '#dce8c7aa' : '#fff5d8dd'; c.fill(); c.strokeStyle = passed ? '#4f6d3c' : '#a86b24'; c.lineWidth = 2.6; c.stroke(); // 方向箭头(指向 +X 即 gate.angle) c.beginPath(); c.moveTo(-12 + pulse, 0); c.lineTo(14 + pulse, 0); c.moveTo(6 + pulse, -7); c.lineTo(15 + pulse, 0); c.lineTo(6 + pulse, 7); c.strokeStyle = passed ? '#3f5d2f' : '#9c4e18'; c.lineWidth = 2.8; c.lineCap = 'round'; c.lineJoin = 'round'; c.stroke(); c.restore(); } function drawNest(c: CanvasRenderingContext2D, pos: Point2D, time: number, calm: boolean, selected = false, multi = false): void { c.save(); c.translate(pos.x, pos.y); // 呼吸引导光环:让玩家一眼锁定起点「从蚁巢开始拖线」 if (calm || (multi && selected)) { for (let k = 0; k < 2; k++) { const phase = ((time / 1450) + k * 0.5) % 1; const rx = 28 + phase * 32; const ry = 16 + phase * 22; const alpha = (1 - phase) * (calm ? 0.65 : 0.42); ellipse(c, 0, 2, rx, ry); c.strokeStyle = `rgba(226, 158, 42, ${alpha.toFixed(3)})`; c.lineWidth = 2.6 - phase * 1.4; c.stroke(); } } ellipse(c, 0, 10, 33, 10); c.fillStyle = '#6b51252d'; c.fill(); const grad = c.createLinearGradient(0, -12, 0, 14); grad.addColorStop(0, '#d8a66a'); grad.addColorStop(1, '#855633'); ellipse(c, 0, 2, 27, 14); c.fillStyle = grad; c.fill(); c.strokeStyle = calm ? '#ffdb78' : (selected && multi ? '#fff6dc' : '#6e482c'); c.lineWidth = calm ? 2.6 : (selected && multi ? 2.4 : 1.4); c.stroke(); for (let i = 0; i < 8; i++) { const x = (i - 3.5) * 5.6; c.strokeStyle = i % 2 ? '#f0bc817e' : '#66452673'; c.lineWidth = 1.3; line(c, x, -6 + Math.abs(x) * 0.16, x - 4, 7); } ellipse(c, 0, 3, 10, 5.4); c.fillStyle = '#473224'; c.fill(); c.strokeStyle = '#d9a458'; c.stroke(); // 清晰可辨的起点胶囊标牌 const tagText = calm ? '按这里拖线' : '蚁巢'; const pillW = calm ? 84 : 46; const pillH = 22; c.save(); roundRectPath(c, -pillW / 2, 18, pillW, pillH, 11); c.fillStyle = calm ? '#4a2f18eb' : '#5e4328cc'; c.fill(); c.strokeStyle = calm ? '#f2c567' : '#d5b07488'; c.lineWidth = calm ? 1.6 : 1; c.stroke(); c.fillStyle = '#fff6dc'; c.font = calm ? 'bold 12px sans-serif' : '600 12px sans-serif'; c.textAlign = 'center'; c.textBaseline = 'middle'; c.fillText(tagText, 0, 29.5); c.restore(); c.restore(); } const AUTO_GUIDE_LEVEL_IDS = new Set(['first-stitch', 'double-yarn', 'cross-dual-intro']); /** 首次落笔前的轻盈手势起手引导:仅前 3 关新手教学自动展示,第 4 关起保留独立选巢/起手思考空间 */ function drawStartDragGuide(c: CanvasRenderingContext2D, level: LevelDef, time: number): void { if (!AUTO_GUIDE_LEVEL_IDS.has(level.id)) return; const firstDemo = level.demo[0]; if (!firstDemo || firstDemo.length < 2) return; const isIntro = level.id === 'first-stitch'; const pts = (isIntro ? firstDemo : firstDemo.slice(0, 2)).map(([x, y]) => ({ x, y })); // 计算折线总长与各段长度 const segLens: number[] = []; let totalLen = 0; for (let i = 1; i < pts.length; i++) { const d = Math.hypot(pts[i].x - pts[i - 1].x, pts[i].y - pts[i - 1].y); segLens.push(d); totalLen += d; } if (totalLen < 12) return; c.save(); // 1. 流动金色虚线起手轨迹 c.beginPath(); c.moveTo(pts[0].x, pts[0].y); for (let i = 1; i < pts.length; i++) c.lineTo(pts[i].x, pts[i].y); c.strokeStyle = 'rgba(218, 146, 36, 0.58)'; c.lineWidth = 4; c.lineCap = 'round'; c.lineJoin = 'round'; c.setLineDash([8, 9]); c.lineDashOffset = -(time / 36) % 17; c.stroke(); c.setLineDash([]); // 2. 循环滑行的手势引导光点(起手按压停顿 -> 平滑拖向毛线球 -> 轻柔淡出) const cycle = (time % 2100) / 2100; let progress = 0; let alpha = 1; let pressScale = 1; if (cycle < 0.16) { progress = 0; alpha = Math.min(1, cycle / 0.1); pressScale = 1.18 - (cycle / 0.16) * 0.24; } else if (cycle < 0.84) { const u = (cycle - 0.16) / 0.68; progress = u < 0.5 ? 2 * u * u : 1 - Math.pow(-2 * u + 2, 2) / 2; alpha = 1; pressScale = 0.94; } else { progress = 1; alpha = Math.max(0, 1 - (cycle - 0.84) / 0.16); pressScale = 0.94 + ((cycle - 0.84) / 0.16) * 0.24; } let targetDist = progress * totalLen; let gx = pts[0].x; let gy = pts[0].y; let angle = Math.atan2(pts[1].y - pts[0].y, pts[1].x - pts[0].x); for (let i = 0; i < segLens.length; i++) { if (targetDist <= segLens[i] || i === segLens.length - 1) { const t = segLens[i] > 0 ? Math.min(1, Math.max(0, targetDist / segLens[i])) : 1; gx = pts[i].x + (pts[i + 1].x - pts[i].x) * t; gy = pts[i].y + (pts[i + 1].y - pts[i].y) * t; angle = Math.atan2(pts[i + 1].y - pts[i].y, pts[i + 1].x - pts[i].x); break; } targetDist -= segLens[i]; } c.globalAlpha = alpha; c.translate(gx, gy); // 外层按压柔晕 ellipse(c, 0, 0, 21 * pressScale, 21 * pressScale); c.fillStyle = 'rgba(255, 224, 130, 0.34)'; c.fill(); c.strokeStyle = 'rgba(255, 248, 224, 0.88)'; c.lineWidth = 2.4; c.stroke(); // 内核指尖白金光珠 ellipse(c, 0, 0, 8.5, 8.5); c.fillStyle = '#fff9e6'; c.fill(); // 指向前方的小金箭羽 c.rotate(angle); c.beginPath(); c.moveTo(14, -5); c.lineTo(22, 0); c.lineTo(14, 5); c.strokeStyle = '#d98a1e'; c.lineWidth = 2.6; c.lineCap = 'round'; c.lineJoin = 'round'; c.stroke(); c.restore(); } function drawPatch(c: CanvasRenderingContext2D, section: SectionDef, stitched: boolean): void { const palette = YARN_PALETTES[section.color]; const w = section.rx; const h = section.ry; c.save(); c.translate(section.x, section.y); c.beginPath(); if (section.patch === 'flower') { for (let i = 0; i <= 120; i++) { const a = TAU * i / 120; const radius = 0.75 + 0.23 * Math.cos(6 * a); const x = Math.cos(a) * w * radius; const y = Math.sin(a) * h * radius; if (i === 0) c.moveTo(x, y); else c.lineTo(x, y); } } else if (section.patch === 'leaf') { c.moveTo(-w, h * 0.28); c.quadraticCurveTo(-w * 0.57, -h, w, -h * 0.38); c.quadraticCurveTo(w * 0.3, h, -w, h * 0.28); } else if (section.patch === 'bell') { c.moveTo(-w, h * 0.5); c.quadraticCurveTo(-w * 0.5, h * 0.16, -w * 0.54, -h * 0.27); c.bezierCurveTo(-w * 0.37, -h, w * 0.37, -h, w * 0.54, -h * 0.27); c.quadraticCurveTo(w * 0.5, h * 0.16, w, h * 0.5); c.quadraticCurveTo(0, h, -w, h * 0.5); } else if (section.patch === 'boat') { c.moveTo(-w, h * 0.1); c.quadraticCurveTo(0, h * 1.5, w, h * 0.1); c.lineTo(w * 0.18, h * 0.1); c.lineTo(w * 0.18, -h); c.quadraticCurveTo(-w * 0.49, -h * 0.63, -w * 0.48, h * 0.1); c.lineTo(-w, h * 0.1); } else { c.moveTo(-w, 0); c.bezierCurveTo(-w * 0.5, -h * 1.22, w * 0.54, h * 0.17, w, -h * 0.55); c.bezierCurveTo(w * 0.55, h * 1.25, -w * 0.43, h * 0.13, -w, 0); } c.closePath(); c.fillStyle = stitched ? palette[1] : `${ROUTE_COLORS[section.color]}20`; c.fill(); c.lineWidth = stitched ? 1.6 : 1.9; c.strokeStyle = RINGS[section.color]; if (!stitched) c.setLineDash([4, 5]); c.stroke(); if (stitched) { c.save(); c.clip(); c.lineWidth = 1.2; for (let y = -h - w; y < h + w; y += 2.8) { c.strokeStyle = y % 4 < 1 ? palette[1] : palette[2]; line(c, -w - 2, y, w + 2, y - w * 0.72); } c.restore(); const detail = section.patch; if (detail === 'flower' || detail === 'bell') { ellipse(c, 0, detail === 'bell' ? h * 0.75 : 0, w * 0.19, h * 0.17); c.fillStyle = palette[0]; c.fill(); } if (detail === 'leaf') { c.strokeStyle = palette[3]; c.lineWidth = 1.4; line(c, -w * 0.72, h * 0.18, w * 0.74, -h * 0.32); } } c.restore(); } function drawSection(c: CanvasRenderingContext2D, section: SectionDef, stitched: boolean): void { if (section.patch) { drawPatch(c, section, stitched); return; } if (section.garden) { drawGardenSection(c, section, stitched); return; } if (section.sakura) { drawSakuraSection(c, section, stitched); return; } if (section.lotus) { drawLotusSection(c, section, stitched); return; } if (section.side) { drawLeafShape(c, section.side, stitched); return; } for (const i of section.petals ?? []) { drawPetal(c, (i * TAU) / 12 - Math.PI / 2, i, stitched, 0); } } function sectionBounds(section: SectionDef): { x: number; y: number; w: number; h: number } { if (section.patch) { return { x: section.x - section.rx - 5, y: section.y - section.ry - 5, w: 2 * section.rx + 10, h: 2 * section.ry + 10 }; } if (section.garden) { return { x: Math.max(0, section.x - 52), y: Math.max(0, section.y - 52), w: 104, h: 104 }; } if (section.sakura) { return { x: Math.max(0, section.x - 68), y: Math.max(0, section.y - 60), w: 136, h: 120 }; } if (section.lotus === 'pad-left' || section.lotus === 'pad-right') { return { x: Math.max(0, section.x - 95), y: Math.max(0, section.y - 68), w: 190, h: 136 }; } if (section.lotus) { return { x: Math.max(0, section.x - 120), y: Math.max(0, section.y - 95), w: 240, h: 150 }; } if (section.side) { return section.side > 0 ? { x: 300, y: 280, w: 118, h: 86 } : { x: 202, y: 310, w: 118, h: 86 }; } return { x: 164, y: 68, w: 272, h: 274 }; } function drawTargetPin( c: CanvasRenderingContext2D, target: SectionDef, selected: boolean, time: number, step?: number, locked = false, ): void { c.save(); c.translate(target.x, target.y); // 柔和醒目的呼吸光环:让目标针位第一眼就能轻松找到 if (!locked) { const breath = 0.5 + 0.5 * Math.sin(time / 320 + target.x * 0.03); const haloR = (selected ? 25 : 21) + breath * 5.5; ellipse(c, 0, 0, haloR, haloR); c.fillStyle = selected ? `rgba(255, 224, 120, ${(0.28 + breath * 0.2).toFixed(3)})` : `rgba(255, 232, 152, ${(0.18 + breath * 0.18).toFixed(3)})`; c.fill(); c.strokeStyle = selected ? '#f2b838cc' : '#e0a94288'; c.lineWidth = selected ? 2.2 : 1.5; c.stroke(); } const pulse = locked ? 0 : Math.sin(time / 280 + target.y * 0.02) * (selected ? 1.8 : 1.2); const radius = (locked ? 15 : selected ? 19.5 : 16.5) + pulse; ellipse(c, 0, 0, radius + 3.5, radius + 3.5); c.fillStyle = locked ? '#e8dfd0e2' : selected ? '#fff6c6f0' : '#fff8e2eb'; c.fill(); ellipse(c, 0, 0, radius, radius); c.lineWidth = locked ? 2 : selected ? 2.6 : 2.0; c.strokeStyle = locked ? '#7c6543' : RINGS[target.color]; if (locked) c.setLineDash([2.5, 2.5]); c.stroke(); c.setLineDash([]); c.fillStyle = locked ? '#8d7a5b' : RINGS[target.color]; mark(c, target.color, 0, 0, locked ? 7 : selected ? 10 : 9.2); if (locked) { // 醒目小铜锁徽章 c.save(); c.translate(14, -14); ellipse(c, 0, 0, 11, 11); c.fillStyle = '#fff3d2'; c.fill(); c.strokeStyle = '#996420'; c.lineWidth = 1.6; c.stroke(); // 锁梁 c.beginPath(); c.arc(0, -1.5, 3.6, Math.PI, 0); c.strokeStyle = '#7c4c15'; c.lineWidth = 1.8; c.stroke(); // 锁身 c.fillStyle = '#b77424'; c.fillRect(-5, -1, 10, 7.5); c.restore(); } else if (step) { ellipse(c, 15, -15, 10, 10); c.fillStyle = '#fdf3d4'; c.fill(); c.strokeStyle = RINGS[target.color]; c.lineWidth = 1.2; c.stroke(); c.fillStyle = RINGS[target.color]; c.font = 'bold 12px sans-serif'; c.textAlign = 'center'; c.textBaseline = 'middle'; c.fillText(String(step), 15, -15); } c.restore(); } function drawUnlockLinks( c: CanvasRenderingContext2D, level: LevelDef, lines: GameState['lines'], drawing: GameState['drawing'], time: number, ): void { const secMap = new Map(); for (const t of level.targets) secMap.set(t.id, t); // 1) 针位机关锁连线(前置针位 → 上锁针位) if (level.locks) { for (const [lockedId, reqId] of Object.entries(level.locks)) { if (!isTargetLocked(level, lockedId, lines, drawing)) continue; const from = secMap.get(reqId); const to = secMap.get(lockedId); if (!from || !to) continue; const ang = Math.atan2(to.y - from.y, to.x - from.x); const d = Math.hypot(to.x - from.x, to.y - from.y); if (d < 36) continue; const sx = from.x + Math.cos(ang) * 20; const sy = from.y + Math.sin(ang) * 20; const ex = to.x - Math.cos(ang) * 22; const ey = to.y - Math.sin(ang) * 22; c.save(); c.beginPath(); c.moveTo(sx, sy); c.lineTo(ex, ey); c.setLineDash([5, 5]); c.lineDashOffset = -(time / 48) % 10; c.strokeStyle = '#a46a21bb'; c.lineWidth = 2; c.stroke(); c.setLineDash([]); // 箭头 c.translate(ex, ey); c.rotate(ang); c.beginPath(); c.moveTo(2, 0); c.lineTo(-7, -4.5); c.lineTo(-7, 4.5); c.closePath(); c.fillStyle = '#a46a21cc'; c.fill(); c.restore(); } } // 2) 吊桥水洼连线(触发针位 → 吊桥水洼) for (const pond of level.ponds) { if (!pond.bridgeSectionId || isPondBridged(pond, lines)) continue; const from = secMap.get(pond.bridgeSectionId); if (!from) continue; const ang = Math.atan2(pond.y - from.y, pond.x - from.x); const sx = from.x + Math.cos(ang) * 20; const sy = from.y + Math.sin(ang) * 20; const ex = pond.x - Math.cos(ang) * (pond.rx * 0.55); const ey = pond.y - Math.sin(ang) * (pond.ry * 0.7); c.save(); c.beginPath(); c.moveTo(sx, sy); c.lineTo(ex, ey); c.setLineDash([4, 5]); c.lineDashOffset = -(time / 52) % 9; c.strokeStyle = '#6c8044aa'; c.lineWidth = 1.9; c.stroke(); c.restore(); } } function samplePolylineUpTo(points: Point2D[], cum: number[], dist: number): Point2D[] { if (points.length < 2 || dist <= 0) return points.length ? [points[0]] : []; const out: Point2D[] = [points[0]]; for (let i = 1; i < points.length; i++) { const cEnd = cum[i] ?? 0; const cStart = cum[i - 1] ?? 0; if (cEnd <= dist) { out.push(points[i]); } else { const span = cEnd - cStart; const t = span > 0 ? Math.max(0, Math.min(1, (dist - cStart) / span)) : 0; out.push({ x: points[i - 1].x + (points[i].x - points[i - 1].x) * t, y: points[i - 1].y + (points[i].y - points[i - 1].y) * t, }); break; } } return out; } function pointAndAngleAtDist(points: Point2D[], cum: number[], dist: number): { x: number; y: number; angle: number } | null { if (points.length < 2 || dist < 0) return null; for (let i = 1; i < points.length; i++) { const cEnd = cum[i] ?? 0; const cStart = cum[i - 1] ?? 0; if (cEnd >= dist || i === points.length - 1) { const span = cEnd - cStart; const t = span > 0 ? Math.max(0, Math.min(1, (dist - cStart) / span)) : 1; return { x: points[i - 1].x + (points[i].x - points[i - 1].x) * t, y: points[i - 1].y + (points[i].y - points[i - 1].y) * t, angle: Math.atan2(points[i].y - points[i - 1].y, points[i].x - points[i - 1].x), }; } } return null; } function drawRoute( c: CanvasRenderingContext2D, points: Point2D[], color: YarnColor | null, status: 'drawing' | 'running' | 'done' | 'invalid', age: number, keepVisible = false, cum?: number[], traveled?: number, ): void { if (points.length < 2) return; c.save(); c.lineJoin = 'round'; c.lineCap = 'round'; // 行进中(running):前方保留轻柔虚线蓝图,蚂蚁身后逐段「长出」饱满立体彩色绣线! if (status === 'running' && cum && traveled !== undefined) { // 1. 前方浅色待织蓝图 c.beginPath(); c.moveTo(points[0].x, points[0].y); for (let i = 1; i < points.length; i++) c.lineTo(points[i].x, points[i].y); c.setLineDash([6, 7]); c.lineWidth = 2.6; c.strokeStyle = 'rgba(130, 98, 52, 0.34)'; c.stroke(); c.setLineDash([]); // 2. 蚂蚁身后实时织出的饱满彩色毛线 const grown = samplePolylineUpTo(points, cum, traveled); if (grown.length >= 2) { c.beginPath(); c.moveTo(grown[0].x, grown[0].y); for (let i = 1; i < grown.length; i++) c.lineTo(grown[i].x, grown[i].y); c.lineWidth = 7.4; c.strokeStyle = '#533b1e88'; c.stroke(); c.lineWidth = 5.2; c.strokeStyle = color ? ROUTE_COLORS[color] : '#a47747'; c.stroke(); c.lineWidth = 1.4; c.strokeStyle = '#fff5c8de'; c.stroke(); } c.restore(); return; } if (status === 'done') { c.globalAlpha *= keepVisible ? 0.72 : Math.max(0.1, 0.44 * (1 - age / 950)); } c.beginPath(); c.moveTo(points[0].x, points[0].y); for (let i = 1; i < points.length; i++) c.lineTo(points[i].x, points[i].y); c.lineWidth = status === 'drawing' ? 7 : 5.4; c.strokeStyle = status === 'invalid' ? '#a14f35b0' : '#60472570'; c.stroke(); c.lineWidth -= 2; c.strokeStyle = status === 'invalid' ? '#e49365' : (color ? ROUTE_COLORS[color] : '#a47747'); c.stroke(); c.lineWidth = 1.25; c.strokeStyle = '#fff2bdc9'; c.stroke(); c.restore(); } function drawHintPath(c: CanvasRenderingContext2D, hint: HintState, time: number): void { const points = hint.points; c.save(); if (points && points.length >= 2) { c.lineJoin = 'round'; c.lineCap = 'round'; c.beginPath(); c.moveTo(points[0].x, points[0].y); for (let i = 1; i < points.length; i++) c.lineTo(points[i].x, points[i].y); c.strokeStyle = hint.tier === 1 ? '#fff5d188' : '#fff5d1b8'; c.lineWidth = hint.tier === 1 ? 4.5 : 6; c.setLineDash(hint.tier === 1 ? [6, 12] : [8, 10]); c.lineDashOffset = -(time / 28); c.stroke(); c.strokeStyle = hint.tier === 1 ? '#9c622296' : '#9c6222c4'; c.lineWidth = hint.tier === 1 ? 2.1 : 2.6; c.stroke(); c.setLineDash([]); } if (hint.ball) { const pulse = 24 + Math.sin(time / 240) * 3; ellipse(c, hint.ball.x, hint.ball.y, pulse, pulse); c.strokeStyle = '#fff7d4'; c.lineWidth = 2.6; c.stroke(); } if (hint.target && hint.tier === 1) { const pulse = 22 + Math.sin(time / 240 + 1.2) * 2.5; ellipse(c, hint.target.x, hint.target.y, pulse, pulse); c.strokeStyle = '#ffe599cc'; c.lineWidth = 2.2; c.setLineDash([4, 4]); c.stroke(); c.setLineDash([]); } c.restore(); } export class EmbroideryRenderer { private readonly background: OffscreenLayer; private readonly heart: OffscreenLayer; private readonly heartBox = { x: 246, y: 152, w: 108, h: 106 }; private readonly surface: OffscreenLayer; private readonly frameLayer: OffscreenLayer; private readonly loupeLayer: OffscreenLayer; private readonly renderScale: number; private loupeSide: 'left' | 'right' = 'left'; private readonly overlays = new Map(); private readonly ballSprites = new Map(); private readonly artworkCache = new Map(); private readonly artworkLoading = new Set(); private readonly artworkListeners: (() => void)[] = []; private qrImage: any = null; private qrLoadAttempted = false; private readonly texture: Texture2D; private readonly spriteFrame: SpriteFrame; private workshopLayer: OffscreenLayer | null = null; private workshopTexture: Texture2D | null = null; private workshopSpriteFrame: SpriteFrame | null = null; private unspoolLayer: OffscreenLayer | null = null; private unspoolTexture: Texture2D | null = null; private unspoolSpriteFrame: SpriteFrame | null = null; private activeLevel: LevelDef | null = null; constructor(renderScale = 1.5) { this.renderScale = renderScale; this.background = createLayer(HOOP_WIDTH, HOOP_HEIGHT, 2); drawHoop(this.background.ctx); drawSurround(this.background.ctx); this.heart = createLayer(this.heartBox.w, this.heartBox.h, 2); this.heart.ctx.save(); this.heart.ctx.translate(-this.heartBox.x, -this.heartBox.y); drawFlowerHeart(this.heart.ctx); this.heart.ctx.restore(); this.surface = createLayer(HOOP_WIDTH, HOOP_HEIGHT, 2); this.frameLayer = createLayer(HOOP_WIDTH, HOOP_HEIGHT, renderScale); this.loupeLayer = createLayer(152, 152, 2); const imageAsset = new ImageAsset(this.frameLayer.canvas); this.texture = new Texture2D(); this.texture.image = imageAsset; this.spriteFrame = new SpriteFrame(); this.spriteFrame.texture = this.texture; this.tryLoadMiniProgramQr(); } private tryLoadMiniProgramQr(): void { if (this.qrLoadAttempted) return; this.qrLoadAttempted = true; // 若已在 assets/resources/branding/wx_qr.(png|jpg) 放置真实小程序码,则自动加载用于导出海报 resources.load('branding/wx_qr', ImageAsset, (err, imgAsset) => { if (!err && imgAsset?.data) { this.qrImage = imgAsset.data; return; } resources.load('branding/wx_qr/texture', Texture2D, (err2, tex) => { if (!err2 && (tex?.image as any)?.data) { this.qrImage = (tex.image as any).data; } }); }); } public getSpriteFrame(): SpriteFrame { return this.spriteFrame; } public onArtworkReady(cb: () => void): void { this.artworkListeners.push(cb); } private notifyArtworkReady(): void { if (this.activeLevel?.artworkImage && this.artworkCache.has(this.activeLevel.artworkImage)) { this.rebuildSurface(this.activeLevel); } for (const cb of this.artworkListeners) cb(); } private ensureArtworkLoaded(name: string): BakedArtwork | null { const cached = this.artworkCache.get(name); if (cached) return cached; if (this.artworkLoading.has(name)) return null; this.artworkLoading.add(name); const finish = (source: any) => { this.artworkLoading.delete(name); if (!source) return; const baked = this.bakeArtwork(source); if (baked) { this.artworkCache.set(name, baked); this.notifyArtworkReady(); } }; resources.load(`artworks/${name}`, ImageAsset, (err, imgAsset) => { if (!err && imgAsset?.data) { finish(imgAsset.data); return; } resources.load(`artworks/${name}/texture`, Texture2D, (err2, tex) => { if (err2 || !tex) { this.artworkLoading.delete(name); return; } finish((tex.image as any)?.data); }); }); return null; } private bakeArtwork(source: any): BakedArtwork | null { const dim = 512; const colorLayer = createLayer(HOOP_WIDTH, HOOP_HEIGHT, 1); const cc = colorLayer.ctx; cc.save(); ellipse(cc, CX, CY, 246, 246); cc.clip(); cc.drawImage(source, CX - dim / 2, CY - dim / 2, dim, dim); cc.restore(); // 采样四角亚麻底色,分离绣品主体与底布,生成素描底稿 + 透明柔和边缘成品层 const imgData = cc.getImageData(0, 0, HOOP_WIDTH, HOOP_HEIGHT); const d = imgData.data; // 采样图片内侧边缘区域均值作为底布参考色(偏暖米白亚麻) let bgR = 242; let bgG = 233; let bgB = 212; const samplePts = [ [CX - 180, CY + 130], [CX + 180, CY + 130], [CX, CY + 180], [CX - 150, CY - 160], [CX + 150, CY - 160], ]; let sr = 0; let sg = 0; let sb = 0; let sc = 0; for (const [sx, sy] of samplePts) { const idx = (Math.round(sy) * HOOP_WIDTH + Math.round(sx)) * 4; if (d[idx + 3] > 200) { sr += d[idx]; sg += d[idx + 1]; sb += d[idx + 2]; sc++; } } if (sc > 0) { bgR = sr / sc; bgG = sg / sc; bgB = sb / sc; } const sketchLayer = createLayer(HOOP_WIDTH, HOOP_HEIGHT, 1); const sketchData = sketchLayer.ctx.createImageData(HOOP_WIDTH, HOOP_HEIGHT); const sd = sketchData.data; const cleanColorLayer = createLayer(HOOP_WIDTH, HOOP_HEIGHT, 1); const cleanData = cleanColorLayer.ctx.createImageData(HOOP_WIDTH, HOOP_HEIGHT); const cd = cleanData.data; const rowHits = new Uint16Array(HOOP_HEIGHT); const colHits = new Uint16Array(HOOP_WIDTH); for (let y = 0; y < HOOP_HEIGHT; y++) { for (let x = 0; x < HOOP_WIDTH; x++) { const i = (y * HOOP_WIDTH + x) * 4; const a = d[i + 3]; if (a < 10) continue; const r = d[i]; const g = d[i + 1]; const b = d[i + 2]; const distFromCenter = Math.hypot(x - CX, y - CY); if (distFromCenter > 246) continue; // 柔和径向边缘羽化,避免与绣绷内缘出现生硬接缝 const edgeFade = distFromCenter > 224 ? Math.max(0, (246 - distFromCenter) / 22) : 1; // 计算像素与亚麻底布的色差(色差小的视为纯底布,淡出透出程序化真亚麻底布) const dr = r - bgR; const dg = g - bgG; const db = b - bgB; const colorDist = Math.sqrt(dr * dr + dg * dg + db * db); const subjectAlpha = Math.max(0, Math.min(1, (colorDist - 18) / 28)) * edgeFade; if (subjectAlpha > 0.01) { cd[i] = r; cd[i + 1] = g; cd[i + 2] = b; cd[i + 3] = Math.round(subjectAlpha * 255); // 仅统计高置信绣线主体像素,过滤四角亚麻明暗噪点,确保主体包围盒绝对精准 if (subjectAlpha > 0.55 && colorDist > 34 && distFromCenter < 218) { rowHits[y]++; colHits[x]++; } // 素描底稿:暖褐淡彩线稿风(保留微弱明暗层次,像铅笔拓在亚麻布上的绣稿) const lum = 0.299 * r + 0.587 * g + 0.114 * b; const inkStrength = Math.max(0.18, Math.min(1, (238 - lum) / 140)); sd[i] = Math.round(138 - inkStrength * 34); sd[i + 1] = Math.round(115 - inkStrength * 30); sd[i + 2] = Math.round(86 - inkStrength * 24); sd[i + 3] = Math.round(subjectAlpha * (0.16 + inkStrength * 0.26) * 255); } } } let minX = HOOP_WIDTH; let maxX = 0; let minY = HOOP_HEIGHT; let maxY = 0; for (let x = 0; x < HOOP_WIDTH; x++) { if (colHits[x] >= 5) { if (x < minX) minX = x; if (x > maxX) maxX = x; } } for (let y = 0; y < HOOP_HEIGHT; y++) { if (rowHits[y] >= 5) { if (y < minY) minY = y; if (y > maxY) maxY = y; } } cleanColorLayer.ctx.putImageData(cleanData, 0, 0); sketchLayer.ctx.putImageData(sketchData, 0, 0); const pad = 10; const validBox = maxX > minX + 24 && maxY > minY + 24; const bx = validBox ? Math.max(0, minX - pad) : 80; const by = validBox ? Math.max(0, minY - pad) : 80; const bw = validBox ? Math.min(HOOP_WIDTH - bx, maxX - minX + pad * 2) : HOOP_WIDTH - 160; const bh = validBox ? Math.min(HOOP_HEIGHT - by, maxY - minY + pad * 2) : HOOP_HEIGHT - 160; return { colorCanvas: cleanColorLayer.canvas, sketchCanvas: sketchLayer.canvas, subjectBox: { x: bx, y: by, w: bw, h: bh }, }; } private drawArtworkGuideRing(c: CanvasRenderingContext2D, section: SectionDef): void { c.save(); ellipse(c, section.x, section.y, section.rx * 0.92, section.ry * 0.92); c.fillStyle = `${ROUTE_COLORS[section.color]}1e`; c.fill(); c.strokeStyle = `${RINGS[section.color]}95`; c.lineWidth = 1.6; c.setLineDash([4, 4]); c.stroke(); c.setLineDash([]); c.restore(); } private drawArtworkReveal(c: CanvasRenderingContext2D, baked: BakedArtwork, target: SectionDef, progress: number): void { if (progress <= 0) return; c.save(); ellipse(c, target.x, target.y, target.rx * 1.42 * progress, target.ry * 1.42 * progress); c.clip(); c.drawImage(baked.colorCanvas, 0, 0, HOOP_WIDTH, HOOP_HEIGHT); c.restore(); } private getOverlay(section: SectionDef): OverlayEntry { let entry = this.overlays.get(section.id); if (entry) return entry; const box = sectionBounds(section); const part = createLayer(box.w, box.h, 2); part.ctx.save(); part.ctx.translate(-box.x, -box.y); drawSection(part.ctx, section, true); part.ctx.restore(); entry = { canvas: part.canvas, ...box }; this.overlays.set(section.id, entry); return entry; } private drawOverlay(c: CanvasRenderingContext2D, section: SectionDef): void { const entry = this.getOverlay(section); c.drawImage(entry.canvas, entry.x, entry.y, entry.w, entry.h); } private getBallSprite(color: YarnColor): any { let sprite = this.ballSprites.get(color); if (sprite) return sprite; const part = createLayer(BALL_SPRITE_SIZE, BALL_SPRITE_SIZE, 2); const c = part.ctx; const colors = YARN_PALETTES[color]; c.translate(BALL_SPRITE_HALF, BALL_SPRITE_HALF); c.lineCap = 'round'; c.beginPath(); c.moveTo(-12, 14); c.bezierCurveTo(-17, 25, -29, 15, -27, 24); c.strokeStyle = colors[0]; c.lineWidth = 3; c.stroke(); c.strokeStyle = colors[2]; c.lineWidth = 1.7; c.stroke(); ellipse(c, 0, 0, 19.5, 19.5); c.fillStyle = colors[0]; c.fill(); c.save(); c.clip(); for (let j = -25; j <= 26; j += 3.8) { c.beginPath(); c.moveTo(j - 16, -25); c.quadraticCurveTo(j + 8, -6, j + 16, 25); c.lineWidth = 3.35; c.strokeStyle = j % 2 ? colors[1] : colors[2]; c.stroke(); c.translate(0.65, -0.65); c.lineWidth = 0.8; c.strokeStyle = colors[3]; c.stroke(); c.translate(-0.65, 0.65); } for (let j = -8; j <= 7; j += 3.75) { c.beginPath(); c.moveTo(-22, j + 17); c.quadraticCurveTo(-2, j - 10, 23, j - 17); c.lineWidth = 4; c.strokeStyle = colors[0]; c.stroke(); c.lineWidth = 2.7; c.strokeStyle = j > 0 ? colors[1] : colors[2]; c.stroke(); } c.restore(); sprite = part.canvas; this.ballSprites.set(color, sprite); return sprite; } private drawBall(c: CanvasRenderingContext2D, x: number, y: number, color: YarnColor, scale = 1, rotation = 0, highlight = false): void { c.save(); c.translate(x, y); c.scale(scale, scale); ellipse(c, 1, 14, 18, 7); c.fillStyle = '#63461d35'; c.fill(); c.rotate(rotation); c.drawImage(this.getBallSprite(color), -BALL_SPRITE_HALF, -BALL_SPRITE_HALF, BALL_SPRITE_SIZE, BALL_SPRITE_SIZE); if (highlight) { ellipse(c, 0, 0, 23, 23); c.strokeStyle = '#fff7cb'; c.lineWidth = 2.2; c.stroke(); } c.restore(); } private drawAnt(c: CanvasRenderingContext2D, data: AntState, time: number, sizeScale = 1.32, miniCarryColor?: YarnColor): void { const pulling = data.carry.length > 0 || Boolean(miniCarryColor); const cycle = !data.walk ? time / 320 : time / (data.carry.length > 1 ? 76 : 54); const swayAngle = data.walk ? Math.sin(cycle) * (pulling ? 0.13 : 0.08) : Math.sin(cycle) * 0.05; const stretchX = data.walk && pulling ? 1.16 + Math.sin(cycle * 2) * 0.06 : data.walk ? 1.05 : 1.0; const stretchY = data.walk && pulling ? 0.89 - Math.sin(cycle * 2) * 0.04 : 1.0; const bob = data.walk ? Math.sin(cycle * 2) * 1.1 : Math.sin(cycle) * 0.7; c.save(); c.translate(data.x, data.y); c.scale(sizeScale, sizeScale); // 地面柔影(随行走起伏) ellipse(c, 0, 5, 20 * stretchX, 7.5); c.fillStyle = '#3825183c'; c.fill(); c.rotate(data.angle + swayAngle); c.translate(0, bob); c.scale(stretchX, stretchY); c.lineCap = 'round'; // 6 条活泼摆动的工蚁步足(三足交替步态) for (const s of [-1, 1]) { for (let i = 0; i < 3; i++) { const x = i * 6.2 - 8.2; const swing = data.walk ? Math.sin(cycle + i * 2.1 + s * 1.5) * 4.4 : Math.sin(cycle + i) * 0.7; c.beginPath(); c.moveTo(x, s * 3.2); c.quadraticCurveTo(x + 4.5, s * 8.8, x - 2.2 + swing, s * 13.2); c.strokeStyle = '#3f261b'; c.lineWidth = 2.0; c.stroke(); } } // 饱满亮泽栗棕甲壳(腹 / 胸 / 头三段 + 高光反光) const abdomen = c.createLinearGradient(0, -9, 0, 10); abdomen.addColorStop(0, '#9c6240'); abdomen.addColorStop(0.55, '#6b3e26'); abdomen.addColorStop(1, '#36211a'); for (const [x, rx, ry] of [[-12.5, 9.6, 8.0], [-1.8, 5.8, 5.6], [7.6, 8.4, 7.6]]) { ellipse(c, x, 0, rx, ry); c.fillStyle = abdomen; c.fill(); c.strokeStyle = '#3a241b'; c.lineWidth = 1.15; c.stroke(); } // 腹部高光与可爱的细金条纹 ellipse(c, -13.5, -2.8, 4.8, 2.2); c.fillStyle = 'rgba(255, 224, 178, 0.35)'; c.fill(); c.strokeStyle = 'rgba(246, 198, 120, 0.42)'; c.lineWidth = 1.2; line(c, -10, -5.5, -10, 5.5); line(c, -15, -5.0, -15, 5.0); // 灵动挥舞的长触角(待机时也会俏皮轻晃) for (const s of [-1, 1]) { const flick = Math.sin(time / 135 + s * 0.9) * (data.walk ? 3.0 : 2.2); c.beginPath(); c.moveTo(12, s * 3.8); c.quadraticCurveTo(17.5, s * 11.2, 23.5, s * (9.2 + flick)); c.lineWidth = 1.8; c.strokeStyle = '#472a1e'; c.stroke(); ellipse(c, 23.5, s * (9.2 + flick), 1.6, 1.6); c.fillStyle = '#d99e52'; c.fill(); } // 灵气大眼睛(双眸高光) for (const s of [-1, 1]) { ellipse(c, 10.6, s * 3.2, 2.5, 1.9); c.fillStyle = '#fff9e6'; c.fill(); ellipse(c, 11.4, s * 3.2, 1.2, 1.1); c.fillStyle = '#24140e'; c.fill(); } c.restore(); // 扛球动画(拖球时球随蚂蚁步伐轻盈晃动) data.carry.forEach((ball, index) => { const lift = 1 - Math.max(0, 1 - (time - ball.pickedAt) / 145) ** 2; const x = ball.x + (data.x - 4 + index * 14 + Math.sin(cycle * 0.8 + index) * 1.8 - ball.x) * lift; const y = ball.y + (data.y - 16 - index * 14 - ball.y) * lift; this.drawBall(c, x, y, ball.color, 0.96 - lift * 0.34, cycle * 0.08 + lift * 0.85); }); // 队列跟班小工蚁背上的迷你彩线团 if (miniCarryColor && data.carry.length === 0) { this.drawBall(c, data.x - Math.cos(data.angle) * 6, data.y - 11, miniCarryColor, 0.44, cycle * 0.09); } } public selectLevel(level: LevelDef): void { this.activeLevel = level; if (level.artworkImage) this.ensureArtworkLoaded(level.artworkImage); this.rebuildSurface(level); } private rebuildSurface(level: LevelDef): void { const c = this.surface.ctx; c.clearRect(0, 0, HOOP_WIDTH, HOOP_HEIGHT); c.drawImage(this.background.canvas, 0, 0, HOOP_WIDTH, HOOP_HEIGHT); const baked = level.artworkImage ? this.artworkCache.get(level.artworkImage) : null; if (baked) { c.drawImage(baked.sketchCanvas, 0, 0, HOOP_WIDTH, HOOP_HEIGHT); for (const target of level.targets) { this.drawArtworkGuideRing(c, target); } return; } if (level.motif === 'lotus') drawLotusBase(c); else if (level.motif === 'sakura') drawSakuraBase(c); else if (level.motif === 'garden') drawGardenBase(c); else if (level.motif === 'sunflower') drawFlowerBase(c); const motifSections = MOTIFS[level.motif] ?? []; for (const section of motifSections) { if (level.targets.indexOf(section) !== -1) drawSection(c, section, false); else this.drawOverlay(c, section); } if (level.motif === 'custom') { for (const target of level.targets) { if (target.patch) drawSection(c, target, false); else this.drawArtworkGuideRing(c, target); } } } private drawScene(c: CanvasRenderingContext2D, state: GameState, portrait = false, activeNestIndex = 0): void { const level = this.activeLevel; if (!level) return; const { time, lines, drawing, complete, demo, hint } = state; const baked = level.artworkImage ? this.ensureArtworkLoaded(level.artworkImage) : null; c.drawImage(this.surface.canvas, 0, 0, HOOP_WIDTH, HOOP_HEIGHT); if (baked && portrait) { // 导出藏品海报模式:按真实非透明包围盒,把绣品上下左右精准居中在绣绷正中(适度放大至黄金视觉比例) c.drawImage(this.background.canvas, 0, 0, HOOP_WIDTH, HOOP_HEIGHT); const box = baked.subjectBox; const scale = Math.min(1.16, 368 / box.w, 356 / box.h); const dw = box.w * scale; const dh = box.h * scale; c.save(); ellipse(c, CX, CY, 244, 244); c.clip(); c.drawImage(baked.colorCanvas, box.x, box.y, box.w, box.h, CX - dw / 2, CY - dh / 2, dw, dh); c.restore(); return; } if (baked && complete) { const bloom = Math.min(1, (time - state.completedAt) / 420); if (bloom > 0) { const box = baked.subjectBox; const scale = Math.min(1.18, 368 / box.w, 360 / box.h); const dw = box.w * scale; const dh = box.h * scale; c.save(); c.globalAlpha = bloom; c.drawImage(this.background.canvas, 0, 0, HOOP_WIDTH, HOOP_HEIGHT); ellipse(c, CX, CY, 244, 244); c.clip(); c.drawImage(baked.colorCanvas, box.x, box.y, box.w, box.h, CX - dw / 2, CY - dh / 2, dw, dh); c.restore(); } } for (const entry of lines) { for (const { target, arrivedAt, origin } of entry.deliveries) { if (arrivedAt === undefined) continue; const elapsed = time - arrivedAt; const progress = portrait ? 1 : Math.min(1, Math.max(0, (elapsed - 150) / 560)); if (baked) { if (!portrait && !complete) this.drawArtworkReveal(c, baked, target, progress); } else if (progress >= 1) { this.drawOverlay(c, target); } else if (progress > 0) { c.save(); ellipse(c, target.x, target.y, progress * 106, progress * 106); c.clip(); this.drawOverlay(c, target); c.restore(); } if (!portrait && elapsed >= 0 && elapsed < 205) { const t = elapsed / 205; this.drawBall(c, origin.x + (target.x - origin.x) * t, origin.y + (target.y - origin.y) * t, target.color, 0.64 * (1 - t), t * 1.4); } if (!portrait && progress > 0 && progress < 1) { c.save(); c.globalAlpha = Math.sin(progress * Math.PI); c.fillStyle = 'ivory'; for (let j = 0; j < 4; j++) { const sign = target.id === 'left' ? -1 : 1; const x = target.x + (8 + j * 9) * Math.sin(j + progress * 2) + sign * progress * 24; const y = target.y + Math.cos(j + progress * 2) * (9 + j * 9); c.fillRect(x, y, 2.5, 2.5); } c.restore(); } } } if (!baked && level.motif === 'sunflower') { c.drawImage(this.heart.canvas, this.heartBox.x, this.heartBox.y, this.heartBox.w, this.heartBox.h); } if (portrait) return; const clean = complete ? Math.min(1, (time - state.completedAt) / 350) : 0; if (clean > 0) { c.save(); c.globalAlpha = clean; this.drawAnt(c, { x: 381, y: 458, angle: -1.42, carry: [], walk: false }, time); c.restore(); } if (clean >= 1) return; c.save(); c.globalAlpha = 1 - clean; for (const zone of level.roughZones) drawRoughZone(c, zone); for (const pond of level.ponds) drawPond(c, pond, isPondBridged(pond, lines)); if (hint && !demo && !drawing) drawHintPath(c, hint, time); else if (!lines.length && !demo && !drawing && !complete) drawStartDragGuide(c, level, time); for (const entry of lines) { drawRoute( c, entry.points, entry.color, entry.finished ? 'done' : 'running', time - entry.arrivedAt, Boolean(level.noCross), entry.cum, entry.traveled, ); } if (drawing) drawRoute(c, drawing.points, drawing.color, drawing.blocked ? 'invalid' : 'drawing', 0, false); const allEntries = drawing ? [...lines, drawing] : lines; // 染缸、纽扣、单向穿针眼、线轴补给 for (const dye of level.dyes) drawDyeVat(c, dye, time, dyeRemainingUses(dye, lines, drawing)); const threadedBtnIds = new Set(); for (const entry of allEntries) for (const b of entry.buttons) threadedBtnIds.add(b.button.id); for (const btn of level.buttons) drawButtonAnchor(c, btn, threadedBtnIds.has(btn.id), time); const passedGateSet = new Set(); for (const entry of allEntries) for (const g of entry.gates) passedGateSet.add(g.gate.id); for (const gate of level.gates) drawGate(c, gate, passedGateSet.has(gate.id), time); const collectedSpoolIds = new Set(); for (const l of lines) { for (const s of l.spools) { if (s.collectedAt !== undefined) collectedSpoolIds.add(s.spool.id); } } for (const spool of level.spools) { if (collectedSpoolIds.has(spool.id)) continue; drawSpool(c, spool, time); } const stopsByTarget = new Map(); for (const entry of allEntries) { entry.deliveries.forEach((stop, index) => { stopsByTarget.set(stop.target.id, { stop, step: index + 1 }); }); } for (const target of level.targets) { const planned = stopsByTarget.get(target.id); if (planned?.stop.arrivedAt !== undefined) continue; const locked = isTargetLocked(level, target.id, lines, drawing); const needsStop = !locked && Boolean(drawing && drawing.color === target.color && drawing.picks.length > drawing.deliveries.length); drawTargetPin(c, target, Boolean(planned || needsStop), time, planned?.step, locked); // 「新针开旧夹」:在对应开夹针位旁绘制与旧布木夹完全一致的微型徽标 const linkedClip = level.unspool?.clips?.find(clip => clip.unlockSectionId === target.id && !isClipUnlocked(clip, lines, drawing)); if (linkedClip) { const pulsing = state.unspool?.pulseTargetId === target.id && time < (state.unspool.pulseUntil ?? 0); if (pulsing) { const wave = (time % 650) / 650; c.save(); ellipse(c, target.x, target.y, target.rx + 8 + wave * 18, target.ry + 8 + wave * 18); c.strokeStyle = ROUTE_COLORS[linkedClip.badgeColor]; c.globalAlpha = Math.max(0.15, 0.9 - wave * 0.75); c.lineWidth = 3.5; c.stroke(); c.restore(); } const bx = target.x + Math.max(22, target.rx * 0.72); const by = target.y - Math.max(20, target.ry * 0.68); c.save(); roundRectPath(c, bx - 14, by - 11, 28, 22, 6); c.fillStyle = '#8b5e36'; c.fill(); c.strokeStyle = pulsing ? '#fff1a8' : '#573619'; c.lineWidth = pulsing ? 2.4 : 1.5; c.stroke(); this.drawClipEmblem(c, bx, by, 7.5, linkedClip.badgeColor, linkedClip.badgeShape); c.restore(); } } drawUnlockLinks(c, level, lines, drawing, time); const multiNest = level.nests.length > 1; level.nests.forEach((nestPos, idx) => { drawNest(c, nestPos, time, lines.length === 0 && !drawing, idx === activeNestIndex, multiNest); }); if (level.unspool) { const deadlocked = Boolean(state.unspool?.deadlocked); const animSlot = state.unspool?.animating?.slotIndex ?? -1; const capacity = level.unspool.capacity ?? level.unspool.slots.length; const occupiedCount = state.unspool?.slotBallIds.filter(id => id != null).length ?? 0; const capFull = occupiedCount >= capacity; level.unspool.slots.forEach((slot, idx) => { const occupied = state.unspool?.slotBallIds[idx] != null; const receiving = animSlot === idx; const capBlocked = !occupied && capFull; c.save(); c.translate(slot.x, slot.y); c.globalAlpha = capBlocked ? 0.56 : 1; ellipse(c, 2, 12, 30, 15); c.fillStyle = '#63503d28'; c.fill(); ellipse(c, 0, 2, 28, 25); c.fillStyle = deadlocked ? '#e7b9aa' : '#ddd0b9'; c.fill(); c.strokeStyle = deadlocked ? '#975449' : receiving ? '#826146' : '#a08e73'; c.lineWidth = receiving || deadlocked ? 2.8 : 1.6; c.stroke(); ellipse(c, 0, 2, 21, 18); c.fillStyle = occupied ? '#c2b091' : '#f2e7d4'; c.fill(); if (!occupied) { c.fillStyle = '#6d5e4e'; c.font = `700 12px ${SERIF_FONT}`; c.textAlign = 'center'; c.textBaseline = 'middle'; c.fillText(capBlocked ? '满格' : `线碗${idx + 1}`, 0, 2); } c.restore(); }); } for (const ball of level.balls) { let pickedAt: number | undefined; for (const l of lines) { const hit = l.picks.find(ev => ev.ball.id === ball.id); if (hit && hit.pickedAt !== undefined) { pickedAt = hit.pickedAt; break; } } if (pickedAt !== undefined) continue; const highlighted = Boolean(drawing?.picks.some(p => p.ball.id === ball.id)); if (level.solidBalls && !highlighted) { c.save(); ellipse(c, ball.x, ball.y, 24, 24); c.strokeStyle = '#5c412888'; c.lineWidth = 2; c.setLineDash([4, 3]); c.stroke(); c.restore(); } this.drawBall(c, ball.x, ball.y, ball.color, 1, 0, highlighted); } if (drawing?.showTip && drawing.points.length > 0) { const tip = drawing.points[drawing.points.length - 1]; const finger = drawing.fingerTip; if (finger && Math.hypot(finger.x - tip.x, finger.y - tip.y) > 16) { c.save(); c.beginPath(); c.moveTo(finger.x, finger.y); c.lineTo(tip.x, tip.y); c.setLineDash([5, 6]); c.lineWidth = 2; c.strokeStyle = 'rgba(255, 246, 220, 0.78)'; c.stroke(); c.setLineDash([]); ellipse(c, finger.x, finger.y, 8, 8); c.lineWidth = 2; c.strokeStyle = 'rgba(255, 246, 220, 0.65)'; c.stroke(); c.restore(); } ellipse(c, tip.x, tip.y, 11, 11); c.lineWidth = 6; c.strokeStyle = '#fff6dc'; c.stroke(); c.lineWidth = 2.5; c.strokeStyle = drawing.blocked ? '#a14f35' : '#705332'; c.stroke(); } for (const entry of lines) { if (!entry.finished) { const leadDist = entry.traveled ?? 0; // 演示模式或串双球时,派出一队小工蚁排成整齐小队跟着领头蚁一起织画! const followerCount = demo ? 3 : (entry.picks.length >= 2 ? 2 : 1); for (let f = followerCount; f >= 1; f--) { const fDist = leadDist - f * 38; if (fDist < 12) continue; const pose = pointAndAngleAtDist(entry.points, entry.cum, fDist); if (!pose) continue; this.drawAnt( c, { x: pose.x, y: pose.y, angle: pose.angle, carry: [], walk: entry.ant.walk }, time + f * 180, 0.94, entry.color ?? undefined, ); } this.drawAnt(c, entry.ant, time, 1.34); } } if (!lines.length && !drawing && !demo) { const idleNest = level.nests[activeNestIndex] ?? level.nests[0] ?? NEST; const firstBall = level.balls[0]; const faceAng = firstBall ? Math.atan2(firstBall.y - (idleNest.y - 16), firstBall.x - (idleNest.x + 42)) : -2.4; const idleWiggle = Math.sin(time / 480) * 0.14; this.drawAnt( c, { x: idleNest.x + 44, y: idleNest.y - 16, angle: faceAng + idleWiggle, carry: [], walk: false }, time, 1.34, ); } c.restore(); } private drawLoupe(c: CanvasRenderingContext2D, tip: Point2D, blocked: boolean): void { const size = 152; const r = size / 2; const zoom = 1.7; const srcSize = size / zoom; const srcHalf = srcSize / 2; // 带滞回的左右自动避让:针尖偏左时放右上角,偏右时放左上角 if (tip.x < 265) this.loupeSide = 'right'; else if (tip.x > 335) this.loupeSide = 'left'; const lx = this.loupeSide === 'left' ? 92 : HOOP_WIDTH - 92; const ly = 92; // 先把针尖周围区域拷贝到独立离屏画布,避免 drawImage 自绘 const lc = this.loupeLayer.ctx; lc.save(); lc.fillStyle = '#677556'; lc.fillRect(0, 0, size, size); const sx = (tip.x - srcHalf) * this.renderScale; const sy = (tip.y - srcHalf) * this.renderScale; const sw = srcSize * this.renderScale; const sh = srcSize * this.renderScale; lc.drawImage(this.frameLayer.canvas, sx, sy, sw, sh, 0, 0, size, size); lc.restore(); // 合成圆形放大镜浮窗 c.save(); c.shadowColor = 'rgba(28, 34, 20, 0.34)'; c.shadowBlur = 14; c.shadowOffsetY = 5; ellipse(c, lx, ly, r, r); c.fillStyle = '#677556'; c.fill(); c.restore(); c.save(); ellipse(c, lx, ly, r - 3, r - 3); c.clip(); c.drawImage(this.loupeLayer.canvas, lx - r, ly - r, size, size); // 中心轻细十字准星 c.strokeStyle = blocked ? 'rgba(196, 80, 52, 0.55)' : 'rgba(112, 83, 50, 0.35)'; c.lineWidth = 1.2; line(c, lx - 16, ly, lx - 6, ly); line(c, lx + 6, ly, lx + 16, ly); line(c, lx, ly - 16, lx, ly - 6); line(c, lx, ly + 6, lx, ly + 16); c.restore(); // 木质与米白双层镜框 c.save(); ellipse(c, lx, ly, r - 2.5, r - 2.5); c.lineWidth = 5; c.strokeStyle = '#fff6df'; c.stroke(); ellipse(c, lx, ly, r, r); c.lineWidth = 2.6; c.strokeStyle = blocked ? '#c05538' : '#7b552f'; c.stroke(); c.restore(); } public renderFrame(state: GameState, activeNestIndex = 0): void { const c = this.frameLayer.ctx; c.clearRect(0, 0, HOOP_WIDTH, HOOP_HEIGHT); this.drawScene(c, state, false, activeNestIndex); const drawing = state.drawing; if (drawing?.showTip && drawing.loupeEnabled !== false && drawing.points.length > 0) { this.drawLoupe(c, drawing.points[drawing.points.length - 1], Boolean(drawing.blocked)); } this.texture.uploadData(this.frameLayer.canvas); } public createProgressSpriteFrame(motif: LevelDef['motif'], stitchedIds: Set, width = 340): SpriteFrame { const scale = width / HOOP_WIDTH; const height = Math.round(HOOP_HEIGHT * scale); const canvas = createPlatformCanvas(width, height); const c = canvas.getContext('2d') as CanvasRenderingContext2D; c.scale(scale, scale); c.drawImage(this.background.canvas, 0, 0, HOOP_WIDTH, HOOP_HEIGHT); if (motif === 'lotus') drawLotusBase(c); else if (motif === 'sakura') drawSakuraBase(c); else if (motif === 'garden') drawGardenBase(c); else drawFlowerBase(c); for (const section of MOTIFS[motif]) { const stitched = stitchedIds.has(section.id); if (stitched) this.drawOverlay(c, section); else drawSection(c, section, false); } if (motif === 'sunflower') { c.drawImage(this.heart.canvas, this.heartBox.x, this.heartBox.y, this.heartBox.w, this.heartBox.h); } const imageAsset = new ImageAsset(canvas); const texture = new Texture2D(); texture.image = imageAsset; const frame = new SpriteFrame(); frame.texture = texture; return frame; } private computeOpaqueBounds(canvas: any, w: number, h: number): { x: number; y: number; w: number; h: number } { try { const ctx = canvas.getContext('2d') as CanvasRenderingContext2D; const data = ctx.getImageData(0, 0, w, h).data; let minX = w; let minY = h; let maxX = 0; let maxY = 0; for (let y = 0; y < h; y += 2) { for (let x = 0; x < w; x += 2) { const a = data[(y * w + x) * 4 + 3]; if (a > 18) { if (x < minX) minX = x; if (x > maxX) maxX = x; if (y < minY) minY = y; if (y > maxY) maxY = y; } } } if (maxX > minX + 12 && maxY > minY + 12) { const pad = 6; const bx = Math.max(0, minX - pad); const by = Math.max(0, minY - pad); return { x: bx, y: by, w: Math.min(w - bx, maxX - minX + pad * 2), h: Math.min(h - by, maxY - minY + pad * 2), }; } } catch { // 安全降级 } return { x: 80, y: 60, w: w - 160, h: h - 120 }; } /** 大厅绣绷画架纯绣品预览:仅展示上下左右绝对居中的精美绣画,不展示毛线球、蚂蚁、蚁巢等机关元素 */ public createThumbnailSpriteFrame(level: LevelDef, _completed: boolean, width = 260): SpriteFrame { const scale = width / HOOP_WIDTH; const height = Math.round(HOOP_HEIGHT * scale); const canvas = createPlatformCanvas(width, height); const c = canvas.getContext('2d') as CanvasRenderingContext2D; c.scale(scale, scale); c.drawImage(this.background.canvas, 0, 0, HOOP_WIDTH, HOOP_HEIGHT); const baked = level.artworkImage ? this.ensureArtworkLoaded(level.artworkImage) : null; if (baked) { const box = baked.subjectBox; const fit = Math.min(1.55, 368 / box.w, 368 / box.h); const dw = box.w * fit; const dh = box.h * fit; c.save(); ellipse(c, CX, CY, 244, 244); c.clip(); c.drawImage(baked.colorCanvas, box.x, box.y, box.w, box.h, CX - dw / 2, CY - dh / 2, dw, dh); c.restore(); } else { // 将完整程序化绣花绘制到透明离屏层,按真实像素包围盒上下左右精准居中至绣绷正心 const fallbackMotif = level.motif === 'custom' ? 'sunflower' : level.motif; const motifCanvas = createPlatformCanvas(HOOP_WIDTH, HOOP_HEIGHT); const mc = motifCanvas.getContext('2d') as CanvasRenderingContext2D; if (fallbackMotif === 'lotus') drawLotusBase(mc); else if (fallbackMotif === 'sakura') drawSakuraBase(mc); else if (fallbackMotif === 'garden') drawGardenBase(mc); else drawFlowerBase(mc); for (const section of MOTIFS[fallbackMotif] ?? []) { this.drawOverlay(mc, section); } if (fallbackMotif === 'sunflower') { mc.drawImage(this.heart.canvas, this.heartBox.x, this.heartBox.y, this.heartBox.w, this.heartBox.h); } const box = this.computeOpaqueBounds(motifCanvas, HOOP_WIDTH, HOOP_HEIGHT); const fit = Math.min(1.18, 368 / box.w, 368 / box.h); const dw = box.w * fit; const dh = box.h * fit; c.save(); ellipse(c, CX, CY, 244, 244); c.clip(); c.drawImage(motifCanvas, box.x, box.y, box.w, box.h, CX - dw / 2, CY - dh / 2, dw, dh); c.restore(); } const imageAsset = new ImageAsset(canvas); const texture = new Texture2D(); texture.image = imageAsset; const frame = new SpriteFrame(); frame.texture = texture; return frame; } private drawPosterLogo(c: CanvasRenderingContext2D, cx: number, cy: number): void { c.save(); c.translate(cx, cy); // 圆形双线绣印 Logo ellipse(c, 0, 0, 22, 22); c.fillStyle = '#fff9ea'; c.fill(); c.strokeStyle = '#b47834'; c.lineWidth = 2; c.stroke(); ellipse(c, 0, 0, 17, 17); c.strokeStyle = '#c8945299'; c.lineWidth = 1.1; c.setLineDash([2.5, 2.5]); c.stroke(); c.setLineDash([]); // 中心八瓣绣花印记 c.fillStyle = '#9a5b24'; for (let i = 0; i < 6; i++) { c.save(); c.rotate((i * TAU) / 6); ellipse(c, 0, -6.5, 2.8, 5.2); c.fill(); c.restore(); } ellipse(c, 0, 0, 3.6, 3.6); c.fillStyle = '#e5a93c'; c.fill(); c.restore(); } private drawPosterQrSlot(c: CanvasRenderingContext2D, cx: number, cy: number, radius: number): void { c.save(); // 柔白圆底与金线边框 ellipse(c, cx, cy, radius + 4, radius + 4); c.fillStyle = '#fffdf6'; c.fill(); c.strokeStyle = '#d3be94'; c.lineWidth = 1.6; c.stroke(); if (this.qrImage) { c.save(); ellipse(c, cx, cy, radius, radius); c.clip(); c.drawImage(this.qrImage, cx - radius, cy - radius, radius * 2, radius * 2); c.restore(); c.restore(); return; } // 未配置真实小程序码时,绘制雅致的太阳码风格占位图(放入 resources/branding/wx_qr 即可自动替换) c.translate(cx, cy); const ringColor = '#4e5f41'; c.strokeStyle = ringColor; c.fillStyle = ringColor; c.lineCap = 'round'; const rand = rng(92013); for (let ring = 0; ring < 3; ring++) { const r = 17 + ring * 7.5; const segs = 10 + ring * 4; for (let s = 0; s < segs; s++) { if (rand() < 0.34) continue; const a0 = (s / segs) * TAU; const span = ((0.35 + rand() * 0.45) * TAU) / segs; c.beginPath(); c.arc(0, 0, r, a0, a0 + span); c.lineWidth = 2.6; c.stroke(); } } // 三个太阳码定位圆点 for (const ang of [-Math.PI * 0.78, -Math.PI * 0.16, Math.PI * 0.62]) { const px = Math.cos(ang) * 28; const py = Math.sin(ang) * 28; ellipse(c, px, py, 5.2, 5.2); c.fillStyle = '#fffdf6'; c.fill(); c.strokeStyle = ringColor; c.lineWidth = 2; c.stroke(); ellipse(c, px, py, 2.2, 2.2); c.fillStyle = ringColor; c.fill(); } // 中心绣花徽章 ellipse(c, 0, 0, 11, 11); c.fillStyle = '#fff9ea'; c.fill(); c.strokeStyle = '#b47834'; c.lineWidth = 1.4; c.stroke(); c.fillStyle = '#9a5b24'; mark(c, 'gold', 0, 0, 5.5); c.restore(); } public getWorkshopSpriteFrame(): SpriteFrame { if (!this.workshopSpriteFrame) { this.workshopLayer = createLayer(480, 152, 1.5); const imageAsset = new ImageAsset(this.workshopLayer.canvas); this.workshopTexture = new Texture2D(); this.workshopTexture.image = imageAsset; this.workshopSpriteFrame = new SpriteFrame(); this.workshopSpriteFrame.texture = this.workshopTexture; this.renderWorkshopFrame(0, 0); } return this.workshopSpriteFrame; } /** 放置蚂蚁工坊实时动态小舞台:青瓷布垫 + 线轴线头堆 + 3 只忙碌/打盹的小蚂蚁 */ public renderWorkshopFrame(timeMs: number, pendingThreads: number): void { if (!this.workshopLayer || !this.workshopTexture) return; const W = 480; const H = 152; const c = this.workshopLayer.ctx; c.clearRect(0, 0, W, H); // 1) 青瓷亚麻工作布垫 + 内缝线 const bg = c.createLinearGradient(0, 0, W, H); bg.addColorStop(0, '#627966'); bg.addColorStop(1, '#4c6151'); roundRectPath(c, 2, 2, W - 4, H - 4, 20); c.fillStyle = bg; c.fill(); c.strokeStyle = '#d8b46d'; c.lineWidth = 2.4; c.stroke(); c.save(); roundRectPath(c, 11, 11, W - 22, H - 22, 14); c.strokeStyle = 'rgba(247, 232, 194, 0.34)'; c.lineWidth = 1.2; c.setLineDash([5, 5]); c.stroke(); c.restore(); // 柔和午后阳光斜射斑块 c.save(); c.fillStyle = 'rgba(255, 244, 206, 0.11)'; c.beginPath(); c.moveTo(46, 8); c.lineTo(192, 8); c.lineTo(132, H - 8); c.lineTo(18, H - 8); c.closePath(); c.fill(); c.restore(); // 2) 左侧装饰线球 + 右侧待收金丝线头小筐 this.drawBall(c, 58, 78, 'gold', 0.85, Math.sin(timeMs / 1800) * 0.15, false); this.drawBall(c, 92, 108, 'rose', 0.68, -0.3, false); // 右侧线头小木碟 ellipse(c, 412, 82, 42, 34); c.fillStyle = '#432a17'; c.fill(); c.strokeStyle = '#cfa45a'; c.lineWidth = 2.2; c.stroke(); ellipse(c, 412, 82, 33, 25); c.fillStyle = '#623f24'; c.fill(); // 根据待领线头数量堆叠亮晶晶的柔丝线团 const bundleCount = Math.min(8, pendingThreads > 0 ? Math.max(1, Math.ceil(pendingThreads / 5)) : 0); const bundleOffsets: [number, number, YarnColor][] = [ [-12, 6, 'gold'], [11, 7, 'rose'], [0, -4, 'green'], [-14, -7, 'sky'], [14, -6, 'lavender'], [0, 10, 'coral'], [-6, -11, 'gold'], [7, -11, 'rose'], ]; for (let i = 0; i < bundleCount; i++) { const [bx, by, col] = bundleOffsets[i]; this.drawBall(c, 412 + bx, 80 + by, col, 0.42, i * 0.7, false); } // 3) 中央搬运小径虚线 c.save(); c.beginPath(); c.moveTo(108, 84); c.quadraticCurveTo(238, 42, 366, 82); c.strokeStyle = 'rgba(255, 236, 178, 0.35)'; c.lineWidth = 2; c.setLineDash([6, 6]); c.stroke(); c.restore(); // 4) 3 只活灵活现的小工蚁(2 只沿弧线搬运线球,1 只在左下角舒展触角打盹) const t1 = ((timeMs / 4600) % 1 + 1) % 1; const t2 = ((t1 + 0.48) % 1); const sampleArc = (t: number) => { const inv = 1 - t; const x = inv * inv * 112 + 2 * inv * t * 238 + t * t * 362; const y = inv * inv * 84 + 2 * inv * t * 42 + t * t * 82; const dx = 2 * inv * (238 - 112) + 2 * t * (362 - 238); const dy = 2 * inv * (42 - 84) + 2 * t * (82 - 42); return { x, y, angle: Math.atan2(dy, dx) }; }; const a1 = sampleArc(t1); const a2 = sampleArc(t2); this.drawAnt(c, { x: a1.x, y: a1.y, angle: a1.angle, carry: [], walk: true }, timeMs, 1.05, 'gold'); this.drawAnt(c, { x: a2.x, y: a2.y, angle: a2.angle, carry: [], walk: true }, timeMs + 340, 0.94, 'green'); this.drawAnt( c, { x: 224, y: 116, angle: -0.25 + Math.sin(timeMs / 900) * 0.14, carry: [], walk: false }, timeMs, 0.92, ); this.workshopTexture.uploadData(this.workshopLayer.canvas); } public getUnspoolClothSpriteFrame(): SpriteFrame { if (!this.unspoolSpriteFrame) { this.unspoolLayer = createLayer(UNSPOOL_CLOTH_WIDTH, UNSPOOL_CLOTH_HEIGHT, 1.5); const imageAsset = new ImageAsset(this.unspoolLayer.canvas); this.unspoolTexture = new Texture2D(); this.unspoolTexture.image = imageAsset; this.unspoolSpriteFrame = new SpriteFrame(); this.unspoolSpriteFrame.texture = this.unspoolTexture; } return this.unspoolSpriteFrame; } private drawClipEmblem( c: CanvasRenderingContext2D, x: number, y: number, r: number, color: TargetColor, shape: OldClothClipDef['badgeShape'], ): void { c.save(); c.translate(x, y); ellipse(c, 0, 0, r + 1.5, r + 1.5); c.fillStyle = '#fbf4e4'; c.fill(); c.fillStyle = ROUTE_COLORS[color]; c.strokeStyle = RINGS[color]; c.lineWidth = 1.2; c.beginPath(); if (shape === 'leaf') { c.moveTo(-r * 0.72, r * 0.45); c.quadraticCurveTo(-r * 0.35, -r * 0.78, r * 0.76, -r * 0.52); c.quadraticCurveTo(r * 0.42, r * 0.76, -r * 0.72, r * 0.45); c.closePath(); c.fill(); c.stroke(); } else if (shape === 'flower') { for (let i = 0; i < 5; i++) { const a = (i * Math.PI * 2) / 5 - Math.PI / 2; c.moveTo(Math.cos(a) * r * 0.42 + r * 0.32, Math.sin(a) * r * 0.42); c.arc(Math.cos(a) * r * 0.42, Math.sin(a) * r * 0.42, r * 0.32, 0, Math.PI * 2); } c.fill(); } else if (shape === 'bell') { c.arc(0, -r * 0.1, r * 0.56, Math.PI, 0); c.lineTo(r * 0.68, r * 0.52); c.lineTo(-r * 0.68, r * 0.52); c.closePath(); c.fill(); c.stroke(); } else { c.arc(0, 0, r * 0.58, 0, Math.PI * 2); c.fill(); c.stroke(); } c.restore(); } /** 「拆旧织新」上方旧绣片 2D 实时渲染:底层→顶层压叠投影、交叉阴影断口、旧布木夹与可抽亮色线头脉冲 */ public renderUnspoolClothFrame( config: UnspoolConfig, runtime: UnspoolRuntimeState, timeMs: number, lines: RouteState[] = [], ): void { if (!this.unspoolLayer || !this.unspoolTexture) return; const W = UNSPOOL_CLOTH_WIDTH; const H = UNSPOOL_CLOTH_HEIGHT; const c = this.unspoolLayer.ctx; c.clearRect(0, 0, W, H); const fabric = c.createLinearGradient(0, 0, W, H); fabric.addColorStop(0, '#f5eddb'); fabric.addColorStop(1, '#e9dfcc'); roundRectPath(c, 1, 1, W - 2, H - 2, 15); c.fillStyle = fabric; c.fill(); c.strokeStyle = '#cfc0a7'; c.lineWidth = 1.5; c.stroke(); c.save(); roundRectPath(c, 3, 3, W - 6, H - 6, 14); c.clip(); if (config.patchDividerX) { const dx = config.patchDividerX; c.fillStyle = '#e7eef1'; c.fillRect(dx, 0, W - dx, H); c.fillStyle = '#6f8ea11c'; for (let x = dx + 6; x < W; x += 12) c.fillRect(x, 0, 1.2, H); for (let y = 6; y < H; y += 12) c.fillRect(dx, y, W - dx, 1.2); c.fillStyle = '#8069490f'; for (let x = 3; x < dx; x += 5) c.fillRect(x, 0, 0.8, H); c.beginPath(); c.moveTo(dx, 6); c.lineTo(dx, H - 6); c.strokeStyle = '#8f775d99'; c.lineWidth = 1.5; c.setLineDash([4, 4]); c.stroke(); c.setLineDash([]); } else { c.fillStyle = '#8069490d'; for (let x = 3; x < W; x += 5) c.fillRect(x, 0, 0.7, H); c.fillStyle = '#ffffff66'; for (let y = 3; y < H; y += 5) c.fillRect(0, y, W, 0.8); } roundRectPath(c, 9, 9, W - 18, H - 18, 9); c.strokeStyle = '#9c836d66'; c.lineWidth = 1; c.setLineDash([3, 4]); c.stroke(); c.restore(); const removed = new Set(runtime.removedIds); const remaining = config.strands.filter(s => !removed.has(s.id)); if (remaining.length === 0) { c.fillStyle = '#5f4627'; c.font = `700 18px ${SERIF_FONT}`; c.textAlign = 'center'; c.textBaseline = 'middle'; c.fillText('旧线已拆完,拖蚂蚁开始落针', W / 2, H / 2); this.unspoolTexture.uploadData(this.unspoolLayer.canvas); return; } const coversSomeone = new Set(); for (const s of remaining) { for (const cid of s.coveredBy) { if (!removed.has(cid)) coversSomeone.add(cid); } } for (const strand of remaining) { const lockedClip = isStrandClipLocked(config, strand, lines); const exposed = isStrandExposed(config, strand, removed, lines); const isUpper = exposed || coversSomeone.has(strand.id); const isAnim = runtime.animating?.strandId === strand.id; const progress = isAnim ? Math.min(1, Math.max(0, runtime.animating!.progress)) : 0; const rawPts = strand.points.map(([x, y]) => ({ x, y })); const segLens: number[] = []; let totalLen = 0; for (let i = 1; i < rawPts.length; i++) { const d = Math.hypot(rawPts[i].x - rawPts[i - 1].x, rawPts[i].y - rawPts[i - 1].y); segLens.push(d); totalLen += d; } const keepLen = totalLen * (1 - progress); const drawPts: Point2D[] = [rawPts[0]]; let acc = 0; for (let i = 1; i < rawPts.length; i++) { const seg = segLens[i - 1]; if (acc + seg >= keepLen) { const t = seg > 0 ? Math.max(0, Math.min(1, (keepLen - acc) / seg)) : 0; drawPts.push({ x: rawPts[i - 1].x + (rawPts[i].x - rawPts[i - 1].x) * t, y: rawPts[i - 1].y + (rawPts[i].y - rawPts[i - 1].y) * t, }); break; } drawPts.push(rawPts[i]); acc += seg; } const palette = YARN_PALETTES[strand.color]; const mainColor = ROUTE_COLORS[strand.color]; const darkColor = RINGS[strand.color]; const lightColor = palette[3]; const width = exposed ? 13.5 : 10.5; c.save(); c.lineCap = 'round'; c.lineJoin = 'round'; c.globalAlpha = exposed ? 1 : lockedClip ? 0.56 : 0.72; if (drawPts.length >= 2) { const traceSmoothPath = () => { c.beginPath(); c.moveTo(drawPts[0].x, drawPts[0].y); for (let i = 1; i < drawPts.length - 1; i++) { const prev = drawPts[i - 1]; const joint = drawPts[i]; const next = drawPts[i + 1]; const inScale = Math.min(0.25, 9 / Math.hypot(joint.x - prev.x, joint.y - prev.y)); const outScale = Math.min(0.25, 9 / Math.hypot(next.x - joint.x, next.y - joint.y)); c.lineTo(joint.x + (prev.x - joint.x) * inScale, joint.y + (prev.y - joint.y) * inScale); c.quadraticCurveTo( joint.x, joint.y, joint.x + (next.x - joint.x) * outScale, joint.y + (next.y - joint.y) * outScale, ); } const last = drawPts[drawPts.length - 1]; c.lineTo(last.x, last.y); }; if (isUpper && coversSomeone.has(strand.id)) { traceSmoothPath(); c.strokeStyle = '#efe5d2ee'; c.lineWidth = width + 6; c.stroke(); } traceSmoothPath(); c.shadowColor = exposed ? '#3b281496' : '#543f2526'; c.shadowBlur = exposed ? 6 : 1.5; c.shadowOffsetY = exposed ? 3.5 : 1; c.strokeStyle = darkColor; c.lineWidth = width; c.stroke(); c.shadowColor = 'transparent'; c.strokeStyle = mainColor; c.lineWidth = width - 1.4; c.stroke(); c.beginPath(); for (let i = 1; i < drawPts.length; i++) { const start = drawPts[i - 1]; const end = drawPts[i]; const len = Math.hypot(end.x - start.x, end.y - start.y); if (len < 10) continue; const dx = (end.x - start.x) / len; const dy = (end.y - start.y) / len; for (let along = 5; along <= len - 5; along += 4.3) { const x = start.x + dx * along; const y = start.y + dy * along; const radius = width * 0.37; c.moveTo(x + dy * radius - dx * 1.9, y - dx * radius - dy * 1.9); c.quadraticCurveTo(x + dx * 0.5, y + dy * 0.5, x - dy * radius + dx * 1.9, y + dx * radius + dy * 1.9); } } c.strokeStyle = lightColor + '9c'; c.lineWidth = 1.1; c.stroke(); } const tip = drawPts[drawPts.length - 1]; if (isAnim) { this.drawBall(c, tip.x, tip.y, strand.color, 0.45 + progress * 0.58, progress * 11, true); } else if (exposed) { const halo = 12.5 + Math.sin(timeMs / 280 + tip.x * 0.03) * 1.5; ellipse(c, tip.x, tip.y, halo, halo); c.fillStyle = '#fff9e4c8'; c.fill(); c.strokeStyle = darkColor; c.lineWidth = 1.6; c.stroke(); ellipse(c, tip.x, tip.y, 5.5, 5.5); c.fillStyle = mainColor; c.fill(); } c.restore(); } if (config.clips?.length) { for (const clip of config.clips) { const unlocked = isClipUnlocked(clip, lines); c.save(); if (!unlocked) { roundRectPath(c, clip.x, clip.y, clip.w, clip.h, 10); c.fillStyle = '#dce4e888'; c.fill(); c.strokeStyle = '#7b6345aa'; c.lineWidth = 1.6; c.setLineDash([5, 4]); c.stroke(); c.setLineDash([]); const cx = clip.x + 18; const cy = clip.y + clip.h / 2; c.shadowColor = '#2e1d0f55'; c.shadowBlur = 6; c.shadowOffsetY = 2; roundRectPath(c, cx - 22, cy - 30, 44, 60, 10); const wood = c.createLinearGradient(cx - 22, cy - 30, cx + 22, cy + 30); wood.addColorStop(0, '#ab7846'); wood.addColorStop(1, '#784c27'); c.fillStyle = wood; c.fill(); c.shadowColor = 'transparent'; c.strokeStyle = '#4e2f15'; c.lineWidth = 1.8; c.stroke(); c.fillStyle = '#e7c985'; c.fillRect(cx - 18, cy - 3, 36, 5); this.drawClipEmblem(c, cx, cy - 11, 11, clip.badgeColor, clip.badgeShape); c.fillStyle = '#fff5df'; c.font = `700 11px ${SERIF_FONT}`; c.textAlign = 'center'; c.textBaseline = 'middle'; c.fillText(clip.label ?? '旧夹', cx, cy + 16); } else { const cx = clip.x + clip.w - 34; const cy = clip.y + 13; roundRectPath(c, cx - 24, cy - 9, 48, 18, 9); c.fillStyle = '#6b8555d8'; c.fill(); c.fillStyle = '#fffaf0'; c.font = `700 11px ${SERIF_FONT}`; c.textAlign = 'center'; c.textBaseline = 'middle'; c.fillText('已开夹', cx, cy + 0.5); } c.restore(); } } this.unspoolTexture.uploadData(this.unspoolLayer.canvas); } public exportArtwork( state: GameState, meta: { stars?: number; lengthText?: string; chapterName?: string; levelNumber?: number; frame?: PosterFrameDef; quoteText?: string; }, onDone?: (msg: string) => void, ): void { if (!this.activeLevel) return; this.renderAndSavePoster(this.activeLevel, state, meta, onDone); } public exportGalleryArtwork( level: LevelDef, meta: { stars?: number; lengthText?: string; chapterName?: string; levelNumber?: number; frame?: PosterFrameDef; quoteText?: string; }, onDone?: (msg: string) => void, ): void { this.renderAndSavePoster(level, null, meta, onDone); } private renderAndSavePoster( level: LevelDef, state: GameState | null, meta: { stars?: number; lengthText?: string; chapterName?: string; levelNumber?: number; frame?: PosterFrameDef; quoteText?: string; }, onDone?: (msg: string) => void, ): void { const frameTheme = meta.frame ?? POSTER_FRAMES[0]; const PW = 640; const PH = 860; const output = createLayer(PW, PH, 2); const c = output.ctx; // 1) 可定制装裱底板(青瓷松绿 / 温润胡桃 / 山茶胭脂 / 宋韵描金) const bgGrad = c.createLinearGradient(0, 0, 0, PH); bgGrad.addColorStop(0, frameTheme.bgTop); bgGrad.addColorStop(1, frameTheme.bgBottom); c.fillStyle = bgGrad; c.fillRect(0, 0, PW, PH); // 细腻双线装裱边框装饰 roundRectPath(c, 16, 16, PW - 32, PH - 32, 24); c.strokeStyle = frameTheme.border; c.lineWidth = 1.6; c.stroke(); if (frameTheme.id === 'gilded' || frameTheme.id === 'walnut') { roundRectPath(c, 24, 24, PW - 48, PH - 48, 19); c.strokeStyle = frameTheme.border; c.lineWidth = 0.9; c.stroke(); } // 2) 顶部藏品眉题 c.fillStyle = frameTheme.accent; c.font = `600 14px ${SERIF_FONT}`; c.textAlign = 'center'; c.textBaseline = 'middle'; const numTag = meta.levelNumber ? `NO.${String(meta.levelNumber).padStart(2, '0')} · ` : ''; const chTag = meta.chapterName ? `${meta.chapterName}` : '手工刺绣藏品'; c.fillText(`✦ ${numTag}${chTag} ✦`, PW / 2, 46); // 3) 中央木质绣绷(绣品主体上下左右完全居中) const hoopLayer = createLayer(HOOP_WIDTH, HOOP_HEIGHT, 2); const hc = hoopLayer.ctx; const baked = level.artworkImage ? this.ensureArtworkLoaded(level.artworkImage) : null; if (baked && state && this.activeLevel?.id === level.id) { this.drawScene(hc, state, true); } else if (baked) { hc.drawImage(this.background.canvas, 0, 0, HOOP_WIDTH, HOOP_HEIGHT); const box = baked.subjectBox; const fit = Math.min(1.55, 368 / box.w, 368 / box.h); const dw = box.w * fit; const dh = box.h * fit; hc.save(); ellipse(hc, CX, CY, 244, 244); hc.clip(); hc.drawImage(baked.colorCanvas, box.x, box.y, box.w, box.h, CX - dw / 2, CY - dh / 2, dw, dh); hc.restore(); } else { hc.drawImage(this.background.canvas, 0, 0, HOOP_WIDTH, HOOP_HEIGHT); const motifCanvas = createPlatformCanvas(HOOP_WIDTH, HOOP_HEIGHT); const mc = motifCanvas.getContext('2d') as CanvasRenderingContext2D; if (level.motif === 'lotus') drawLotusBase(mc); else if (level.motif === 'sakura') drawSakuraBase(mc); else if (level.motif === 'garden') drawGardenBase(mc); else if (level.motif === 'sunflower') drawFlowerBase(mc); for (const section of MOTIFS[level.motif] ?? []) { this.drawOverlay(mc, section); } if (level.motif === 'sunflower') { mc.drawImage(this.heart.canvas, this.heartBox.x, this.heartBox.y, this.heartBox.w, this.heartBox.h); } const box = this.computeOpaqueBounds(motifCanvas, HOOP_WIDTH, HOOP_HEIGHT); const fit = Math.min(1.18, 368 / box.w, 368 / box.h); const dw = box.w * fit; const dh = box.h * fit; hc.save(); ellipse(hc, CX, CY, 244, 244); hc.clip(); hc.drawImage(motifCanvas, box.x, box.y, box.w, box.h, CX - dw / 2, CY - dh / 2, dw, dh); hc.restore(); } c.drawImage(hoopLayer.canvas, (PW - HOOP_WIDTH) / 2, 62, HOOP_WIDTH, HOOP_HEIGHT); // 4) 底部留白充足的宣纸藏品卡片(含可定制雅致落款 + 小程序码) const cardX = 32; const cardY = 696; const cardW = PW - 64; const cardH = 136; c.save(); c.shadowColor = 'rgba(28, 34, 21, 0.24)'; c.shadowBlur = 18; c.shadowOffsetY = 6; roundRectPath(c, cardX, cardY, cardW, cardH, 20); c.fillStyle = frameTheme.cardBg; c.fill(); c.restore(); roundRectPath(c, cardX, cardY, cardW, cardH, 20); c.strokeStyle = frameTheme.cardStroke; c.lineWidth = 1.8; c.stroke(); // 左侧:Logo + 品牌名 + 落款短句 this.drawPosterLogo(c, cardX + 44, cardY + 40); c.textAlign = 'left'; c.textBaseline = 'middle'; c.fillStyle = '#2f2b20'; c.font = `700 22px ${SERIF_FONT}`; c.fillText('蚂蚁织画', cardX + 78, cardY + 32); c.fillStyle = '#7e7057'; c.font = `500 13px ${SERIF_FONT}`; c.fillText(meta.quoteText || '一针一线 · 慢享手作好时光', cardX + 78, cardY + 53); // 细虚线分隔 c.save(); c.beginPath(); c.moveTo(cardX + 24, cardY + 74); c.lineTo(cardX + cardW - 148, cardY + 74); c.strokeStyle = '#d8c59e'; c.lineWidth = 1.1; c.setLineDash([4, 4]); c.stroke(); c.restore(); // 作品名 + 成绩信息 c.fillStyle = '#2d291e'; c.font = `700 19px ${SERIF_FONT}`; c.fillText(`《${level.name}》`, cardX + 22, cardY + 97); const starCount = meta.stars ?? 3; const starText = '★'.repeat(starCount) + '☆'.repeat(3 - starCount); c.fillStyle = '#b46c1f'; c.font = `700 16px ${SERIF_FONT}`; c.fillText(starText, cardX + 24, cardY + 119); const now = new Date(); const dateStr = `${now.getFullYear()}.${String(now.getMonth() + 1).padStart(2, '0')}.${String(now.getDate()).padStart(2, '0')}`; const infoText = `${meta.lengthText ? '用线 ' + meta.lengthText + ' · ' : ''}${dateStr}`; c.fillStyle = '#73654d'; c.font = `500 13.5px ${SERIF_FONT}`; c.fillText(infoText, cardX + 86, cardY + 119); // 右侧:竖向细隔线 + 小程序码位 c.save(); c.beginPath(); c.moveTo(cardX + cardW - 130, cardY + 20); c.lineTo(cardX + cardW - 130, cardY + cardH - 20); c.strokeStyle = '#e0d0ae'; c.lineWidth = 1.2; c.stroke(); c.restore(); const qrCx = cardX + cardW - 65; const qrCy = cardY + 56; this.drawPosterQrSlot(c, qrCx, qrCy, 38); c.textAlign = 'center'; c.fillStyle = '#76684f'; c.font = `500 11.5px ${SERIF_FONT}`; c.fillText('扫码一起刺绣', qrCx, cardY + 114); if (typeof wx !== 'undefined' && typeof output.canvas.toTempFilePath === 'function') { output.canvas.toTempFilePath({ fileType: 'png', success: (res: any) => { this.saveTempImageToWeChatAlbum(res.tempFilePath, onDone); }, fail: () => onDone?.('导出图片失败'), }); return; } if (typeof document !== 'undefined' && typeof output.canvas.toDataURL === 'function') { const link = document.createElement('a'); link.download = '蚂蚁织画-' + level.name + '.png'; link.href = output.canvas.toDataURL('image/png'); link.click(); onDone?.('已开始下载绣画藏品海报 PNG'); } } /** 微信小游戏相册保存 + 拒绝授权后的 wx.openSetting 友好引导恢复 */ private saveTempImageToWeChatAlbum(filePath: string, onDone?: (msg: string) => void): void { if (typeof wx === 'undefined' || typeof wx.saveImageToPhotosAlbum !== 'function') { onDone?.('当前环境不支持直接保存到相册'); return; } wx.saveImageToPhotosAlbum({ filePath, success: () => onDone?.('绣画海报已保存到相册'), fail: (err: any) => { const msg = String(err?.errMsg || ''); const denied = msg.includes('auth deny') || msg.includes('auth denied') || msg.includes('authorize'); if (denied && typeof wx.showModal === 'function' && typeof wx.openSetting === 'function') { wx.showModal({ title: '需要相册权限', content: '开启相册写入权限后,即可把专属手绣藏品海报保存到手机相册。', confirmText: '去开启', cancelText: '稍后再说', success: (modalRes: any) => { if (!modalRes?.confirm) { onDone?.('未开启相册权限,可在右上角设置中随时打开'); return; } wx.openSetting({ success: (settingRes: any) => { if (settingRes?.authSetting?.['scope.writePhotosAlbum']) { wx.saveImageToPhotosAlbum({ filePath, success: () => onDone?.('绣画海报已保存到相册'), fail: () => onDone?.('保存失败,请稍后重试'), }); } else { onDone?.('尚未勾选相册权限'); } }, fail: () => onDone?.('无法打开权限设置页'), }); }, }); return; } onDone?.(msg.includes('cancel') ? '已取消保存' : '保存失败或未授权相册'); }, }); } }