Skip to content

armgit / anime

Public Imported from GitHub
0

JavaScript animation engine

Add steps() easing

Julian Garnier49451b253f9f
diff --git a/anime.js b/anime.js
index 833c5735..46815678 100644
--- a/anime.js
+++ b/anime.js
@@ -117,14 +117,13 @@
function elastic(amplitude = 1, period = .5) {
return t => {
const offset = period / (2 * Math.PI) * Math.asin(1 / amplitude);
- return t === 0 || t === 1 ? t : -(amplitude * Math.pow(2, 10 * (t -= 1)) * Math.sin( (t - offset) * (Math.PI * 2) / period));
+ if (t === 0 || t === 1) return t;
+ return -(amplitude * Math.pow(2, 10 * (t -= 1)) * Math.sin( (t - offset) * (Math.PI * 2) / period));
}
}
- function step(steps) {
- return t => {
- Math.round(percentComplete * steps) * (1 / steps) * (endValue - startValue);
- }
+ function steps(steps = 10) {
+ return t => Math.round(t * steps) * (1 / steps);
}
// BezierEasing https://github.com/gre/bezier-easing
@@ -225,7 +224,7 @@
[0.470, 0.000, 0.745, 0.715], /* InSine */
[0.950, 0.050, 0.795, 0.035], /* InExpo */
[0.600, 0.040, 0.980, 0.335], /* InCirc */
- [0.600, -0.280, 0.735, 0.045], /* InBack */
+ [0.600,-0.280, 0.735, 0.045], /* InBack */
elastic /* InElastic */
], Out: [
[0.250, 0.460, 0.450, 0.940], /* OutQuad */
@@ -245,19 +244,21 @@
[0.445, 0.050, 0.550, 0.950], /* InOutSine */
[1.000, 0.000, 0.000, 1.000], /* InOutExpo */
[0.785, 0.135, 0.150, 0.860], /* InOutCirc */
- [0.680, -0.550, 0.265, 1.550], /* InOutBack */
+ [0.680,-0.550, 0.265, 1.550], /* InOutBack */
(a, p) => t => t < .5 ? elastic(a, p)(t * 2) / 2 : 1 - elastic(a, p)(t * -2 + 2) / 2 /* InOutElastic */
]
}
let eases = {
- linear: [0.250, 0.250, 0.750, 0.750]
+ linear: [0.250, 0.250, 0.750, 0.750],
+ ease: [0.250, 0.100, 0.250, 1.000],
+ in: [0.420, 0.000, 1.000, 1.000],
+ out: [0.000, 0.000, 0.580, 1.000],
+ inOut: [0.420, 0.000, 0.580, 1.000]
}
for (let type in equations) {
- equations[type].forEach((ease, i) => {
- eases['ease'+type+names[i]] = ease;
- });
+ equations[type].forEach((ease, i) => { eases['ease'+type+names[i]] = ease; });
}
return eases;
@@ -272,7 +273,7 @@
switch (name) {
case 'spring' : return spring(string, tween.duration);
case 'cubic-bezier' : return bezier.apply(this, args);
- case 'step' : return step.apply(this, args);
+ case 'steps' : return steps.apply(this, args);
default : return is.fnc(ease) ? ease.apply(this, args) : bezier.apply(this, ease);
}
}
diff --git a/documentation/examples/anime-easings.html b/documentation/examples/anime-easings.html
index 799c57f8..10cbb81f 100644
--- a/documentation/examples/anime-easings.html
+++ b/documentation/examples/anime-easings.html
@@ -36,6 +36,12 @@
margin: 10px;
}
+ .sprite {
+ width: 200px;
+ height: 300px;
+ background-image: url(img/sprite-example.png);
+ }
+
/* .color-02 {
animation-name: spring;
animation-duration: 10s;
@@ -57,6 +63,7 @@
</style>
</head>
<body>
+ <div class="sprite"></div>
<div class="item color-01"></div>
<div class="item color-02"></div>
<div class="item color-03"></div>
@@ -65,22 +72,23 @@
<script>
// anime.speed = .1;
var anim01 = anime({
- targets: '.color-01',
- translateX: 1000,
- // easing: 'spring 1 100 10 0'
- easing: 'out-elastic'
- })
- var anim02 = anime({
- targets: '.color-02',
- translateX: 1000,
- easing: 'spring(10, 20, 10, 10)'
+ targets: '.sprite',
+ backgroundPosition: '-2000px 0px',
+ easing: 'steps(10)',
+ duration: 10000,
+ loop: true
})
- var anim03 = anime({
- targets: '.color-03',
- translateX: 1000,
- easing: 'in-out-elastic'
- })
- console.log(anim01);
+ // var anim02 = anime({
+ // targets: '.color-02',
+ // translateX: 1000,
+ // easing: 'spring(10, 20, 10, 10)'
+ // })
+ // var anim03 = anime({
+ // targets: '.color-03',
+ // translateX: 1000,
+ // easing: 'in-out-elastic'
+ // })
+ // console.log(anim01);
// anime({
// targets: '.color-03',
// translateX: 1000,
diff --git a/documentation/examples/anime-infinite-animation.html b/documentation/examples/anime-infinite-animation.html
index b1dc4936..87145b1e 100644
--- a/documentation/examples/anime-infinite-animation.html
+++ b/documentation/examples/anime-infinite-animation.html
@@ -51,7 +51,7 @@
var rows = 22;
var numberOfEls = rows * rows;
var duration = 1000;
- var stagger =duration / rows / 10;
+ var stagger = duration / rows / 10;
var midScreenX = window.innerWidth / 2;
var midScreenY = window.innerHeight / 2;
var radius = Math.sqrt(midScreenX * midScreenX + midScreenY * midScreenY);
@@ -67,7 +67,7 @@
el.classList.add('particule');
el.style.backgroundColor = 'hsl(' + hue + ', 100%, 50%)';
- anime({
+ var anim = anime({
targets: el,
translateY: '1em',
scale: [.5, .1],
diff --git a/documentation/examples/img/sprite-example.png b/documentation/examples/img/sprite-example.png
new file mode 100644
index 00000000..d556af8d
Binary files /dev/null and b/documentation/examples/img/sprite-example.png differ