Socket
Socket
Sign inDemoInstall

@hscmap/inertial-mouse

Package Overview
Dependencies
0
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @hscmap/inertial-mouse

Inertial Mouse for TypeScript * Wraps native MouseEvent and emit pseudo mousemove events with inertial smooth motion. * [Working Demo](https://michitaro.github.io/inertial-mouse)


Version published
Maintainers
1
Created

Readme

Source

Inertial Mouse

Inertial Mouse for TypeScript

  • Wraps native MouseEvent and emit pseudo mousemove events with inertial smooth motion.
  • Working Demo

Install

npm instasll --save @hscmap/inertial-mouse

Example (source code of the working demo)

import { InertialMouse, V2 } from "@hscmap/inertial-mouse"


window.addEventListener('load', e => {
    const sandbox = document.querySelector('.sandbox') as HTMLElement
    const cursor = document.querySelector('.cursor') as HTMLElement
    const dxText = document.querySelector('[name="dx"]') as HTMLInputElement
    const dyText = document.querySelector('[name="dy"]') as HTMLInputElement

    function setCursorPosition(r: V2) {
        cursor.style.left = `${r.x}px`
        cursor.style.top = `${r.y}px`
    }

    const im = new InertialMouse(sandbox, {
        down: (e) => {
            setCursorPosition(e.r)
            cursor.classList.add('pressed')
        },
        up: () => {
            cursor.classList.remove('pressed')
        },
        move: (e) => {
            setCursorPosition(e.r)
            dxText.value = `${e.d.x}`
            dyText.value = `${e.d.y}`
        },
        movestart: () => cursor.classList.add('moving'),
        moveend: () => cursor.classList.remove('moving'),
    })

    document.addEventListener('keydown', e => im.stop())

    document.addEventListener('change', e => {
        const target = e.target as HTMLInputElement
        if (target.matches('[name="slick"]'))
            im.physics.slick = target.checked
        if (target.matches('[name="friction"]'))
            im.physics.friction = Number(target.value)
        if (target.matches('[name="k"]'))
            im.physics.k = Number(target.value)
    })
})


document.addEventListener('selectstart', e => e.preventDefault()) // disable selection```

FAQs

Last updated on 05 Sep 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc