« get me outta code hell

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
diff options
context:
space:
mode:
Diffstat (limited to 'util/smoothen.js')
-rw-r--r--util/smoothen.js16
1 files changed, 16 insertions, 0 deletions
diff --git a/util/smoothen.js b/util/smoothen.js
new file mode 100644
index 0000000..55ba23c
--- /dev/null
+++ b/util/smoothen.js
@@ -0,0 +1,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)
+  }
+}