Search code examples
reactjskonvajskonvajs-reactjs

React Konva draw half circumference without diameter


I draw this half-circumference with the react Konva Arc object: enter image description here

This is my code:

import React, { useState } from "react";
import {Circle, Shape, Rect, Line, Group, Arc } from "react-konva";


export const Pitch = props => {
    const [width, setWidth] = useState(28);
    const [height, setHeight] = useState(15);
    const wWidth = 850;
    const wHeight = 440;
    const widthScaleFactor = wWidth / width;
    const heightScaleFactor = wHeight / height;
    const xOffset = 2 * widthScaleFactor;
    const yOffset = 2 * heightScaleFactor;
    const rectWidth = wWidth - 2*xOffset;
    const rectHeight = wHeight - 2*yOffset;
    return (
        <Group>
            <Rect
              x={0}
              y={0}
              width={wWidth}
              height={wHeight}
              fill="green"
            />
            <Rect
              x={xOffset}
              y={yOffset}
              width={rectWidth}
              height={rectHeight}
              fill="green"
              stroke="white"
              strokeWidth={4}
            />
            <Line
              x={xOffset}
              y={yOffset}
              points={[0, 0.9*heightScaleFactor, 1.575*widthScaleFactor, 0.9*heightScaleFactor]}
              stroke="white"
              strokeWidth={4}
            />
            <Line
              x={xOffset}
              y={-yOffset}
              points={[0, wHeight - 0.9*heightScaleFactor, 1.575*widthScaleFactor, wHeight - 0.9*heightScaleFactor]}
              stroke="white"
              strokeWidth={4}
            />
            <Arc
            x={xOffset + 1.575*widthScaleFactor }
            y={yOffset + rectHeight/2}
            angle={180}
            rotation={-90}
            innerRadius={0}
            outerRadius={rectHeight/2 - 0.9*heightScaleFactor}
            stroke="white"
            strokeWidth={4}
            />

        </Group>
        )
};

Is it possible do not draw the diameter of the circumference ?

Maybe should I have to use a different component ? I looked at the documentation of the Arc object but I can't get a solution.

Thanks who can help me


Solution

  • You can use Konva.Path shape to draw arc as you want:

    <Path
      stroke="white"
      strokeWidth={4}
      data={`M 0 ${-radius} A ${radius} ${radius} 0 0 1 0 ${radius}`}
    />
    

    Demo: https://codesandbox.io/s/react-konva-draw-arc-tpl6k