Skip to content

armgit / anime

Public Imported from GitHub
0

JavaScript animation engine

Added support for SVGPolyline elements

Jeroen Rinzemaf940a3b7bdb5
diff --git a/anime.js b/anime.js
index 015cf492..44960e32 100644
--- a/anime.js
+++ b/anime.js
@@ -406,14 +406,58 @@
function isPath(val) {
return is.obj(val) && objectHas(val, 'totalLength');
}
-
+
function getSvgElementLength(el) {
const constructor = el.constructor
switch (constructor) {
- case SVGLineElement: return ((x1, x2, y1, y2) => Math.sqrt( (x2-=x1)*x2 + (y2-=y1)*y2 ))(el.getAttribute('x1'), el.getAttribute('x2'), el.getAttribute('y1'), el.getAttribute('y2'));
- case SVGRectElement: return (el.getAttribute('width')*2) + (el.getAttribute('height')*2);
- case SVGPathElement: return el.getTotalLength();
+ case SVGPolylineElement: return getSvgPolylineLength(el);
+ case SVGLineElement: return getSvgLineLength(el);
+ case SVGRectElement: return getSvgRectLength(el);
+ case SVGPathElement: return getSvgPathLength(el);
+ }
+ }
+
+ function getSvgLineLength(el) {
+ let x1 = el.getAttribute('x1')
+ let x2 = el.getAttribute('x2')
+
+ let y1 = el.getAttribute('y1')
+ let y2 = el.getAttribute('y2')
+
+ let totalLength = Math.sqrt( ( x2-=x1 ) * x2 + ( y2-=y1 ) * y2 )
+ return totalLength
+ }
+
+ function getSvgRectLength(el) {
+ const width = el.getAttribute('width')
+ const height = el.getAttribute('height')
+
+ let totalLength = ( width*2 ) + ( height*2 )
+ return totalLength
+ }
+
+ function getSvgPathLength(el) {
+ let totalLength = el.getTotalLength()
+ return totalLength
+ }
+
+ function getSvgPolylineLength(el) {
+ const points = el.points.numberOfItems
+ let totalLength = 0
+ let prevPos
+
+ for (var i = 0 ; i < points;i++) {
+ const pos = el.points.getItem(i)
+
+ if (i > 0) {
+ const distance = Math.sqrt( Math.pow(pos.x - prevPos.x, 2) + Math.pow(pos.y - prevPos.y, 2))
+ totalLength += distance
+ }
+
+ prevPos = pos
}
+
+ return totalLength
}
function setDashoffset(el) {
@@ -676,7 +720,7 @@
instance.remaining = direction === 'alternate' && loops === 1 ? 2 : loops;
for (let i = arrayLength(instance.children); i--; ){
const child = instance.children[i];
- child.seek(child.offset);
+ child.seek(child.offset);
child.reset();
}
}