Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v9] feat: Add click threshold to events #3034

Open
wants to merge 1 commit into
base: v9
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/fiber/src/core/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ export interface EventManager<TTarget> {
* explicit user interaction, for instance when the camera moves a hoverable object underneath the cursor.
*/
update?: () => void
/** Threshold for the amont the pointer can move before a click is canceled. */
clickThreshold: number
}

export interface PointerCaptureTarget {
Expand Down Expand Up @@ -399,7 +401,7 @@ export function createEvents(store: RootStore) {

// Any other pointer goes here ...
return function handleEvent(event: DomEvent) {
const { onPointerMissed, internal } = store.getState()
const { onPointerMissed, internal, events } = store.getState()

// prepareRay(event)
internal.lastEvent.current = event
Expand All @@ -412,6 +414,9 @@ export function createEvents(store: RootStore) {
const hits = intersect(event, filter)
const delta = isClickEvent ? calculateDistance(event) : 0

// If the delta is greater than our threshold, cancel the click
if (isClickEvent && delta > events.clickThreshold) return

// Save initial coordinates on pointer-down
if (name === 'onPointerDown') {
internal.initialClick = [event.offsetX, event.offsetY]
Expand Down
2 changes: 1 addition & 1 deletion packages/fiber/src/core/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export const createStore = (
gl: null as unknown as THREE.WebGLRenderer,
camera: null as unknown as Camera,
raycaster: null as unknown as THREE.Raycaster,
events: { priority: 1, enabled: true, connected: false },
events: { priority: 1, enabled: true, connected: false, clickThreshold: 7 },
scene: null as unknown as THREE.Scene,
xr: null as unknown as XRManager,

Expand Down
1 change: 1 addition & 0 deletions packages/fiber/src/native/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ export function createTouchEvents(store: RootStore): EventManager<HTMLElement> {

set((state) => ({ events: { ...state.events, connected: false } }))
},
clickThreshold: 7,
}
}
1 change: 1 addition & 0 deletions packages/fiber/src/web/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ export function createPointerEvents(store: RootStore): EventManager<HTMLElement>
set((state) => ({ events: { ...state.events, connected: undefined } }))
}
},
clickThreshold: 7,
}
}