【发布时间】:2018-08-07 23:01:49
【问题描述】:
我正在尝试在选定矩形的角上绘制小矩形。问题是它们没有出现在角落。
我正在动态绘制较大的矩形,当我双击任何矩形时,它们会被选中(当前选定的矩形存储在全局变量 SelectedRectangle 中)并且较小的矩形必须出现在所选矩形的角上。
请帮助我。这是我的代码 -
var selectedRectangle = someSelectedRectangle;
export default class MyRectangle extends React.Component {
constructor(props) {
super(props);
this.selectShape = this.selectShape.bind (this);
this.showCorners = this.showCorners.bind (this);
this.drawRect = this.drawRect.bind (this);
}
selectShape (e) {
if(selectedRectangle)
this.showCorners ();
}
showCorners () {
this.drawRect (selectedRectangle.x, selectedRectangle.y);
this.drawRect (selectedRectangle.x + selectedRectangle.width, selectedRectangle.y);
this.drawRect (selectedRectangle.x + selectedRectangle.width, selectedRectangle.y + selectedRectangle.height);
this.drawRect (selectedRectangle.x, selectedRectangle.y + selectedRectangle.height);
}
drawRect (x, y) {
var layer = this.refs.layer;
var rect = new Konva.Rect({
x : {x},
y : {y},
width : 10,
height : 10,
fill : "black",
draggable : "true",
});
layer.add(rect);
}
render() {
return (
<div>
<Stage
ref = 'stage'
width = {window.innerWidth}
height = {window.innerHeight}
>
<Layer ref = 'layer'>
{this.state.shapes.map((shape) => {
return (
<Group>
<Rect
name = 'rect'
x = {shape.x}
y = {shape.y}
width = {shape.width}
height = {shape.height}
isDrawingMode = {this.state.isDrawingMode}
strokeWidth = {2}
draggable = "true"
stroke = "yellow"
fill = "green"
opacity = {0.4}
onDblClick = {(e) => this.selectShape(e)}
/>
</ Group >
);
})}
</ Layer>
</ Stage>
</ div>
);
}
}
【问题讨论】:
-
如果您使用 react-konva,请不要手动创建/添加矩形。相反,在渲染函数中定义它们。请为您的问题创建一个在线演示。
-
这里是整个代码的链接。在我系统的 Visual Studio Code 编辑器中正常工作时,它在此编辑器中抛出错误。抱歉,我没听懂你想说什么。 codesandbox.io/s/7zyqxrjj46
-
是否只需要在渲染函数中添加矩形?
标签: javascript reactjs konvajs