Skip to content

pqml/spring

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

〰️ Spring

📚 Documentation | 🎉 Example | 🌐 Internet modules


Requirements


API


new Spring([options])

Create a new Spring instance.

Param Type Default Description
[options] Object {} Spring options.
[options.initial] number 0 Initial value / targetValue of your spring.
[options.tension] number 0.1 Tension/Stifness of your spring.
[options.friction] number 0.2 From 0 to 1. Friction (Damping) of your spring.
[options.step] number 10 Timestep of the physics solver (in ms). Step > 16.67ms will give you pretty bad results.
[options.onStart] function onStart will be called when the spring starts moving.
[options.onStop] function onStop will be called when your the spring stops moving.
[options.precisionStop] number 0.0001 Minimum distance between value and target to consider the spring stopped.
[options.perfectStop] boolean false Define 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

Param Type Description
newCurrent number New current value.

spring.setTarget(newTarget)

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

Kind: instance method of Spring
Category: Methods

Param Type Description
newTarget number New target value.

spring.setTension(tensionValue)

Update tension of the spring

Kind: instance method of Spring
Category: Methods

Param Type Description
tensionValue number New tension value.

spring.setFriction(frictionValue)

Update friction of the spring

Kind: instance method of Spring
Category: Methods

Param Type Description
frictionValue number New 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

Param Type Description
dt number Elapsed 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

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published