首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用react-konva绘制圆角垂直线?

如何使用react-konva绘制圆角垂直线?
EN

Stack Overflow用户
提问于 2019-05-12 07:54:15
回答 1查看 667关注 0票数 0

我需要用react-konva做圆角垂直线,用现有的API能实现吗?如果是,是如何实现的?

我使用bezier API for Line类,它工作得很好。不知何故,现在我需要修改贝塞尔曲线,使其成为四舍五入的垂直线。

像这样的东西:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-15 08:19:44

有很多方法可以实现它。可以使用tension创建曲线,也可以使用lineCap特性。

但为了有更多的控制,最好是创建自定义形状。

代码语言:javascript
复制
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}
    />
  );
};

演示:https://codesandbox.io/s/757nw05p6

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56095148

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档