touch-scroll-physics
v1.3.0
Published
integrates touch scrolling physics
Downloads
43
Readme
touch-scroll-physics
Physics for a touch-based scroll panel. Assumes a single dimension but could easily be extended to two or more.
Works best with scroll panes that have a fixed bounding region and need to "bounce" on either end. Can also "dip" into the cell edges (for example: carousels, list views, grids).
Makes no assumptions about the method of input or render engine (Canvas/DOM/WebGL/etc) but works well alongside touches. Simple example:
var scroller = require('touch-scroll-physics')({
cellSize: 100, //size of the content
viewSize: 400 //size of the viewport / clip region
})
//tick the scroller
function render(dt) {
scroller.update(dt)
}
//and hook up mouse to scroller start(x) / move(x) / end(x)
See test.js for a more complete example with cells.
Demo
Run the demo here or build it yourself like so:
git clone https://github.com/Jam3/touch-scroll-physics.git
cd touch-scroll-physics
npm install
npm start
Then open localhost:9966
. You can enter npm run build
to build the distribution bundle.
Usage
scroller = ScrollPhysics([opt])
Creates a new scroller with the given options.
cellSize
the size of each cell (default zero)viewSize
the size of your viewport (default zero)totalCells
total number of cells (default 1)gutterSize
the size of the gutter (default 1/4 of viewSize)dipToClosestCell
whether to smoothly snap to the edge of each cell (default false)
In the case of arbitrary scrolling (no "pages") you will typically just use a totalCells
of 1 with the content size for cellSize
.
scroller.start(value)
Called to initiate a scroll event with the given value (e.g. X mouse position).
scroller.move(value)
Called to signal a move event (e.g. mouse move X).
scroller.end(value)
Called to end the scroll state at the given position (e.g. mouse up at X).
scroller.value
The value of the scroller. Typically when rendering you would translate content by the negative of this.
scroller.momentum
The current momentum for the scroller. May be useful for triggering events on swipe.
License
MIT, see LICENSE.md for details.