touch-velocity

0.0.4 • Public • Published

touch-velocity

Calculate (nearly) instantaneous velocity for a thing. Things like touch or mouse events. Calculating the velocity by looking at the last two events is wildly inaccurate, since the time difference between the two events being fired is often < 1ms.

This library smooths that out a bit by calculating over the last 100ms.

example

  var Velocity = require('touch-velocity')
    , velocity = new Velocity
    , touchTarget = document.querySelector('#touch-target')
  
  touchTarget.addEventListener('touchmove', function(evt) {
    velocity.updatePosition(evt.touches[0].pageX)
  })
 
  touchTarget.addEventListener('touchend', function(evt) {
    console.log(velocity.getVelocity())
  })

api

updatePosition(newPosition)

Updates the current position of the thing whose velocity you are tracking. You should do this any time you know that the thing's position has changed i.e. a touch event.

getVelocity()

Gets the (nearly) instantaneous velocity of the thing you're tracking.

TODO

  • Work with 2d, accept both x and y positions.
  • Accept event objects

Dependencies (0)

    Dev Dependencies (3)

    Package Sidebar

    Install

    npm i touch-velocity

    Weekly Downloads

    2

    Version

    0.0.4

    License

    MIT

    Unpacked Size

    3.56 kB

    Total Files

    8

    Last publish

    Collaborators

    • coderzach