Created
December 5, 2018 01:44
-
-
Save thomasJang/b5170c41f3dced26722f824248241b2a to your computer and use it in GitHub Desktop.
mouseEventSubscribe.ts
This file contains hidden or 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
| export interface IMousePosition { | |
| pageX: number; | |
| pageY: number; | |
| clientX: number; | |
| clientY: number; | |
| } | |
| export type MouseEventSubscribeCallbackFn = (mpos: IMousePosition) => void; | |
| const mouseEventSubscribe = ( | |
| callBack: MouseEventSubscribeCallbackFn, | |
| onEnd: () => void, | |
| ) => { | |
| const onMousemoveWindow = (e: MouseEvent) => { | |
| callBack({ | |
| pageX: e.pageX, | |
| pageY: e.pageY, | |
| clientX: e.clientX, | |
| clientY: e.clientY, | |
| }); | |
| }; | |
| window.addEventListener('mousemove', onMousemoveWindow, false); | |
| window.addEventListener('mouseup', () => { | |
| window.removeEventListener('mousemove', onMousemoveWindow); | |
| if (onEnd) { | |
| onEnd(); | |
| } | |
| }); | |
| }; | |
| export default mouseEventSubscribe; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment