How to restrict dragging nodes by an axis? #904
-
I would like to have a node to be dragged only vertically or horizontally. I tried playing with events but it didn't work. In d3 we could override drag behavior inside 'drag' event but it seems it does not work here. here is a node example i tried: {
id: 'id1',
label: 'drag only vertivally',
position: { x: 100, y: 100 },
events: {
drag({ event }) {
if (event.movementX !== 0) {
event.preventDefault() //no effect
}
},
},
style: { backgroundColor: 'rgba(16, 185, 129, 0.5)', width: '600px', height: '200px' },
} as Node, |
Beta Was this translation helpful? Give feedback.
Answered by
bcakmakoglu
May 1, 2023
Replies: 1 comment 2 replies
-
There's no option to restrict axis of movement. onNodesChange((changes) => {
const positionChanges = changes.filter((change) => change.type === 'position') as NodePositionChange[]
if (positionChanges.length) {
applyNodeChanges(
positionChanges.map((change) => {
return {
...change,
position: {
y: change.from.y,
x: change.position.x,
},
}
}),
)
} else {
applyNodeChanges(changes)
}
}) <VueFlow v-model="elements" :apply-default="false" /> |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
bcakmakoglu
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's no option to restrict axis of movement.
You could do this by handling node changes yourself something like this
Here's an example