Coder Social home page Coder Social logo

lucasleandro1204 / pointer-lock-movement Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zheeeng/pointer-lock-movement

0.0 0.0 0.0 934 KB

A pointer lock movement manager for customizing your own creative UI.

Home Page: http://pointer-lock-movement.zheeeng.me/

License: MIT License

JavaScript 3.73% TypeScript 95.44% HTML 0.83%

pointer-lock-movement's Introduction

Pointer Lock Movement

NPM

publish workflow pages workflow npm version

A pointer lock movement manager for customizing your own creative UI. Inspired by Figma's number input element: Dragging on an input label and moves a virtual cursor continuously in an infinite looping area and slides the input's figure value.

pointer-lock-movement

This tool toggles the pointer's lock state when user is interacting with a specific HTML element. Its registered callback is triggered when a mouse/trackPad/other pointing device delivers PointerEvent under the pointer-locked state. You can configure its behaviors as you like.

๐Ÿงฉ Installation

yarn add pointer-lock-movement (or npm/pnpm)

๐Ÿ‘‡ Usage

import { isSupportPointerLock, pointerLockMovement } from 'pointer-lock-movement'

if (isSupportPointerLock()) {
    const cleanup = pointerLockMovement(TOGGLE_ELEMENT, OPTIONS);

    REQUEST_TO_DISPOSE_THE_LISTENED_EVENTS_CALLBACK(() => {
      cleanup()
    })
}

๐Ÿ“Ž Example

Enhance your input-number component:

const [value, setValue] = useState(0);

const pointerLockerRef = useRef<HTMLDivElement>(null)

useEffect(
  () => {
    if (!pointerLockerRef.current) {
      return
    }

    return pointerLockMovement(
      pointerLockerRef.current,
      {
          onMove: evt => setValue(val => val + evt.movementX),
          cursor: 'โŸบ',
      }
    )
  },
  [],
)

return (
  <label>
    <div ref={resizeElRef}>โŸบ</div>
    <input value={value} onChange={e => setValue(e.currentTarget.value)} />
  </label>
)

See more examples:

  1. Input Number
  2. Magnifying Glass

๐Ÿ‘‡ API

Name signature description
isSupportPointerLock () => boolean predicates pointer lock is supported
pointerLockMovement (element: Element, option?: PointerLockMovementOption) => () => void stars the pointer lock managing for a specific element and returns cleanup function

๐Ÿ“ Type Definition

type MoveState = {
    status: 'moving' | 'stopped',
    movementX: number,
    movementY: number,
    offsetX: number,
    offsetY: number,
}

type PointerLockMovementOption = {
    onLock?: (locked: boolean) => void,
    onMove?: (event: PointerEvent, moveState: MoveState) => void,
    cursor?: string | HTMLElement | Partial<CSSStyleDeclaration>,
    screen?: DOMRect | HTMLElement | Partial<CSSStyleDeclaration>,
    zIndex?: number,
    loopBehavior?: 'loop' | 'stop' | 'infinite',
    trigger?: 'drag' | 'toggle',
}
  • onLock registers callback to listen locking state changing
  • onMove registers callback to listen pointer movement, it carries the corresponding event and the moving state. If the loopBehavior is configured to stop and the virtual cursor reached the edge of the screen, the moveState.status will be read as stopped.
  • cursor is used as the virtual cursor. By default, the cursor is an empty DIV element:
    • if it is a string, it will be used as the cursor's text content,
    • if it is an HTMLElement, it will be used as the virtual cursor,
    • if it is an object with a snake-case property names, it will be applied as the cursor's CSS style.
  • screen is used as the virtual screen, it usually defines the edges of the virtual cursor. By default, we count the edges of the browser's viewport.
    • if it is a DOMRect, it will be assumed as the size and position information of the virtual screen,
    • if it is an HTMLElement, it will be rendered into the DOM structure,
    • if it is an object with a snake-case property name, it will be regarded as the CSS style and render a virtual screen element with this style.
  • zIndex is used as the z-index CSS property of the virtual cursor/screen with the default value 99999, it is useful when there are other elements over it.
  • loopBehavior is used to control the behavior of the virtual cursor when it reaches the edge of the screen. By default, it is loop.
    • loop: the virtual cursor will be moved to the other side of the screen
    • stop: the virtual cursor will be stopped at the edge of the screen
    • infinite: the virtual cursor will be moved out of the screen
  • trigger is used to control the triggering way of the virtual cursor. By default, it is drag.
    • drag: the virtual cursor movement will be toggled by pointer-down and pointer-up events.
    • toggle: the virtual cursor movement will be toggled by pointer events.

pointer-lock-movement's People

Contributors

zheeeng avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.