Skip to content

Instantly share code, notes, and snippets.

@Jberivera
Created February 12, 2019 21:52
Show Gist options
  • Save Jberivera/64e95490f97ffeede6b9cb98dd4ddf87 to your computer and use it in GitHub Desktop.
Save Jberivera/64e95490f97ffeede6b9cb98dd4ddf87 to your computer and use it in GitHub Desktop.
class DiffClamp {
constructor(min, max) {
this.min = min;
this.max = max;
this.lastValue = 0;
this.value = 0;
}
getValue(value) {
const diff = value - this.lastValue;
this.lastValue = value;
this.value = Math.min(Math.max(this.value + diff, this.min), this.max);
return this.value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment