Skip to content

Repository files navigation

〰️ Spring

📚 Documentation | 🎉 Example | 🌐 Internet modules


Requirements


API


new Spring([options])

Create a new Spring instance.

ParamTypeDefaultDescription
[options]Object{}Spring options.
[options.initial]number0Initial value / targetValue of your spring.
[options.tension]number0.1Tension/Stifness of your spring.
[options.friction]number0.2From 0 to 1. Friction (Damping) of your spring.
[options.step]number10Timestep of the physics solver (in ms). Step > 16.67ms will give you pretty bad results.
[options.onStart]functiononStart will be called when the spring starts moving.
[options.onStop]functiononStop will be called when your the spring stops moving.
[options.precisionStop]number0.0001Minimum distance between value and target to consider the spring stopped.
[options.perfectStop]booleanfalseDefine if value is set precisely to targetValue when the spring stops moving.

Example

import{raf}from'@internet/raf'importSpringfrom'@internet/spring'constmove=newSpring({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


About

〰️ Framerate-independant spring physics

Resources

Stars

25 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages