Konva.Rect 对象删除元素后界面不更新的解决方法
这是因为 Konva.Rect 对象是通过引用传递的,所以删除数组中的元素并不会影响到界面上的矩形。要想更新界面上的矩形,你需要手动删除该矩形并重新绘制。
可以通过调用rect.remove()方法来删除矩形,并通过layer.add(rect)方法将更新后的矩形添加到图层中,最后调用layer.draw()方法重新绘制图层。
示例代码如下:
this.newData.forEach((point, index) => {
if (point.x >= this.rect.attrs.x &&
point.x <= (this.rect.attrs.x + this.rect.attrs.width) &&
point.y >= this.rect.attrs.y &&
point.y <= (this.rect.attrs.y + this.rect.attrs.height)) {
this.newData.splice(index, 1);
this.rect.remove(); // 删除旧的矩形
this.rect = new Konva.Rect({ ... }); // 创建新的矩形
layer.add(this.rect); // 添加到图层中
layer.draw(); // 重新绘制图层
}
});
请确保layer是正确的 Konva.Layer 对象,可以通过stage.find('Layer')[0]来获取。
原文地址: https://www.cveoy.top/t/topic/pcIN 著作权归作者所有。请勿转载和采集!