167 lines
3.9 KiB
TypeScript
167 lines
3.9 KiB
TypeScript
import React, { useEffect, useRef } from 'react';
|
|
|
|
interface Props {
|
|
video: HTMLVideoElement;
|
|
idx: number;
|
|
title: string;
|
|
className?: string;
|
|
noCrop?: boolean;
|
|
}
|
|
|
|
const VideoCanvas: React.FC<Props> = ({ video, idx, title, className, noCrop }) => {
|
|
const canvasRef = useRef<HTMLCanvasElement | null>(null);
|
|
const rafRef = useRef<number | null>(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 (
|
|
<div className={`${idx == 1 ? 'flex flex-grow' : ''}relative w-full h-full rounded bg-black ${className}`}>
|
|
<canvas
|
|
ref={canvasRef}
|
|
//width={640}
|
|
//height={360}
|
|
className="w-full h-full"
|
|
/>
|
|
|
|
<div className="absolute inset-0 pointer-events-none">
|
|
{/*<div className="absolute top-5 left-12 px-2 py-0.5 bg-black/60 rounded text-[10px] text-cyan-400">
|
|
{title}
|
|
</div>*/}
|
|
{/*<div className="absolute top-5 right-12 text-cyan-400 text-[8px] flex items-center gap-1">
|
|
<span className="w-1.5 h-1.5 rounded-full bg-cyan-400 animate-pulse" />
|
|
LIVE
|
|
</div>*/}
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default VideoCanvas;
|