import React, { useEffect, useRef } from 'react'; interface Props { video: HTMLVideoElement; idx: number; title: string; className?: string; noCrop?: boolean; } const VideoCanvas: React.FC = ({ video, idx, title, className, noCrop }) => { const canvasRef = useRef(null); const rafRef = useRef(null); const stopRender = () => { if (rafRef.current) { cancelAnimationFrame(rafRef.current); rafRef.current = null; } clearCanvas(); }; const clearCanvas = () => { const canvas = canvasRef.current; if (!canvas) return; const ctx = canvas.getContext('2d'); if (!ctx) return; ctx.fillStyle = '#000'; ctx.fillRect(0, 0, canvas.width, canvas.height); }; // 🔥 绘制汽车影像辅助线 const drawGuideLines = (ctx: CanvasRenderingContext2D, canvas: HTMLCanvasElement) => { const w = canvas.width; const h = canvas.height; ctx.save(); ctx.lineWidth = 2; ctx.strokeStyle = 'rgba(0, 229, 255, 0.7)'; ctx.globalAlpha = 0.8; // 根据视角画不同辅助线 if (title.includes('前') || title.includes('front')) { // 前视角引导线(梯形) ctx.beginPath(); ctx.moveTo(w * 0.25, h); ctx.lineTo(w * 0.45, h * 0.3); ctx.moveTo(w * 0.75, h); ctx.lineTo(w * 0.55, h * 0.3); ctx.stroke(); // 中线 ctx.beginPath(); ctx.setLineDash([5, 5]); ctx.moveTo(w / 2, h); ctx.lineTo(w / 2, h * 0.3); ctx.stroke(); ctx.setLineDash([]); } if (title.includes('后') || title.includes('back')) { // 后视角倒车线(扇形/倒车轨迹) ctx.beginPath(); ctx.moveTo(w * 0.3, h * 0.7); ctx.lineTo(w * 0.15, h * 0.3); ctx.moveTo(w * 0.7, h * 0.7); ctx.lineTo(w * 0.85, h * 0.3); ctx.stroke(); // 倒车底线 ctx.beginPath(); ctx.moveTo(w * 0.2, h * 0.8); ctx.lineTo(w * 0.8, h * 0.8); ctx.stroke(); } if (title.includes('左') || title.includes('left')) { // 左侧线 ctx.beginPath(); ctx.moveTo(w * 0.8, h); ctx.lineTo(w * 0.8, 0); ctx.moveTo(w * 0.7, h); ctx.lineTo(w * 0.7, 0); ctx.stroke(); } if (title.includes('右') || title.includes('right')) { // 右侧线 ctx.beginPath(); ctx.moveTo(w * 0.2, h); ctx.lineTo(w * 0.2, 0); ctx.moveTo(w * 0.3, h); ctx.lineTo(w * 0.3, 0); ctx.stroke(); } ctx.restore(); }; const startDraw = () => { const canvas = canvasRef.current; if (!canvas) return; const ctx = canvas.getContext('2d'); if (!ctx) return; clearCanvas(); const render = () => { if (video.readyState >= 2) { const vw = video.videoWidth; const vh = video.videoHeight; let sx = 0; let sy = 0; let sw = vw; let sh = vh; if (!noCrop) { sw = vw / 2; sh = vh / 2; switch (idx) { case 2: sx = sw; break; case 3: sy = sh; break; case 4: sx = sw; sy = sh; break; } } ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.drawImage(video, sx, sy, sw, sh, 0, 0, canvas.width, canvas.height); // 🔥 绘制辅助线 //drawGuideLines(ctx, canvas); } rafRef.current = requestAnimationFrame(render); }; render(); }; useEffect(() => { stopRender(); if (!video) return; startDraw(); return stopRender; }, [video, idx, title]); return (
{/*
{title}
*/} {/*
LIVE
*/}
); }; export default VideoCanvas;