Skip to content

usePointer

Category
Export Size
1.14 kB
Last Changed
15 minutes ago

响应式的 指针状态

Demo

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

基本用法

js
import { usePointer } from '@vueuse/core'

const { x, y, pressure, pointerType } = usePointer()

组件用法

默认情况下,该组件将跟踪 window 上的指针。

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

要在元素内跟踪本地位置,请设置 target="self"

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

类型声明

显示类型声明
typescript
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 {
  /**
   * 监听的指针类型。
   *
   * @default ['mouse', 'touch', 'pen']
   */
  pointerTypes?: PointerType[]
  /**
   * 初始值
   */
  initialValue?: MaybeRef<Partial<UsePointerState>>
  /**
   * @default window
   */
  target?: MaybeRef<EventTarget | null | undefined> | Document | Window
}
/**
 * 响应式指针状态
 *
 * @see https://vueuse.org/usePointer
 * @param options
 */
export declare function usePointer(options?: UsePointerOptions): {
  isInside: Ref<boolean, boolean>
  pressure: Ref<number, number>
  pointerId: Ref<number, number>
  tiltX: Ref<number, number>
  tiltY: Ref<number, number>
  width: Ref<number, number>
  height: Ref<number, number>
  twist: Ref<number, number>
  pointerType: Ref<PointerType | null, PointerType | null>
  x: Ref<number, number>
  y: Ref<number, number>
}
export type UsePointerReturn = ReturnType<typeof usePointer>

Source

SourceDemoDocs

贡献者

Anthony Fu
丶远方
Anthony Fu
cunzaizhuyi
vaakian X
Jelf
zz

变更日志

v10.3.0 on 7/30/2023
ee425 - fix: add pointerup event for pressure field (#3217)
v9.11.0 on 1/17/2023
d5321 - fix(components): mark defineComponent as pure (#2623)

Released under the MIT License.