我需要用react-konva制作圆角垂直线,使用现有的API可以实现吗?如果是,怎么办?
我在 Line 类中使用了贝塞尔曲线 API,效果很好。现在我需要以某种方式将贝塞尔曲线修改为圆角垂直线。
Sth like this:
您可以通过多种方式实施它。您可以使用tension https://konvajs.org/api/Konva.Line.html#tension创建曲线或使用lineCap https://konvajs.org/api/Konva.Shape.html#lineCap财产。
但为了获得更多控制,最好创建自定义形状。
const RADIUS = 20;
const Line = ({ points }) => {
return (
<Shape
points={points}
sceneFunc={(context, shape) => {
const width = points[1].x - points[0].x;
const height = points[1].y - points[0].y;
const dir = Math.sign(height);
const radius = Math.min(RADIUS, Math.abs(height / 2), Math.abs(width / 2));
context.beginPath();
context.moveTo(points[0].x, points[0].y);
context.lineTo(points[0].x + width / 2 - RADIUS, points[0].y);
context.quadraticCurveTo(
points[0].x + width / 2,
points[0].y,
points[0].x + width / 2,
points[0].y + dir * radius
);
context.lineTo(points[0].x + width / 2, points[1].y - dir * radius);
context.quadraticCurveTo(
points[0].x + width / 2,
points[1].y,
points[0].x + width / 2 + radius,
points[1].y
);
context.lineTo(points[1].x, points[1].y);
context.fillStrokeShape(shape);
}}
stroke="black"
strokeWidth={2}
/>
);
};
Demo: https://codesandbox.io/s/757nw05p6 https://codesandbox.io/s/757nw05p6
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)