fix: 优化预警记录 od

This commit is contained in:
YuanHongbo 2024-04-21 15:43:25 +08:00
parent e25180d3ee
commit 93f0670ded
2 changed files with 19 additions and 17 deletions

View File

@ -16,6 +16,9 @@ const backEndData = [
// warningTime: "2023-03-01 ", // warningTime: "2023-03-01 ",
warningTimestamp: Date.now(), warningTimestamp: Date.now(),
// warningTimeFormat:"YYYY-MM-DD" // warningTimeFormat:"YYYY-MM-DD"
odRect:{
"x": 0.553125, "y": 0.29722223, "w": 0.048958335, "h": 0.2462963
}
}, },
{ {
imageKey: 'https://i.yourimageshare.com/lRHiD2UnAT.png', imageKey: 'https://i.yourimageshare.com/lRHiD2UnAT.png',
@ -27,6 +30,9 @@ const backEndData = [
// warningTime: "2023-03-01 ", // warningTime: "2023-03-01 ",
warningTimestamp: Date.now(), warningTimestamp: Date.now(),
// warningTimeFormat:"YYYY-MM-DD" // warningTimeFormat:"YYYY-MM-DD"
odRect:{
"x": 0.553125, "y": 0.29722223, "w": 0.048958335, "h": 0.2462963
}
} }
] ]
@ -43,6 +49,7 @@ const dataSource = backEndData.map(o => {
warningInfo: [`盒子${o.boxId}`, `位置${o.position}`], warningInfo: [`盒子${o.boxId}`, `位置${o.position}`],
cabietText: `柜子ID: ${o.cabietId}`, cabietText: `柜子ID: ${o.cabietId}`,
warningTimestamp: o.warningTimestamp, warningTimestamp: o.warningTimestamp,
odRect: o.odRect
} }
}) })

View File

@ -95,47 +95,42 @@ export const WarningRecordCard: React.FC<WarningRecordCardProps> = (props) => {
// 显示 od // 显示 od
// 定义图片和canvas的引用 // 定义图片和canvas的引用
const imgRef = useRef<HTMLImageElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null); const canvasRef = useRef<HTMLCanvasElement>(null);
useEffect(() => { useEffect(() => {
// 获取图片和canvas元素
const img = imgRef.current;
const canvas = canvasRef.current; const canvas = canvasRef.current;
const ctx = canvas?.getContext('2d'); const ctx = canvas?.getContext('2d');
if (!canvas || !ctx) return const img = new Image();
img.width = imgStyle?.width ? parseInt(imgStyle?.width as string) : 336
img.height = imgStyle?.height ? parseInt(imgStyle?.height as string) : 203
debugger
if (!canvas || !ctx || !imageKey) return;
img.src = imageKey;
// 确保图片已经加载完成 img.onload = () => {
if (img?.complete) {
// 设置canvas的尺寸与图片相同 // 确保在图片加载完成后设置画布尺寸
canvas.width = img.width; canvas.width = img.width;
canvas.height = img.height; canvas.height = img.height;
// const odRect = {
// "x": 0.553125, "y": 0.29722223, "w": 0.048958335, "h": 0.2462963
// }
// 根据odRect对象计算矩形的起始坐标和尺寸
const x = get(odRect, 'x', 0) * img.width; const x = get(odRect, 'x', 0) * img.width;
const y = get(odRect, 'y', 0) * img.height; const y = get(odRect, 'y', 0) * img.height;
const width = get(odRect, 'w', 0) * img.width; const width = get(odRect, 'w', 0) * img.width;
const height = get(odRect, 'h', 0) * img.height; const height = get(odRect, 'h', 0) * img.height;
// 绘制矩形 // 绘制图片和矩形应在图片加载完成后进行
ctx.drawImage(img, 0, 0, img.width, img.height);
ctx.strokeStyle = '#FFF566'; ctx.strokeStyle = '#FFF566';
ctx.strokeRect(x, y, width, height); ctx.strokeRect(x, y, width, height);
} };
}, []); }, []);
return ( return (
<div className={componentName} key={id} onClick={handleClick} style={style}> <div className={componentName} key={id} onClick={handleClick} style={style}>
<Card <Card
//<img alt="预警图" src={imageKey} style={{ borderRadius: 0, ...imgStyle }} />
cover={<div className={`${componentName}-cover-img`} > cover={<div className={`${componentName}-cover-img`} >
<img ref={imgRef} alt="预警图" src={imageKey} style={{ borderRadius: 0, ...imgStyle }} /> <canvas ref={canvasRef} />
<canvas ref={canvasRef} style={{ position: 'absolute', top: 10, left: 10 }} />
</div>} </div>}
style={{ width: 356, height: 302, padding: 10, borderRadius: 4, ...selectedCardStyle, ...cardStyle }} style={{ width: 356, height: 302, padding: 10, borderRadius: 4, ...selectedCardStyle, ...cardStyle }}
{...cardProps} {...cardProps}