Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
/content/animation-easing-functions.gif
include <easing-functions.scad>;
function undulate(t) = abs((t - .5) * 2);
length = 5;
fullWidth = 200;
textSize = 5;
margin = length;
textGutter = length;
fgColor = "black";
bgColor = "white";
module demo(label, weight) {
width = weight * fullWidth;
color(bgColor) {
polygon(
[
[0, 0],
[0, length],
[fullWidth, length],
[fullWidth, 0]
],
paths = [[0, 1, 2, 3]]
);
}
color(fgColor) {
polygon(
[
[0, 0],
[0, length],
[width, length],
[width, 0]
],
paths = [[0, 1, 2, 3]]
);
}
translate([textGutter * -1, 0, 0]) {
text(
label,
size = textSize,
halign = "right"
);
}
}
easing_functions = [
["linear", linear(undulate($t))],
["ease_in_quad", ease_in_quad(undulate($t))],
["ease_out_quad", ease_out_quad(undulate($t))],
["ease_in_out_quad", ease_in_out_quad(undulate($t))],
["ease_in_cubic", ease_in_cubic(undulate($t))],
["ease_out_cubic", ease_out_cubic(undulate($t))],
["ease_in_out_cubic", ease_in_out_cubic(undulate($t))],
["ease_in_quart", ease_in_quart(undulate($t))],
["ease_out_quart", ease_out_quart(undulate($t))],
["ease_in_out_quart", ease_in_out_quart(undulate($t))],
["ease_in_quint", ease_in_quint(undulate($t))],
["ease_out_quint", ease_out_quint(undulate($t))],
["ease_in_out_quint", ease_in_out_quint(undulate($t))],
];
for (i = [0 : len(easing_functions) -1 ]) {
easing_function = easing_functions[i];
name = easing_function[0];
value = easing_function[1];
y = (length + margin) * i * -1;
translate([0, y, 0]) {
demo(name, value);
}
}
// http://gizma.com/easing/
// no easing, no acceleration
function linear(t) = (
t
);
// accelerating from zero velocity
function ease_in_quad(t) = (
pow(t, 2)
);
// decelerating to zero velocity
function ease_out_quad(t) = (
t * (2 - t)
);
// acceleration until halfway, then deceleration
function ease_in_out_quad(t) = (
t < .5
? 2 * pow(t, 2)
: -1 + (4 - 2 * t) * t
);
// accelerating from zero velocity
function ease_in_cubic(t) = (
pow(t, 3)
);
// decelerating to zero velocity
function ease_out_cubic(t) = (
pow(t - 1, 3) + 1
);
// acceleration until halfway, then deceleration
function ease_in_out_cubic(t) = (
t < .5
? 4 * pow(t, 3)
: (t - 1) * (2 * t - 2) * (2 * t - 2) + 1
);
// accelerating from zero velocity
function ease_in_quart(t) = (
pow(t, 4)
);
// decelerating to zero velocity
function ease_out_quart(t) = (
1 - (t - 1) * pow(t - 1, 3)
);
// acceleration until halfway, then deceleration
function ease_in_out_quart(t) = (
t < .5
? .5 * pow(t / .5, 4)
: -.5 * (pow(t / .5 - 2, 4) - 2)
);
// accelerating from zero velocity
function ease_in_quint(t) = (
pow(t, 5)
);
// decelerating to zero velocity
function ease_out_quint(t) = (
pow((t - 1), 5) + 1
);
// acceleration until halfway, then deceleration
function ease_in_out_quint(t) = (
t < .5
? .5 * pow(t / .5, 5)
: .5 * (pow(t / .5 - 2, 5) + 2)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment