Is it possible to get the position of the carets absolute screen space position? My intention is to hide the caret and replace it with a custom component that may 'fly' around the screen.
Asked
Active
Viewed 377 times
0
-
Do you mean the cursor? – user0101 Jun 30 '20 at 12:19
-
@user0101 probably a better way to describe it hehe – meds Jun 30 '20 at 12:22
-
How about https://stackoverflow.com/questions/7790725/javascript-track-mouse-position? – user0101 Jun 30 '20 at 12:24
-
Does this answer your question? [Javascript - Track mouse position](https://stackoverflow.com/questions/7790725/javascript-track-mouse-position) – user0101 Jun 30 '20 at 12:45
1 Answers
2
You can get caret position using the Selection API, getting the ranges (collapsed==true
when just the caret is showing), and their client rects.
document.addEventListener('selectionchange', () => {
console.log(getSelection()?.getRangeAt(0)?.getClientRects()?.[0])
)
These client rects are in screen space, so you probably also want to track document scroll and get the rects again.
{x: 286.796875, y: 209, width: 0, height: 19, top: 209, …}

Jason Harwig
- 43,743
- 5
- 43
- 44