Skip to content

armgit / anime

Public Imported from GitHub
0

JavaScript animation engine

Update timeline example

Julian Garnierd21a829aae96
diff --git a/documentation/assets/js/anime.player.js b/documentation/assets/js/anime.player.js
new file mode 100644
index 00000000..8bab792e
--- /dev/null
+++ b/documentation/assets/js/anime.player.js
@@ -0,0 +1,124 @@
+import anime from '../../../src/index.js';
+
+function animePlayer(instance, containerEl) {
+
+ function createEl(type, className, parentEl) {
+ var el = document.createElement(type);
+ if (className) el.classList.add(className);
+ if (parentEl) parentEl.appendChild(el);
+ return el;
+ }
+
+ function createButton(value, prentEl, action) {
+ var el = createEl('button', 'ap-button');
+ if (parentEl) parentEl.appendChild(el);
+ if (action) el.addEventListener('click', action);
+ return el;
+ }
+
+ var parentEl = containerEl || document.body;
+ var timelineEl = createEl('div', 'ap-timeline', parentEl);
+ var needleEl = createEl('div', 'ap-needle', timelineEl);
+ var animations = [];
+ var colors = ['#FF1461','#FF7C72','#FBF38C','#A6FF8F','#18FF92','#1CE2B2','#5EF3FB','#61C3FF','#5A87FF','#8453E3','#C26EFF','#FB89FB'];
+ var colorIndex = 0;
+
+ anime.setValue(timelineEl, {
+ overflow: 'auto',
+ display: 'block',
+ position: 'absolute',
+ left: 0,
+ bottom: 0,
+ width: '100%',
+ height: '100%',
+ maxHeight: '50vh',
+ paddingTop: 4
+ });
+
+ function convertMStoEM(ms) { return ms / 100; }
+ function convertEMtoMS(em) { return parseFloat(em) * 250; }
+
+ function createAnimationLog(animObj, timelineOffset) {
+ var anim = animObj;
+ anim.player = {};
+ anim.player.trackEl = createEl('div', 'ap-track', timelineEl);
+ anim.player.animationEl = createEl('div', 'ap-animation', anim.player.trackEl);
+ anim.player.delayEl = createEl('div', 'ap-delay', anim.player.animationEl);
+ anim.player.endDelayEl = createEl('div', 'ap-delay', anim.player.animationEl);
+ anime.setValue(anim.player.trackEl, {
+ position: 'relative',
+ width: '100%',
+ height: 4,
+ marginBottom: 2,
+ });
+ anime.setValue(anim.player.animationEl, {
+ position: 'relative',
+ display: 'flex',
+ justifyContent: 'space-between',
+ width: 'auto',
+ height: '100%',
+ backgroundColor: 'currentColor',
+ borderRadius: '.5rem'
+ });
+ anime.setValue([anim.player.delayEl, anim.player.endDelayEl], {
+ width: '100%',
+ height: '100%',
+ backgroundColor: 'rgba(0,0,0,.5)'
+ });
+ anim.update = function() {
+ anime.setValue(anim.player.animationEl, {
+ left: convertMStoEM(timelineOffset) + 'em',
+ width: convertMStoEM(anim.duration) + 'em'
+ });
+ anime.setValue(anim.player.delayEl, {width: (anim.delay / anim.duration) * 100 + '%'});
+ anime.setValue(anim.player.endDelayEl, {width: (anim.endDelay / anim.duration) * 100 + '%'});
+ }
+ anime.setValue(anim.player.animationEl, {color: colors[colorIndex]});
+ colorIndex++;
+ if (!colors[colorIndex]) colorIndex = 0;
+ anim.update();
+ animations.push(anim);
+ return anim;
+ }
+
+ instance.pause();
+
+ var playerAnimation = anime({
+ targets: needleEl,
+ translateX: convertMStoEM(instance.duration) + 'em',
+ duration: instance.duration,
+ direction: instance.direction,
+ loop: instance.loop,
+ easing: 'linear',
+ update: function(a) {
+ instance.seek(a.currentTime);
+ }
+ });
+
+ if (instance.children.length) {
+ instance.children.forEach(function(child) {
+ console.log(child.timelineOffset);
+ child.animations.forEach(function(anim) {
+ createAnimationLog(anim, child.timelineOffset);
+ });
+ })
+ } else {
+ instance.animations.forEach(function(anim) {
+ createAnimationLog(anim);
+ });
+ }
+
+ anime.setValue(needleEl, {
+ position: 'absolute',
+ zIndex: 2,
+ top: 0,
+ left: 0,
+ width: 2,
+ height: timelineEl.scrollHeight,
+ marginLeft: -1,
+ backgroundColor: '#FFF'
+ });
+
+}
+
+export default animePlayer;
\ No newline at end of file
diff --git a/documentation/examples/anime-timeline.html b/documentation/examples/anime-timeline.html
index f23e9274..42451227 100644
--- a/documentation/examples/anime-timeline.html
+++ b/documentation/examples/anime-timeline.html
@@ -21,13 +21,23 @@
<style>
body {
+ position: absolute;
+ width: 100%;
+ height: 100%;
+ }
+
+ .debug,
+ .demo {
+ position: relative;
+ width: 100%;
+ height: 50vh;
+ }
+
+ .demo {
display: flex;
flex-wrap: no-wrap;
justify-content: space-around;
align-items: center;
- position: absolute;
- width: 100%;
- height: 100%;
}
.el {
@@ -37,14 +47,23 @@
margin: 10px;
}
+ .player {
+ position: relative;
+ top: 50px;
+ height: calc(50vh - 50px);
+ }
+
.controls {
+ opacity: 1;
display: flex;
- position: fixed;
+ position: absolute;
right: 0;
- bottom: 0;
+ top: 0;
left: 0;
- height: 100px;
- background: white;
+ height: 46px;
+ padding: 0 10px 0 10px;
+ background: #000;
+ color: #FFF;
}
.buttons {
@@ -58,7 +77,6 @@
.timeline {
display: flex;
flex-grow: 1;
- padding: 20px;
}
.progress {
@@ -69,29 +87,37 @@
</head>
<body>
- <div class="el"></div>
- <div class="el"></div>
- <div class="el"></div>
- <div class="el"></div>
- <div class="el"></div>
- <div class="el"></div>
- <div class="el"></div>
-
- <div class="controls">
- <div class="buttons">
- <button class="play">Play</button>
- <button class="pause">Pause</button>
- <button class="restart">Restart</button>
- </div>
- <div class="timeline">
- <input class="progress" type="range" min="0" max="100" step=".1" value="0">
+ <div class="demo">
+
+ <div class="el"></div>
+ <div class="el"></div>
+ <div class="el"></div>
+ <div class="el"></div>
+ <div class="el"></div>
+ <div class="el"></div>
+ <div class="el"></div>
+
+ </div>
+
+ <div class="debug">
+ <div class="controls">
+ <div class="buttons">
+ <button class="play">Play</button>
+ <button class="pause">Pause</button>
+ <button class="restart">Restart</button>
+ </div>
+ <div class="timeline">
+ <input class="progress" type="range" min="0" max="100" step=".1" value="0">
+ </div>
</div>
+ <div class="player"></div>
</div>
</body>
<script type="module">
import anime from '../../src/index.js';
+ import animePlayer from '../assets/js/anime.player.js'
var els = document.querySelectorAll('.el');
var controlsProgressEl = document.querySelector('.controls .progress');
@@ -99,18 +125,13 @@
var tl = anime.timeline({
targets: els,
direction: 'alternate',
- duration: 250,
+ duration: 800,
delay: function(el, i) { return 75 * i; },
update: function(anim) {
controlsProgressEl.value = anim.progress;
},
- begin: function(anim) {
- console.log('tl begin');
- },
- complete: function(anim) {
- console.log('tl complete');
- },
- autoplay: true
+ autoplay: true,
+ easing: 'easeOutQuad'
});
document.querySelector('.controls .play').onclick = tl.play;
@@ -121,57 +142,39 @@
.add({
translateY: -100,
scale: 8,
- background: '#FF1461',
- begin: function() { console.log('animation 1 began')},
- changeBegin: function() { console.log('animation 1 change began')},
- changeComplete: function() { console.log('animation 1 change completed')},
- complete: function() { console.log('animation 1 completed')}
+ background: '#FF1461'
})
.add({
background: '#18FF92',
rotate: 45,
- begin: function() { console.log('animation 2 began')},
- changeBegin: function() { console.log('animation 2 change began')},
- changeComplete: function() { console.log('animation 2 change completed')},
- complete: function() { console.log('animation 2 completed')}
- })
+ }, '-=400')
.add({
- background: '#5A87FF',
- begin: function() { console.log('animation 3 began')},
- changeBegin: function() { console.log('animation 3 change began')},
- changeComplete: function() { console.log('animation 3 change completed')},
- complete: function() { console.log('animation 3 completed')}
- })
+ background: '#5A87FF'
+ }, '-=400')
.add({
background: '#FBF38C',
rotate: 90,
- begin: function() { console.log('animation 4 began')},
- changeBegin: function() { console.log('animation 4 change began')},
- changeComplete: function() { console.log('animation 4 change completed')},
- complete: function() { console.log('animation 4 completed')}
- })
+ }, '-=400')
.add({
scaleX: 3,
scaleY: .5,
background: '#FBF38C',
- begin: function() { console.log('animation 5 began')},
- changeBegin: function() { console.log('animation 5 change began')},
- changeComplete: function() { console.log('animation 5 change completed')},
- complete: function() { console.log('animation 5 completed')}
+ }, '-=400');
+
+ controlsProgressEl.addEventListener('input', function() {
+ tl.seek(tl.duration * (controlsProgressEl.value / 100));
});
- // controlsProgressEl.addEventListener('input', function() {
- // tl.seek(tl.duration * (controlsProgressEl.value / 100));
- // });
+ for (var i = 0; i < els.length; i++) {
+ els[i].addEventListener('mouseenter', function() {
+ anime.remove(this);
+ this.style.backgroundColor = 'red';
+ }, false);
+ }
- // for (var i = 0; i < els.length; i++) {
- // els[i].addEventListener('mouseenter', function() {
- // anime.remove(this);
- // this.style.backgroundColor = 'red';
- // }, false);
- // }
+ var playerEl = document.querySelector('.player');
-console.log(tl);
+ animePlayer(tl, playerEl);
</script>
<!-- <script src="../assets/js/vendors/stats.min.js"></script> -->
diff --git a/src/index.js b/src/index.js
index cbe973d1..c0fbb5af 100644
--- a/src/index.js
+++ b/src/index.js
@@ -932,6 +932,10 @@ function anime(params = {}) {
child.seek(time - child.timelineOffset);
}
}
+ // for (let i = 0; i < childrenLength; i++) {
+ // const child = children[i];
+ // child.seek(time - child.timelineOffset);
+ // }
}
function setAnimationsProgress(insTime) {
@@ -1038,6 +1042,7 @@ function anime(params = {}) {
setAnimationsProgress(insTime);
} else {
if (insTime <= insDelay && instance.currentTime !== 0) {
+ console.log(instance.id, ' update');
setAnimationsProgress(0);
}
if ((insTime >= insDuration && instance.currentTime !== insDuration) || !insDuration) {