|
|
@ -31,104 +31,167 @@ export default function StandardLayer(props: { |
|
|
|
rightPadding: number; |
|
|
|
topPadding: number; |
|
|
|
bottomPadding: number; |
|
|
|
columns: number; |
|
|
|
rows: number; |
|
|
|
}) { |
|
|
|
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 canvasRef = useRef<HTMLCanvasElement | null>(null); |
|
|
|
|
|
|
|
const draw = useCallback((ctx: CanvasRenderingContext2D) => { |
|
|
|
// 使用context对象进行绘图
|
|
|
|
ctx.fillStyle = "blue"; |
|
|
|
ctx.strokeStyle = "blue"; |
|
|
|
|
|
|
|
ctx.translate(400, 100); |
|
|
|
ctx.moveTo(-400, 0); |
|
|
|
ctx.lineTo(400, 0); |
|
|
|
ctx.moveTo(0, -100); |
|
|
|
ctx.lineTo(0, 500); |
|
|
|
ctx.stroke(); |
|
|
|
|
|
|
|
ctx.beginPath(); |
|
|
|
ctx.strokeStyle = "red"; |
|
|
|
// ---- 右侧
|
|
|
|
let point0 = pointsRight[0]; |
|
|
|
let point1 = pointsRight[1]; |
|
|
|
ctx.moveTo(point0[0], point0[1]); |
|
|
|
let param = calculateCircleBelow(point0[0], point0[1], point1[0], point1[1], 300 * unitPx); |
|
|
|
console.log(param); |
|
|
|
ctx.arc(param.center.x, param.center.y, 300 * unitPx, param.anglesForCenter.startAngle, param.anglesForCenter.endAngle); |
|
|
|
|
|
|
|
point0 = pointsRight[1]; |
|
|
|
point1 = pointsRight[2]; |
|
|
|
ctx.moveTo(point0[0], point0[1]); |
|
|
|
param = calculateCircleBelow(point0[0], point0[1], point1[0], point1[1], 80 * unitPx); |
|
|
|
console.log(param); |
|
|
|
ctx.arc(param.center.x, param.center.y, 80 * unitPx, param.anglesForCenter.startAngle, param.anglesForCenter.endAngle); |
|
|
|
|
|
|
|
point0 = pointsRight[2]; |
|
|
|
point1 = pointsRight[3]; |
|
|
|
ctx.moveTo(point0[0], point0[1]); |
|
|
|
param = calculateCircleBelow(point0[0], point0[1], point1[0], point1[1], 13 * unitPx); |
|
|
|
console.log(param); |
|
|
|
ctx.arc(param.center.x, param.center.y, 13 * unitPx, param.anglesForCenter.startAngle, param.anglesForCenter.endAngle); |
|
|
|
|
|
|
|
point0 = pointsRight[3]; |
|
|
|
point1 = pointsRight[4]; |
|
|
|
ctx.moveTo(point0[0], point0[1]); |
|
|
|
ctx.lineTo(point1[0], point1[1]); |
|
|
|
|
|
|
|
point0 = pointsRight[4]; |
|
|
|
point1 = pointsRight[5]; |
|
|
|
ctx.moveTo(point0[0], point0[1]); |
|
|
|
param = calculateCircleAbove(point0[0], point0[1], point1[0], point1[1], 5 * unitPx); |
|
|
|
console.log(param); |
|
|
|
ctx.arc(param.center.x, param.center.y, 5 * unitPx, param.anglesForCenter.startAngle, param.anglesForCenter.endAngle); |
|
|
|
|
|
|
|
point0 = pointsRight[5]; |
|
|
|
point1 = pointsRight[6]; |
|
|
|
ctx.moveTo(point0[0], point0[1]); |
|
|
|
ctx.lineTo(point1[0], point1[1]); |
|
|
|
|
|
|
|
// ---- 左侧
|
|
|
|
point0 = pointsLeft[0]; |
|
|
|
point1 = pointsLeft[1]; |
|
|
|
ctx.moveTo(point0[0], point0[1]); |
|
|
|
ctx.lineTo(point1[0], point1[1]); |
|
|
|
|
|
|
|
point0 = pointsLeft[1]; |
|
|
|
point1 = pointsLeft[2]; |
|
|
|
ctx.moveTo(point0[0], point0[1]); |
|
|
|
param = calculateCircleAbove(point0[0], point0[1], point1[0], point1[1], 5 * unitPx); |
|
|
|
console.log(param); |
|
|
|
ctx.arc(param.center.x, param.center.y, 5 * unitPx, param.anglesForCenter.startAngle, param.anglesForCenter.endAngle); |
|
|
|
|
|
|
|
point0 = pointsLeft[2]; |
|
|
|
point1 = pointsLeft[3]; |
|
|
|
ctx.moveTo(point0[0], point0[1]); |
|
|
|
ctx.lineTo(point1[0], point1[1]); |
|
|
|
|
|
|
|
point0 = pointsLeft[3]; |
|
|
|
point1 = pointsLeft[4]; |
|
|
|
ctx.moveTo(point0[0], point0[1]); |
|
|
|
param = calculateCircleBelow(point0[0], point0[1], point1[0], point1[1], 13 * unitPx); |
|
|
|
console.log(param); |
|
|
|
ctx.arc(param.center.x, param.center.y, 13 * unitPx, param.anglesForCenter.startAngle, param.anglesForCenter.endAngle); |
|
|
|
|
|
|
|
point0 = pointsLeft[4]; |
|
|
|
point1 = pointsLeft[5]; |
|
|
|
ctx.moveTo(point0[0], point0[1]); |
|
|
|
param = calculateCircleBelow(point0[0], point0[1], point1[0], point1[1], 80 * unitPx); |
|
|
|
console.log(param); |
|
|
|
ctx.arc(param.center.x, param.center.y, 80 * unitPx, param.anglesForCenter.startAngle, param.anglesForCenter.endAngle); |
|
|
|
|
|
|
|
point0 = pointsLeft[5]; |
|
|
|
point1 = pointsLeft[6]; |
|
|
|
ctx.moveTo(point0[0], point0[1]); |
|
|
|
param = calculateCircleBelow(point0[0], point0[1], point1[0], point1[1], 300 * unitPx); |
|
|
|
console.log(param); |
|
|
|
ctx.arc(param.center.x, param.center.y, 300 * unitPx, param.anglesForCenter.startAngle, param.anglesForCenter.endAngle); |
|
|
|
|
|
|
|
ctx.stroke(); |
|
|
|
// ctx.fillRect(100, 100, 150, 100); // 绘制一个矩形
|
|
|
|
}, []); |
|
|
|
const draw = useCallback( |
|
|
|
(ctx: CanvasRenderingContext2D) => { |
|
|
|
ctx.strokeStyle = "#999"; |
|
|
|
// 偏移原点
|
|
|
|
const xOffset = (xEndPx - xStartPx) / 2; |
|
|
|
const yOffset = yStepPx * 2; |
|
|
|
const yMax = yEndPx - yStartPx - yOffset; |
|
|
|
ctx.translate(xStartPx + xOffset, yStartPx + yOffset); |
|
|
|
// 绘制原点交叉线
|
|
|
|
ctx.moveTo(-xOffset, 0); |
|
|
|
ctx.lineTo(xOffset, 0); |
|
|
|
ctx.moveTo(0, -yOffset); |
|
|
|
ctx.lineTo(0, yMax); |
|
|
|
ctx.stroke(); |
|
|
|
|
|
|
|
// 绘制x轴y轴 单位数值
|
|
|
|
ctx.beginPath(); |
|
|
|
ctx.fillStyle = "#333333"; |
|
|
|
ctx.textAlign = "center"; |
|
|
|
ctx.font = "normal 14px system"; |
|
|
|
for (let index = -4; index < 5; index++) { |
|
|
|
ctx.fillText((index * 10).toString(), xStepPx * index, yMax + 20); |
|
|
|
} |
|
|
|
for (let index = -1; index < 5; index++) { |
|
|
|
ctx.fillText((-index * 10).toString(), -xOffset - (index > 0 ? 18 : 14), yStepPx * index + 4); |
|
|
|
} |
|
|
|
|
|
|
|
// 绘制标准线
|
|
|
|
ctx.beginPath(); |
|
|
|
ctx.strokeStyle = "red"; |
|
|
|
// ---- 右侧
|
|
|
|
let point0 = pointsRight[0]; |
|
|
|
let point1 = pointsRight[1]; |
|
|
|
ctx.moveTo(point0[0], point0[1]); |
|
|
|
let param = calculateCircleBelow(point0[0], point0[1], point1[0], point1[1], 300 * unitPx); |
|
|
|
console.log(param); |
|
|
|
ctx.arc( |
|
|
|
param.center.x, |
|
|
|
param.center.y, |
|
|
|
300 * unitPx, |
|
|
|
param.anglesForCenter.startAngle, |
|
|
|
param.anglesForCenter.endAngle |
|
|
|
); |
|
|
|
|
|
|
|
point0 = pointsRight[1]; |
|
|
|
point1 = pointsRight[2]; |
|
|
|
ctx.moveTo(point0[0], point0[1]); |
|
|
|
param = calculateCircleBelow(point0[0], point0[1], point1[0], point1[1], 80 * unitPx); |
|
|
|
console.log(param); |
|
|
|
ctx.arc( |
|
|
|
param.center.x, |
|
|
|
param.center.y, |
|
|
|
80 * unitPx, |
|
|
|
param.anglesForCenter.startAngle, |
|
|
|
param.anglesForCenter.endAngle |
|
|
|
); |
|
|
|
|
|
|
|
point0 = pointsRight[2]; |
|
|
|
point1 = pointsRight[3]; |
|
|
|
ctx.moveTo(point0[0], point0[1]); |
|
|
|
param = calculateCircleBelow(point0[0], point0[1], point1[0], point1[1], 13 * unitPx); |
|
|
|
console.log(param); |
|
|
|
ctx.arc( |
|
|
|
param.center.x, |
|
|
|
param.center.y, |
|
|
|
13 * unitPx, |
|
|
|
param.anglesForCenter.startAngle, |
|
|
|
param.anglesForCenter.endAngle |
|
|
|
); |
|
|
|
|
|
|
|
point0 = pointsRight[3]; |
|
|
|
point1 = pointsRight[4]; |
|
|
|
ctx.moveTo(point0[0], point0[1]); |
|
|
|
ctx.lineTo(point1[0], point1[1]); |
|
|
|
|
|
|
|
point0 = pointsRight[4]; |
|
|
|
point1 = pointsRight[5]; |
|
|
|
ctx.moveTo(point0[0], point0[1]); |
|
|
|
param = calculateCircleAbove(point0[0], point0[1], point1[0], point1[1], 5 * unitPx); |
|
|
|
console.log(param); |
|
|
|
ctx.arc(param.center.x, param.center.y, 5 * unitPx, param.anglesForCenter.startAngle, param.anglesForCenter.endAngle); |
|
|
|
|
|
|
|
point0 = pointsRight[5]; |
|
|
|
point1 = pointsRight[6]; |
|
|
|
ctx.moveTo(point0[0], point0[1]); |
|
|
|
ctx.lineTo(point1[0], point1[1]); |
|
|
|
|
|
|
|
// ---- 左侧
|
|
|
|
point0 = pointsLeft[0]; |
|
|
|
point1 = pointsLeft[1]; |
|
|
|
ctx.moveTo(point0[0], point0[1]); |
|
|
|
ctx.lineTo(point1[0], point1[1]); |
|
|
|
|
|
|
|
point0 = pointsLeft[1]; |
|
|
|
point1 = pointsLeft[2]; |
|
|
|
ctx.moveTo(point0[0], point0[1]); |
|
|
|
param = calculateCircleAbove(point0[0], point0[1], point1[0], point1[1], 5 * unitPx); |
|
|
|
console.log(param); |
|
|
|
ctx.arc(param.center.x, param.center.y, 5 * unitPx, param.anglesForCenter.startAngle, param.anglesForCenter.endAngle); |
|
|
|
|
|
|
|
point0 = pointsLeft[2]; |
|
|
|
point1 = pointsLeft[3]; |
|
|
|
ctx.moveTo(point0[0], point0[1]); |
|
|
|
ctx.lineTo(point1[0], point1[1]); |
|
|
|
|
|
|
|
point0 = pointsLeft[3]; |
|
|
|
point1 = pointsLeft[4]; |
|
|
|
ctx.moveTo(point0[0], point0[1]); |
|
|
|
param = calculateCircleBelow(point0[0], point0[1], point1[0], point1[1], 13 * unitPx); |
|
|
|
console.log(param); |
|
|
|
ctx.arc( |
|
|
|
param.center.x, |
|
|
|
param.center.y, |
|
|
|
13 * unitPx, |
|
|
|
param.anglesForCenter.startAngle, |
|
|
|
param.anglesForCenter.endAngle |
|
|
|
); |
|
|
|
|
|
|
|
point0 = pointsLeft[4]; |
|
|
|
point1 = pointsLeft[5]; |
|
|
|
ctx.moveTo(point0[0], point0[1]); |
|
|
|
param = calculateCircleBelow(point0[0], point0[1], point1[0], point1[1], 80 * unitPx); |
|
|
|
console.log(param); |
|
|
|
ctx.arc( |
|
|
|
param.center.x, |
|
|
|
param.center.y, |
|
|
|
80 * unitPx, |
|
|
|
param.anglesForCenter.startAngle, |
|
|
|
param.anglesForCenter.endAngle |
|
|
|
); |
|
|
|
|
|
|
|
point0 = pointsLeft[5]; |
|
|
|
point1 = pointsLeft[6]; |
|
|
|
ctx.moveTo(point0[0], point0[1]); |
|
|
|
param = calculateCircleBelow(point0[0], point0[1], point1[0], point1[1], 300 * unitPx); |
|
|
|
console.log(param); |
|
|
|
ctx.arc( |
|
|
|
param.center.x, |
|
|
|
param.center.y, |
|
|
|
300 * unitPx, |
|
|
|
param.anglesForCenter.startAngle, |
|
|
|
param.anglesForCenter.endAngle |
|
|
|
); |
|
|
|
|
|
|
|
ctx.stroke(); |
|
|
|
// ctx.fillRect(100, 100, 150, 100); // 绘制一个矩形
|
|
|
|
}, |
|
|
|
[xEndPx, xStartPx, xStepPx, yEndPx, yStartPx, yStepPx] |
|
|
|
); |
|
|
|
useEffect(() => { |
|
|
|
const canvas = canvasRef.current; |
|
|
|
if (!canvas) return; |
|
|
|