5 changed files with 144 additions and 54 deletions
-
13src/pages/measure/components/MeasureAction.tsx
-
109src/pages/measure/components/graph/MarkLayer.tsx
-
1src/pages/measure/components/graph/RealtimeLayer.tsx
-
49src/pages/measure/components/graph/StandardLayer.tsx
-
26src/utils/index.ts
@ -1,31 +1,88 @@ |
|||
import { useCallback, useEffect, useRef } from "react"; |
|||
import { calculatePointOnCircle, findSymmetricPoint } from "../../../../utils"; |
|||
|
|||
const marks = [ |
|||
{ x: 9.949007022412, y: 0.1650166186941, degree: -80 }, |
|||
{ x: 25.35, y: 2.184814802617, degree: -60 }, |
|||
{ x: -9.949007022412, y: 0.1650166186941, degree: -100 }, |
|||
{ x: -25.35, y: 2.184814802617, degree: -120 }, |
|||
]; |
|||
|
|||
export default function MarkLayer(props: { |
|||
width: number; |
|||
height: number; |
|||
leftPadding: number; |
|||
rightPadding: number; |
|||
topPadding: number; |
|||
bottomPadding: number; |
|||
width: number; |
|||
height: number; |
|||
leftPadding: number; |
|||
rightPadding: number; |
|||
topPadding: number; |
|||
bottomPadding: number; |
|||
columns: number; |
|||
rows: number; |
|||
}) { |
|||
const canvasRef = useRef<HTMLCanvasElement | null>(null); |
|||
const draw = useCallback((ctx: CanvasRenderingContext2D) => { |
|||
// 使用context对象进行绘图
|
|||
ctx.fillStyle = "skyblue"; // 设置填充颜色
|
|||
ctx.fillRect(50, 50, 150, 100); // 绘制一个矩形
|
|||
}, []); |
|||
useEffect(() => { |
|||
const canvas = canvasRef.current; |
|||
if (!canvas) return; |
|||
|
|||
const context = canvas.getContext("2d"); |
|||
if (!context) return; |
|||
draw(context); |
|||
}, [draw]); |
|||
|
|||
return ( |
|||
<div> |
|||
<canvas ref={canvasRef} width={props.width} height={props.height}></canvas> |
|||
</div> |
|||
); |
|||
const xStartPx = props.leftPadding; |
|||
const xEndPx = props.width - props.rightPadding; |
|||
const xStepPx = (props.width - props.leftPadding - props.rightPadding) / props.columns; |
|||
const yStartPx = props.topPadding; |
|||
// const yEndPx = props.height - props.bottomPadding;
|
|||
const yStepPx = (props.height - props.topPadding - props.bottomPadding) / props.rows; |
|||
|
|||
const unitPx = xStepPx / 10; |
|||
const canvasRef = useRef<HTMLCanvasElement | null>(null); |
|||
|
|||
const calcPoints = useCallback( |
|||
(arr: typeof marks) => { |
|||
return arr.map(p => { |
|||
const p1 = calculatePointOnCircle(p.x * unitPx, p.y * unitPx, xStepPx / 2, p.degree); |
|||
const p2 = findSymmetricPoint(p1.x, p1.y, p.x * unitPx, p.y * unitPx); |
|||
// 角度文本,向外偏移
|
|||
const p3 = calculatePointOnCircle(p.x * unitPx, p.y * unitPx, xStepPx / 2 + 10, p.degree); |
|||
return [p1, p2, p3]; |
|||
}); |
|||
}, |
|||
[unitPx, xStepPx] |
|||
); |
|||
|
|||
const draw = useCallback( |
|||
(ctx: CanvasRenderingContext2D) => { |
|||
// 偏移原点
|
|||
const xOffset = (xEndPx - xStartPx) / 2; |
|||
const yOffset = yStepPx * 2; |
|||
ctx.translate(xStartPx + xOffset, yStartPx + yOffset); |
|||
|
|||
ctx.fillStyle = "#333333"; |
|||
ctx.textAlign = "center"; |
|||
ctx.font = "normal 14px system"; |
|||
|
|||
const lines = calcPoints(marks); |
|||
for (let idx = 0; idx < lines.length; idx++) { |
|||
const line = lines[idx]; |
|||
ctx.moveTo(line[0].x, line[0].y); |
|||
ctx.lineTo(line[1].x, line[1].y); |
|||
|
|||
ctx.save(); |
|||
ctx.translate(line[2].x, line[2].y); |
|||
ctx.rotate(((marks[idx].degree + 90) * Math.PI) / 180); |
|||
ctx.fillText(`${-marks[idx].degree}°`, 0, 0); |
|||
ctx.restore(); |
|||
} |
|||
ctx.stroke(); |
|||
// ctx.fillStyle = "skyblue"; // 设置填充颜色
|
|||
// ctx.fillRect(50, 50, 150, 100); // 绘制一个矩形
|
|||
}, |
|||
[calcPoints, xEndPx, xStartPx, yStartPx, yStepPx] |
|||
); |
|||
|
|||
useEffect(() => { |
|||
const canvas = canvasRef.current; |
|||
if (!canvas) return; |
|||
|
|||
const context = canvas.getContext("2d"); |
|||
if (!context) return; |
|||
draw(context); |
|||
}, [draw]); |
|||
|
|||
return ( |
|||
<div> |
|||
<canvas ref={canvasRef} width={props.width} height={props.height}></canvas> |
|||
</div> |
|||
); |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue