Guest User

Untitled

a guest
Jul 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. function spring(initial, mass, stiffness, damping) {
  2. let _mass = mass
  3. let _stiffness = stiffness
  4. let _damping = damping
  5. let _time = 0
  6. let _acceleration = 0
  7. let _velocity = 0
  8. let _target = 0
  9. let _value = 0
  10.  
  11. return {
  12. set target(t) {
  13. _target = t
  14. },
  15.  
  16. get value() {
  17. return _value
  18. },
  19.  
  20. update() {
  21. let now = Date.now()
  22. let t = now - _time
  23.  
  24. _time = now
  25.  
  26. let spring = _stiffness * (_value - _target)
  27. let damper = _damping * _velocity
  28.  
  29. _acceleration = (spring + damper) / _mass
  30. _velocity += _acceleration * (t / 1000)
  31. _value += _velocity * (t / 1000)
  32. }
  33. }
  34. }
Add Comment
Please, Sign In to add comment