行车辅助线 样式优化
This commit is contained in:
@@ -1,16 +1,19 @@
|
|||||||
import React, { useEffect, useRef, useState } from 'react';
|
import React, { useEffect, useRef } from 'react';
|
||||||
|
|
||||||
type GuideLineProps = {
|
type GuideLineProps = {
|
||||||
/** 方向 forward前进 reverse倒车 */
|
|
||||||
direction: 'forward' | 'reverse';
|
direction: 'forward' | 'reverse';
|
||||||
/** 方向盘转角,单位°:左转负数,右转正数,范围建议 -45 ~ 45 */
|
|
||||||
steer: number;
|
steer: number;
|
||||||
/** 引导线底部(车尾端)两条线间距,默认为容器宽度的 40% */
|
|
||||||
bottomGap?: number;
|
bottomGap?: number;
|
||||||
/** 引导线顶部(远端)两条线间距,默认为容器宽度的 20% */
|
|
||||||
topGap?: number;
|
topGap?: number;
|
||||||
/** 是否显示辅助线 */
|
|
||||||
visible?: boolean;
|
visible?: boolean;
|
||||||
|
|
||||||
|
/** 中间绿色人字线是否闪烁,默认开启 */
|
||||||
|
chevronFlash?: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
type Point = {
|
||||||
|
x: number;
|
||||||
|
y: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
const CarReverseGuideLine: React.FC<GuideLineProps> = ({
|
const CarReverseGuideLine: React.FC<GuideLineProps> = ({
|
||||||
@@ -19,160 +22,416 @@ const CarReverseGuideLine: React.FC<GuideLineProps> = ({
|
|||||||
bottomGap,
|
bottomGap,
|
||||||
topGap,
|
topGap,
|
||||||
visible = true,
|
visible = true,
|
||||||
|
chevronFlash = true,
|
||||||
}) => {
|
}) => {
|
||||||
const canvasRef = useRef<HTMLCanvasElement>(null);
|
const canvasRef = useRef<HTMLCanvasElement>(null);
|
||||||
const containerRef = useRef<HTMLDivElement>(null);
|
const containerRef = useRef<HTMLDivElement>(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(() => {
|
useEffect(() => {
|
||||||
if (!containerRef.current) return;
|
steerRef.current = steer;
|
||||||
const resizeObserver = new ResizeObserver((entries) => {
|
directionRef.current = direction;
|
||||||
for (const entry of entries) {
|
flashRef.current = chevronFlash;
|
||||||
const { width, height } = entry.contentRect;
|
gapRef.current = { bottomGap, topGap };
|
||||||
setSize({ width, height });
|
visibleRef.current = visible;
|
||||||
}
|
}, [
|
||||||
});
|
steer,
|
||||||
resizeObserver.observe(containerRef.current);
|
direction,
|
||||||
return () => resizeObserver.disconnect();
|
chevronFlash,
|
||||||
}, []);
|
bottomGap,
|
||||||
|
topGap,
|
||||||
|
visible,
|
||||||
|
]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const canvas = canvasRef.current;
|
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');
|
const ctx = canvas.getContext('2d');
|
||||||
if (!ctx) return;
|
if (!ctx) return;
|
||||||
|
|
||||||
const { width, height } = size;
|
let width = 0;
|
||||||
ctx.clearRect(0, 0, width, height);
|
let height = 0;
|
||||||
|
let dpr = 1;
|
||||||
|
let rafId = 0;
|
||||||
|
let lastTime = 0;
|
||||||
|
let animationTime = 0;
|
||||||
|
|
||||||
// 基础参数计算
|
const resize = () => {
|
||||||
// 前向视角:下面宽(近处),上面窄(远处)
|
const rect = container.getBoundingClientRect();
|
||||||
// 后向视角:上面宽(远处固定点),下面窄(近处转弯点)
|
|
||||||
let bGap = bottomGap || (direction === 'reverse' ? width * 0.25 : width * 0.45);
|
|
||||||
let tGap = topGap || (direction === 'reverse' ? width * 0.45 : width * 0.25);
|
|
||||||
|
|
||||||
const maxWheelAngle = 45;
|
width = rect.width;
|
||||||
const wheelAngleRad = Math.max(-maxWheelAngle, Math.min(maxWheelAngle, steer)) * Math.PI / 180;
|
height = rect.height;
|
||||||
const step = 2;
|
|
||||||
|
|
||||||
// 迭代计算轨迹点函数
|
// 避免高分辨率车机屏幕上的辅助线模糊
|
||||||
const getPathPoints = (side: 'left' | 'right'): [number, number][] => {
|
dpr = Math.min(window.devicePixelRatio || 1, 2);
|
||||||
const points: [number, number][] = [];
|
|
||||||
const totalDist = direction === 'reverse' ? height * 0.75 : height * 0.7;
|
|
||||||
const stepCount = Math.floor(totalDist / step);
|
|
||||||
|
|
||||||
if (direction === 'forward') {
|
canvas.width = Math.round(width * dpr);
|
||||||
// 前视角:底部固定,向上延伸并转向
|
canvas.height = Math.round(height * dpr);
|
||||||
let x = width / 2;
|
|
||||||
let y = height - 20;
|
|
||||||
let angle = -Math.PI / 2;
|
|
||||||
|
|
||||||
for (let i = 0; i <= stepCount; i++) {
|
canvas.style.width = `${width}px`;
|
||||||
const progress = i / stepCount;
|
canvas.style.height = `${height}px`;
|
||||||
const currentGap = bGap * (1 - progress) + tGap * progress;
|
|
||||||
const sideOffset = side === 'left' ? -currentGap / 2 : currentGap / 2;
|
|
||||||
|
|
||||||
if (Math.abs(wheelAngleRad) > 0.001) {
|
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
||||||
// 恢复前视转向灵敏度:使用固定曲率
|
};
|
||||||
const curvature = height * 0.5;
|
|
||||||
const turnStepRad = (step * Math.tan(wheelAngleRad)) / curvature;
|
const resizeObserver = new ResizeObserver(resize);
|
||||||
angle += turnStepRad;
|
resizeObserver.observe(container);
|
||||||
x += Math.cos(angle) * step;
|
resize();
|
||||||
y += Math.sin(angle) * step;
|
|
||||||
} else {
|
/**
|
||||||
y -= step;
|
* 计算车道中心线。
|
||||||
}
|
* steer < 0 左转,steer > 0 右转。
|
||||||
points.push([x + sideOffset, y]);
|
*/
|
||||||
if (y < 0) break;
|
/**
|
||||||
}
|
* 计算转向后的车道中心
|
||||||
|
*
|
||||||
|
* 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 {
|
} else {
|
||||||
// 后视角:顶部固定点,向下延伸至底部并随转向摆动
|
// 后视:下方弯曲,上方稳定
|
||||||
// 我们从顶部 (width/2, height - totalDist) 开始向下画
|
curveProgress = progress;
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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.beginPath();
|
||||||
ctx.lineWidth = 4;
|
ctx.moveTo(points[0].x, points[0].y);
|
||||||
ctx.strokeStyle = color;
|
|
||||||
ctx.setLineDash(dash);
|
for (let i = 1; i < points.length; i++) {
|
||||||
ctx.lineJoin = 'round';
|
ctx.lineTo(points[i].x, points[i].y);
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.strokeStyle = gradient;
|
||||||
|
ctx.lineWidth = Math.max(3, width * 0.004);
|
||||||
ctx.lineCap = 'round';
|
ctx.lineCap = 'round';
|
||||||
|
ctx.lineJoin = 'round';
|
||||||
ctx.moveTo(pts[0][0], pts[0][1]);
|
ctx.setLineDash([]);
|
||||||
for (let i = 1; i < pts.length; i++) {
|
|
||||||
ctx.lineTo(pts[i][0], pts[i][1]);
|
|
||||||
}
|
|
||||||
ctx.stroke();
|
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)'); // 中间黄色
|
* phase 为动画相位:
|
||||||
gradient.addColorStop(1, 'rgba(255, 0, 0, 0.8)'); // 远处红色
|
* 1. 亮度呼吸
|
||||||
|
* 2. 线条流动
|
||||||
|
*/
|
||||||
|
const drawChevron = (
|
||||||
|
progress: number,
|
||||||
|
phase: number,
|
||||||
|
opacity: number
|
||||||
|
) => {
|
||||||
|
const centerX = getCenterX(progress);
|
||||||
|
|
||||||
drawPath(leftPath, gradient);
|
const isReverse = directionRef.current === 'reverse';
|
||||||
drawPath(rightPath, gradient);
|
|
||||||
|
|
||||||
// ========== 横向距离标记虚线 ==========
|
const y =
|
||||||
ctx.lineWidth = 2;
|
height * 0.12 +
|
||||||
ctx.setLineDash([10, 8]);
|
progress * height * 0.76;
|
||||||
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 [lx, ly] = leftPath[idx];
|
const gap =
|
||||||
const [rx, ry] = rightPath[idx];
|
width *
|
||||||
|
(0.10 + progress * 0.06);
|
||||||
|
|
||||||
// 根据距离改变颜色
|
const size =
|
||||||
ctx.strokeStyle = ratio < 0.3 ? 'rgba(0, 255, 0, 0.6)' :
|
height *
|
||||||
ratio < 0.7 ? 'rgba(255, 255, 0, 0.6)' : 'rgba(255, 0, 0, 0.6)';
|
(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.beginPath();
|
||||||
ctx.moveTo(lx, ly);
|
ctx.moveTo(leftX, outerY);
|
||||||
ctx.lineTo(rx, ry);
|
ctx.lineTo(cx, tipY);
|
||||||
|
ctx.lineTo(rightX, outerY);
|
||||||
ctx.stroke();
|
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 (
|
return (
|
||||||
<div
|
<div
|
||||||
@@ -184,14 +443,17 @@ const CarReverseGuideLine: React.FC<GuideLineProps> = ({
|
|||||||
height: '100%',
|
height: '100%',
|
||||||
pointerEvents: 'none',
|
pointerEvents: 'none',
|
||||||
zIndex: 5,
|
zIndex: 5,
|
||||||
display: visible ? 'block' : 'none'
|
display: visible ? 'block' : 'none',
|
||||||
|
overflow: 'hidden',
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<canvas
|
<canvas
|
||||||
ref={canvasRef}
|
ref={canvasRef}
|
||||||
width={size.width}
|
style={{
|
||||||
height={size.height}
|
width: '100%',
|
||||||
style={{ width: '100%', height: '100%' }}
|
height: '100%',
|
||||||
|
display: 'block',
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user