Skip to content

armgit / anime

Public Imported from GitHub
0

JavaScript animation engine

Initial states implementation

Julian Garnierd98c5b183603
diff --git a/anime.js b/anime.js
index b39b2240..1305efa6 100644
--- a/anime.js
+++ b/anime.js
@@ -128,7 +128,7 @@
return t => {
const a = Math.max(amplitude, 1);
return (t === 0 || t === 1) ? t :
- -a * Math.pow(2, 10 * (t - 1)) * Math.sin((((t - 1) - (period / (2 * Math.PI) * Math.asin(1 / a))) * (Math.PI * 2)) / period);
+ -a * Math.pow(2, 10 * (t - 1)) * Math.sin((((t - 1) - (period / (Math.PI * 2) * Math.asin(1 / a))) * (Math.PI * 2)) / period);
}
}
@@ -426,6 +426,10 @@
return val(animatable.target, animatable.id, animatable.total);
}
+ function getAttribute(el, prop) {
+ return el.getAttribute(prop);
+ }
+
function getCSSValue(el, prop) {
if (prop in el.style) {
return el.style[prop] ||
@@ -435,7 +439,7 @@
function getAnimationType(el, prop) {
if (is.dom(el) && arrayContains(validTransforms, prop)) return 'transform';
- if (is.dom(el) && (el.getAttribute(prop) || (is.svg(el) && el[prop]))) return 'attribute';
+ if (is.dom(el) && (getAttribute(el, prop) || (is.svg(el) && el[prop]))) return 'attribute';
if (is.dom(el) && (prop !== 'transform' && getCSSValue(el, prop))) return 'css';
if (el[prop] != null) return 'object';
}
@@ -461,7 +465,7 @@
switch (getAnimationType(target, propName)) {
case 'transform': return getTransformValue(target, propName, animatable);
case 'css': return getCSSValue(target, propName);
- case 'attribute': return target.getAttribute(propName);
+ case 'attribute': return getAttribute(target, propName);
default: return target[propName] || 0;
}
}
@@ -494,17 +498,17 @@
}
function getCircleLength(el) {
- return 2 * Math.PI * el.getAttribute('r');
+ return Math.PI * 2 * getAttribute(el, 'r');
}
function getRectLength(el) {
- return (el.getAttribute('width') * 2) + (el.getAttribute('height') * 2);
+ return (getAttribute(el, 'width') * 2) + (getAttribute(el, 'height') * 2);
}
function getLineLength(el) {
return getDistance(
- {x: el.getAttribute('x1'), y: el.getAttribute('y1')},
- {x: el.getAttribute('x2'), y: el.getAttribute('y2')}
+ {x: getAttribute(el, 'x1'), y: getAttribute(el, 'y1')},
+ {x: getAttribute(el, 'x2'), y: getAttribute(el, 'y2')}
);
}
@@ -739,6 +743,7 @@
const properties = getProperties(instanceSettings, tweenSettings, params);
const animations = getAnimations(animatables, properties);
return mergeObjects(instanceSettings, {
+ states: {default: params},
children: [],
animatables: animatables,
animations: animations,
@@ -996,6 +1001,15 @@
instance.play();
}
+ instance.goTo = function(stateName, bypassAnimation) {
+ const nextState = instance.states[stateName];
+ const defaultState = instance.states.default;
+ const params = mergeObjects(nextState, defaultState);
+ anime.remove(defaultState.targets);
+ const animation = anime(params);
+ if (bypassAnimation) animation.seek(animation.duration);
+ }
+
instance.finished = promise;
instance.reset();
@@ -1004,7 +1018,7 @@
setInstanceProgress(instance.startTime - (instance.duration * Math.floor(instance.startTime / instance.duration)));
}
- if (instance.autoplay) instance.play();
+ if (instance.autoplay && instance.animations.length) instance.play();
return instance;
diff --git a/documentation/examples/anime-states.html b/documentation/examples/anime-states.html
new file mode 100644
index 00000000..f5662e24
--- /dev/null
+++ b/documentation/examples/anime-states.html
@@ -0,0 +1,74 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>Tests | 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.2.0.2.js"></script> -->
+ <!-- <script src="../assets/js/anime.1.3.js"></script> -->
+ <style>
+ <style>
+ body {
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ min-height: 100vh;
+ }
+ .item {
+ width: 100px;
+ height: 100px;
+ border-radius: 50%;
+ background-color: currentColor;
+ margin: 10px;
+ }
+ </style>
+</head>
+<body>
+ <div class="item color-01"></div>
+ <div class="item color-02"></div>
+ <div class="item color-03"></div>
+ <script>
+
+ var animation = anime({
+ targets: '.item',
+ duration: 2000,
+ easing: 'easeOutExpo',
+ update: function() {
+ console.log('updating')
+ }
+ })
+
+ animation.states.left = {
+ translateX: -100,
+ duration: 1000,
+ easing: 'easeInOutQuad'
+ }
+
+ animation.states.right = {
+ translateX: 100
+ }
+
+ animation.states.top = {
+ translateY: -100
+ }
+
+ animation.states.bottom = {
+ translateY: 100
+ }
+
+ </script>
+</body>
+</html>
\ No newline at end of file
diff --git a/documentation/examples/anime-tests.html b/documentation/examples/anime-tests.html
index 0ad5b38a..09fd817e 100644
--- a/documentation/examples/anime-tests.html
+++ b/documentation/examples/anime-tests.html
@@ -61,7 +61,7 @@
color02El.addEventListener('mouseenter', function() {
anime.remove(this);
}, false);
- console.log(animation.animations[0].tweens[0].hasDuration);
+ console.log(animation);
</script>
</body>
</html>
\ No newline at end of file