/** * 画面上的叠加图形:瞄准器、网格,以及"图像落在哪"的那一道换算。 * * # 为什么单独拆出来 * * 这三段代码现在有两个使用者:`VideoSurface` 里的真画面,和设置面板里的小预览窗。预览窗 * 存在的意义就是"改完立刻看见",如果它另写一套绘制,两边迟早会不一样——而出现差异时, * 用户看到的是预览里对、画面上偏,这比没有预览更糟。 * * 所以这里只放**纯函数**:给 ctx 和数字,画完就退出。不碰 React、不碰请求、不认识 store。 */ import type { CrosshairConfig, GridConfig } from "./types"; /** 图像在窗格里的落点与大小(CSS 像素)。 */ export interface Geometry { left: number; top: number; drawWidth: number; drawHeight: number; } export interface Box { clientWidth: number; clientHeight: number; } /** * 把一帧按 `contain` 放进窗格,再乘缩放。 * * 平移是**屏幕像素**而不是画面比例:拖动时手感必须是 1:1 的,否则放大之后拖动会变黏。 */ export function computeLayout( box: Box, frameWidth: number, frameHeight: number, view: { zoom: number; x: number; y: number }, ): Geometry { const width = box.clientWidth; const height = box.clientHeight; if (frameWidth <= 0 || frameHeight <= 0) { return { left: 0, top: 0, drawWidth: 0, drawHeight: 0 }; } const fit = Math.min(width / frameWidth, height / frameHeight); const drawWidth = frameWidth * fit * view.zoom; const drawHeight = frameHeight * fit * view.zoom; return { left: (width - drawWidth) / 2 + view.x, top: (height - drawHeight) / 2 + view.y, drawWidth, drawHeight, }; } /** * 瞄准点的屏幕位置。 * * 图像坐标系里的 `(0.5 + offsetX, 0.5 + offsetY)` 再换到屏幕。镜像时横向要翻过来, * 否则准星会站在画面的另一边——偏移量是相对**摄像头光轴**定的,光轴在镜像之后换了位置。 */ export function aimPoint( geometry: Geometry, crosshair: CrosshairConfig | null, mirrored: boolean, canvasWidth: number, canvasHeight: number, ): { x: number; y: number } { if (!crosshair || geometry.drawWidth <= 0) { return { x: canvasWidth / 2, y: canvasHeight / 2 }; } const rawX = geometry.left + (0.5 + crosshair.offsetX) * geometry.drawWidth; return { x: mirrored ? canvasWidth - rawX : rawX, y: geometry.top + (0.5 + crosshair.offsetY) * geometry.drawHeight, }; } /** 画网格。网格画在**图像矩形**里:它标的是画面里的比例,用来估距离。 */ export function paintGrid( ctx: CanvasRenderingContext2D, geometry: Geometry, grid: GridConfig, ): void { if (geometry.drawWidth <= 0 || geometry.drawHeight <= 0) return; ctx.save(); ctx.strokeStyle = `rgba(255, 255, 255, ${grid.opacity})`; ctx.lineWidth = 1; const x0 = geometry.left; const y0 = geometry.top; const x1 = x0 + geometry.drawWidth; const y1 = y0 + geometry.drawHeight; if (grid.style === "thirds") { for (const fraction of [1 / 3, 2 / 3]) { line(ctx, x0 + geometry.drawWidth * fraction, y0, x0 + geometry.drawWidth * fraction, y1); line(ctx, x0, y0 + geometry.drawHeight * fraction, x1, y0 + geometry.drawHeight * fraction); } } else if (grid.style === "squares") { const divisions = Math.max(2, grid.divisions); for (let i = 1; i < divisions; i += 1) { const fraction = i / divisions; line(ctx, x0 + geometry.drawWidth * fraction, y0, x0 + geometry.drawWidth * fraction, y1); line(ctx, x0, y0 + geometry.drawHeight * fraction, x1, y0 + geometry.drawHeight * fraction); } } else { line(ctx, x0 + geometry.drawWidth / 2, y0, x0 + geometry.drawWidth / 2, y1); line(ctx, x0, y0 + geometry.drawHeight / 2, x1, y0 + geometry.drawHeight / 2); } ctx.restore(); } /** * 画瞄准器。 * * 两遍描边:先一圈半透明的深色(光环),再画本体。浅色背景下亮绿准星会消失,这一圈让 * 它在任何画面上都看得见——这是可读性要求,不是装饰。 */ export function paintCrosshair( ctx: CanvasRenderingContext2D, cx: number, cy: number, shortSide: number, config: CrosshairConfig, ): void { const arm = Math.max(3, config.size * shortSide); const gap = config.gap * shortSide; const thickness = Math.max(1, config.thickness * shortSide); const stroke = (color: string, width: number, radius: number) => { ctx.strokeStyle = color; ctx.lineWidth = width; ctx.beginPath(); if (config.style === "circle-dot") { ctx.arc(cx, cy, radius, 0, Math.PI * 2); } else { ctx.moveTo(cx - radius, cy); ctx.lineTo(cx - gap, cy); ctx.moveTo(cx + gap, cy); ctx.lineTo(cx + radius, cy); ctx.moveTo(cx, cy - radius); ctx.lineTo(cx, cy - gap); ctx.moveTo(cx, cy + gap); ctx.lineTo(cx, cy + radius); } ctx.stroke(); }; stroke("rgba(0, 0, 0, 0.55)", thickness + 2, arm + 1); stroke(config.color, thickness, arm); if (config.style !== "cross") { const dot = Math.max(1.2, thickness * 0.9); ctx.fillStyle = "rgba(0, 0, 0, 0.55)"; ctx.beginPath(); ctx.arc(cx, cy, dot + 1, 0, Math.PI * 2); ctx.fill(); ctx.fillStyle = config.color; ctx.beginPath(); ctx.arc(cx, cy, dot, 0, Math.PI * 2); ctx.fill(); } } /** 半像素偏移,让 1px 的线落在像素中心而不是跨两个像素发虚。 */ function line( ctx: CanvasRenderingContext2D, x0: number, y0: number, x1: number, y1: number, ): void { ctx.beginPath(); ctx.moveTo(Math.round(x0) + 0.5, Math.round(y0) + 0.5); ctx.lineTo(Math.round(x1) + 0.5, Math.round(y1) + 0.5); ctx.stroke(); }