Last active
July 20, 2022 20:23
-
-
Save davidkpiano/dbf16417b7e0a728da19a714b73c8597 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Machine({ | |
initial: 'idle', | |
context: { | |
// the position of the box | |
x: 0, | |
y: 0, | |
// where you clicked | |
pointerx: 0, | |
pointery: 0, | |
// how far from where you clicked | |
dx: 0, // how far: x | |
dy: 0, // how far: y | |
}, | |
states: { | |
idle: { | |
on: { | |
// event: nextstate | |
mousedown: { | |
target: 'dragging', | |
actions: assign((context, mouseEvent) => { | |
return { | |
...context, | |
pointerx: mouseEvent.clientX, | |
pointery: mouseEvent.clientY | |
} | |
}) | |
} | |
} | |
}, | |
dragging: { | |
on: { | |
mousemove: { | |
target: 'dragging', | |
actions: assign((context, mouseEvent) => { | |
return { | |
...context, | |
dx: mouseEvent.clientX - context.pointerx, | |
dy: mouseEvent.clientY - context.pointery | |
} | |
}) | |
}, | |
mouseup: { | |
target: 'idle', | |
actions: assign((context) => { | |
return { | |
...context, | |
x: context.x + context.dx, | |
y: context.y + context.dy, | |
dx: 0, | |
dy: 0 | |
} | |
}) | |
// change context.x and context.y | |
} | |
} | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment