[英]React Konva center text in square
我正在尝试使用Rect
和Text
组件创建一个带有一些文本居中的按钮。
这是一段代码:
<Stage>
<Layer>
<Rect
x={window.innerWidth - 50}
y={window.innerHeight - 50}
width={30}
height={30}
fill='#f2f1f0'
stroke='#777'
strokeWidth={1}
>
<Text
fontSize={20}
text="+"
stroke='#777'
strokeWidth={1}
align="center"
/>
<Rect />
</Layer>
</Stage>
但我得到了这个错误:
Uncaught (in promise) TypeError: parentInstance.add is not a function
是否有正确的方法将Text
组件放入Rect
组件中?
尝试这个:
const App = () => {
const textRef = React.useRef();
const [size, setSize] = React.useState({ x: 0, y: 0 });
React.useEffect(() => {
setSize({
width: textRef.current.width(),
height: textRef.current.height()
});
}, []);
return (
<Stage width={window.innerWidth} height={window.innerHeight}>
<Layer>
<Group x={20} y={50}>
<Rect
x={window.innerWidth - 50}
y={window.innerHeight - 50}
width={size.width}
height={size.height}
fill='#f2f1f0'
stroke='#777'
strokeWidth={1}
/>
<Text
ref={textRef}
fontSize={20}
text="+"
stroke='#777'
strokeWidth={1}
align="center"
/>
</Group>
</Layer>
</Stage>
);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.