车道辅助线 初始化

This commit is contained in:
mmc
2026-10-13 15:29:47 +08:00
parent d20ebb3fab
commit 694a78ef9b

View File

@@ -1,4 +1,4 @@
import React from 'react';
import React, { useRef, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
interface ReverseCameraOverlayProps {
@@ -7,17 +7,9 @@ interface ReverseCameraOverlayProps {
visible?: boolean;
}
/**
* 倒车影像叠加层 - 模拟真实汽车倒车影像效果
*
* 核心逻辑:
* - 直行时:左右两条引导线为平行直线
* - 转向时:两条线作为整体向同一侧弯曲(平行曲线),模拟真实倒车轨迹
* - 颜色从下到上:红(近/危险) → 黄 → 绿(远/安全)
*/
const ReverseCameraOverlay: React.FC<ReverseCameraOverlayProps> = ({ x, y, visible = true }) => {
const { t } = useTranslation();
if (!visible) return null;
const canvasRef = useRef<HTMLCanvasElement>(null);
const clamp = (v: number) => Math.max(-1, Math.min(1, v));
const steer = clamp(x || 0);
@@ -27,297 +19,214 @@ const ReverseCameraOverlay: React.FC<ReverseCameraOverlayProps> = ({ x, y, visib
const isForward = move < -0.05;
const guideOpacity = isReverse ? 1 : isForward ? 0.25 : 0.5;
// ===== 基础几何 =====
// SVG 坐标系
const W = 400;
const H = 260;
// 车宽(画面底部两条引导线的间距)
const LOGICAL_W = 400;
const LOGICAL_H = 260;
const carWidth = 160;
const carCenter = W / 2; // 200
const carLeft = carCenter - carWidth / 2; // 120
const carRight = carCenter + carWidth / 2; // 280
// 引导线起点(画面底部,紧贴车尾)
const carCenter = LOGICAL_W / 2;
const carLeft = carCenter - carWidth / 2;
const carRight = carCenter + carWidth / 2;
const bottomY = 250;
// 引导线终点(画面上方远处)
const topY = 20;
// ===== 转向曲线生成 =====
// 真实倒车影像中,方向盘转动 → 两条引导线作为"整体"向转向侧弯曲
// 采用三次贝塞尔,控制点随 steer 偏移,左右两条线保持平行(间距恒定)
const isStraight = Math.abs(steer) < 0.03;
// 转向强度决定弯曲程度(弧度)
const turnStrength = steer; // -1 ~ 1
const topOffset = steer * 340;
const topSpacing = 40;
//const leftTopX = carCenter + topOffset - topSpacing / 2;
//const rightTopX = carCenter + topOffset + topSpacing / 2;
// 弯曲量:顶部偏移越大,弧度越明显
// 左转(steer<0) → 顶部向左偏移;右转(steer>0) → 顶部向右偏移
const topOffset = turnStrength * 140; // 顶部最大偏移量
const bend = 0.30;
const leftTopX = carCenter + topOffset - topSpacing / 2 * (1 - bend);
const rightTopX = carCenter + topOffset + topSpacing / 2 * (1 + bend);
// 左右两条线的水平间距(保持恒定,模拟车宽)
const lineSpacing = 60; // 两条线在顶部的水平间距(透视收缩后比底部窄)
const ctrl1Y = 200;
const ctrl2Y = 110;
// 左侧线终点
const leftTopX = carCenter + topOffset - lineSpacing / 2;
// 右侧线终点
const rightTopX = carCenter + topOffset + lineSpacing / 2;
const cubicBezier = (t: number, p0: number, p1: number, p2: number, p3: number): number => {
const u = 1 - t;
return u * u * u * p0 + 3 * u * u * t * p1 + 3 * u * t * t * p2 + t * t * t * p3;
};
// 控制点:让曲线从底部的"车宽"平滑过渡到顶部的"收缩间距"
// 控制点1(靠近底部):基本保持车宽,轻微随转向偏移
const ctrl1Offset = turnStrength * 30;
const ctrl1Y = 190;
const buildPoints = (
startX: number,
endX: number,
isStraight: boolean,
// 新增:控制点权重,外侧线传更大的值
aggressiveness: number = 0.30
) => {
const SEG = 80;
const pts: { x: number; y: number }[] = [];
// 关键:控制点权重随 aggressiveness 增大 → 曲线中段更凸向转向侧
const c1x = startX + (endX - startX) * aggressiveness;
const c2x = startX + (endX - startX) * (aggressiveness + 0.32);
for (let i = 0; i <= SEG; i++) {
const t = i / SEG;
if (isStraight) {
pts.push({ x: startX, y: bottomY - (bottomY - topY) * t });
} else {
pts.push({
x: cubicBezier(t, startX, c1x, c2x, endX),
y: cubicBezier(t, bottomY, ctrl1Y, ctrl2Y, topY),
});
}
}
return pts;
};
// 控制点2(靠近顶部):跟随顶部偏移,形成自然弧线
const ctrl2Offset = topOffset * 0.85;
const ctrl2Y = 90;
const colorStops = [
{ from: 0, to: 0.30, color: '#ef4444' },
{ from: 0.30, to: 0.65, color: '#fbbf24' },
{ from: 0.65, to: 1.00, color: '#22c55e' },
];
// 左侧引导线路径
const leftPath = `
M ${carLeft} ${bottomY}
C ${carLeft + ctrl1Offset} ${ctrl1Y},
${leftTopX + ctrl2Offset * 0.15} ${ctrl2Y},
${leftTopX} ${topY}
`;
const drawGuideLine = (ctx: CanvasRenderingContext2D, startX: number, endX: number, aggressiveness: number = 0.30
) => {
const pts = buildPoints(startX, endX, isStraight, aggressiveness);
const SEG = pts.length - 1;
colorStops.forEach(({ from, to, color }) => {
const si = Math.round(from * SEG);
const ei = Math.round(to * SEG);
ctx.beginPath();
ctx.strokeStyle = color;
ctx.lineWidth = 12;
ctx.lineCap = 'round';
ctx.lineJoin = 'round';
ctx.shadowColor = color;
ctx.shadowBlur = color === '#22c55e' ? 12 : 8;
ctx.globalAlpha = 0.4;
for (let i = si; i <= ei; i++) {
const { x, y } = pts[i];
if (i === si) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
}
ctx.stroke();
ctx.globalAlpha = 1;
ctx.beginPath();
ctx.lineWidth = 6;
ctx.shadowBlur = 0;
for (let i = si; i <= ei; i++) {
const { x, y } = pts[i];
if (i === si) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
}
ctx.stroke();
});
ctx.shadowColor = 'transparent';
ctx.shadowBlur = 0;
};
// 右侧引导线路径(与左侧保持平行偏移)
const rightPath = `
M ${carRight} ${bottomY}
C ${carRight + ctrl1Offset} ${ctrl1Y},
${rightTopX + ctrl2Offset * 0.15} ${ctrl2Y},
${rightTopX} ${topY}
`;
const drawCenterDashed = (ctx: CanvasRenderingContext2D, startX: number, endX: number, aggressiveness: number = 0.30
) => {
const pts = buildPoints(startX, endX, isStraight, aggressiveness);
ctx.beginPath();
ctx.strokeStyle = 'rgba(255,255,255,0.65)';
ctx.lineWidth = 3;
ctx.setLineDash([12, 10]);
ctx.lineCap = 'butt';
pts.forEach(({ x, y }, i) => {
if (i === 0) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
});
ctx.stroke();
ctx.setLineDash([]);
};
// 中央虚线路径(中线,偏移量为左右均值)
const centerPath = `
M ${carCenter} ${bottomY}
C ${carCenter + ctrl1Offset} ${ctrl1Y},
${carCenter + topOffset * 0.85 + ctrl2Offset * 0.15} ${ctrl2Y},
${carCenter + topOffset} ${topY}
`;
const drawBumperMarkers = (ctx: CanvasRenderingContext2D) => {
ctx.shadowColor = '#ef4444';
ctx.shadowBlur = 8;
[carLeft, carRight].forEach((x) => {
ctx.beginPath();
ctx.strokeStyle = '#ef4444';
ctx.lineWidth = 6;
ctx.lineCap = 'round';
ctx.moveTo(x, bottomY - 16);
ctx.lineTo(x, bottomY);
ctx.stroke();
});
ctx.shadowColor = 'transparent';
ctx.shadowBlur = 0;
};
// ===== 颜色分段渲染函数 =====
// 将一条曲线分成红/黄/绿三段,沿曲线方向着色
const renderGradientPath = (path: string, glowFilter: string) => (
<React.Fragment>
{/* 红色段(近,0-30%) */}
<path
d={path}
pathLength={100}
stroke="#ef4444"
strokeWidth="7"
fill="none"
strokeLinecap="round"
filter={glowFilter}
strokeDasharray="30 100"
/>
{/* 黄色段(中,30-65%) */}
<path
d={path}
pathLength={100}
stroke="#fbbf24"
strokeWidth="7"
fill="none"
strokeLinecap="round"
filter={glowFilter}
strokeDasharray="35 100"
strokeDashoffset="-30"
/>
{/* 绿色段(远,65-100%) */}
<path
d={path}
pathLength={100}
stroke="#22c55e"
strokeWidth="7"
fill="none"
strokeLinecap="round"
filter="url(#glow-green)"
strokeDasharray="35 100"
strokeDashoffset="-65"
/>
</React.Fragment>
);
useEffect(() => {
const canvas = canvasRef.current;
if (!canvas) return;
const ctx = canvas.getContext('2d');
if (!ctx) return;
const dpr = window.devicePixelRatio || 1;
canvas.width = LOGICAL_W * dpr;
canvas.height = LOGICAL_H * dpr;
ctx.save();
ctx.scale(dpr, dpr);
ctx.clearRect(0, 0, LOGICAL_W, LOGICAL_H);
if (isStraight) {
drawGuideLine(ctx, carLeft, carLeft);
drawGuideLine(ctx, carRight, carRight);
drawCenterDashed(ctx, carCenter, carCenter);
} else {
// 关键:左转(steer<0)时左线是外侧 → 更弯;右转反之
const leftAggro = steer < 0 ? 0.25 : 0.50;
const rightAggro = steer > 0 ? 0.45 : 0.20;
drawGuideLine(ctx, carLeft, leftTopX, leftAggro);
drawGuideLine(ctx, carRight, rightTopX, rightAggro);
drawCenterDashed(ctx, carCenter, carCenter + topOffset);
}
drawBumperMarkers(ctx);
ctx.restore();
}, [steer, move, isStraight]);
return (
<div
style={{
position: 'absolute',
inset: 0,
pointerEvents: 'none',
zIndex: 5,
overflow: 'hidden',
opacity: guideOpacity,
position: 'absolute', inset: 0,
pointerEvents: 'none', zIndex: 5,
overflow: 'hidden', opacity: guideOpacity,
transition: 'opacity 0.3s ease',
}}
>
<svg
width="100%"
height="100%"
viewBox={`0 0 ${W} ${H}`}
preserveAspectRatio="none"
style={{ position: 'absolute', inset: 0 }}
>
<defs>
{/* 发光滤镜 */}
<filter id="glow-trajectory" x="-50%" y="-50%" width="200%" height="200%">
<feGaussianBlur stdDeviation="3" result="blur" />
<feMerge>
<feMergeNode in="blur" />
<feMergeNode in="SourceGraphic" />
</feMerge>
</filter>
{/* 强发光(绿色段) */}
<filter id="glow-green" x="-50%" y="-50%" width="200%" height="200%">
<feGaussianBlur stdDeviation="4" result="blur" />
<feMerge>
<feMergeNode in="blur" />
<feMergeNode in="SourceGraphic" />
</feMerge>
</filter>
</defs>
<canvas ref={canvasRef} style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }} />
{/* 左侧引导线 - 红黄绿三段 */}
{renderGradientPath(leftPath, "url(#glow-trajectory)")}
{/* 右侧引导线 - 红黄绿三段 */}
{renderGradientPath(rightPath, "url(#glow-trajectory)")}
{/* 中央虚线参考线 */}
<path
d={centerPath}
stroke="rgba(255,255,255,0.7)"
strokeWidth="4"
fill="none"
strokeLinecap="butt"
strokeDasharray="14 10"
/>
{/* 车身底部边界标记(车尾角) */}
<line
x1={carLeft}
y1={bottomY - 18}
x2={carLeft}
y2={bottomY}
stroke="#ef4444"
strokeWidth="6"
strokeLinecap="round"
filter="url(#glow-trajectory)"
/>
<line
x1={carRight}
y1={bottomY - 18}
x2={carRight}
y2={bottomY}
stroke="#ef4444"
strokeWidth="6"
strokeLinecap="round"
filter="url(#glow-trajectory)"
/>
</svg>
{/* 顶部档位指示 */}
<div
style={{
position: 'absolute',
top: 10,
left: '50%',
transform: 'translateX(-50%)',
display: 'flex',
alignItems: 'center',
gap: 6,
padding: '5px 16px',
borderRadius: 16,
background: 'rgba(0,0,0,0.6)',
color: '#fff',
fontSize: 13,
fontWeight: 700,
backdropFilter: 'blur(6px)',
letterSpacing: 1.5,
border: isReverse ? '1px solid rgba(245,158,11,0.6)' : '1px solid rgba(255,255,255,0.15)',
boxShadow: isReverse ? '0 0 12px rgba(245,158,11,0.4)' : 'none',
}}
>
{isReverse ? (
<>
<span style={{ color: '#f59e0b', fontSize: 16 }}>R</span>
<span>{t('devices.reverse')}</span>
</>
) : isForward ? (
<>
<span style={{ color: '#22c55e', fontSize: 16 }}>D</span>
<span>{t('devices.forward')}</span>
</>
) : (
<>
<span style={{ color: '#94a3b8', fontSize: 16 }}>N</span>
<span>{t('devices.neutral')}</span>
</>
)}
<div style={{
position: 'absolute', top: 10, left: '50%', transform: 'translateX(-50%)',
display: 'flex', alignItems: 'center', gap: 6, padding: '5px 16px', borderRadius: 16,
background: 'rgba(0,0,0,0.6)', color: '#fff', fontSize: 13, fontWeight: 700,
backdropFilter: 'blur(6px)', letterSpacing: 1.5,
border: isReverse ? '1px solid rgba(245,158,11,0.6)' : '1px solid rgba(255,255,255,0.15)',
boxShadow: isReverse ? '0 0 12px rgba(245,158,11,0.4)' : 'none',
}}>
{isReverse ? <><span style={{ color: '#f59e0b', fontSize: 16 }}>R</span><span>{t('devices.reverse')}</span></> :
isForward ? <><span style={{ color: '#22c55e', fontSize: 16 }}>D</span><span>{t('devices.forward')}</span></> :
<><span style={{ color: '#94a3b8', fontSize: 16 }}>N</span></>}
</div>
{/* 左转箭头 */}
{steer < -0.1 && (
<div
style={{
position: 'absolute',
left: 16,
top: '50%',
transform: `translateY(-50%) translateX(${steer * 12}px)`,
color: '#fbbf24',
fontSize: 42,
fontWeight: 900,
textShadow: '0 0 12px rgba(251,191,36,0.8)',
opacity: Math.min(1, Math.abs(steer)),
transition: 'all 0.1s linear',
}}
>
◀
</div>
<div style={{
position: 'absolute', left: 16, top: '50%',
transform: `translateY(-50%) translateX(${steer * 12}px)`,
color: '#fbbf24', fontSize: 42, fontWeight: 900,
textShadow: '0 0 12px rgba(251,191,36,0.8)',
opacity: Math.min(1, Math.abs(steer)), transition: 'all 0.1s linear',
}}>◀</div>
)}
{/* 右转箭头 */}
{steer > 0.1 && (
<div
style={{
position: 'absolute',
right: 16,
top: '50%',
transform: `translateY(-50%) translateX(${steer * 12}px)`,
color: '#fbbf24',
fontSize: 42,
fontWeight: 900,
textShadow: '0 0 12px rgba(251,191,36,0.8)',
opacity: Math.min(1, Math.abs(steer)),
transition: 'all 0.1s linear',
}}
>
▶
</div>
<div style={{
position: 'absolute', right: 16, top: '50%',
transform: `translateY(-50%) translateX(${steer * 12}px)`,
color: '#fbbf24', fontSize: 42, fontWeight: 900,
textShadow: '0 0 12px rgba(251,191,36,0.8)',
opacity: Math.min(1, Math.abs(steer)), transition: 'all 0.1s linear',
}}>▶</div>
)}
{/* 底部信息条 */}
<div
style={{
position: 'absolute',
bottom: 10,
left: '50%',
transform: 'translateX(-50%)',
display: 'flex',
alignItems: 'center',
gap: 12,
padding: '5px 16px',
borderRadius: 14,
background: 'rgba(0,0,0,0.6)',
color: '#e2e8f0',
fontSize: 11,
backdropFilter: 'blur(6px)',
}}
>
<span>
{t('devices.steer')}: <strong style={{ color: '#fbbf24' }}>{(steer * 100).toFixed(0)}%</strong>
</span>
<div style={{
position: 'absolute', bottom: 10, left: '50%', transform: 'translateX(-50%)',
display: 'flex', alignItems: 'center', gap: 12, padding: '5px 16px', borderRadius: 14,
background: 'rgba(0,0,0,0.6)', color: '#e2e8f0', fontSize: 11, backdropFilter: 'blur(6px)',
}}>
<span>{t('devices.steer')}: <strong style={{ color: '#fbbf24' }}>{(steer * 100).toFixed(0)}%</strong></span>
<span style={{ width: 1, height: 11, background: '#475569' }} />
<span>
{t('devices.throttle')}: <strong style={{ color: isReverse ? '#f59e0b' : '#22c55e' }}>{Math.abs(move * 100).toFixed(0)}%</strong>
</span>
<span>{t('devices.throttle')}: <strong style={{ color: isReverse ? '#f59e0b' : '#22c55e' }}>{Math.abs(move * 100).toFixed(0)}%</strong></span>
</div>
</div>
);