车道辅助线 初始化

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