0.0.3 • Published 7 years ago

@internet/spring v0.0.3

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

:wavy_dash: Spring

:books: Documentation | :tada: Example | :globe_with_meridians: Internet modules

Requirements

API

new Spring(options)

Create a new Spring instance.

ParamTypeDefaultDescription
optionsObject{}Spring options.
options.initialnumber0Initial value / targetValue of your spring.
options.tensionnumber0.1Tension/Stifness of your spring.
options.frictionnumber0.2From 0 to 1. Friction (Damping) of your spring.
options.stepnumber10Timestep of the physics solver (in ms). Step > 16.67ms will give you pretty bad results.
options.onStartfunctiononStart will be called when the spring starts moving.
options.onStopfunctiononStop will be called when your the spring stops moving.
options.precisionStopnumber0.0001Minimum distance between value and target to consider the spring stopped.
options.perfectStopbooleanfalseDefine if value is set precisely to targetValue when the spring stops moving.

Example

import { raf } from '@internet/raf'
import Spring from '@internet/spring'

const move = new Spring({ initial: 0 })
move.setTarget(300)
raf.add(dt => {
  move.update(dt)
  console.log(move.value)
})

spring.setValue(newCurrent)

Change the current position of the spring. Can retrigger onStart / onStop.

Kind: instance method of Spring
Category: Methods

ParamTypeDescription
newCurrentnumberNew current value.

spring.setTarget(newTarget)

Update target / resting position of the spring. Can retrigger onStart / onStop.

Kind: instance method of Spring
Category: Methods

ParamTypeDescription
newTargetnumberNew target value.

spring.setTension(tensionValue)

Update tension of the spring

Kind: instance method of Spring
Category: Methods

ParamTypeDescription
tensionValuenumberNew tension value.

spring.setFriction(frictionValue)

Update friction of the spring

Kind: instance method of Spring
Category: Methods

ParamTypeDescription
frictionValuenumberNew friction value.

spring.start()

Force re-start of the spring. Only needed if you force-stop the spring with stop()

Kind: instance method of Spring
Category: Methods


spring.stop()

Force-stop the spring.

Kind: instance method of Spring
Category: Methods


spring.update(dt)

Update the spring physic state

Kind: instance method of Spring
Category: Methods

ParamTypeDescription
dtnumberElapsed time since the last frame (in ms)

spring.dispose()

Stop the spring and remove callbacks referenced in onStart and onStop.

Kind: instance method of Spring
Category: Methods


spring.initial : number

Initial position of the spring

Kind: instance property of Spring
Category: Properties


spring.value : number

Current position of the spring

Kind: instance property of Spring
Category: Properties


spring.previous : number

Previous frame position of the spring

Kind: instance property of Spring
Category: Properties


spring.velocity : number

Current velocity of the spring

Kind: instance property of Spring
Category: Properties


spring.onStart : function

Optional function called when the spring starts

Kind: instance property of Spring
Category: Properties


spring.onStop : function

Optional function called when the spring stops

Kind: instance property of Spring
Category: Properties