VelocityTracker

Allows you to have the velocity of particular properties automatically tracked for you so that you can access them anytime using the VelocityTracker's getVelocity() method, likemyTracker.getVelocity("y"). For example, let's say there's an object that the user interacts with by dragging it or maybe it is being tweened and then at some point you want to create a tween based on that velocity. Normally, you'd need to write your own tracking code that records that object's x and y properties (as well as time stamps) so that when it comes time to feed thevelocity into whatever other code you need to run, you'd have the necessary data to calculate it. But let's face it: that can be cumbersome to do manually, and that's precisely why VelocityTracker exists.

VelocityTracker is in the ThrowPropsPlugin JavaScript file and it's a membership benefit of Club GreenSock (so it's not in the public downloads). You can access the important methods directly through ThrowPropsPlugin, like ThrowPropsPlugin.track().

Use the static VelocityTracker.track() method to start tracking properties. You generally should not use the VelocityTracker's constructor because there needs to only be one VelocityTracker instance associated with any particular object. The track() method will return the instance that you can then use to getVelocity() like:

//first, start tracking "x" and "y":
var tracker = VelocityTracker.track(obj, "x,y");

//then, after at least 100ms and 2 "ticks", we can get the velocity of each property:
var vx = tracker.getVelocity("x");
var vy = tracker.getVelocity("y");

What kinds of properties can be tracked?

Pretty much any numeric property of any object can be tracked, including function-based ones. For example, obj.x or obj.rotation or even obj.myCustomProp(). In fact, for getters and setters that start with the word "get" or "set" (like getCustomProp() and setCustomProp()), it will automatically find the matching counterpart method and use the getter appropriately, so you can track the getter or setter and it'll work. You cannot, however, track custom plugin-related values like "directionalRotation" or "autoAlpha" or "physics2D" because those aren't real properties of the object. You should instead track the real properties that those plugins affect, like "rotation" or "alpha" or "x" or "y".

This class is used in ThrowPropsPlugin to make it easy to create velocity-based tweens that smoothly transition an object's movement (or rotation or whatever) and glide to a stop.

Note: In order to report accurately, at least 100ms and 2 ticks of the core tweening engine must have been elapsed before you check velocity.

Properties

target : Object

Returns the target object with which the VelocityTracker is associated.

Methods

addProp( prop:String, type:String ) :

Adds a property to track

getByTarget( target:Object ) : VelocityTracker

[static] Returns the VelocityTracker associated with a particular object.

getVelocity( prop:String ) : Number

Returns the current velocity of the given property.

isTracking( target:Object, prop:String ) : Boolean

[static] Allows you to discern whether the velocity of a particular target or one of its properties is being tracked (typically initiated using the VelocityTracker.track() method).

isTrackingProp( prop:String ) : Boolean

Allows you to discern whether the velocity of a particular property is being tracked.

removeProp( prop:String ) :

Stops tracking a particular property

track( target:Object, props:String, type:String ) : VelocityTracker

[static] Allows you to have the velocity of particular properties automatically tracked for you so that you can access them anytime using the VelocityTracker's getVelocity() method, like myTracker.getVelocity("y").

untrack( target:Object, props:String ) :

[static] Stops tracking the velocity of certain properties (or all properties of an object), like ones initiated with the track() method.

Copyright 2017, GreenSock. All rights reserved. This work is subject to theterms of useor for Club GreenSock members, the software agreement that was issued with the membership.

Get GSAP

Version: 2.0.2 updated 2018-08-27

Core

    Extras

      Plugins

        By using GreenSock code, you agree to the terms of use.

        For an all-access pass to premium content

        Join Club GreenSock