Skip to content

armgit / anime

Public Imported from GitHub
0

JavaScript animation engine

Viewbox aware and responsive svg animations

Peter574dd4c8e9a4
diff --git a/anime.js b/anime.js
index 64730fac..453c693c 100644
--- a/anime.js
+++ b/anime.js
@@ -52,6 +52,21 @@
return str.indexOf(text) > -1;
}
+ if (!Element.prototype.matches)
+ Element.prototype.matches = Element.prototype.msMatchesSelector ||
+ Element.prototype.webkitMatchesSelector;
+
+ if (!Element.prototype.closest)
+ Element.prototype.closest = function(s) {
+ var el = this;
+ if (!document.documentElement.contains(el)) return null;
+ do {
+ if (el.matches(s)) return el;
+ el = el.parentElement || el.parentNode;
+ } while (el !== null);
+ return null;
+ };
+
const is = {
arr: a => Array.isArray(a),
obj: a => stringContains(Object.prototype.toString.call(a), 'Object'),
@@ -475,9 +490,19 @@
function getPath(path, percent) {
const el = is.str(path) ? selectString(path)[0] : path;
const p = percent || 100;
+ const svg = el.closest("svg");
+ const viewBox = svg.viewBox.baseVal;
+
+ const offsetX = (typeof viewBox !== 'undefined' ) ? 0 - viewBox.x : 0;
+ const offsetY = (typeof viewBox !== 'undefined' ) ? 0 - viewBox.y : 0;
+
return function(prop) {
return {
el: el,
+ svg: svg,
+ viewBox: viewBox,
+ offsetX: offsetX,
+ offsetY: offsetY,
property: prop,
totalLength: getTotalLength(el) * (p / 100)
}
@@ -489,12 +514,20 @@
const l = progress + offset >= 1 ? progress + offset : 0;
return path.el.getPointAtLength(l);
}
+
+ var scaleX = 1;
+ var scaleY = 1;
+ if (typeof path.viewBox !== 'undefined' ) {
+ scaleX = (path.svg.width.baseVal.value / path.viewBox.width);
+ scaleY = (path.svg.height.baseVal.value / path.viewBox.height);
+ }
+
const p = point();
const p0 = point(-1);
const p1 = point(+1);
switch (path.property) {
- case 'x': return p.x;
- case 'y': return p.y;
+ case 'x': return (p.x + path.offsetX) * scaleX;
+ case 'y': return (p.y + path.offsetY) * scaleY;
case 'angle': return Math.atan2(p1.y - p0.y, p1.x - p0.x) * 180 / Math.PI;
}
}