Skip to content

Instantly share code, notes, and snippets.

@Kryzarel
Last active November 25, 2024 05:05
Show Gist options
  • Save Kryzarel/bba64622057f21a1d6d44879f9cd7bd4 to your computer and use it in GitHub Desktop.
Save Kryzarel/bba64622057f21a1d6d44879f9cd7bd4 to your computer and use it in GitHub Desktop.
C# Easing Functions
using System;
namespace Kryz.Tweening
{
// Made with the help of this great post: https://joshondesign.com/2013/03/01/improvedEasingEquations
// --------------------------------- Other Related Links --------------------------------------------------------------------
// Original equations, bad formulation: https://github.com/danro/jquery-easing/blob/master/jquery.easing.js
// A few equations, very simplified: https://gist.github.com/gre/1650294
// Easings.net equations, simplified: https://github.com/ai/easings.net/blob/master/src/easings/easingsFunctions.ts
public static class EasingFunctions
{
public static float Linear(float t) => t;
public static float InQuad(float t) => t * t;
public static float OutQuad(float t) => 1 - InQuad(1 - t);
public static float InOutQuad(float t)
{
if (t < 0.5) return InQuad(t * 2) / 2;
return 1 - InQuad((1 - t) * 2) / 2;
}
public static float InCubic(float t) => t * t * t;
public static float OutCubic(float t) => 1 - InCubic(1 - t);
public static float InOutCubic(float t)
{
if (t < 0.5) return InCubic(t * 2) / 2;
return 1 - InCubic((1 - t) * 2) / 2;
}
public static float InQuart(float t) => t * t * t * t;
public static float OutQuart(float t) => 1 - InQuart(1 - t);
public static float InOutQuart(float t)
{
if (t < 0.5) return InQuart(t * 2) / 2;
return 1 - InQuart((1 - t) * 2) / 2;
}
public static float InQuint(float t) => t * t * t * t * t;
public static float OutQuint(float t) => 1 - InQuint(1 - t);
public static float InOutQuint(float t)
{
if (t < 0.5) return InQuint(t * 2) / 2;
return 1 - InQuint((1 - t) * 2) / 2;
}
public static float InSine(float t) => 1 - (float)Math.Cos(t * Math.PI / 2);
public static float OutSine(float t) => (float)Math.Sin(t * Math.PI / 2);
public static float InOutSine(float t) => (float)(Math.Cos(t * Math.PI) - 1) / -2;
public static float InExpo(float t) => (float)Math.Pow(2, 10 * (t - 1));
public static float OutExpo(float t) => 1 - InExpo(1 - t);
public static float InOutExpo(float t)
{
if (t < 0.5) return InExpo(t * 2) / 2;
return 1 - InExpo((1 - t) * 2) / 2;
}
public static float InCirc(float t) => -((float)Math.Sqrt(1 - t * t) - 1);
public static float OutCirc(float t) => 1 - InCirc(1 - t);
public static float InOutCirc(float t)
{
if (t < 0.5) return InCirc(t * 2) / 2;
return 1 - InCirc((1 - t) * 2) / 2;
}
public static float InElastic(float t) => 1 - OutElastic(1 - t);
public static float OutElastic(float t)
{
float p = 0.3f;
return (float)Math.Pow(2, -10 * t) * (float)Math.Sin((t - p / 4) * (2 * Math.PI) / p) + 1;
}
public static float InOutElastic(float t)
{
if (t < 0.5) return InElastic(t * 2) / 2;
return 1 - InElastic((1 - t) * 2) / 2;
}
public static float InBack(float t)
{
float s = 1.70158f;
return t * t * ((s + 1) * t - s);
}
public static float OutBack(float t) => 1 - InBack(1 - t);
public static float InOutBack(float t)
{
if (t < 0.5) return InBack(t * 2) / 2;
return 1 - InBack((1 - t) * 2) / 2;
}
public static float InBounce(float t) => 1 - OutBounce(1 - t);
public static float OutBounce(float t)
{
float div = 2.75f;
float mult = 7.5625f;
if (t < 1 / div)
{
return mult * t * t;
}
else if (t < 2 / div)
{
t -= 1.5f / div;
return mult * t * t + 0.75f;
}
else if (t < 2.5 / div)
{
t -= 2.25f / div;
return mult * t * t + 0.9375f;
}
else
{
t -= 2.625f / div;
return mult * t * t + 0.984375f;
}
}
public static float InOutBounce(float t)
{
if (t < 0.5) return InBounce(t * 2) / 2;
return 1 - InBounce((1 - t) * 2) / 2;
}
}
}
@Ryanarg
Copy link

Ryanarg commented Sep 2, 2023

Noice

@anchikai
Copy link

Noice

@iiKiZii
Copy link

iiKiZii commented Sep 23, 2023

Noise

@kevincastejon
Copy link

Noisette

@TheSillyDoggo
Copy link

is that a PIZZA TOWER reference?!?!

@fryderykhuang
Copy link

Noice

@Artromskiy
Copy link

Noice

@Scurvyez
Copy link

Noice

@ercantomac
Copy link

Noice

@DesertImage
Copy link

Noice

@PierreGlsAtomic
Copy link

Noice

@Jack-Love10200
Copy link

Noice

@TinyMatt
Copy link

Noice

@ashifolfi
Copy link

Noice

@CustomPhase
Copy link

The InSine formula is slightly incorrect, it should be 1 - cos, not -cos.

@Kryzarel
Copy link
Author

Kryzarel commented Jul 9, 2024

@CustomPhase

The InSine formula is slightly incorrect, it should be 1 - cos, not -cos.

Nice catch. Fixed.

@Kryzarel
Copy link
Author

Kryzarel commented Jul 9, 2024

Also, wtf is up with all the "Noice" comments o.O

@ashifolfi
Copy link

Also, wtf is up with all the "Noice" comments o.O

idk but it's kinda funny tbh

@rumaniel
Copy link

Noice

@suveer-r
Copy link

Noice

@GhoulMage
Copy link

Noice

@Titory0
Copy link

Titory0 commented Aug 14, 2024

Noice

@moontr3
Copy link

moontr3 commented Aug 23, 2024

Noice

@Fallbork
Copy link

Noice

@Artromskiy
Copy link

I smile every time I get a notification from here

Noice

@skull-issue
Copy link

Noice

@TheSillyDoggo
Copy link

I smile every time I get a notification from here

Noice

same :3
reminds me of how i felt 2 years ago

@Fallbork
Copy link

yus owo

@Heat-Space
Copy link

Noice

@nathancarlmitchell
Copy link

Noice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment