// 第一人称枪模:2D Canvas 手绘,但用「侧面 + 顶面挤压」画出 3/4 视角的体积感, // 配色压到和场景同一个明度区间,避免像贴纸。 // 整把枪烤进离屏画布,每帧只 drawImage 一次(手机上省很多)。 const TOP_DX = 18; const TOP_DY = -13; const C = { metal: ["#343b44", "#4b545f"], metalDark: ["#2a2f36", "#3a424b"], metalMid: ["#3d444d", "#535c67"], wood: ["#473527", "#54402f"], woodTop: ["#52402f", "#5f4b38"], mag: ["#33271d", "#3e3025"], grip: ["#2e2b26", "#3d3a33"], glove: ["#3a3733", "#4a463f"], steel: ["#8a939d", "#a8b2bc"], }; export class ViewModel { constructor(canvas) { this.canvas = canvas; this.g = canvas.getContext("2d"); const coarse = window.matchMedia && window.matchMedia("(pointer: coarse)").matches; this.dpr = Math.min(window.devicePixelRatio || 1, coarse ? 1.5 : 2); this._cache = null; this._cacheId = -1; this.resize(); } resize() { const w = Math.max(1, Math.round(window.innerWidth * this.dpr)); const h = Math.max(1, Math.round(window.innerHeight * this.dpr)); if (this.canvas.width !== w || this.canvas.height !== h) { this.canvas.width = w; this.canvas.height = h; } } clear() { this.g.setTransform(1, 0, 0, 1, 0, 0); this.g.clearRect(0, 0, this.canvas.width, this.canvas.height); } /** s = {weapon, bobX, bobY, kick, swayX, muzzle, alive, ads, crouch} */ render(s) { this.resize(); const g = this.g; this.clear(); if (!s || s.alive === false) return; const W = this.canvas.width; const H = this.canvas.height; const touch = document.body.classList.contains("touch"); const ads = s.ads || 0; const crouch = s.crouch || 0; // 开镜:枪身收窄、角度放平、往屏幕中心收;蹲下:整把枪随视点下沉 const scale = Math.min(W / 1280, H / 720) * (touch ? 0.66 : 0.78) * (1 - ads * 0.12); const kick = s.kick || 0; g.save(); // 锚点在枪口,枪身往右下角延伸 g.translate( W * ((touch ? 0.55 : 0.52) - ads * 0.08) + (s.bobX || 0) * 900 * (1 - ads * 0.6) + (s.swayX || 0) * 500 * (1 - ads * 0.6) + kick * 22, H * ((touch ? 0.63 : 0.58) + crouch * 0.04) + (s.bobY || 0) * 900 + kick * 9 ); g.rotate(0.3 - ads * 0.24 + kick * 0.022); g.scale(scale, scale); const art = this.bake(s.weapon); g.drawImage(art.shadow, -54, -140, 900, 400); g.drawImage(art.gun, -54, -140, 900, 400); if (s.muzzle > 0) { const a = Math.min(1, s.muzzle * 14); this.drawFlash(g, 14, 100, a); } g.restore(); } /* ------------------------------- 烘焙 ------------------------------- */ bake(weaponId) { if (this._cache && this._cacheId === weaponId) return this._cache; const pad = 40; const w = 900; const h = 400; const dpr = this.dpr; // 1) 先把枪画到一张透明画布上 const gun = document.createElement("canvas"); gun.width = Math.round(w * dpr); gun.height = Math.round(h * dpr); const gg = gun.getContext("2d"); gg.scale(dpr, dpr); gg.translate(pad, pad); gg.lineJoin = "round"; if (weaponId === 1) this.drawPistol(gg); else this.drawRifle(gg); // 2) 用它的剪影做一层模糊投影(沿轮廓,不是一块矩形) const shadow = document.createElement("canvas"); shadow.width = gun.width; shadow.height = gun.height; const sg = shadow.getContext("2d"); sg.filter = "blur(9px)"; sg.drawImage(gun, 8 * dpr, 16 * dpr); sg.filter = "none"; sg.globalCompositeOperation = "source-in"; sg.fillStyle = "rgba(0,0,0,0.55)"; sg.fillRect(0, 0, shadow.width, shadow.height); this._cache = { gun, shadow }; this._cacheId = weaponId; return this._cache; } /* ------------------------------- AK-47 ------------------------------- */ drawRifle(g) { // 枪管 / 消焰器 / 准星 / 导气 this.box(g, 58, 96, 300, 118, C.metalDark); this.box(g, 14, 86, 58, 126, C.metalDark); this.box(g, 240, 94, 282, 134, C.metalMid); this.box(g, 252, 58, 270, 96, C.metalDark); this.box(g, 276, 82, 456, 100, C.metalDark); // 护木(木) this.poly(g, [[300, 84], [472, 84], [464, 152], [308, 152]], C.wood); this.poly(g, [[306, 68], [462, 68], [458, 88], [310, 88]], C.woodTop); // 机匣 this.box(g, 462, 80, 690, 160, C.metal); this.box(g, 468, 62, 684, 82, C.metalMid); this.box(g, 634, 44, 678, 64, C.metalDark); // 照门 // 枪托(收腰 + 托底) this.poly(g, [[690, 84], [806, 100], [812, 172], [700, 164], [690, 140]], C.metal); this.poly(g, [[800, 96], [818, 100], [822, 176], [806, 172]], C.metalDark); // 弹匣(弧形)+ 握把 this.poly(g, [[548, 150], [610, 150], [650, 264], [636, 294], [572, 288]], C.mag); this.poly(g, [[686, 152], [724, 152], [744, 262], [698, 272]], C.grip); // 扳机护圈 g.strokeStyle = "rgba(28,32,38,0.95)"; g.lineWidth = 9; g.beginPath(); g.moveTo(650, 158); g.quadraticCurveTo(672, 200, 708, 172); g.stroke(); // 手(左手托护木、右手握把) this.glove(g, 348, 132, 434, 200, 0.05, 1, true); this.glove(g, 692, 174, 754, 252, 0.3, -1, false); // 细节:木纹、铆钉、高光 g.save(); g.globalAlpha = 0.22; g.strokeStyle = "#3a2410"; g.lineWidth = 2; for (let i = 0; i < 3; i++) { g.beginPath(); g.moveTo(312, 104 + i * 16); g.quadraticCurveTo(380, 100 + i * 16, 458, 108 + i * 16); g.stroke(); } g.restore(); g.fillStyle = "rgba(24,28,34,0.9)"; for (const [x, y] of [[486, 120], [486, 146], [666, 120], [666, 146]]) { g.beginPath(); g.arc(x, y, 2.6, 0, Math.PI * 2); g.fill(); } // 弹匣压筋 + 底板 g.save(); g.globalAlpha = 0.4; g.strokeStyle = "#33230f"; g.lineWidth = 4; for (let i = 0; i < 3; i++) { g.beginPath(); g.moveTo(566, 182 + i * 34); g.quadraticCurveTo(602, 186 + i * 34, 636, 190 + i * 34); g.stroke(); } g.globalAlpha = 0.9; g.fillStyle = "#3a2a18"; g.beginPath(); g.moveTo(566, 282); g.lineTo(636, 288); g.lineTo(640, 300); g.lineTo(570, 296); g.closePath(); g.fill(); g.restore(); this.rim(g, [[300, 84], [472, 84]], 0.4); this.rim(g, [[462, 80], [690, 80]], 0.5); this.rim(g, [[468, 62], [684, 62]], 0.55); this.rim(g, [[58, 96], [240, 96]], 0.45); this.rim(g, [[306, 68], [462, 68]], 0.4); } /** 手:圆角 + 拇指凸起 + 指缝,读起来像只手套 */ glove(g, x0, y0, x1, y1, tilt, dir, fingers) { g.save(); g.translate((x0 + x1) / 2, (y0 + y1) / 2); g.rotate(tilt); const w = x1 - x0; const h = y1 - y0; const r = Math.min(w, h) * 0.3; const grd = g.createLinearGradient(0, -h / 2, 0, h / 2); grd.addColorStop(0, "#3d3934"); grd.addColorStop(1, "#282622"); g.fillStyle = grd; g.beginPath(); g.moveTo(-w / 2 + r, -h / 2); g.quadraticCurveTo(w / 2, -h / 2 - 4, w / 2, -h / 2 + r); g.quadraticCurveTo(w / 2 + 3, h / 2 - r, w / 2 - r, h / 2); g.quadraticCurveTo(-w / 2, h / 2 + 4, -w / 2, h / 2 - r); g.quadraticCurveTo(-w / 2 - 3, -h / 2 + r, -w / 2 + r, -h / 2); g.closePath(); g.fill(); // 拇指 g.beginPath(); g.ellipse(-w * 0.18 * dir, -h * 0.42, w * 0.2, h * 0.16, -0.3 * dir, 0, Math.PI * 2); g.fillStyle = "#46423b"; g.fill(); // 指缝 g.strokeStyle = "rgba(18,20,22,0.55)"; g.lineWidth = 2; for (let i = 0; i < 2; i++) { g.beginPath(); g.moveTo(-w * 0.1 + i * w * 0.26, -h * 0.05); g.quadraticCurveTo(w * 0.02 + i * w * 0.26, h * 0.16, -w * 0.06 + i * w * 0.26, h * 0.34); g.stroke(); } g.strokeStyle = "rgba(12,14,17,0.5)"; g.lineWidth = 1.5; g.stroke(); // 搭在护木上的手指 if (fingers) { for (let i = 0; i < 3; i++) { const fx = -w * 0.32 + i * w * 0.3; g.beginPath(); g.roundRect(fx, -h * 0.62, w * 0.26, h * 0.3, 5); g.fillStyle = i === 1 ? "#403c36" : "#45413a"; g.fill(); g.strokeStyle = "rgba(12,14,17,0.4)"; g.lineWidth = 1.2; g.stroke(); } } g.restore(); } /* ------------------------------- 沙鹰 ------------------------------- */ drawPistol(g) { // 套筒 this.box(g, 120, 84, 430, 132, C.metal); // 套筒前端 this.box(g, 90, 90, 122, 128, C.metalMid); // 枪口 this.box(g, 66, 96, 92, 120, C.steel); // 机匣 / 扳机护圈 this.box(g, 140, 132, 410, 160, C.metalDark); g.strokeStyle = "rgba(28,32,38,0.95)"; g.lineWidth = 9; g.beginPath(); g.moveTo(210, 158); g.quadraticCurveTo(238, 206, 300, 176); g.stroke(); // 握把 this.poly(g, [[330, 150], [420, 150], [446, 300], [352, 300]], C.grip); // 手 this.glove(g, 350, 182, 448, 300, 0.34, -1); // 准星 / 照门 this.box(g, 108, 70, 128, 90, C.metalDark); this.box(g, 392, 66, 424, 86, C.metalDark); this.rim(g, [[120, 84], [430, 84]], 0.5); this.rim(g, [[330, 150], [420, 150]], 0.35); } /* ------------------------------- 绘制件 ------------------------------- */ /** 带顶面的长方体:先画顶面(亮),再画侧面(暗),最后描边 */ box(g, x0, y0, x1, y1, colors) { const [side, top] = colors; // 顶面 g.beginPath(); g.moveTo(x0, y0); g.lineTo(x1, y0); g.lineTo(x1 + TOP_DX, y0 + TOP_DY); g.lineTo(x0 + TOP_DX, y0 + TOP_DY); g.closePath(); g.fillStyle = top; g.fill(); // 侧面 const grd = g.createLinearGradient(0, y0, 0, y1); grd.addColorStop(0, shade(side, 14)); grd.addColorStop(0.55, side); grd.addColorStop(1, shade(side, -18)); g.fillStyle = grd; g.fillRect(x0, y0, x1 - x0, y1 - y0); g.strokeStyle = "rgba(10,14,19,0.34)"; g.lineWidth = 1.1; g.strokeRect(x0 + 0.5, y0 + 0.5, x1 - x0 - 1, y1 - y0 - 1); } /** 任意侧面多边形 + 顶面(沿顶边挤出一条,模拟 3/4 视角) */ poly(g, pts, colors, topEdge) { const [side, top] = colors; if (topEdge !== null) { const e = topEdge || [pts[0], pts[1]]; g.beginPath(); g.moveTo(e[0][0], e[0][1]); g.lineTo(e[1][0], e[1][1]); g.lineTo(e[1][0] + TOP_DX, e[1][1] + TOP_DY); g.lineTo(e[0][0] + TOP_DX, e[0][1] + TOP_DY); g.closePath(); g.fillStyle = top; g.fill(); } // 侧面 const xs = pts.map((p) => p[0]); const ys = pts.map((p) => p[1]); const grd = g.createLinearGradient(0, Math.min(...ys), 0, Math.max(...ys)); grd.addColorStop(0, shade(side, 12)); grd.addColorStop(0.6, side); grd.addColorStop(1, shade(side, -20)); g.fillStyle = grd; g.beginPath(); pts.forEach(([x, y], i) => (i ? g.lineTo(x, y) : g.moveTo(x, y))); g.closePath(); g.fill(); g.strokeStyle = "rgba(10,14,19,0.38)"; g.lineWidth = 1.2; g.stroke(); } /** 顶边高光:让边缘吃到场景里的暖光 */ rim(g, pts, alpha) { g.save(); g.strokeStyle = `rgba(232,208,178,${alpha})`; g.lineWidth = 1.8; g.beginPath(); pts.forEach(([x, y], i) => (i ? g.lineTo(x, y) : g.moveTo(x, y))); g.stroke(); g.restore(); } drawFlash(g, x, y, a) { g.save(); g.translate(x, y); g.globalCompositeOperation = "lighter"; const r = 95 * (0.6 + a * 0.7); const grd = g.createRadialGradient(0, 0, 0, 0, 0, r); grd.addColorStop(0, `rgba(255,246,214,${0.95 * a})`); grd.addColorStop(0.35, `rgba(255,190,90,${0.7 * a})`); grd.addColorStop(1, "rgba(255,120,20,0)"); g.fillStyle = grd; g.beginPath(); g.arc(0, 0, r, 0, Math.PI * 2); g.fill(); g.strokeStyle = `rgba(255,225,160,${0.8 * a})`; g.lineWidth = 5; g.beginPath(); g.moveTo(-r * 0.85, 0); g.lineTo(r * 0.55, 0); g.moveTo(0, -r * 0.5); g.lineTo(0, r * 0.5); g.stroke(); g.restore(); } } /** #rrggbb 调亮/调暗 */ function shade(hex, amount) { const n = parseInt(hex.slice(1), 16); const clamp = (v) => Math.max(0, Math.min(255, v)); const r = clamp(((n >> 16) & 255) + amount); const gg = clamp(((n >> 8) & 255) + amount); const b = clamp((n & 255) + amount); return `rgb(${r},${gg},${b})`; }