繁体   English   中英

如何在 React Konva 中的矩形内添加文本?

[英]How to add text inside rectangle in React Konva?

我正在使用 React Konva 绘制形状。 我想在最后一个矩形内插入一个问号。 我正在使用的代码如下。 但它并不完全有效:

<Grid item xs={12} lg={3} style={{border:'5px 5px 5px 5px solid white'}}>
    <Stage width={1200} height={1200}>

      <Layer>
      
        <RegularPolygon
          sides={3}
          x={50}
          y={100}
          width={100}
          height={100}
          stroke="orange"
        />

<RegularPolygon
          sides={3}
          x={200}
          y={100}
          width={100}
          height={100}
        //   fill="red"
          stroke="#00FFFF"
        />

<Rect
        
          x={300}
          y={50}
          width={80}
          height={80}
       
          stroke="orange"
        //   shadowBlur={5}
        />
  <Group x={450} y={30}>
<Rect
          x={430}
          y={40}
          width={100}
          height={100}
        
          stroke="white"
          strokeWidth={5}
          
        >
          <Text
                  fontSize={20}
                  text="?"
                  stroke='white'
                  strokeWidth={5}
                  align="center"
                />
        </Rect>
        </Group>
      </Layer>
    </Stage>
    </Grid>

我已经添加了带有文本的组标签以使其工作,但它不起作用。

您是否尝试过不使用Rect包装Text并将x & y道具添加到Text

<Rect
  x={430}
  y={40}
  width={100}
  height={100}
  stroke="white"
  strokeWidth={5}
/>
<Text
  fontSize={20}
  text="?"
  stroke='white'
  strokeWidth={5}
  align="center"
  // similar coordinates than your Rect component
  x={480}
  y={90}
/>

检查这个codesandbox示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM