Skip to content

Commit 747f32a

Browse files
authored
Merge pull request #4 from zheeeng/bump
fix: prevent effect logic
2 parents d2c3a46 + d5622b2 commit 747f32a

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

packages/pointer-lock-movement/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pointer-lock-movement",
3-
"version": "0.1.8",
3+
"version": "0.1.9",
44
"author": "Zheeeng <[email protected]>",
55
"description": "A pointer lock movement manager for customizing your own creative UI.",
66
"keywords": [

packages/pointer-lock-movement/src/pointer-lock-movement.ts

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -82,50 +82,54 @@ export const pointerLockMovement = (
8282
}
8383

8484
const move: CoData<MoveContext, PointerEvent> = (context, effect) => payload => {
85-
if (payload.defaultPrevented) {
86-
return move(context, effect)
85+
const contextPatch: Pick<MoveContext, 'event' | 'movementX' | 'movementY' | 'x' | 'y' | 'status'> = {
86+
event: payload,
87+
movementX: payload.movementX,
88+
movementY: payload.movementY,
89+
x: context.x + context.movementX,
90+
y: context.y + context.movementY,
91+
status: 'moving',
8792
}
8893

89-
context.event = payload
90-
context.movementX = payload.movementX
91-
context.movementY = payload.movementY
92-
context.x += context.movementX
93-
context.y += context.movementY
94-
context.status = 'moving'
95-
9694
if (options.loopBehavior === 'loop') {
97-
if (context.x > context.maxWidth) {
98-
context.x -= context.maxWidth
99-
} else if (context.x < 0) {
100-
context.x += context.maxWidth
95+
if (contextPatch.x > context.maxWidth) {
96+
contextPatch.x -= context.maxWidth
97+
} else if (contextPatch.x < 0) {
98+
contextPatch.x += context.maxWidth
10199
}
102100

103-
if (context.y > context.maxHeight) {
104-
context.y -= context.maxHeight
105-
} else if (context.y < 0) {
106-
context.y += context.maxHeight
101+
if (contextPatch.y > context.maxHeight) {
102+
contextPatch.y -= context.maxHeight
103+
} else if (contextPatch.y < 0) {
104+
contextPatch.y += context.maxHeight
107105
}
108106
} else if (options.loopBehavior === 'stop') {
109-
if (context.x > context.maxWidth) {
110-
context.x = context.maxWidth
111-
context.status = 'stopped'
112-
} else if (context.x < 0) {
113-
context.x = 0
107+
if (contextPatch.x > context.maxWidth) {
108+
contextPatch.x = context.maxWidth
114109
context.status = 'stopped'
110+
} else if (contextPatch.x < 0) {
111+
contextPatch.x = 0
112+
contextPatch.status = 'stopped'
115113
}
116114

117-
if (context.y > context.maxHeight) {
118-
context.y = context.maxHeight
119-
context.status = 'stopped'
120-
} else if (context.y < 0) {
121-
context.y = 0
115+
if (contextPatch.y > context.maxHeight) {
116+
contextPatch.y = context.maxHeight
122117
context.status = 'stopped'
118+
} else if (contextPatch.y < 0) {
119+
contextPatch.y = 0
120+
contextPatch.status = 'stopped'
123121
}
124122
}
125123

126-
effect(context)
124+
const newContext = { ...context, ...contextPatch }
125+
126+
effect(newContext)
127+
128+
if (newContext.event.defaultPrevented) {
129+
return move(context, effect)
130+
}
127131

128-
return move(context, effect)
132+
return move(newContext, effect)
129133
}
130134

131135
function startup () {

0 commit comments

Comments
 (0)