Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including touch-velocity with all npm packages installed. Try it out:

var touchVelocity = require("touch-velocity")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

touch-velocity v0.0.4

Calculates velocity

#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

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free