tweening
This commit is contained in:
parent
0905bdb4fd
commit
b394bcce40
|
@ -447,39 +447,52 @@ Ease.elastic = {
|
||||||
: (Math.pow(2, -20 * t + 10) * Math.sin((20 * t - 11.125) * this.c5)) / 2 + 1;
|
: (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.c4 = 2*Math.PI/3;
|
||||||
Ease.elastic.c5 = 2*Math.PI / 4.5;
|
Ease.elastic.c5 = 2*Math.PI / 4.5;
|
||||||
|
|
||||||
var Tween = {
|
var Tween = {
|
||||||
default: {
|
default: {
|
||||||
ease: "inout", /* easing at end and beginning of tween */
|
loop: "restart", /* none, restart, yoyo, circle */
|
||||||
loop: "restart", /* none, restart, yoyo, increment */
|
|
||||||
time: 1, /* seconds to do */
|
time: 1, /* seconds to do */
|
||||||
ease: Ease.linear
|
ease: Ease.linear,
|
||||||
|
whole: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
start(obj, target, start, end, options)
|
start(obj, target, tvals, options)
|
||||||
{
|
{
|
||||||
|
var start = tvals[0];
|
||||||
|
var end = tvals[1];
|
||||||
|
|
||||||
var defn = Object.create(this.default);
|
var defn = Object.create(this.default);
|
||||||
Object.assign(defn, options);
|
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;
|
defn.accum = 0;
|
||||||
|
|
||||||
if (defn.loop === 'yoyo')
|
var slices = tvals.length - 1;
|
||||||
defn.fn = function(dt) {
|
var slicelen = 1 / slices;
|
||||||
defn.accum += dt;
|
|
||||||
defn.pct = (defn.accum % (defn.time*2)) / (defn.time*2);
|
|
||||||
|
|
||||||
if (defn.pct < 0.5)
|
|
||||||
obj[target] = start.lerp(end, defn.ease(defn.pct/0.5));
|
|
||||||
else
|
|
||||||
obj[target] = end.lerp(start, defn.ease((defn.pct-0.5)/0.5));
|
|
||||||
};
|
|
||||||
else
|
|
||||||
defn.fn = function(dt) {
|
defn.fn = function(dt) {
|
||||||
defn.accum += dt;
|
defn.accum += dt;
|
||||||
defn.pct = (defn.accum % defn.time) / defn.time;
|
defn.pct = (defn.accum % defn.time) / defn.time;
|
||||||
obj[target] = start.lerp(end, defn.ease(defn.pct));
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
obj[target] = tvals[i].lerp(tvals[i+1], nval);
|
||||||
};
|
};
|
||||||
|
|
||||||
defn.restart = function() { defn.accum = 0; };
|
defn.restart = function() { defn.accum = 0; };
|
||||||
|
@ -490,12 +503,6 @@ var Tween = {
|
||||||
|
|
||||||
return defn;
|
return defn;
|
||||||
},
|
},
|
||||||
|
|
||||||
lerp(s, e, t)
|
|
||||||
{
|
|
||||||
t = Math.clamp(t,0,1);
|
|
||||||
return ((e - s) * t) + s;
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue