Created
July 7, 2019 08:59
-
-
Save yoggy/38c1ae7f9b208553fec1b5e5b06ab0da to your computer and use it in GitHub Desktop.
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
// | |
// easing_test.pde | |
// | |
// see also...http://gizma.com/easing/ | |
// | |
import java.lang.reflect.*; | |
import java.util.*; | |
float easing_00_linear(float p) { | |
return p; | |
} | |
float easing_10_in_quad(float p) { | |
return p * p; | |
} | |
float easing_11_out_quad(float p) { | |
return -1.0 * p * (p - 2); | |
} | |
float easing_12_in_out_quad(float p) { | |
p *= 2; | |
float v = 0.0f; | |
if (p < 1.0) { | |
v = easing_10_in_quad(p) * 0.5; | |
} else { | |
v = easing_11_out_quad(p - 1.0) * 0.5 + 0.5; | |
} | |
return v; | |
} | |
float easing_20_in_cubic(float p) { | |
return p * p * p; | |
} | |
float easing_21_out_cubic(float p) { | |
p --; // 0.0~1.0 → -1.0~0.0 | |
return (p * p * p + 1); | |
} | |
float easing_22_in_out_cubic(float p) { | |
p *= 2; | |
float v = 0.0f; | |
if (p < 1.0) { | |
v = easing_20_in_cubic(p) * 0.5; | |
} else { | |
v = easing_21_out_cubic(p - 1.0) * 0.5 + 0.5; | |
} | |
return v; | |
} | |
float easing_30_in_quint(float p) { | |
return p * p * p * p; | |
} | |
float easing_31_out_quint(float p) { | |
p --; // 0.0~1.0 → -1.0~0.0 | |
return (p * p * p * p + 1); | |
} | |
float easing_32_in_out_quint(float p) { | |
p *= 2; | |
float v = 0.0f; | |
if (p < 1.0) { | |
v = easing_30_in_quint(p) * 0.5; | |
} else { | |
v = easing_31_out_quint(p - 1.0) * 0.5 + 0.5; | |
} | |
return v; | |
} | |
float easing_40_in_sine(float p) { | |
return 1.0 - cos(p * (PI/2)); | |
} | |
float easing_41_out_sine(float p) { | |
return sin(p * (PI/2)); | |
} | |
float easing_42_in_out_sine(float p) { | |
p *= 2; | |
float v = 0.0f; | |
if (p < 1.0) { | |
v = easing_40_in_sine(p) * 0.5; | |
} else { | |
v = easing_41_out_sine(p - 1.0) * 0.5 + 0.5; | |
} | |
return v; | |
} | |
float easing_50_in_expo(float p) { | |
return pow(2, 10 * (p - 1) ); | |
} | |
float easing_51_out_expo(float p) { | |
return -pow(2, -10 * p) + 1; | |
} | |
float easing_52_in_out_expo(float p) { | |
p *= 2; | |
float v = 0.0f; | |
if (p < 1.0) { | |
v = easing_50_in_expo(p) * 0.5; | |
} else { | |
v = easing_51_out_expo(p - 1.0) * 0.5 + 0.5; | |
} | |
return v; | |
} | |
float easing_60_in_circ(float p) { | |
return -1.0 * (sqrt(1 - p*p) - 1); | |
} | |
float easing_61_out_circ(float p) { | |
p --; // 0.0~1.0 → -1.0~0.0 | |
return sqrt(1 - p * p); | |
} | |
float easing_62_in_out_circ(float p) { | |
p *= 2; | |
float v = 0.0f; | |
if (p < 1.0) { | |
v = easing_60_in_circ(p) * 0.5; | |
} else { | |
v = easing_61_out_circ(p - 1.0) * 0.5 + 0.5; | |
} | |
return v; | |
} | |
float call_easing_by_name(String name, float p) { | |
// range check | |
if (p < 0.0f) p = 0.0f; | |
if (p > 1.0f) p = 1.0f; | |
float v = 0.0f; | |
Class cls = this.getClass(); | |
try { | |
Method m = cls.getDeclaredMethod(name, float.class); | |
v = (float)m.invoke(this, p); | |
} | |
catch(Exception e) { | |
println(e); | |
} | |
return v; | |
} | |
String [] get_easing_names() { | |
Class cls = this.getClass(); | |
Method [] methods = cls.getDeclaredMethods(); | |
ArrayList<String> names = new ArrayList<String>(); | |
for (int i = 0; i < methods.length; ++i) { | |
String n = methods[i].getName(); | |
try { | |
if ("easing_".equals(n.substring(0, 7))) { | |
names.add(methods[i].getName()); | |
} | |
} | |
catch(Exception e) { | |
} | |
} | |
Collections.sort(names); | |
return (String[])names.toArray(new String[0]); | |
} | |
String [] easing_names; | |
int current_easing_idx = 1; | |
void setup() { | |
size(640, 480); | |
background(255, 255, 255); | |
stroke(0, 0, 0); | |
easing_names = get_easing_names(); | |
println(easing_names); | |
redraw(); | |
} | |
void draw() { | |
// 注意: | |
// drawで何もしないときでも、keyPressed()などを使う場合は、関数を定義しておくこと。 | |
// draw()が定義されていないと、keyPressed()やmousePressed()などの関数が呼び出されない… | |
} | |
void redraw() | |
{ | |
background(255, 255, 255); | |
draw_easing(); | |
} | |
void draw_easing() { | |
String easing_name = easing_names[current_easing_idx]; | |
textSize(32); | |
fill(0, 0, 0); | |
text(easing_name, 10, 32); | |
for ( int x = 0; x < width; ++x) { | |
float p0 = x / (float)width; | |
float p1 = (x + 1) / (float)width; | |
float v0 = call_easing_by_name(easing_name, p0); | |
float v1 = call_easing_by_name(easing_name, p1); | |
line(x, height * (1.0 - v0), x + 1, height * (1 - v1)); | |
} | |
} | |
void keyPressed() { | |
println(key); | |
if (key == CODED) { | |
switch(keyCode) { | |
case RIGHT: | |
current_easing_idx = (current_easing_idx + 1) % easing_names.length; | |
break; | |
case LEFT: | |
current_easing_idx = (current_easing_idx - 1) < 0 ? (easing_names.length - 1) : (current_easing_idx - 1); | |
break; | |
} | |
redraw(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment