-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: change onPointerEvents to usePointerEvents and add more events
- Loading branch information
Showing
12 changed files
with
109 additions
and
63 deletions.
There are no files selected for viewing
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
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
This file was deleted.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import type { BoundingRect } from "./useBoundingRect"; | ||
import { useBoundingRect } from "./useBoundingRect"; | ||
|
||
type HandlerArgs = { | ||
pointer: { | ||
x: number; | ||
y: number; | ||
}; | ||
canvasRect: BoundingRect; | ||
canvasCenter: { | ||
x: number; | ||
y: number; | ||
}; | ||
}; | ||
|
||
type PointerEventsHandlers = { | ||
enter?: ({ pointer, canvasRect, canvasCenter }: HandlerArgs) => void; | ||
move?: ({ pointer, canvasRect, canvasCenter }: HandlerArgs) => void; | ||
leave?: ({ pointer, canvasRect, canvasCenter }: HandlerArgs) => void; | ||
down?: ({ pointer, canvasRect, canvasCenter }: HandlerArgs) => void; | ||
up?: ({ pointer, canvasRect, canvasCenter }: HandlerArgs) => void; | ||
}; | ||
|
||
/** | ||
* Listen to common pointer events and provide additional infos about the canvas | ||
*/ | ||
export function usePointerEvents(canvas: HTMLCanvasElement, handlers: PointerEventsHandlers) { | ||
const { rect: canvasRect, center: canvasCenter } = useBoundingRect(canvas); | ||
|
||
const activeHandlers = Object.fromEntries( | ||
Object.entries(handlers) | ||
.filter(([, handler]) => typeof handler === "function") | ||
.map(([handlerName, handlerFunction]) => [ | ||
handlerName, | ||
(e: PointerEvent) => { | ||
handlerFunction({ | ||
pointer: { x: e.clientX, y: e.clientY }, | ||
canvasRect, | ||
canvasCenter, | ||
}); | ||
}, | ||
]), | ||
); | ||
|
||
function listen() { | ||
for (const [event, handler] of Object.entries(activeHandlers)) { | ||
canvas.addEventListener(`pointer${event as keyof PointerEventsHandlers}`, handler, { | ||
passive: true, | ||
}); | ||
} | ||
} | ||
|
||
function stop() { | ||
for (const [event, handler] of Object.entries(activeHandlers)) { | ||
canvas.removeEventListener(`pointer${event as keyof PointerEventsHandlers}`, handler); | ||
} | ||
} | ||
|
||
listen(); | ||
|
||
return { stop, listen }; | ||
} |
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
4 changes: 2 additions & 2 deletions
4
lib/tests/__screenshots__/pointer-move/pointer-move-android.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
lib/tests/__screenshots__/pointer-move/pointer-move-chromium.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
lib/tests/__screenshots__/pointer-move/pointer-move-firefox.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
lib/tests/__screenshots__/pointer-move/pointer-move-iphone.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
lib/tests/__screenshots__/pointer-move/pointer-move-safari.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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