Skip to content

Commit

Permalink
split easing equations out from Piu for more general uses #1309
Browse files Browse the repository at this point in the history
  • Loading branch information
phoddie committed Feb 20, 2024
1 parent 47b4362 commit 09be19b
Show file tree
Hide file tree
Showing 8 changed files with 404 additions and 355 deletions.
3 changes: 3 additions & 0 deletions examples/manifest_piu.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"include": [
"$(MODULES)/base/easing/manifest.json"
],
"modules": {
"commodetto/Bitmap": "$(COMMODETTO)/commodettoBitmap",
"commodetto/PocoCore": "$(COMMODETTO)/commodettoPocoCore",
Expand Down
342 changes: 342 additions & 0 deletions modules/base/easing/easing.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,342 @@
/*
* 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);
}
}
45 changes: 45 additions & 0 deletions modules/base/easing/easing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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";

0 comments on commit 09be19b

Please sign in to comment.