From 1034b6dbf8fd3c55b47ef3ac9de3c221e0eed51d Mon Sep 17 00:00:00 2001 From: mmc <1556375442@qq.com> Date: Sat, 17 Oct 2026 16:19:14 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=8C=E8=BD=A6=E8=BE=85=E5=8A=A9=E7=BA=BF?= =?UTF-8?q?=20=E6=A0=B7=E5=BC=8F=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../devices/CarReverseGuideLine.tsx | 516 +++++++++++++----- 1 file changed, 389 insertions(+), 127 deletions(-) diff --git a/src/components/devices/CarReverseGuideLine.tsx b/src/components/devices/CarReverseGuideLine.tsx index 8afd30e..6eb5d47 100644 --- a/src/components/devices/CarReverseGuideLine.tsx +++ b/src/components/devices/CarReverseGuideLine.tsx @@ -1,16 +1,19 @@ -import React, { useEffect, useRef, useState } from 'react'; +import React, { useEffect, useRef } from 'react'; type GuideLineProps = { - /** 方向 forward前进 reverse倒车 */ direction: 'forward' | 'reverse'; - /** 方向盘转角,单位°:左转负数,右转正数,范围建议 -45 ~ 45 */ steer: number; - /** 引导线底部(车尾端)两条线间距,默认为容器宽度的 40% */ bottomGap?: number; - /** 引导线顶部(远端)两条线间距,默认为容器宽度的 20% */ topGap?: number; - /** 是否显示辅助线 */ visible?: boolean; + + /** 中间绿色人字线是否闪烁,默认开启 */ + chevronFlash?: boolean; +}; + +type Point = { + x: number; + y: number; }; const CarReverseGuideLine: React.FC = ({ @@ -19,160 +22,416 @@ const CarReverseGuideLine: React.FC = ({ bottomGap, topGap, visible = true, + chevronFlash = true, }) => { const canvasRef = useRef(null); const containerRef = useRef(null); - const [size, setSize] = useState({ width: 0, height: 0 }); - // 监听容器大小变化 + const steerRef = useRef(steer); + const directionRef = useRef(direction); + const flashRef = useRef(chevronFlash); + const gapRef = useRef({ bottomGap, topGap }); + const visibleRef = useRef(visible); + + // 更新参数,不触发 React 组件重新渲染 useEffect(() => { - if (!containerRef.current) return; - const resizeObserver = new ResizeObserver((entries) => { - for (const entry of entries) { - const { width, height } = entry.contentRect; - setSize({ width, height }); - } - }); - resizeObserver.observe(containerRef.current); - return () => resizeObserver.disconnect(); - }, []); + steerRef.current = steer; + directionRef.current = direction; + flashRef.current = chevronFlash; + gapRef.current = { bottomGap, topGap }; + visibleRef.current = visible; + }, [ + steer, + direction, + chevronFlash, + bottomGap, + topGap, + visible, + ]); useEffect(() => { const canvas = canvasRef.current; - if (!canvas || size.width === 0 || size.height === 0 || !visible) return; + const container = containerRef.current; + + if (!canvas || !container) return; const ctx = canvas.getContext('2d'); if (!ctx) return; - const { width, height } = size; - ctx.clearRect(0, 0, width, height); + let width = 0; + let height = 0; + let dpr = 1; + let rafId = 0; + let lastTime = 0; + let animationTime = 0; - // 基础参数计算 - // 前向视角:下面宽(近处),上面窄(远处) - // 后向视角:上面宽(远处固定点),下面窄(近处转弯点) - let bGap = bottomGap || (direction === 'reverse' ? width * 0.25 : width * 0.45); - let tGap = topGap || (direction === 'reverse' ? width * 0.45 : width * 0.25); + const resize = () => { + const rect = container.getBoundingClientRect(); - const maxWheelAngle = 45; - const wheelAngleRad = Math.max(-maxWheelAngle, Math.min(maxWheelAngle, steer)) * Math.PI / 180; - const step = 2; + width = rect.width; + height = rect.height; - // 迭代计算轨迹点函数 - const getPathPoints = (side: 'left' | 'right'): [number, number][] => { - const points: [number, number][] = []; - const totalDist = direction === 'reverse' ? height * 0.75 : height * 0.7; - const stepCount = Math.floor(totalDist / step); + // 避免高分辨率车机屏幕上的辅助线模糊 + dpr = Math.min(window.devicePixelRatio || 1, 2); - if (direction === 'forward') { - // 前视角:底部固定,向上延伸并转向 - let x = width / 2; - let y = height - 20; - let angle = -Math.PI / 2; + canvas.width = Math.round(width * dpr); + canvas.height = Math.round(height * dpr); - for (let i = 0; i <= stepCount; i++) { - const progress = i / stepCount; - const currentGap = bGap * (1 - progress) + tGap * progress; - const sideOffset = side === 'left' ? -currentGap / 2 : currentGap / 2; + canvas.style.width = `${width}px`; + canvas.style.height = `${height}px`; - if (Math.abs(wheelAngleRad) > 0.001) { - // 恢复前视转向灵敏度:使用固定曲率 - const curvature = height * 0.5; - const turnStepRad = (step * Math.tan(wheelAngleRad)) / curvature; - angle += turnStepRad; - x += Math.cos(angle) * step; - y += Math.sin(angle) * step; - } else { - y -= step; - } - points.push([x + sideOffset, y]); - if (y < 0) break; - } + ctx.setTransform(dpr, 0, 0, dpr, 0, 0); + }; + + const resizeObserver = new ResizeObserver(resize); + resizeObserver.observe(container); + resize(); + + /** + * 计算车道中心线。 + * steer < 0 左转,steer > 0 右转。 + */ + /** + * 计算转向后的车道中心 + * + * forward: + * 下方近端相对稳定,上方远端逐渐弯曲 + * + * reverse: + * 上方远端相对稳定,下方近端逐渐弯曲 + */ + const getCenterX = (progress: number) => { + const angle = Math.max( + -45, + Math.min(45, steerRef.current) + ); + + // 方向盘转角转换为曲率响应 + const turn = Math.tan( + (angle * Math.PI) / 180 + ); + + // progress: 0 = 顶部远端,1 = 底部近端 + let curveProgress: number; + + if (directionRef.current === 'forward') { + // 前视:上方弯曲,下方稳定 + curveProgress = 1 - progress; } else { - // 后视角:顶部固定点,向下延伸至底部并随转向摆动 - // 我们从顶部 (width/2, height - totalDist) 开始向下画 - let x = width / 2; - let startY = height - totalDist - 20; - let y = startY; - let angle = Math.PI / 2; // 向下 - - for (let i = 0; i <= stepCount; i++) { - const progress = i / stepCount; // 0 是顶部,1 是底部 - // 后视角透视:progress=0(顶部)时使用 tGap,progress=1(底部)时使用 bGap - const currentGap = tGap * (1 - progress) + bGap * progress; - const sideOffset = side === 'left' ? currentGap / 2 : -currentGap / 2; - - if (Math.abs(wheelAngleRad) > 0.001) { - // 转向逻辑:从顶部向下走,底部随 steer 摆动 - // steer 为负(左打)时,底部应该向左摆动(x减小) - const curvature = height * 0.5; // 与前视角保持一致的曲率灵敏度 - const turnStepRad = (step * Math.tan(wheelAngleRad)) / curvature; - angle -= turnStepRad; // 后视角向下绘制,减去增量以实现正确的左右转向 - x += Math.cos(angle) * step; - y += Math.sin(angle) * step; - } else { - y += step; - } - points.push([x + sideOffset, y]); - } - // 后视角的点是从上往下的,为了统一绘制逻辑(从下往上渐变),我们翻转数组 - points.reverse(); + // 后视:下方弯曲,上方稳定 + curveProgress = progress; } - return points; + // 平滑曲率,避免轨迹弯曲突兀 + const curveFactor = + curveProgress * curveProgress; + + // 转向最大横向偏移 + const maxOffset = width * 0.40; + + const offset = + turn * + maxOffset * + curveFactor; + + return width / 2 + offset; + }; + /** + * 计算左右车道线。 + */ + /** + * 计算左右车道线 + * + * forward: + * 近端下方宽,远端上方窄 + * + * reverse: + * 远端上方宽,近端下方窄 + */ + const getLanePoint = ( + progress: number, + side: 'left' | 'right' + ): Point => { + const { + bottomGap: customBottom, + topGap: customTop, + } = gapRef.current; + + const isReverse = directionRef.current === 'reverse'; + + // 前进:底部宽,顶部窄 + // 倒车:底部窄,顶部宽 + const defaultBottomGap = width * ( + isReverse ? 0.25 : 0.45 + ); + + const defaultTopGap = width * ( + isReverse ? 0.45 : 0.25 + ); + + const bGap = customBottom ?? defaultBottomGap; + const tGap = customTop ?? defaultTopGap; + + // progress: 0 = 远端顶部,1 = 近端底部 + const gap = + tGap * (1 - progress) + + bGap * progress; + + // 左右车道对称分布 + const sideOffset = + side === 'left' + ? -gap / 2 + : gap / 2; + + const centerX = getCenterX(progress); + + // 前进、倒车都保持近端在下方 + const y = + height * 0.12 + + progress * height * 0.76; + + return { + x: centerX + sideOffset, + y, + }; }; - // 绘制左右两条轨迹 - const leftPath = getPathPoints('left'); - const rightPath = getPathPoints('right'); + /** + * 绘制一条平滑车道线。 + */ + const drawLane = (side: 'left' | 'right') => { + const points: Point[] = []; + const count = 100; + + for (let i = 0; i <= count; i++) { + const progress = i / count; + points.push(getLanePoint(progress, side)); + } + + if (points.length < 2) return; + + const gradient = ctx.createLinearGradient( + 0, + height * 0.95, + 0, + height * 0.12 + ); + + gradient.addColorStop(0, '#18D99A'); + gradient.addColorStop(0.45, '#16C99A'); + gradient.addColorStop(0.75, '#E4B83B'); + gradient.addColorStop(1, '#FF7043'); - const drawPath = (pts: [number, number][], color: string, dash: number[] = []) => { - if (pts.length < 2) return; ctx.beginPath(); - ctx.lineWidth = 4; - ctx.strokeStyle = color; - ctx.setLineDash(dash); - ctx.lineJoin = 'round'; + ctx.moveTo(points[0].x, points[0].y); + + for (let i = 1; i < points.length; i++) { + ctx.lineTo(points[i].x, points[i].y); + } + + ctx.strokeStyle = gradient; + ctx.lineWidth = Math.max(3, width * 0.004); ctx.lineCap = 'round'; - - ctx.moveTo(pts[0][0], pts[0][1]); - for (let i = 1; i < pts.length; i++) { - ctx.lineTo(pts[i][0], pts[i][1]); - } + ctx.lineJoin = 'round'; + ctx.setLineDash([]); ctx.stroke(); }; - // 渐变色绘制 - const gradient = ctx.createLinearGradient(0, height, 0, 0); - gradient.addColorStop(0, 'rgba(0, 255, 0, 0.8)'); // 近处绿色 - gradient.addColorStop(0.5, 'rgba(255, 255, 0, 0.8)'); // 中间黄色 - gradient.addColorStop(1, 'rgba(255, 0, 0, 0.8)'); // 远处红色 + /** + * 绘制中间动态绿色人字线。 + * + * phase 为动画相位: + * 1. 亮度呼吸 + * 2. 线条流动 + */ + const drawChevron = ( + progress: number, + phase: number, + opacity: number + ) => { + const centerX = getCenterX(progress); - drawPath(leftPath, gradient); - drawPath(rightPath, gradient); + const isReverse = directionRef.current === 'reverse'; - // ========== 横向距离标记虚线 ========== - ctx.lineWidth = 2; - ctx.setLineDash([10, 8]); - const markCount = 4; - for (let m = 1; m <= markCount; m++) { - const ratio = m / (markCount + 1); - const idx = Math.floor(leftPath.length * ratio); - if (idx >= leftPath.length || idx >= rightPath.length) continue; + const y = + height * 0.12 + + progress * height * 0.76; - const [lx, ly] = leftPath[idx]; - const [rx, ry] = rightPath[idx]; + const gap = + width * + (0.10 + progress * 0.06); - // 根据距离改变颜色 - ctx.strokeStyle = ratio < 0.3 ? 'rgba(0, 255, 0, 0.6)' : - ratio < 0.7 ? 'rgba(255, 255, 0, 0.6)' : 'rgba(255, 0, 0, 0.6)'; + const size = + height * + (0.075 + progress * 0.035); + // 人字线整体跟随转向弯曲 + const offset = + Math.tan( + (Math.max(-45, Math.min(45, steerRef.current)) * + Math.PI) / + 180 + ) * + width * + 0.04 * + progress; + + const cx = getCenterX(progress); + // 绿色发光效果 + ctx.save(); + + ctx.globalAlpha = opacity; + ctx.lineCap = 'round'; + ctx.lineJoin = 'round'; + + // 外层柔和光晕 + ctx.shadowColor = '#00FFAA'; + ctx.shadowBlur = width * 0.012; + ctx.strokeStyle = 'rgba(0, 255, 170, 0.22)'; + ctx.lineWidth = width * 0.018; + + // 前视:人字线尖端朝下 ∨ + // 倒车:人字线尖端朝上 ∧ + const isForward = directionRef.current === 'forward'; + + const leftX = cx - gap; + const rightX = cx + gap; + + // 前视时尖端在上方,倒车时尖端在下方 + const tipY = isForward ? y - size : y; + const outerY = isForward ? y : y - size; + + // 外层光晕 ctx.beginPath(); - ctx.moveTo(lx, ly); - ctx.lineTo(rx, ry); + ctx.moveTo(leftX, outerY); + ctx.lineTo(cx, tipY); + ctx.lineTo(rightX, outerY); ctx.stroke(); - } - }, [direction, steer, size, bottomGap, topGap, visible]); + // 内层清晰线 + ctx.beginPath(); + ctx.moveTo(leftX, outerY); + ctx.lineTo(cx, tipY); + ctx.lineTo(rightX, outerY); + ctx.stroke(); + // 内层清晰绿色线 + ctx.shadowBlur = width * 0.004; + ctx.strokeStyle = '#17E6A1'; + ctx.lineWidth = width * 0.008; + + + + // 外层光晕 + ctx.beginPath(); + ctx.moveTo(leftX, outerY); + ctx.lineTo(cx, tipY); + ctx.lineTo(rightX, outerY); + ctx.stroke(); + + // 内层清晰线 + ctx.beginPath(); + ctx.moveTo(leftX, outerY); + ctx.lineTo(cx, tipY); + ctx.lineTo(rightX, outerY); + ctx.stroke(); + + ctx.restore(); + }; + + /** + * 绘制横向距离标记。 + */ + const drawDistanceMarks = () => { + const marks = [0.25, 0.45, 0.65, 0.85]; + + ctx.save(); + + ctx.lineWidth = 1.5; + ctx.setLineDash([8, 6]); + + marks.forEach((progress) => { + const left = getLanePoint(progress, 'left'); + const right = getLanePoint(progress, 'right'); + + ctx.strokeStyle = + progress < 0.35 + ? 'rgba(0, 255, 170, 0.55)' + : progress < 0.7 + ? 'rgba(255, 210, 50, 0.50)' + : 'rgba(255, 90, 50, 0.45)'; + + ctx.beginPath(); + ctx.moveTo(left.x, left.y); + ctx.lineTo(right.x, right.y); + ctx.stroke(); + }); + + ctx.restore(); + }; + + /** + * 动画主循环 + */ + const render = (time: number) => { + rafId = requestAnimationFrame(render); + + if (!width || !height || !visibleRef.current) { + ctx.clearRect(0, 0, width, height); + return; + } + + // 限制动画时间增量,避免后台恢复后跳变 + const delta = lastTime + ? Math.min((time - lastTime) / 1000, 0.05) + : 0; + + lastTime = time; + animationTime += delta; + + ctx.clearRect(0, 0, width, height); + + // 底层车道线 + drawLane('left'); + drawLane('right'); + + drawDistanceMarks(); + + // 绿色人字线独立闪烁 + const flash = flashRef.current + ? 0.5 + 0.5 * Math.sin(animationTime * 2.8) + : 1; + + // 三组人字线,形成向远处延伸的效果 + const chevrons = [ + { progress: 0.25, opacity: 0.38 }, + { progress: 0.48, opacity: 0.65 }, + { progress: 0.70, opacity: 1.0 }, + ]; + + chevrons.forEach((item, index) => { + // 不同人字线有轻微相位差 + const phase = + animationTime * 2.8 - index * 0.8; + + const opacity = flashRef.current + ? 0.25 + 0.75 * (0.5 + 0.5 * Math.sin(phase)) + : 1; + + drawChevron( + item.progress, + phase, + opacity * item.opacity + ); + }); + }; + + rafId = requestAnimationFrame(render); + + return () => { + cancelAnimationFrame(rafId); + resizeObserver.disconnect(); + }; + }, []); return (
= ({ height: '100%', pointerEvents: 'none', zIndex: 5, - display: visible ? 'block' : 'none' + display: visible ? 'block' : 'none', + overflow: 'hidden', }} >
);