Skip to content

usePointer

Category
Export Size
973 B
Last Changed
last month

Reactive pointer state.

Demo

{
  "x": 0,
  "y": 0,
  "pointerId": 0,
  "pressure": 0,
  "tiltX": 0,
  "tiltY": 0,
  "width": 0,
  "height": 0,
  "twist": 0,
  "pointerType": null,
  "isInside": false
}

Basic Usage

ts
import { 
usePointer
} from '@vueuse/core'
const {
x
,
y
,
pressure
,
pointerType
} =
usePointer
()

Component Usage

This function also provides a renderless component version via the @vueuse/components package. Learn more about the usage.

By default, the component will track the pointer on window

vue
<template>
  <UsePointer v-slot="{ 
x
,
y
}">
x: {{
x
}}
y: {{
y
}}
</UsePointer> </template>

To track local position in the element, set target="self":

vue
<template>
  <UsePointer v-slot="{ 
x
,
y
}"
target
="self">
x: {{
x
}} y: {{
y
}}
</UsePointer> </template>

Type Declarations

Show Type Declarations
ts
export interface UsePointerState extends Position {
  
pressure
: number
pointerId
: number
tiltX
: number
tiltY
: number
width
: number
height
: number
twist
: number
pointerType
:
PointerType
| null
} export interface UsePointerOptions extends ConfigurableWindow { /** * Pointer types that listen to. * * @default ['mouse', 'touch', 'pen'] */
pointerTypes
?:
PointerType
[]
/** * Initial values */
initialValue
?:
MaybeRef
<
Partial
<UsePointerState>>
/** * @default window */
target
?:
MaybeRef
<EventTarget | null | undefined> | Document | Window
} export interface UsePointerReturn {
pressure
:
Ref
<number>
pointerId
:
Ref
<number>
tiltX
:
Ref
<number>
tiltY
:
Ref
<number>
width
:
Ref
<number>
height
:
Ref
<number>
twist
:
Ref
<number>
pointerType
:
Ref
<
PointerType
| null>
x
:
Ref
<number>
y
:
Ref
<number>
isInside
:
ShallowRef
<boolean>
} /** * Reactive pointer state. * * @see https://vueuse.org/usePointer * @param options */ export declare function
usePointer
(
options
?: UsePointerOptions,
): UsePointerReturn

Source

SourceDemoDocs

Contributors

Anthony Fu
Vida Xie
IlyaL
broBinChen
SerKo
IlyaL
Anthony Fu
cunzaizhuyi
丶远方
vaakian X
Jelf
zz

Changelog

8c521 - feat(components)!: refactor components and make them consistent (#4912)
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
ee425 - fix: add pointerup event for pressure field (#3217)

Released under the MIT License.