reactjs - 矩形移动到 React Konva 中的原始绘制位置
问题描述
我所做的 - 我在 React Konva 中点击和拖动鼠标制作了 2 个矩形。绘制它们后,我可以拖动并更改这些矩形的位置。但是,当我在更改先前绘制的矩形的位置后尝试制作第三个矩形时,它们会移回我不想要的原始绘制位置。我怎么解决这个问题?
这是我的代码 -
constructor(props){
this.state = {
shapes : [],
isDrawing : false,
isDrawingMode : true,
image : null,
}
}
// I tried adding many things here but nothing worked as I wanted
moveRects(e) {
var shapesList = this.state.shapes;
shapesList[e.target.index].x = e.target._lastPos.x;
shapesList[e.target.index].y = e.target._lastPos.y;
console.log(shapesList[e.target._id]);
}
render() {
return (
<div>
<input type = "checkbox" checked = {this.state.isDrawingMode} onChange = {this.handleCheckboxChange} />
<label>Drawing Mode</label>
<Stage
ref = 'stage'
width = {window.innerWidth}
height = {window.innerHeight}
onContentClick = {this.handleClick}
onContentMouseMove = {this.handleMouseMove}
>
<Layer ref = 'layer'>
<Image image = {this.state.image} />
{this.state.shapes.map((shape) => {
return (
<Group>
<Rect
x = {shape.x}
y = {shape.y}
width = {shape.width}
height = {shape.height}
isDrawingMode = {this.state.isDrawingMode}
stroke = {2}
draggable = "true"
onDragEnd = {(e) => this.moveRects(e)}
/>
</Group >
);
})}
</Layer>
</Stage>
</div>
);
}
解决方案
推荐阅读
- reactjs - React useCallback 在第一次更改时不更新状态 onChange
- javascript - 构建和上传项目时在服务器中找不到文件
- android - AAPT:错误:找不到资源可绘制/edittext_search_donation_request
- groovy - 我有身份证清单。现在我想将它们存储在数组中以匹配 id
- sharepoint-online - SharePoint - 对所有网站的权限
- javascript - 如何对数组进行排序,但同时在页面以 Javascript 加载时选择一个项目
- excel - 如何创建一个匹配 2 列名称的公式,然后对另一个相邻列求和
- css - 无法在条纹元素中使用 @font-face 导入自定义字体
- r - 如何重新编码为新变量并将其添加到 tibble
- javascript - 如何在页面加载时使用 javascript 打开弹出窗口而不阻止网页浏览器的弹出窗口?