You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 1 година
123456789101112131415161718
  1. export function clamp(v, min, max) {
  2. return Math.max(min, Math.min(v, max))
  3. }
  4. export function rubberband(distance, dimension, constant) {
  5. if (dimension === 0 || Math.abs(dimension) === Infinity) return Math.pow(distance, constant * 5)
  6. return (distance * dimension * constant) / (dimension + constant * distance)
  7. }
  8. /**
  9. * Calculates the rubberbanding effect from a given `position` value, two bounds `min`, `max` and an elasticity `constant`.
  10. */
  11. export function rubberbandIfOutOfBounds(position, min, max, constant = 0.15) {
  12. if (constant === 0) return clamp(position, min, max)
  13. if (position < min) return -rubberband(min - position, max - min, constant) + min
  14. if (position > max) return +rubberband(position - max, max - min, constant) + max
  15. return position
  16. }