React Konva 在拖动时更改 zIndex
React Konva change zIndex on drag
嘿,我有一些用 React konva 编写的拖放游戏。我的可拖动部分看起来像这样(为方便起见进行了简化
return this.props.areas.map((area) =>
<Image draggable=true
{...area.imageProps}
onDragStart={() => {...}}
onDragEnd={() => {...}}
onDragMove={() => {...}}/> )
我希望当前被拖动的那块是最高层(最高zIndex)上的一块。
问题是 React-Konva 不支持 zIndexing,并且似乎希望您在 render 方法中根据顺序强制执行 zIndexing。
我的解决方案是用 currentDragIndex 更新 reactState
...<Image
onDragStart = {this.setState({currentDragIndex: area.index})}
/>
然后我尝试在渲染方法中重新排序。然而,这会导致拖动事件被中断。
谁有好的解决办法?
如果您有一些项目的数组,最简单的解决方案是通过将拖动节点移动到数组的末尾来更改该数组。在您的 render
函数中,您只需要从数组中提取项目。
handleDragStart = e => {
const id = e.target.name();
const items = this.state.items.slice();
const item = items.find(i => i.id === id);
const index = items.indexOf(item);
// remove from the list:
items.splice(index, 1);
// add to the top
items.push(item);
this.setState({
items
});
};
您可以只使用 moveToTop() 方法
onDragStart={(el) => {el.target.moveToTop();}}
嘿,我有一些用 React konva 编写的拖放游戏。我的可拖动部分看起来像这样(为方便起见进行了简化
return this.props.areas.map((area) =>
<Image draggable=true
{...area.imageProps}
onDragStart={() => {...}}
onDragEnd={() => {...}}
onDragMove={() => {...}}/> )
我希望当前被拖动的那块是最高层(最高zIndex)上的一块。
问题是 React-Konva 不支持 zIndexing,并且似乎希望您在 render 方法中根据顺序强制执行 zIndexing。
我的解决方案是用 currentDragIndex 更新 reactState
...<Image
onDragStart = {this.setState({currentDragIndex: area.index})}
/>
然后我尝试在渲染方法中重新排序。然而,这会导致拖动事件被中断。
谁有好的解决办法?
如果您有一些项目的数组,最简单的解决方案是通过将拖动节点移动到数组的末尾来更改该数组。在您的 render
函数中,您只需要从数组中提取项目。
handleDragStart = e => {
const id = e.target.name();
const items = this.state.items.slice();
const item = items.find(i => i.id === id);
const index = items.indexOf(item);
// remove from the list:
items.splice(index, 1);
// add to the top
items.push(item);
this.setState({
items
});
};
您可以只使用 moveToTop() 方法
onDragStart={(el) => {el.target.moveToTop();}}