diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 6a7c867..31a6bdc 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -11,6 +11,8 @@ jobs: - uses: actions/checkout@v2 - name: 'Install Node' uses: actions/setup-node@v2 + - name: Install dependencies + run: npm install - name: "Pack files" run: npm run prepublish - uses: mikeal/merge-release@master diff --git a/src/computations.ts b/src/computations.ts index e80eb25..f12d7f2 100644 --- a/src/computations.ts +++ b/src/computations.ts @@ -1,6 +1,5 @@ import type { Reference, PositionVector } from './index' -// return current mouse position relative to reference element in listener function getMouseOverVector (event: MouseEvent): PositionVector { const position = getCursorPosition(event, event.currentTarget as HTMLElement) const dimension = getRefSize(event.currentTarget as HTMLElement) @@ -8,8 +7,8 @@ function getMouseOverVector (event: MouseEvent): PositionVector { return { x: (Math.round((position.x / dimension.x) * 100) / 100), y: (Math.round((position.y / dimension.y) * 100) / 100), - gx: event.clientX, // global x position - gy: event.clientY // global y position + gx: event.clientX, + gy: event.clientY } } @@ -20,17 +19,20 @@ function getRefSize (el: Reference) { y: el.innerHeight } } + if (el instanceof Document) { return { x: el.body.clientWidth, y: el.body.clientHeight } } + return { x: el.offsetWidth, y: el.offsetHeight } } + function getCursorPosition (ev: MouseEvent, el: Reference) { if (el instanceof Window || el instanceof Document) { return { @@ -39,6 +41,7 @@ function getCursorPosition (ev: MouseEvent, el: Reference) { } } const rect = el.getBoundingClientRect() + return { x: ((ev.clientX - rect.left)), y: ((ev.clientY - rect.top)) diff --git a/src/index.ts b/src/index.ts index 0d16ca6..4a78635 100644 --- a/src/index.ts +++ b/src/index.ts @@ -91,7 +91,6 @@ class Mouser { } ] - // handle mouse leave for window if (this.reference instanceof Window) { this.eventListeners.push({ element: document, @@ -124,7 +123,6 @@ class Mouser { } private setRestState () { - // memo last global recorded value this.vector = { ...this.vector, ...this.restState } this.dispatchEvents(this.vector) } @@ -135,7 +133,6 @@ class Mouser { }) } - // determine element update based on given rate private shouldUpdate (): boolean { let counter = 0 if (this.refreshRate === 0) return true