prosperon/scripts/tween.js

217 lines
4.8 KiB
JavaScript
Raw Normal View History

2023-12-28 07:57:22 -06:00
/* Take numbers from 0 to 1 and remap them to easing functions */
var Ease = {
linear(t) { return t; },
in(t) { return t*t; },
out(t) {
var d = 1-t;
return 1 - d*d
},
inout(t) {
var d = -2*t + 2;
return t < 0.5 ? 2 * t * t : 1 - (d * d) / 2;
},
};
function make_easing_fns(num) {
var obj = {};
obj.in = function(t) {
return Math.pow(t,num);
};
obj.out = function(t) {
return 1 - Math.pow(1 - t, num);
};
var mult = Math.pow(2, num-1);
obj.inout = function(t) {
return t < 0.5 ? mult * Math.pow(t, num) : 1 - Math.pow(-2 * t + 2, num) / 2;
};
return obj;
};
Ease.quad = make_easing_fns(2);
Ease.cubic = make_easing_fns(3);
Ease.quart = make_easing_fns(4);
Ease.quint = make_easing_fns(5);
Ease.expo = {
in(t) {
return t === 0 ? 0 : Math.pow(2, 10 * t - 10);
},
out(t) {
return t === 1 ? 1 : 1 - Math.pow(2, -10 * t);
},
inout(t) {
return t === 0 ? 0 : t === 1 ? 1 : t < 0.5 ? Math.pow(2, 20 * t - 10) / 2 : (2 - Math.pow(2, -20 * t + 10)) / 2;
}
};
Ease.bounce = {
in(t) {
return 1 - this.out(t - 1);
},
out(t) {
var n1 = 7.5625;
var d1 = 2.75;
if (t < 1 / d1) { return n1 * t * t; }
else if (t < 2 / d1) { return n1 * (t -= 1.5 / d1) * t + 0.75; }
else if (t < 2.5 / d1) { return n1 * (t -= 2.25 / d1) * t + 0.9375; }
else
return n1 * (t -= 2.625 / d1) * t + 0.984375;
},
inout(t) {
return t < 0.5 ? (1 - this.out(1 - 2 * t)) / 2 : (1 + this.out(2 * t - 1)) / 2;
}
};
Ease.sine = {
in(t) { return 1 - Math.cos((t * Math.PI)/2); },
out(t) { return Math.sin((t*Math.PI)/2); },
inout(t) { return -(Math.cos(Math.PI*t) - 1) / 2; }
};
Ease.elastic = {
in(t) {
return t === 0 ? 0 : t === 1 ? 1 : -Math.pow(2, 10*t-10) * Math.sin((t * 10 - 10.75) * this.c4);
},
out(t) {
return t === 0 ? 0 : t === 1 ? 1 : Math.pow(2, -10*t) * Math.sin((t * 10 - 0.75) * this.c4) + 1;
},
inout(t) {
t === 0 ? 0 : t === 1 ? 1 : t < 0.5 ?
-(Math.pow(2, 20 * t - 10) * Math.sin((20 * t - 11.125) * this.c5)) / 2
: (Math.pow(2, -20 * t + 10) * Math.sin((20 * t - 11.125) * this.c5)) / 2 + 1;
},
};
Ease.elastic.c4 = 2*Math.PI/3;
Ease.elastic.c5 = 2*Math.PI / 4.5;
2024-07-18 12:39:58 -05:00
var tween = function(from, to, time, fn)
2024-04-01 08:13:57 -05:00
{
var start = profile.secs(profile.now());
var update = function(dt) {
var elapsed = profile.secs(profile.now()) - start;
2024-07-18 12:39:58 -05:00
fn(from.lerp(to,elapsed/time));
2024-04-01 08:13:57 -05:00
if (elapsed >= time) {
2024-07-18 12:39:58 -05:00
fn(to);
if (stop.then) stop.then();
2024-04-01 08:13:57 -05:00
stop();
}
};
var stop = Register.update.register(update);
2024-07-18 12:39:58 -05:00
return stop;
2024-04-01 08:13:57 -05:00
}
2023-12-28 07:57:22 -06:00
var Tween = {
default: {
2024-04-14 14:53:41 -05:00
loop: "hold",
2023-12-28 07:57:22 -06:00
/*
loop types
none: when done, return to first value
hold: hold last value of tween
restart: restart at beginning, looping
yoyo: go up and then back down
circle: go up and back down, looped
*/
time: 1, /* seconds to do */
ease: Ease.linear,
whole: true, /* True if time is for the entire tween, false if each stage */
cb: function(){},
},
start(obj, target, tvals, options)
{
var defn = Object.create(this.default);
Object.assign(defn, options);
if (defn.loop === 'circle')
tvals.push(tvals[0]);
else if (defn.loop === 'yoyo') {
for (var i = tvals.length-2; i >= 0; i--)
tvals.push(tvals[i]);
}
defn.accum = 0;
var slices = tvals.length - 1;
var slicelen = 1 / slices;
defn.fn = function(dt) {
defn.accum += dt;
if (defn.accum >= defn.time && defn.loop === 'hold') {
2024-01-01 07:44:43 -06:00
if (typeof target === 'string')
obj[target] = tvals[tvals.length-1];
2024-04-01 08:13:57 -05:00
else
target(tvals[tvals.length-1]);
2024-01-01 07:44:43 -06:00
2023-12-28 07:57:22 -06:00
defn.pause();
2024-04-01 08:13:57 -05:00
defn.cb.call(obj);
return;
2023-12-28 07:57:22 -06:00
}
defn.pct = (defn.accum % defn.time) / defn.time;
if (defn.loop === 'none' && defn.accum >= defn.time)
defn.stop();
var t = defn.whole ? defn.ease(defn.pct) : defn.pct;
var nval = t / slicelen;
var i = Math.trunc(nval);
nval -= i;
if (!defn.whole)
nval = defn.ease(nval);
2024-01-01 07:44:43 -06:00
if (typeof target === 'string')
obj[target] = tvals[i].lerp(tvals[i+1], nval);
else
target(tvals[i].lerp(tvals[i+1],nval));
2023-12-28 07:57:22 -06:00
};
var playing = false;
defn.play = function() {
if (playing) return;
2024-01-03 14:26:42 -06:00
defn._end = Register.update.register(defn.fn.bind(defn));
2023-12-28 07:57:22 -06:00
playing = true;
};
defn.restart = function() {
defn.accum = 0;
2024-01-01 07:44:43 -06:00
if (typeof target === 'string')
obj[target] = tvals[0];
else
target(tvals[0]);
2023-12-28 07:57:22 -06:00
};
defn.stop = function() { if (!playing) return; defn.pause(); defn.restart(); };
defn.pause = function() {
2024-01-03 14:26:42 -06:00
defn._end();
2023-12-28 07:57:22 -06:00
if (!playing) return;
playing = false;
};
return defn;
},
};
Tween.make = Tween.start;
2024-04-01 08:13:57 -05:00
return {Tween, Ease, tween};