Skip to content

Commit

Permalink
Add "back" easings, fix error in elastic easings
Browse files Browse the repository at this point in the history
  • Loading branch information
Rycochet committed Aug 2, 2020
1 parent ad94ba9 commit c093620
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 13 deletions.
1 change: 1 addition & 0 deletions packages/velocity/src/Velocity/easing/_all.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
///<reference path="easings.ts" />
///<reference path="back.ts" />
///<reference path="bezier.ts" />
///<reference path="bounce.ts" />
///<reference path="elastic.ts" />
Expand Down
56 changes: 56 additions & 0 deletions packages/velocity/src/Velocity/easing/back.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
///<reference path="easings.ts" />
/*
* VelocityJS.org (C) 2014-2017 Julian Shapiro.
*
* Licensed under the MIT license. See LICENSE file in the project root for details.
*
* Back easings, based on code from https://github.com/yuichiroharai/easeplus-velocity
*/

namespace VelocityStatic.Easing {
export function registerBackIn(name: string, amount: number) {
registerEasing([name, function(percentComplete: number, startValue: number, endValue: number): number {
if (percentComplete === 0) {
return startValue;
}
if (percentComplete === 1) {
return endValue;
}
return Math.pow(percentComplete, 2) * ((amount + 1) * percentComplete - amount) * (endValue - startValue);
}]);
}

export function registerBackOut(name: string, amount: number) {
registerEasing([name, function(percentComplete: number, startValue: number, endValue: number): number {
if (percentComplete === 0) {
return startValue;
}
if (percentComplete === 1) {
return endValue;
}
return (Math.pow(--percentComplete, 2) * ((amount + 1) * percentComplete + amount) + 1) * (endValue - startValue);
}]);
}

export function registerBackInOut(name: string, amount: number) {
amount *= 1.525;
registerEasing([name, function(percentComplete: number, startValue: number, endValue: number): number {
if (percentComplete === 0) {
return startValue;
}
if (percentComplete === 1) {
return endValue;
}
return ((percentComplete *= 2) < 1
? (Math.pow(percentComplete, 2) * ((amount + 1) * percentComplete - amount))
: (Math.pow(percentComplete -= 2, 2) * ((amount + 1) * percentComplete + amount) + 2)
) * 0.5 * (endValue - startValue);
}]);
}

registerBackIn("easeInBack", 1.7);
registerBackOut("easeOutBack", 1.7);
registerBackInOut("easeInOutBack", 1.7);

// TODO: Expose these as actions to register custom easings?
};
5 changes: 4 additions & 1 deletion packages/velocity/src/Velocity/easing/bounce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ namespace VelocityStatic.Easing {
if (percentComplete === 1) {
return endValue;
}
return (percentComplete < 0.5 ? easeInBounce(percentComplete * 2) * .5 : easeOutBounce(percentComplete * 2 - 1) * 0.5 + 0.5) * (endValue - startValue);
return (percentComplete < 0.5
? easeInBounce(percentComplete * 2) * .5
: easeOutBounce(percentComplete * 2 - 1) * 0.5 + 0.5
) * (endValue - startValue);
}]);
};
8 changes: 4 additions & 4 deletions packages/velocity/src/Velocity/easing/elastic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ namespace VelocityStatic.Easing {
const s = period / pi2 * Math.asin(1 / amplitude);

percentComplete = percentComplete * 2 - 1;
if (percentComplete < 0) {
return -0.5 * (amplitude * Math.pow(2, 10 * percentComplete) * Math.sin((percentComplete - s) * pi2 / period));
}
return amplitude * Math.pow(2, -10 * percentComplete) * Math.sin((percentComplete - s) * pi2 / period) * 0.5 + 1;
return (percentComplete < 0
? -0.5 * (amplitude * Math.pow(2, 10 * percentComplete) * Math.sin((percentComplete - s) * pi2 / period))
: amplitude * Math.pow(2, -10 * percentComplete) * Math.sin((percentComplete - s) * pi2 / period) * 0.5 + 1
) * (endValue - startValue);
}]);
}

Expand Down
62 changes: 58 additions & 4 deletions packages/velocity/velocity.js
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,63 @@ var VelocityStatic;
})(Easing = VelocityStatic.Easing || (VelocityStatic.Easing = {}));
})(VelocityStatic || (VelocityStatic = {}));

///<reference path="easings.ts" />
/*
* VelocityJS.org (C) 2014-2017 Julian Shapiro.
*
* Licensed under the MIT license. See LICENSE file in the project root for details.
*
* Back easings, based on code from https://github.com/yuichiroharai/easeplus-velocity
*/
var VelocityStatic;

(function(VelocityStatic) {
var Easing;
(function(Easing) {
function registerBackIn(name, amount) {
Easing.registerEasing([ name, function(percentComplete, startValue, endValue) {
if (percentComplete === 0) {
return startValue;
}
if (percentComplete === 1) {
return endValue;
}
return Math.pow(percentComplete, 2) * ((amount + 1) * percentComplete - amount) * (endValue - startValue);
} ]);
}
Easing.registerBackIn = registerBackIn;
function registerBackOut(name, amount) {
Easing.registerEasing([ name, function(percentComplete, startValue, endValue) {
if (percentComplete === 0) {
return startValue;
}
if (percentComplete === 1) {
return endValue;
}
return (Math.pow(--percentComplete, 2) * ((amount + 1) * percentComplete + amount) + 1) * (endValue - startValue);
} ]);
}
Easing.registerBackOut = registerBackOut;
function registerBackInOut(name, amount) {
amount *= 1.525;
Easing.registerEasing([ name, function(percentComplete, startValue, endValue) {
if (percentComplete === 0) {
return startValue;
}
if (percentComplete === 1) {
return endValue;
}
return ((percentComplete *= 2) < 1 ? Math.pow(percentComplete, 2) * ((amount + 1) * percentComplete - amount) : Math.pow(percentComplete -= 2, 2) * ((amount + 1) * percentComplete + amount) + 2) * .5 * (endValue - startValue);
} ]);
}
Easing.registerBackInOut = registerBackInOut;
registerBackIn("easeInBack", 1.7);
registerBackOut("easeOutBack", 1.7);
registerBackInOut("easeInOutBack", 1.7);
// TODO: Expose these as actions to register custom easings?
})(Easing = VelocityStatic.Easing || (VelocityStatic.Easing = {}));
})(VelocityStatic || (VelocityStatic = {}));

///<reference path="easings.ts" />
/*
* VelocityJS.org (C) 2014-2017 Julian Shapiro.
Expand Down Expand Up @@ -1795,10 +1852,7 @@ var VelocityStatic;
}
var s = period / pi2 * Math.asin(1 / amplitude);
percentComplete = percentComplete * 2 - 1;
if (percentComplete < 0) {
return -.5 * (amplitude * Math.pow(2, 10 * percentComplete) * Math.sin((percentComplete - s) * pi2 / period));
}
return amplitude * Math.pow(2, -10 * percentComplete) * Math.sin((percentComplete - s) * pi2 / period) * .5 + 1;
return (percentComplete < 0 ? -.5 * (amplitude * Math.pow(2, 10 * percentComplete) * Math.sin((percentComplete - s) * pi2 / period)) : amplitude * Math.pow(2, -10 * percentComplete) * Math.sin((percentComplete - s) * pi2 / period) * .5 + 1) * (endValue - startValue);
} ]);
}
Easing.registerElasticInOut = registerElasticInOut;
Expand Down
2 changes: 1 addition & 1 deletion packages/velocity/velocity.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/velocity/velocity.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/velocity/velocity.ui.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c093620

Please sign in to comment.