-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(arcs): move arc hover detection from core to arcs package
- Loading branch information
Showing
5 changed files
with
47 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { getDistance, getAngle } from '@nivo/core' | ||
import { Arc } from './types' | ||
|
||
/** | ||
* Check if cursor is in given ring. | ||
*/ | ||
export const isCursorInRing = ( | ||
centerX: number, | ||
centerY: number, | ||
radius: number, | ||
innerRadius: number, | ||
cursorX: number, | ||
cursorY: number | ||
) => { | ||
const distance = getDistance(cursorX, cursorY, centerX, centerY) | ||
|
||
return distance < radius && distance > innerRadius | ||
} | ||
|
||
/** | ||
* Search for an arc being under cursor. | ||
*/ | ||
export const findArcUnderCursor = <A extends Arc = Arc>( | ||
centerX: number, | ||
centerY: number, | ||
radius: number, | ||
innerRadius: number, | ||
arcs: A[], | ||
cursorX: number, | ||
cursorY: number | ||
): A | undefined => { | ||
if (!isCursorInRing(centerX, centerY, radius, innerRadius, cursorX, cursorY)) { | ||
return undefined | ||
} | ||
|
||
const cursorAngle = getAngle(cursorX, cursorY, centerX, centerY) | ||
|
||
return arcs.find( | ||
({ startAngle, endAngle }) => cursorAngle >= startAngle && cursorAngle < endAngle | ||
) | ||
} |
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 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