Files
web-Iot/src/components/devices/CarReverseGuideLine.tsx

133 lines
4.0 KiB
TypeScript
Raw Normal View History

import React, { useEffect, useRef } from 'react';
type GuideLineProps = {
/** 方向 forward前进 reverse倒车 */
direction: 'forward' | 'reverse';
/** 方向盘转角,单位°:左转负数,右转正数,范围建议 -45 ~ 45 */
steer: number;
/** 画布宽度 */
width?: number;
/** 画布高度 */
height?: number;
/** 引导线底部(车尾端)两条线间距 */
bottomGap?: number;
/** 引导线顶部(远端)两条线间距 */
topGap?: number;
};
const CarReverseGuideLine: React.FC<GuideLineProps> = ({
direction,
steer,
width = 640,
height = 480,
bottomGap = 100,
topGap = 160,
}) => {
const canvasRef = useRef<HTMLCanvasElement>(null);
useEffect(() => {
const canvas = canvasRef.current;
if (!canvas) return;
const ctx = canvas.getContext('2d');
if (!ctx) return;
ctx.clearRect(0, 0, width, height);
// 车尾中心点(画布底部)
const baseX = width / 2;
const baseY = height - 20;
const totalDist = direction === 'reverse' ? height * 0.82 : height * 0.68;
const maxWheelAngle = 45;
// 限制车轮转角范围
const wheelAngleRad = Math.max(-maxWheelAngle, Math.min(maxWheelAngle, steer)) * Math.PI / 180;
const step = 2; // 步长,越小曲线越平滑
// 迭代计算轨迹点函数
const getPathPoints = (side: 'left' | 'right'): [number, number][] => {
const points: [number, number][] = [];
let x = baseX;
let y = baseY;
let angle = -Math.PI / 2; // 初始朝向:向上(画布Y减小方向)
const stepCount = Math.floor(totalDist / step);
points.push([x, y]);
for (let i = 0; i < stepCount; i++) {
// 当前进度 0~1,0=底部起点,1=最远顶部
const progress = i / stepCount;
// 线性插值:从底部gap过渡到顶部gap
const currentGap = bottomGap * (1 - progress) + topGap * progress;
const sideOffset = side === 'left' ? -currentGap / 2 : currentGap / 2;
if (Math.abs(wheelAngleRad) < 0.001) {
// 回正:直线向上
y -= step;
} else {
// 有转向:改变朝向,再前进一小步
const turnStepRad = step * Math.tan(wheelAngleRad) / 240;
// 分母240:控制转弯曲率,调大转弯更平缓;调小弯更急
angle += turnStepRad;
x += Math.cos(angle) * step;
y += Math.sin(angle) * step;
}
// 叠加当前侧边偏移
const px = x + sideOffset;
const py = y;
points.push([px, py]);
// 超出画布提前停止绘制
if (py < 0 || px < 0 || px > width) break;
}
return points;
};
// 绘制左右两条轨迹
ctx.lineWidth = 3;
ctx.strokeStyle = '#0f0';
ctx.setLineDash([]);
const leftPath = getPathPoints('left');
const rightPath = getPathPoints('right');
const drawPath = (pts: [number, number][]) => {
ctx.beginPath();
pts.forEach(([px, py], idx) => {
if (idx === 0) ctx.moveTo(px, py);
else ctx.lineTo(px, py);
});
ctx.stroke();
ctx.beginPath(); // 强制清空当前路径缓存!重点!
};
drawPath(leftPath);
drawPath(rightPath);
// ========== 横向距离标记虚线 ==========
ctx.strokeStyle = '#ff0';
ctx.lineWidth = 2;
ctx.setLineDash([8, 6]);
const markCount = 3;
for (let m = 1; m <= markCount; m++) {
const ratio = m / (markCount + 1);
const idx = Math.floor(leftPath.length * ratio);
if (idx >= leftPath.length) continue;
const [lx, ly] = leftPath[idx];
const [rx, ry] = rightPath[idx];
ctx.beginPath();
ctx.moveTo(lx, ly);
ctx.lineTo(rx, ry);
ctx.stroke();
}
// ========== 车身底部框 ==========
//ctx.setLineDash([]);
//ctx.strokeStyle = '#0ff';
//ctx.lineWidth = 2;
//ctx.beginPath();
//ctx.rect(baseX - bottomGap / 2, baseY - 30, bottomGap, 30);
//ctx.stroke();
}, [direction, steer, width, height, bottomGap, topGap]);
return (
<div style={{ position: 'relative', width, height }}>
{/* 背景倒车画面,可以替换 video / img */}
<canvas
ref={canvasRef}
width={width}
height={height}
style={{ position: 'absolute', inset: 0, pointerEvents: 'none' }}
/>
</div>
);
};
export default CarReverseGuideLine;