Created
February 17, 2024 06:49
-
-
Save phoddie/a7b5bcad8c2d324ee25ace4fa58cb271 to your computer and use it in GitHub Desktop.
break out easing functions to module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2016-2024 Moddable Tech, Inc. | |
* | |
* This file is part of the Moddable SDK Runtime. | |
* | |
* The Moddable SDK Runtime is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU Lesser General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* The Moddable SDK Runtime is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU Lesser General Public License for more details. | |
* | |
* You should have received a copy of the GNU Lesser General Public License | |
* along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>. | |
* | |
*/ | |
#include "mc.xs.h" | |
#include "xsHost.h" | |
void Math_quadEaseIn(xsMachine* the) | |
{ | |
double fraction = xsToNumber(xsArg(0)); | |
xsResult = xsNumber(fraction * fraction); | |
} | |
void Math_quadEaseOut(xsMachine* the) | |
{ | |
double fraction = xsToNumber(xsArg(0)); | |
xsResult = xsNumber(-1 * fraction * (fraction - 2)); | |
} | |
void Math_quadEaseInOut(xsMachine* the) | |
{ | |
double fraction = xsToNumber(xsArg(0)); | |
fraction *= 2; | |
if (fraction < 1) | |
xsResult = xsNumber(fraction * fraction / 2); | |
else { | |
fraction -= 1; | |
xsResult = xsNumber((fraction * (fraction - 2) - 1) / -2); | |
} | |
} | |
void Math_cubicEaseIn(xsMachine* the) | |
{ | |
double fraction = xsToNumber(xsArg(0)); | |
xsResult = xsNumber(fraction * fraction * fraction); | |
} | |
void Math_cubicEaseOut(xsMachine* the) | |
{ | |
double fraction = xsToNumber(xsArg(0)); | |
double f = fraction - 1; | |
xsResult = xsNumber(f * f * f + 1); | |
} | |
void Math_cubicEaseInOut(xsMachine* the) | |
{ | |
double fraction = xsToNumber(xsArg(0)); | |
fraction *= 2; | |
if (fraction < 1) | |
xsResult = xsNumber(fraction * fraction * fraction / 2); | |
else { | |
fraction -= 2; | |
xsResult = xsNumber((fraction * fraction * fraction + 2) / 2); | |
} | |
} | |
void Math_quartEaseIn(xsMachine* the) | |
{ | |
double fraction = xsToNumber(xsArg(0)); | |
xsResult = xsNumber(fraction * fraction * fraction * fraction); | |
} | |
void Math_quartEaseOut(xsMachine* the) | |
{ | |
double fraction = xsToNumber(xsArg(0)); | |
double f = fraction - 1; | |
xsResult = xsNumber(-1 * (f * f * f * f - 1)); | |
} | |
void Math_quartEaseInOut(xsMachine* the) | |
{ | |
double fraction = xsToNumber(xsArg(0)); | |
fraction *= 2; | |
if (fraction < 1) | |
xsResult = xsNumber(fraction * fraction * fraction * fraction / 2); | |
else { | |
fraction -= 2; | |
xsResult = xsNumber((fraction * fraction * fraction * fraction - 2) / -2); | |
} | |
} | |
void Math_quintEaseIn(xsMachine* the) | |
{ | |
double fraction = xsToNumber(xsArg(0)); | |
xsResult = xsNumber(fraction * fraction * fraction * fraction * fraction); | |
} | |
void Math_quintEaseOut(xsMachine* the) | |
{ | |
double fraction = xsToNumber(xsArg(0)); | |
double f = fraction - 1; | |
xsResult = xsNumber(f * f * f * f * f + 1); | |
} | |
void Math_quintEaseInOut(xsMachine* the) | |
{ | |
double fraction = xsToNumber(xsArg(0)); | |
fraction *= 2; | |
if (fraction < 1) | |
xsResult = xsNumber(fraction * fraction * fraction * fraction * fraction / 2); | |
else { | |
fraction -= 2; | |
xsResult = xsNumber((fraction * fraction * fraction * fraction * fraction + 2) / 2); | |
} | |
} | |
void Math_sineEaseIn(xsMachine* the) | |
{ | |
double fraction = xsToNumber(xsArg(0)); | |
xsResult = xsNumber(-1 * cos(fraction * (C_M_PI / 2)) + 1); | |
} | |
void Math_sineEaseOut(xsMachine* the) | |
{ | |
double fraction = xsToNumber(xsArg(0)); | |
xsResult = xsNumber(sin(fraction * (C_M_PI / 2))); | |
} | |
void Math_sineEaseInOut(xsMachine* the) | |
{ | |
double fraction = xsToNumber(xsArg(0)); | |
xsResult = xsNumber(-1.0 / 2.0 * (cos(C_M_PI * fraction) - 1)); | |
} | |
void Math_circularEaseIn(xsMachine* the) | |
{ | |
double fraction = xsToNumber(xsArg(0)); | |
xsResult = xsNumber(-1 * (sqrt(1 - fraction * fraction) - 1)); | |
} | |
void Math_circularEaseOut(xsMachine* the) | |
{ | |
double fraction = xsToNumber(xsArg(0)); | |
fraction -= 1; | |
xsResult = xsNumber(sqrt(1 - fraction * fraction)); | |
} | |
void Math_circularEaseInOut(xsMachine* the) | |
{ | |
double fraction = xsToNumber(xsArg(0)); | |
fraction *= 2; | |
if (fraction < 1) | |
xsResult = xsNumber((sqrt(1 - fraction * fraction) - 1) / -2); | |
else { | |
fraction -= 2; | |
xsResult = xsNumber((sqrt(1 - fraction * fraction) + 1) / 2); | |
} | |
} | |
void Math_exponentialEaseIn(xsMachine* the) | |
{ | |
double fraction = xsToNumber(xsArg(0)); | |
xsResult = xsNumber((fraction == 0) ? 0 : pow(2, 10 * (fraction - 1))); | |
} | |
void Math_exponentialEaseOut(xsMachine* the) | |
{ | |
double fraction = xsToNumber(xsArg(0)); | |
xsResult = xsNumber((fraction == 1) ? 1 : (-pow(2, -10 * fraction) + 1)); | |
} | |
void Math_exponentialEaseInOut(xsMachine* the) | |
{ | |
double fraction = xsToNumber(xsArg(0)); | |
if (fraction == 0) | |
xsResult = xsNumber(0); | |
else if (fraction == 1) | |
xsResult = xsNumber(1); | |
else { | |
fraction *= 2; | |
if (fraction < 1) | |
xsResult = xsNumber(pow(2, 10 * (fraction - 1)) / 2); | |
else | |
xsResult = xsNumber((-pow(2, -10 * --fraction) + 2) / 2); | |
} | |
} | |
void Math_backEaseIn(xsMachine* the) | |
{ | |
xsIntegerValue c = xsToInteger(xsArgc); | |
double fraction = xsToNumber(xsArg(0)); | |
double s = (c > 1) ? xsToNumber(xsArg(1)) : 1.70158; | |
xsResult = xsNumber(fraction * fraction * ((s + 1) * fraction - s)); | |
} | |
void Math_backEaseOut(xsMachine* the) | |
{ | |
xsIntegerValue c = xsToInteger(xsArgc); | |
double fraction = xsToNumber(xsArg(0)); | |
double s = (c > 1) ? xsToNumber(xsArg(1)) : 1.70158; | |
fraction -= 1; | |
xsResult = xsNumber(fraction * fraction * ((s + 1) * fraction + s) + 1); | |
} | |
void Math_backEaseInOut(xsMachine* the) | |
{ | |
xsIntegerValue c = xsToInteger(xsArgc); | |
double fraction = xsToNumber(xsArg(0)); | |
double s = (c > 1) ? xsToNumber(xsArg(1)) : 1.70158; | |
s *= 1.525; | |
fraction *= 2; | |
if (fraction < 1) | |
xsResult = xsNumber((fraction * fraction * (s + 1) * fraction - s) / 2); | |
else { | |
fraction -= 2; | |
xsResult = xsNumber((fraction * fraction * ((s + 1) * fraction + s) + 2) / 2); | |
} | |
} | |
static double bounce(double fraction) | |
{ | |
if (fraction < (1 / 2.75)) | |
fraction = 7.5625 * fraction * fraction; | |
else if (fraction < (2 / 2.75)) { | |
fraction -= (1.5 / 2.75); | |
fraction = 7.5625 * fraction * fraction + 0.75; | |
} | |
else if (fraction < (2.5 / 2.75)) { | |
fraction -= (2.25 / 2.75); | |
fraction = 7.5625 * fraction * fraction + 0.9375; | |
} | |
else { | |
fraction -= (2.625 / 2.75); | |
fraction = 7.5625 * fraction * fraction + 0.984375; | |
} | |
return fraction; | |
} | |
void Math_bounceEaseIn(xsMachine* the) | |
{ | |
double fraction = xsToNumber(xsArg(0)); | |
xsResult = xsNumber(1 - bounce(1 - fraction)); | |
} | |
void Math_bounceEaseOut(xsMachine* the) | |
{ | |
double fraction = xsToNumber(xsArg(0)); | |
xsResult = xsNumber(bounce(fraction)); | |
} | |
void Math_bounceEaseInOut(xsMachine* the) | |
{ | |
double fraction = xsToNumber(xsArg(0)); | |
if (fraction < 0.5) | |
xsResult = xsNumber((1 - bounce(1 - (fraction * 2))) / 2); | |
else | |
xsResult = xsNumber(bounce(fraction * 2 - 1) / 2 + 0.5); | |
} | |
void Math_elasticEaseIn(xsMachine* the) | |
{ | |
xsIntegerValue c = xsToInteger(xsArgc); | |
double fraction = xsToNumber(xsArg(0)); | |
if (fraction == 0) | |
xsResult = xsNumber(0); | |
else if (fraction == 1) | |
xsResult = xsNumber(1); | |
else { | |
double a = (c > 1) ? xsToNumber(xsArg(1)) : 0; | |
double p = (c > 2) ? xsToNumber(xsArg(2)) : 0; | |
double s; | |
if (!p) | |
p = 0.3; | |
if (a < 1) { | |
a = 1; | |
s = p / 4; | |
} | |
else | |
s = p / (2 * C_M_PI) * asin(1 / a); | |
fraction -= 1; | |
xsResult = xsNumber(-(a * pow(2, 10 * fraction) * sin( (fraction - s) * (2 * C_M_PI) / p ))); | |
} | |
} | |
void Math_elasticEaseOut(xsMachine* the) | |
{ | |
xsIntegerValue c = xsToInteger(xsArgc); | |
double fraction = xsToNumber(xsArg(0)); | |
if (fraction == 0) | |
xsResult = xsNumber(0); | |
else if (fraction == 1) | |
xsResult = xsNumber(1); | |
else { | |
double a = (c > 1) ? xsToNumber(xsArg(1)) : 0; | |
double p = (c > 2) ? xsToNumber(xsArg(2)) : 0; | |
double s; | |
if (!p) | |
p = 0.3; | |
if (a < 1) { | |
a = 1; | |
s = p / 4; | |
} | |
else | |
s = p / (2 * C_M_PI) * asin(1 / a); | |
xsResult = xsNumber(a * pow(2, -10 * fraction ) * sin((fraction - s) * (2 * C_M_PI) / p ) + 1); | |
} | |
} | |
void Math_elasticEaseInOut(xsMachine* the) | |
{ | |
xsIntegerValue c = xsToInteger(xsArgc); | |
double fraction = xsToNumber(xsArg(0)); | |
if (fraction == 0) | |
xsResult = xsNumber(0); | |
else if (fraction == 1) | |
xsResult = xsNumber(1); | |
else { | |
double a = (c > 1) ? xsToNumber(xsArg(1)) : 0; | |
double p = (c > 2) ? xsToNumber(xsArg(2)) : 0; | |
double s; | |
fraction *= 2; | |
if (!p) | |
p = 0.45; | |
if (a < 1) { | |
a = 1; | |
s = p / 4; | |
} | |
else | |
s = p / (2 * C_M_PI) * asin(1 / a); | |
fraction -= 1; | |
if (fraction < 0) | |
xsResult = xsNumber((a * pow(2, 10 * fraction) * sin((fraction - s) * (2 * C_M_PI) / p )) / -2); | |
else | |
xsResult = xsNumber(a * pow(2, -10 * fraction) * sin((fraction - s) * 2 * C_M_PI / p ) / 2 + 1); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2016-2024 Moddable Tech, Inc. | |
* | |
* This file is part of the Moddable SDK. | |
* | |
* This work is licensed under the | |
* Creative Commons Attribution 4.0 International License. | |
* To view a copy of this license, visit | |
* <http://creativecommons.org/licenses/by/4.0>. | |
* or send a letter to Creative Commons, PO Box 1866, | |
* Mountain View, CA 94042, USA. | |
* | |
*/ | |
Math.backEaseIn = function(fraction) @ "Math_backEaseIn"; | |
Math.backEaseInOut = function(fraction) @ "Math_backEaseInOut"; | |
Math.backEaseOut = function(fraction) @ "Math_backEaseOut"; | |
Math.bounceEaseIn = function(fraction) @ "Math_bounceEaseIn"; | |
Math.bounceEaseInOut = function(fraction) @ "Math_bounceEaseInOut"; | |
Math.bounceEaseOut = function(fraction) @ "Math_bounceEaseOut"; | |
Math.circularEaseIn = function(fraction) @ "Math_circularEaseIn"; | |
Math.circularEaseInOut = function(fraction) @ "Math_circularEaseInOut"; | |
Math.circularEaseOut = function(fraction) @ "Math_circularEaseOut"; | |
Math.cubicEaseIn = function(fraction) @ "Math_cubicEaseIn"; | |
Math.cubicEaseInOut = function(fraction) @ "Math_cubicEaseInOut"; | |
Math.cubicEaseOut = function(fraction) @ "Math_cubicEaseOut"; | |
Math.elasticEaseIn = function(fraction) @ "Math_elasticEaseIn"; | |
Math.elasticEaseInOut = function(fraction) @ "Math_elasticEaseInOut"; | |
Math.elasticEaseOut = function(fraction) @ "Math_elasticEaseOut"; | |
Math.exponentialEaseIn = function(fraction) @ "Math_exponentialEaseIn"; | |
Math.exponentialEaseInOut = function(fraction) @ "Math_exponentialEaseInOut"; | |
Math.exponentialEaseOut = function(fraction) @ "Math_exponentialEaseOut"; | |
Math.quadEaseIn = function(fraction) @ "Math_quadEaseIn"; | |
Math.quadEaseInOut = function(fraction) @ "Math_quadEaseInOut"; | |
Math.quadEaseOut = function(fraction) @ "Math_quadEaseOut"; | |
Math.quartEaseIn = function(fraction) @ "Math_quartEaseIn"; | |
Math.quartEaseInOut = function(fraction) @ "Math_quartEaseInOut"; | |
Math.quartEaseOut = function(fraction) @ "Math_quartEaseOut"; | |
Math.quintEaseIn = function(fraction) @ "Math_quintEaseIn"; | |
Math.quintEaseInOut = function(fraction) @ "Math_quintEaseInOut"; | |
Math.quintEaseOut = function(fraction) @ "Math_quintEaseOut"; | |
Math.sineEaseIn = function(fraction) @ "Math_sineEaseIn"; | |
Math.sineEaseInOut = function(fraction) @ "Math_sineEaseInOut"; | |
Math.sineEaseOut = function(fraction) @ "Math_sineEaseOut"; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"modules": { | |
"*": "./easing" | |
}, | |
"preload": "easing" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment