Skip to content

Commit c658821

Browse files
committed
fix: (touch) interaction that doesnt emit a move event
1 parent f38cce1 commit c658821

File tree

2 files changed

+28
-33
lines changed

2 files changed

+28
-33
lines changed

packages/pointer-events/src/forward.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -113,28 +113,32 @@ function forwardEvents(
113113
const forwardPointerCapture = options?.forwardPointerCapture ?? true
114114
const pointerMap = new Map<number, Pointer>()
115115
const pointerTypePrefix = options.pointerTypePrefix ?? 'forward-'
116-
const getInnerPointer = ({ pointerId = -1, pointerType = 'mouse', pointerState }: ForwardablePointerEvent) => {
117-
let innerPointer = pointerMap.get(pointerId)
116+
const getInnerPointer = (event: ForwardablePointerEvent, eventType: InternalEventType) => {
117+
let innerPointer = pointerMap.get(event.pointerId)
118118
if (innerPointer != null) {
119119
return innerPointer
120120
}
121-
pointerMap.set(
122-
pointerId,
123-
(innerPointer = new Pointer(
124-
generateUniquePointerId(),
125-
`${pointerTypePrefix}${pointerType}`,
126-
pointerState,
127-
new CameraRayIntersector((nativeEvent, coords) => {
128-
toCoords(nativeEvent, coords)
129-
return getCamera()
130-
}, options),
131-
getCamera,
132-
undefined,
133-
forwardPointerCapture ? setPointerCapture.bind(null, pointerId) : undefined,
134-
forwardPointerCapture ? releasePointerCapture.bind(null, pointerId) : undefined,
135-
options,
136-
)),
121+
innerPointer = new Pointer(
122+
generateUniquePointerId(),
123+
`${pointerTypePrefix}${event.pointerType}`,
124+
event.pointerState,
125+
new CameraRayIntersector((nativeEvent, coords) => {
126+
toCoords(nativeEvent, coords)
127+
return getCamera()
128+
}, options),
129+
getCamera,
130+
undefined,
131+
forwardPointerCapture ? setPointerCapture.bind(null, event.pointerId) : undefined,
132+
forwardPointerCapture ? releasePointerCapture.bind(null, event.pointerId) : undefined,
133+
options,
137134
)
135+
if (eventType != 'move' && eventType != 'wheel') {
136+
//if we start with a non-move event no, we intersect and commit
137+
//this allows enter, down, ... events to be forwarded to the scene even when they dont come with a move event
138+
innerPointer.setIntersection(innerPointer.computeIntersection(scene, event))
139+
innerPointer.commit(event, false)
140+
}
141+
pointerMap.set(event.pointerId, innerPointer)
138142
return innerPointer
139143
}
140144

@@ -175,7 +179,7 @@ function forwardEvents(
175179
}
176180

177181
const onEvent = (type: InternalEventType, event: ForwardablePointerEvent) => {
178-
const pointer = getInnerPointer(event)
182+
const pointer = getInnerPointer(event, type)
179183
if (type === 'move') {
180184
latestMoveEventMap.set(pointer, event)
181185
}
@@ -218,7 +222,7 @@ function forwardEvents(
218222
const length = eventList.length
219223
for (let i = 0; i < length; i++) {
220224
const { type, event } = eventList[i]
221-
const pointer = getInnerPointer(event)
225+
const pointer = getInnerPointer(event, type)
222226
if (type === 'move') {
223227
movedPointerList.push(pointer)
224228
if (latestMoveEventMap.get(pointer) != event) {

packages/pointer-events/src/pointer.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export class Pointer {
179179
}
180180
}
181181

182-
private computeIntersection(scene: Object3D, nativeEvent: NativeEvent) {
182+
computeIntersection(scene: Object3D, nativeEvent: NativeEvent) {
183183
if (this.pointerCapture != null) {
184184
return this.intersector.intersectPointerCapture(this.pointerCapture, nativeEvent)
185185
}
@@ -192,16 +192,7 @@ export class Pointer {
192192
this.intersection = intersection
193193
}
194194

195-
/**
196-
* allows to separately compute and afterwards commit a move
197-
* => do not forget to call commit after computeMove
198-
* can be used to compute the current intersection and disable or enable the pointer before commiting the move
199-
*/
200-
computeMove(scene: Object3D, nativeEvent: NativeEvent) {
201-
this.intersection = this.computeIntersection(scene, nativeEvent)
202-
}
203-
204-
commit(nativeEvent: NativeEvent) {
195+
commit(nativeEvent: NativeEvent, emitMove: boolean = true) {
205196
const camera = this.getCamera()
206197
const prevIntersection = this.prevEnabled ? this.prevIntersection : undefined
207198
const intersection = this.enabled ? this.intersection : undefined
@@ -235,7 +226,7 @@ export class Pointer {
235226
}
236227

237228
//pointer move
238-
if (intersection != null) {
229+
if (emitMove && intersection != null) {
239230
emitPointerEvent(new PointerEvent('pointermove', true, nativeEvent, this, intersection, camera))
240231
}
241232

@@ -258,7 +249,7 @@ export class Pointer {
258249
* computes and commits a move
259250
*/
260251
move(scene: Object3D, nativeEvent: NativeEvent): void {
261-
this.computeMove(scene, nativeEvent)
252+
this.intersection = this.computeIntersection(scene, nativeEvent)
262253
this.commit(nativeEvent)
263254
}
264255

0 commit comments

Comments
 (0)