« get me outta code hell

smoothen.js « util - tui-lib - Pure Node.js library for making visual command-line programs (ala vim, ncdu)
about summary refs log tree commit diff
path: root/util/smoothen.js
blob: 55ba23c486b87505e4a698dc964424847f5b773c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
module.exports = function(tx, x, divisor) {
  // Smoothly transitions givens X to TX using a given divisor. Rounds the
  // amount moved.

  const move = (tx - x) / divisor

  if (move > 0.5) {
    return x + Math.ceil(move)
  } else if (move < -0.5) {
    return x + Math.floor(move)
  } else if (tx > 0) {
    return Math.ceil(tx)
  } else {
    return Math.floor(tx)
  }
}