svg版本 倒车影像

This commit is contained in:
mmc
2026-10-12 17:01:15 +08:00
parent d30f52af4e
commit ae8187e662

View File

@@ -8,24 +8,134 @@ interface ReverseCameraOverlayProps {
}
/**
* 倒车影像叠加层
* @param x 方向盘 X 轴 (-1 ~ 1),负值左转,正值右转
* @param y 方向盘 Y 轴 (-1 ~ 1),负值前进,正值后退
* @param visible 是否显示
* 倒车影像叠加层 - 模拟真实汽车倒车影像效果
*
* 核心逻辑:
* - 直行时:左右两条引导线为平行直线
* - 转向时:两条线作为整体向同一侧弯曲(平行曲线),模拟真实倒车轨迹
* - 颜色从下到上:红(近/危险) → 黄 → 绿(远/安全)
*/
const ReverseCameraOverlay: React.FC<ReverseCameraOverlayProps> = ({ x, y, visible = true }) => {
const { t } = useTranslation();
if (!visible) return null;
// 将 x 限制在 [-1, 1]
const clamp = (v: number) => Math.max(-1, Math.min(1, v));
const steer = clamp(x || 0);
const move = clamp(y || 0);
// 转向角度(用于辅助线弯曲):steer * 35deg
const steerAngle = steer * 35;
// 后退时显示倒车线,前进时减弱
const reverseOpacity = move > 0 ? 1 : 0.35;
const isReverse = move > 0.05;
const isForward = move < -0.05;
const guideOpacity = isReverse ? 1 : isForward ? 0.25 : 0.5;
// ===== 基础几何 =====
// SVG 坐标系
const W = 400;
const H = 260;
// 车宽(画面底部两条引导线的间距)
const carWidth = 160;
const carCenter = W / 2; // 200
const carLeft = carCenter - carWidth / 2; // 120
const carRight = carCenter + carWidth / 2; // 280
// 引导线起点(画面底部,紧贴车尾)
const bottomY = 250;
// 引导线终点(画面上方远处)
const topY = 20;
// ===== 转向曲线生成 =====
// 真实倒车影像中,方向盘转动 → 两条引导线作为"整体"向转向侧弯曲
// 采用三次贝塞尔,控制点随 steer 偏移,左右两条线保持平行(间距恒定)
// 转向强度决定弯曲程度(弧度)
const turnStrength = steer; // -1 ~ 1
// 弯曲量:顶部偏移越大,弧度越明显
// 左转(steer<0) → 顶部向左偏移;右转(steer>0) → 顶部向右偏移
const topOffset = turnStrength * 140; // 顶部最大偏移量
// 左右两条线的水平间距(保持恒定,模拟车宽)
const lineSpacing = 60; // 两条线在顶部的水平间距(透视收缩后比底部窄)
// 左侧线终点
const leftTopX = carCenter + topOffset - lineSpacing / 2;
// 右侧线终点
const rightTopX = carCenter + topOffset + lineSpacing / 2;
// 控制点:让曲线从底部的"车宽"平滑过渡到顶部的"收缩间距"
// 控制点1(靠近底部):基本保持车宽,轻微随转向偏移
const ctrl1Offset = turnStrength * 30;
const ctrl1Y = 190;
// 控制点2(靠近顶部):跟随顶部偏移,形成自然弧线
const ctrl2Offset = topOffset * 0.85;
const ctrl2Y = 90;
// 左侧引导线路径
const leftPath = `
M ${carLeft} ${bottomY}
C ${carLeft + ctrl1Offset} ${ctrl1Y},
${leftTopX + ctrl2Offset * 0.15} ${ctrl2Y},
${leftTopX} ${topY}
`;
// 右侧引导线路径(与左侧保持平行偏移)
const rightPath = `
M ${carRight} ${bottomY}
C ${carRight + ctrl1Offset} ${ctrl1Y},
${rightTopX + ctrl2Offset * 0.15} ${ctrl2Y},
${rightTopX} ${topY}
`;
// 中央虚线路径(中线,偏移量为左右均值)
const centerPath = `
M ${carCenter} ${bottomY}
C ${carCenter + ctrl1Offset} ${ctrl1Y},
${carCenter + topOffset * 0.85 + ctrl2Offset * 0.15} ${ctrl2Y},
${carCenter + topOffset} ${topY}
`;
// ===== 颜色分段渲染函数 =====
// 将一条曲线分成红/黄/绿三段,沿曲线方向着色
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>
);
return (
<div
@@ -35,150 +145,127 @@ const ReverseCameraOverlay: React.FC<ReverseCameraOverlayProps> = ({ x, y, visib
pointerEvents: 'none',
zIndex: 5,
overflow: 'hidden',
opacity: guideOpacity,
transition: 'opacity 0.3s ease',
}}
>
<svg
width="100%"
height="100%"
viewBox="0 0 400 240"
viewBox={`0 0 ${W} ${H}`}
preserveAspectRatio="none"
style={{ position: 'absolute', inset: 0, opacity: reverseOpacity }}
style={{ position: 'absolute', inset: 0 }}
>
<defs>
<linearGradient id="rv-line" x1="0" y1="1" x2="0" y2="0">
<stop offset="0%" stopColor="#22d3ee" stopOpacity="0.95" />
<stop offset="100%" stopColor="#22d3ee" stopOpacity="0.1" />
</linearGradient>
{/* 发光滤镜 */}
<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>
{/* 倒车网格(透视梯形) */}
<g
style={{
transformOrigin: '200px 240px',
transform: `perspective(200px) rotateX(55deg) rotateZ(${-steerAngle * 0.4}deg)`,
transition: 'transform 0.12s linear',
}}
>
{/* 横向网格线 */}
{[0, 1, 2, 3, 4, 5].map((i) => {
const yPos = 240 - i * 30;
const inset = i * 18;
return (
<line
key={`h-${i}`}
x1={inset}
y1={yPos}
x2={400 - inset}
y2={yPos}
stroke="#22d3ee"
strokeOpacity={0.25 + i * 0.08}
strokeWidth={1}
/>
);
})}
{/* 纵向网格线 */}
{[-3, -2, -1, 0, 1, 2, 3].map((i) => {
const bottomX = 200 + i * 40;
const topX = 200 + i * 8;
return (
<line
key={`v-${i}`}
x1={bottomX}
y1={240}
x2={topX}
y2={60}
stroke="#22d3ee"
strokeOpacity={0.18}
strokeWidth={1}
/>
);
})}
</g>
{/* 左侧引导线 - 红黄绿三段 */}
{renderGradientPath(leftPath, "url(#glow-trajectory)")}
{/* 两侧倒车轨迹线(随转向弯曲) */}
<g style={{ transition: 'all 0.12s linear' }}>
{/* 左侧轨迹线 */}
<path
d={`M ${160 + steer * 60} 240 Q ${180 + steer * 40} ${160 + steer * 30} ${200 + steer * 30} ${100 + steer * 20}`}
stroke="#22d3ee"
strokeWidth="2.5"
fill="none"
strokeLinecap="round"
style={{ filter: 'drop-shadow(0 0 4px rgba(34,211,238,0.6))' }}
/>
{/* 右侧轨迹线 */}
<path
d={`M ${240 + steer * 60} 240 Q ${220 + steer * 40} ${160 + steer * 30} ${200 + steer * 30} ${100 + steer * 20}`}
stroke="#22d3ee"
strokeWidth="2.5"
fill="none"
strokeLinecap="round"
style={{ filter: 'drop-shadow(0 0 4px rgba(34,211,238,0.6))' }}
/>
</g>
{/* 右侧引导线 - 红黄绿三段 */}
{renderGradientPath(rightPath, "url(#glow-trajectory)")}
{/* 安全区域(中间梯形) */}
<polygon
points={`${150 + steer * 30},240 ${250 + steer * 30},240 ${220 + steer * 15},170 ${180 + steer * 15},170`}
fill="#22c55e"
fillOpacity="0.06"
stroke="#22c55e"
strokeOpacity="0.4"
strokeWidth="1"
style={{ transition: 'all 0.12s linear' }}
{/* 中央虚线参考线 */}
<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: 8,
top: 10,
left: '50%',
transform: 'translateX(-50%)',
display: 'flex',
alignItems: 'center',
gap: 8,
padding: '4px 12px',
borderRadius: 12,
background: 'rgba(0,0,0,0.55)',
gap: 6,
padding: '5px 16px',
borderRadius: 16,
background: 'rgba(0,0,0,0.6)',
color: '#fff',
fontSize: 12,
fontWeight: 600,
backdropFilter: 'blur(4px)',
letterSpacing: 1,
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',
}}
>
{move > 0 ? (
{isReverse ? (
<>
<span style={{ color: '#f59e0b' }}>R</span>
<span style={{ color: '#f59e0b', fontSize: 16 }}>R</span>
<span>{t('devices.reverse')}</span>
</>
) : move < 0 ? (
) : isForward ? (
<>
<span style={{ color: '#22c55e' }}>D</span>
<span style={{ color: '#22c55e', fontSize: 16 }}>D</span>
<span>{t('devices.forward')}</span>
</>
) : (
<>
<span style={{ color: '#94a3b8' }}>N</span>
<span style={{ color: '#94a3b8', fontSize: 16 }}>N</span>
<span>{t('devices.neutral')}</span>
</>
)}
</div>
{/* 左右转向箭头 */}
{/* 左转箭头 */}
{steer < -0.1 && (
<div
style={{
position: 'absolute',
left: 12,
left: 16,
top: '50%',
transform: `translateY(-50%) translateX(${steer * 20}px)`,
color: '#22d3ee',
fontSize: 36,
transform: `translateY(-50%) translateX(${steer * 12}px)`,
color: '#fbbf24',
fontSize: 42,
fontWeight: 900,
textShadow: '0 0 12px rgba(34,211,238,0.8)',
textShadow: '0 0 12px rgba(251,191,36,0.8)',
opacity: Math.min(1, Math.abs(steer)),
transition: 'all 0.1s linear',
}}
@@ -186,17 +273,18 @@ const ReverseCameraOverlay: React.FC<ReverseCameraOverlayProps> = ({ x, y, visib
◀
</div>
)}
{/* 右转箭头 */}
{steer > 0.1 && (
<div
style={{
position: 'absolute',
right: 12,
right: 16,
top: '50%',
transform: `translateY(-50%) translateX(${steer * 20}px)`,
color: '#22d3ee',
fontSize: 36,
transform: `translateY(-50%) translateX(${steer * 12}px)`,
color: '#fbbf24',
fontSize: 42,
fontWeight: 900,
textShadow: '0 0 12px rgba(34,211,238,0.8)',
textShadow: '0 0 12px rgba(251,191,36,0.8)',
opacity: Math.min(1, Math.abs(steer)),
transition: 'all 0.1s linear',
}}
@@ -205,30 +293,30 @@ const ReverseCameraOverlay: React.FC<ReverseCameraOverlayProps> = ({ x, y, visib
</div>
)}
{/* 底部距离 / 速度 指示条 */}
{/* 底部信息条 */}
<div
style={{
position: 'absolute',
bottom: 8,
bottom: 10,
left: '50%',
transform: 'translateX(-50%)',
display: 'flex',
alignItems: 'center',
gap: 10,
padding: '4px 14px',
borderRadius: 12,
background: 'rgba(0,0,0,0.55)',
gap: 12,
padding: '5px 16px',
borderRadius: 14,
background: 'rgba(0,0,0,0.6)',
color: '#e2e8f0',
fontSize: 11,
backdropFilter: 'blur(4px)',
backdropFilter: 'blur(6px)',
}}
>
<span>
{t('devices.steer')}: <strong style={{ color: '#22d3ee' }}>{(steer * 100).toFixed(0)}%</strong>
{t('devices.steer')}: <strong style={{ color: '#fbbf24' }}>{(steer * 100).toFixed(0)}%</strong>
</span>
<span style={{ width: 1, height: 10, background: '#475569' }} />
<span style={{ width: 1, height: 11, background: '#475569' }} />
<span>
{t('devices.throttle')}: <strong style={{ color: move >= 0 ? '#f59e0b' : '#22c55e' }}>{(move * 100).toFixed(0)}%</strong>
{t('devices.throttle')}: <strong style={{ color: isReverse ? '#f59e0b' : '#22c55e' }}>{Math.abs(move * 100).toFixed(0)}%</strong>
</span>
</div>
</div>