Skip to content

Instantly share code, notes, and snippets.

@m1keall1son
Last active May 24, 2017 22:24
Show Gist options
  • Select an option

  • Save m1keall1son/fe275f9ba81673584d0779c28c17a01f to your computer and use it in GitHub Desktop.

Select an option

Save m1keall1son/fe275f9ba81673584d0779c28c17a01f to your computer and use it in GitHub Desktop.
Animation Controller example
//this code is not tested
class Color {
constructor(r, g, b){
if(arguments.length === 1){
this.r = r.r;
this.g = r.g;
this.b = r.b;
}else{
this.r = r;
this.g = g;
this.b = b;
}
}
}
let Easing = {
LINEAR:0,
IN_QUAD:1, OUT_QUAD:2, IN_OUT_QUAD:3, OUT_IN_QUAD:4,
IN_CUBIC:5, OUT_CUBIC:6, IN_OUT_CUBIC:7, OUT_IN_CUBIC:8,
IN_QUART:9, OUT_QUART:10, IN_OUT_QUART:11, OUT_IN_QUART:12,
IN_QUINT:13, OUT_QUINT:14, IN_OUT_QUINT:15, OUT_IN_QUINT:16,
NUM_EASING:17
}
//base class for animations
class Animation {
constructor( hardware ){
this.hardware = arduino;
this.target = new Color( 0, 0, 0 );
this.duration = 0;
this.easing = Easing.LINEAR;
}
start(){
this.hardware.send( /* animation protocol */ );
}
onComplete(){
//implent this if animation is more complex than one off
}
}
//animations house the interaction with the hardware and spacific actions
class ClearAnimation extends Animation {
start(){
super.start(); //just send an immediat 0 color and 0 duration
}
}
class FadeOutAniamtion extends Animation {
constructor( hardware, duration, easing ){
super( hardware );
this.target = Color(0,0,0);
this.duration = duration || 1;
this.easing = easing || easing.LINEAR;
}
}
class PinPongAnimation extends Animation {
constructor( hardware, start, target, period ){
super( hardware );
this.start = start || Color(0,0,255);
this.target = target || Color(0,0,0);
this.duration = duration || 1.;
}
onComplete(){
//reverse the fade with the same duration
let tmp = this.start;
this.start = this.target;
this.target = tmp;
start();
}
}
//thin wrapper that holds the curret animation
class AnimationController {
constructor(){
this.animation = null;
}
set( animation ){
this.animation = animation;
this.animation.start();
}
//called inside serial receive when an animation complete event comes in from serial
onAnimationComplete(){
if(this.animation)
{
this.animation.onComplete();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment