JavaScript animation engine
timeOffset refactoring / timeline cleanup
Julian Garnier5fb19335e2f4
diff --git a/anime.js b/anime.jsindex e1cf5684..833c5735 100644--- a/anime.js+++ b/anime.js@@ -1,7 +1,7 @@/*** http://animejs.com* JavaScript animation engine- * @version v2.2.1+ * @version v3.0.0* @author Julian Garnier* @copyright ©2017 Julian Garnier* Released under the MIT license@@ -39,7 +39,7 @@duration: 1000,delay: 0,endDelay: 0,- easing: 'out-elastic',+ easing: 'easeOutElastic',round: 0}@@ -658,7 +658,7 @@const unit = getUnit(to) || getUnit(from) || getUnit(originalValue);tween.from = decomposeValue(from, unit);tween.to = decomposeValue(to, unit);- tween.start = previousTween ? previousTween.end : prop.timeOffset;+ tween.start = previousTween ? previousTween.end : 0;tween.end = tween.start + tween.delay + tween.duration + tween.endDelay;tween.easing = parseEasings(tween);tween.isPath = is.pth(tweenValue);@@ -728,12 +728,12 @@// Create Instance- function getInstanceTimings(type, animations, instanceSettings, tweenSettings) {+ function getInstanceTimings(type, animations, tweenSettings) {const isDelay = (type === 'delay');if (animations.length) {return (isDelay ? Math.min : Math.max).apply(Math, animations.map(anim => anim[type]));} else {- return isDelay ? tweenSettings.delay : instanceSettings.timeOffset + tweenSettings.delay + tweenSettings.duration;+ return isDelay ? tweenSettings.delay : tweenSettings.delay + tweenSettings.duration;}}@@ -747,8 +747,8 @@children: [],animatables: animatables,animations: animations,- duration: getInstanceTimings('duration', animations, instanceSettings, tweenSettings),- delay: getInstanceTimings('delay', animations, instanceSettings, tweenSettings)+ duration: getInstanceTimings('duration', animations, tweenSettings),+ delay: getInstanceTimings('delay', animations, tweenSettings)});}@@ -801,22 +801,20 @@function toggleInstanceDirection() {instance.reversed = !instance.reversed;- const children = instance.children;- children.forEach(child => child.reversed = instance.reversed);+ instance.children.forEach(child => child.reversed = instance.reversed);}function adjustTime(time) {return instance.reversed ? instance.duration - time : time;}- function syncInstanceChildren(time, manual) {+ function syncInstanceChildren(time) {const children = instance.children;const childrenLength = children.length;- if (!manual || time >= instance.currentTime) {- for (let i = 0; i < childrenLength; i++) children[i].seek(time);+ if (time >= instance.currentTime) {+ for (let i = 0; i < childrenLength; i++) children[i].seek(time - children[i].timeOffset);} else {- // Manual backward seeking requires looping in reverse- for (let i = childrenLength; i--;) children[i].seek(time);+ for (let i = childrenLength; i--;) children[i].seek(time - children[i].timeOffset);}}@@ -837,8 +835,8 @@const strings = tween.to.strings;const round = tween.round;const numbers = [];- let progress;const toNumbersLength = tween.to.numbers.length;+ let progress;for (let n = 0; n < toNumbersLength; n++) {let value;const toNumber = tween.to.numbers[n];@@ -892,22 +890,22 @@}}- function setInstanceProgress(engineTime, manual) {- const insOffset = instance.timeOffset;+ function setInstanceProgress(engineTime) {const insDuration = instance.duration;+ const insDelay = instance.delay;const insTime = adjustTime(engineTime);- if (instance.children.length) syncInstanceChildren(insTime, manual);- if (insTime >= (insOffset + instance.delay) || !insDuration) {+ if (instance.children.length) syncInstanceChildren(insTime);+ if (insTime >= insDelay || !insDuration) {if (!instance.began) {instance.began = true;setCallback('begin');}setCallback('run');}- if (insTime > insOffset && insTime < insDuration) {+ if (insTime > insDelay && insTime < insDuration) {setAnimationsProgress(insTime);} else {- if (insTime <= insOffset && instance.currentTime !== 0) {+ if (insTime <= insDelay && instance.currentTime !== 0) {setAnimationsProgress(0);if (instance.reversed) countIteration();}@@ -920,6 +918,7 @@if (engineTime >= insDuration) {if (instance.remaining) {startTime = now;+ instance.looped = true;if (instance.direction === 'alternate') toggleInstanceDirection();} else {instance.paused = true;@@ -944,6 +943,7 @@instance.paused = true;instance.began = false;instance.completed = false;+ instance.looped = false;instance.reversed = direction === 'reverse';instance.remaining = direction === 'alternate' && loops === 1 ? 2 : loops;setAnimationsProgress(0);@@ -955,12 +955,13 @@instance.tick = function(t) {now = t;if (!startTime) startTime = now;- const engineTime = (lastTime + now - startTime) * anime.speed;+ const offset = instance.looped ? 0 : -instance.timeOffset;+ const engineTime = (lastTime + now - startTime + offset) * anime.speed;setInstanceProgress(engineTime);}instance.seek = function(time) {- setInstanceProgress(adjustTime(time), true);+ setInstanceProgress(adjustTime(time));}instance.pause = function() {@@ -1032,20 +1033,21 @@tl.pause();tl.duration = 0;tl.add = function(instanceParams, timeOffset) {- tl.children.forEach(i => { i.began = true; i.completed = true; });+ function passThrough(ins) { ins.began = true; ins.completed = true; };+ tl.children.forEach(passThrough);let insParams = mergeObjects(instanceParams, replaceObjectProps(defaultTweenSettings, params));insParams.targets = insParams.targets || params.targets;const tlDuration = tl.duration;insParams.autoplay = false;insParams.direction = tl.direction;insParams.timeOffset = is.und(timeOffset) ? tlDuration : getRelativeValue(timeOffset, tlDuration);- tl.began = true;- tl.completed = true;+ passThrough(tl);tl.seek(insParams.timeOffset);const ins = anime(insParams);- ins.began = true;- ins.completed = true;- if (ins.duration > tlDuration) tl.duration = ins.duration;+ passThrough(ins);+ const totalDuration = ins.duration + insParams.timeOffset;+ if (is.fnc(tl.delay)) tl.delay = ins.delay;+ if (is.fnc(tlDuration) || totalDuration > tlDuration) tl.duration = totalDuration;tl.children.push(ins);tl.seek(0);tl.reset();diff --git a/documentation/examples/anime-infinite-animation.html b/documentation/examples/anime-infinite-animation.htmlnew file mode 100644index 00000000..b1dc4936--- /dev/null+++ b/documentation/examples/anime-infinite-animation.html@@ -0,0 +1,89 @@+<!DOCTYPE html>+<html>+<head>+ <title>domaku | anime.js</title>+ <meta charset="utf-8">+ <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">+ <meta property="og:title" content="anime.js">+ <meta property="og:url" content="http://animejs.com">+ <meta property="og:description" content="Javascript Animation Engine">+ <meta property="og:image" content="http://animejs.com/documentation/assets/img/icons/og.png">+ <meta name="twitter:card" content="summary">+ <meta name="twitter:title" content="anime.js">+ <meta name="twitter:site" content="@juliangarnier">+ <meta name="twitter:description" content="Javascript Animation Engine">+ <meta name="twitter:image" content="http://animejs.com/documentation/assets/img/icons/twitter.png">+ <link rel="apple-touch-icon-precomposed" href="../assets/img/icons/apple-touch-icon.png">+ <link rel="icon" type="image/png" href="../assets/img/icons/favicon.png">+ <link href="../assets/css/anime.css" rel="stylesheet">+ <script src="../../anime.js"></script>+ <!-- <script src="../assets/js/anime.3.0.0-alpha.js"></script> -->+ <!-- <script src="../assets/js/anime.2.2.0.js"></script> -->+ <!-- <script src="../assets/js/anime.2.0.1.js"></script> -->+ <!-- <script src="../assets/js/anime.1.3.js"></script> -->+ <style>+ body {+ overflow: hidden;+ position: absolute;+ width: 100%;+ height: 100%;+ display: flex;+ justify-content: center;+ align-items: center;+ font-size: 24px;+ }+ .wrapper {+ display: flex;+ flex-direction: column;+ flex-wrap: wrap;+ }+ .particule {+ width: 1em;+ height: 1em;+ border-radius: 50%;+ }+ </style>+</head>+<body>+ <div class="wrapper"></div>+ <script>++ var rows = 22;+ var numberOfEls = rows * rows;+ var duration = 1000;+ var stagger =duration / rows / 10;+ var midScreenX = window.innerWidth / 2;+ var midScreenY = window.innerHeight / 2;+ var radius = Math.sqrt(midScreenX * midScreenX + midScreenY * midScreenY);+ var fragment = document.createDocumentFragment();+ var wrapperEl = document.querySelector('.wrapper');++ wrapperEl.style.width = rows + 'em';+ wrapperEl.style.height = rows + 'em';++ for (var i = 0; i < numberOfEls; i++) {+ var hue = 10 + Math.round(40 / numberOfEls * i);+ var el = document.createElement('div');+ el.classList.add('particule');+ el.style.backgroundColor = 'hsl(' + hue + ', 100%, 50%)';++ anime({+ targets: el,+ translateY: '1em',+ scale: [.5, .1],+ direction: 'alternate',+ loop: true,+ easing: 'easeInOutSine',+ duration: duration,+ timeOffset: i * stagger+ });++ fragment.appendChild(el);+ }++ wrapperEl.appendChild(fragment);++ </script>+ <script src="../assets/js/stats.js"></script>+</body>+</html>diff --git a/documentation/examples/anime-speed-test.html b/documentation/examples/anime-speed-test.htmlindex 77534ffb..03ea266b 100644--- a/documentation/examples/anime-speed-test.html+++ b/documentation/examples/anime-speed-test.html@@ -30,54 +30,50 @@display: flex;justify-content: center;align-items: center;- font-size: 20px;+ padding: 8em;+ font-size: 24px;}.wrapper {display: flex;+ flex-direction: column;flex-wrap: wrap;- width: 30em;- height: 30em;}.particule {- width: 1em;- height: 1em;+ width: 1px;+ height: 10em;+ margin-left: 1px;+ transform-origin: 50 50%;}</style></head><body>- <div class="wrapper"></div><script>- var numberOfEls = 900;- var duration = 1000;- var stagger = duration / numberOfEls;+ var numberOfEls = 500;+ var duration = 4000;var midScreenX = window.innerWidth / 2;var midScreenY = window.innerHeight / 2;var radius = Math.sqrt(midScreenX * midScreenX + midScreenY * midScreenY);var fragment = document.createDocumentFragment();- var wrapperEl = document.querySelector('.wrapper');for (var i = 0; i < numberOfEls; i++) {- var hue = 0 + Math.round(40 / numberOfEls * i);+ var hue = 10 + Math.round(40 / numberOfEls * i);var el = document.createElement('div');el.classList.add('particule');- el.style.backgroundColor = 'hsl(' + hue + ', 100%, 40%)';+ el.style.backgroundColor = 'hsl(' + hue + ', 100%, 50%)';anime({targets: el,- translateX: '1em',- translateY: '1em',- scale: .5,- rotate: 90,- direction: 'alternate',+ rotate: [i * 1, '+=360'],loop: true,- easing: 'easeInOutSine',- timeOffset: i * stagger+ easing: 'linear',+ duration: duration});+fragment.appendChild(el);}- wrapperEl.appendChild(fragment);+ document.body.appendChild(fragment);</script><script src="../assets/js/stats.js"></script>diff --git a/documentation/examples/anime-timeline.html b/documentation/examples/anime-timeline.htmlindex 33763e09..7152bd56 100644--- a/documentation/examples/anime-timeline.html+++ b/documentation/examples/anime-timeline.html@@ -149,6 +149,8 @@complete: function() { console.log('animation 5 completed')}});+ console.log(tl);+controlsProgressEl.addEventListener('input', function() {tl.seek(tl.duration * (controlsProgressEl.value / 100));});