JavaScript animation engine
Responsive path data #301
Julian Garnier14c93d74c375
diff --git a/documentation/examples/anime-svg-lines-animations.html b/documentation/examples/anime-svg-lines-animations.htmlindex 4af74d46..7a402c03 100644--- a/documentation/examples/anime-svg-lines-animations.html+++ b/documentation/examples/anime-svg-lines-animations.html@@ -16,7 +16,6 @@<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/documentation.css" rel="stylesheet">- <script src="../../lib/anime.min.js"></script><style>body {display: flex;@@ -27,10 +26,14 @@width: 100%;height: 100%;}- .anim, .anim svg {+ .anim {position: relative;- width: 600px;- height: 400px;+ width: 50%;+ }+ .anim svg {+ position: relative;+ width: 100%;+ height: auto;}.anim path {stroke-width: 2;@@ -62,7 +65,9 @@</g></svg></div>- <script>+ <!-- <script type="module" src="../../src/index.js"></script> -->+ <script type="module">+ import anime from "../../src/index.js";anime({targets: ['line', 'circle', 'polygon', 'polyline', 'path', 'rect'],strokeDashoffset: [anime.setDashoffset, 0],@@ -76,12 +81,13 @@targets: '.item',translateX: pathEl('x'),translateY: pathEl('y'),+ rotate: pathEl('angle'),easing: 'easeInOutSine',duration: 1500,direction: 'alternate',loop: true});</script>- <script src="../assets/js/vendors/stats.min.js"></script>+ <!-- <script src="../assets/js/vendors/stats.min.js"></script> --></body></html>diff --git a/src/index.js b/src/index.jsindex 273ac054..f73b47c0 100644--- a/src/index.js+++ b/src/index.js@@ -557,6 +557,30 @@ function setDashoffset(el) {// Motion path+function getParentSVG(el) {+ let parentEl = el.parentNode;+ while (is.svg(parentEl)) {+ parentEl = parentEl.parentNode;+ if (!is.svg(parentEl.parentNode)) break;+ }+ return parentEl;+}++function getPathCoords(el) {+ const parentSVG = getParentSVG(el);+ const parentSVGRect = parentSVG.getBoundingClientRect();+ const parentSVGWidth = parentSVGRect.width;+ const parentSVGHeight = parentSVGRect.height;+ const parentViewBox = parentSVG.getAttribute('viewBox');+ const viewBoxData = parentViewBox ? parentViewBox.split(' ') : [0, 0, parentSVGWidth, parentSVGHeight];+ return {+ x: viewBoxData[0] / 1,+ y: viewBoxData[1] / 1,+ w: parentSVGWidth / viewBoxData[2],+ h: parentSVGHeight / viewBoxData[3]+ }+}+function getPath(path, percent) {const el = is.str(path) ? selectString(path)[0] : path;const p = percent || 100;@@ -574,12 +598,13 @@ function getPathProgress(path, progress) {const l = progress + offset >= 1 ? progress + offset : 0;return path.el.getPointAtLength(l);}+ const pathCoords = getPathCoords(path.el);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 - pathCoords.x) * pathCoords.w;+ case 'y': return (p.y - pathCoords.y) * pathCoords.h;case 'angle': return Math.atan2(p1.y - p0.y, p1.x - p0.x) * 180 / Math.PI;}}