Skip to content

armgit / anime

Public Imported from GitHub
0

JavaScript animation engine

Add values operator

Julian Garnier9fca4b2f3e94
diff --git a/anime.js b/anime.js
index 0fc71b91..22db10f4 100644
--- a/anime.js
+++ b/anime.js
@@ -372,6 +372,17 @@
return target[propName] || 0;
}
+ function checkValueOperator(value) {
+ const matches = /^(\*=|\+=|-=)/.exec(value);
+ if (matches) return matches[0];
+ }
+
+ const operations = {
+ '+': (x, y) => x + y,
+ '-': (x, y) => x - y,
+ '*': (x, y) => x * y
+ }
+
function validateValue(val, unit) {
if (is.col(val)) return colorToRgb(val);
const originalUnit = getUnit(val);
@@ -513,8 +524,10 @@
const originalValue = getOriginalTargetValue(animatable.target, prop.name);
const previousValue = previousTween ? previousTween.to.original : originalValue;
const from = is.arr(tweenValue) ? tweenValue[0] : previousValue;
- const to = is.arr(tweenValue) ? tweenValue[1] : tweenValue;
+ let to = is.arr(tweenValue) ? tweenValue[1] : tweenValue;
const unit = getUnit(to) || getUnit(from) || getUnit(originalValue);
+ const operator = checkValueOperator(to);
+ if (operator) to = operations[operator[0]](parseFloat(from), parseFloat(to.replace(operator, '')));
tween.from = decomposeValue(from, unit);
tween.to = decomposeValue(to, unit);
tween.start = previousTween ? previousTween.end + tween.delay : tween.delay;
diff --git a/documentation/index.html b/documentation/index.html
index 07531adf..98558ea8 100644
--- a/documentation/index.html
+++ b/documentation/index.html
@@ -910,6 +910,33 @@ var hsl = anime({
</script>
</div>
+<div id="operations" class="demo">
+ <h3 class="demo-title">Operator prefixed value</h3>
+ <div class="demo-content">
+ <div class="square shadow"></div>
+ <div class="square el" style="transform: translateX(100px)"></div>
+ </div>
+<script>var operations = anime({
+ targets: '#operations .el',
+ translateX: {
+ value: '+=150',
+ duration: 1000
+ },
+ width: {
+ value: '-=10',
+ duration: 1800,
+ easing: 'easeInOutSine'
+ },
+ height: {
+ value: '*=4',
+ duration: 1800,
+ easing: 'easeInOutSine'
+ },
+ direction: 'alternate'
+});
+</script>
+</div>
+
<div id="specificTimingsDemo" class="demo">
<h3 class="demo-title">Specific timings value</h3>
<div class="demo-content">