From b394bcce405873132862e0e84dbb80d1bb336c90 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Tue, 6 Jun 2023 13:07:30 +0000 Subject: [PATCH] tweening --- source/scripts/engine.js | 59 ++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/source/scripts/engine.js b/source/scripts/engine.js index 8637d41..6455c9a 100644 --- a/source/scripts/engine.js +++ b/source/scripts/engine.js @@ -447,40 +447,53 @@ Ease.elastic = { : (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; var Tween = { default: { - ease: "inout", /* easing at end and beginning of tween */ - loop: "restart", /* none, restart, yoyo, increment */ + loop: "restart", /* none, restart, yoyo, circle */ 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); 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; - if (defn.loop === 'yoyo') - defn.fn = function(dt) { - defn.accum += dt; - defn.pct = (defn.accum % (defn.time*2)) / (defn.time*2); + var slices = tvals.length - 1; + var slicelen = 1 / slices; - 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.accum += dt; - defn.pct = (defn.accum % defn.time) / defn.time; - obj[target] = start.lerp(end, defn.ease(defn.pct)); - }; + defn.fn = function(dt) { + defn.accum += dt; + defn.pct = (defn.accum % defn.time) / defn.time; + + 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.stop = function() { defn.pause(); defn.restart(); }; @@ -490,12 +503,6 @@ var Tween = { return defn; }, - - lerp(s, e, t) - { - t = Math.clamp(t,0,1); - return ((e - s) * t) + s; - }, };