Skip to content

Instantly share code, notes, and snippets.

@andigu
Last active February 26, 2023 18:03
Show Gist options
  • Save andigu/8356fad72db483e288722a745e8250aa to your computer and use it in GitHub Desktop.
Save andigu/8356fad72db483e288722a745e8250aa to your computer and use it in GitHub Desktop.
export const diffClamp = function(
a: Animated.Value<number>,
min: number,
max: number,
): AnimatedDiffClamp {
return new AnimatedDiffClamp(a, min, max);
};
class AnimatedDiffClamp {
constructor(a: Animated.Value<number>, min: number, max: number) {
this.a = a;
this.emitValue = this.lastInputVal = this.a.__getValue();
this.min = min;
this.max = max;
}
getValue(): number {
const currentInputVal = this.a.__getValue();
const diff = currentInputVal - this.lastInputVal;
this.lastInputVal = currentInputVal;
this.emitValue = Math.min(Math.max(this.emitValue + diff, this.min), this.max);
return this.emitValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment