curve-store
A store for dealing with continuous values. Useful for complex animations. Continue reading for background and example usage. Click here for a demo.
Motivation
The idea for this module came from discussions with @mikolalysenko. Credit largely goes to him. See filtered-vector for prior art.
Curve store is a state container that intelligently deals with mapping discrete values to a continuous curve. Its primary intended use case is for dealing with complex animations over time, though there may be other applications.
The idea is that animation can be defined as a series of positions over time:
given an object at position x, y
at time t
, we should be able to define an
animation by promising that the object will be at some other position x', y'
at
some future time t'
, and infer the points along the path.
This is what curve-store
gives you, a way to define state at a given time:
store;store;
and a way to sample points in between:
store;// give { x: xval, y: yval } interpolated based on the points set above
Users can define how they want the interpolation to be handled. There are a few built in helpers, for example:
;; const store =
defines basic linear interpolation. There are also calculus functions to help build out more complicated curves:
;; const store = ;
You can also provide custom sampling functions, to get e.g. different easing curves (see below for more details).
Installation
$ npm install --save curve-store
Simple Example
;; const store = ; store;store; store;// --> { x: 0.25, dx: 1.0 }
API
createStore(samplers)
Creates a new curve-store
that maps discrete input values onto a set
of continuous output values. The samplers object defines this mapping and defines
how to interpolate between points.
Basic usage:
const store = ;
store.set(time, values)
Set values at a particular point in time.
Example:
store;store;
store.sample(time)
Sample points at a particular time.
Example:
store;// -> outputs { outputX: 0.5 }
The way that sampling occurs is defined based on the samplers object passed
to createStore
.
Custom sampling
;; const store = ; store;store; store;// { myKey: customVal }
Clearing values
// Empties the store.storeclear;
// Removes all values before time tstore;
// Removes all values after time tstores;
LICENSE
MIT