Skip to content

armgit / anime

Public Imported from GitHub
0

JavaScript animation engine

Fix setDashoffset and vector-effect="non-scaling-stroke" miscalculation #793

Julian Garnier7538f345fd9c
diff --git a/examples/svg-line-drawing/index.html b/examples/svg-line-drawing/index.html
index 727c5fdf..47bd9668 100644
--- a/examples/svg-line-drawing/index.html
+++ b/examples/svg-line-drawing/index.html
@@ -12,7 +12,14 @@
line {
stroke-linecap: round;
stroke-linejoin: round;
- stroke-width: 10;
+ stroke-width: 5;
+/* vector-effect: non-scaling-stroke;*/
+ }
+ circle {
+ stroke-linecap: round;
+ stroke-linejoin: round;
+ stroke-width: 5;
+/* vector-effect: non-scaling-stroke;*/
}
</style>
</head>
diff --git a/lib/anime.esm.js b/lib/anime.esm.js
index 29b8287c..4a19e36a 100644
--- a/lib/anime.esm.js
+++ b/lib/anime.esm.js
@@ -1504,6 +1504,23 @@ const morphTo = (path2, precision = .33) => ($path1) => {
return [v1, v2];
};
+/**
+ * @param {SVGGeometryElement} [$el]
+ * @return {Number}
+ */
+const getScaleFactor = $el => {
+ let scaleFactor = 1;
+ if ($el && $el.getCTM) {
+ const ctm = $el.getCTM();
+ if (ctm) {
+ const scaleX = sqrt(ctm.a * ctm.a + ctm.b * ctm.b);
+ const scaleY = sqrt(ctm.c * ctm.c + ctm.d * ctm.d);
+ scaleFactor = (scaleX + scaleY) / 2;
+ }
+ }
+ return scaleFactor;
+};
+
/**
* @typedef {SVGGeometryElement & {
* setAttribute(name: 'draw', value: `${number} ${number}`): void;
@@ -1513,15 +1530,19 @@ const morphTo = (path2, precision = .33) => ($path1) => {
/**
* Creates a proxy that wraps an SVGGeometryElement and adds drawing functionality.
- * @param {SVGGeometryElement} $el - The SVG element to wrap
+ * @param {SVGGeometryElement} $el - The SVG element to transform into a drawable
* @param {number} start - Starting position (0-1)
* @param {number} end - Ending position (0-1)
* @return {DrawableSVGGeometry} - Returns a proxy that preserves the original element's type with additional 'draw' attribute functionality
*/
-function createDrawableProxy($el, start, end) {
- const strokeLineCap = getComputedStyle($el).strokeLinecap;
+const createDrawableProxy = ($el, start, end) => {
const pathLength = K;
+ const computedStyles = getComputedStyle($el);
+ const strokeLineCap = computedStyles.strokeLinecap;
+ // @ts-ignore
+ const $scalled = computedStyles.vectorEffect === 'non-scaling-stroke' ? $el : null;
let currentCap = strokeLineCap;
+
const proxy = new Proxy($el, {
get(target, property) {
const value = target[property];
@@ -1537,16 +1558,15 @@ function createDrawableProxy($el, start, end) {
// const spaceIndex = value.indexOf(' ');
// const v1 = round(+value.slice(0, spaceIndex), precision);
// const v2 = round(+value.slice(spaceIndex + 1), precision);
- const os = v1 * -1e3;
- const d1 = (v2 * pathLength) + os;
- // Prevents linecap to smear by offsetting the dasharray length by 0.01% when v2 is not at max
- const d2 = (pathLength + ((v1 === 0 && v2 === 1) || (v1 === 1 && v2 === 0) ? 0 : 10) - d1);
- // Handle cases where the cap is still visible when the line is completly hidden
+ const scaleFactor = getScaleFactor($scalled);
+ const os = v1 * -1e3 * scaleFactor;
+ const d1 = (v2 * pathLength * scaleFactor) + os;
+ const d2 = (pathLength * scaleFactor +
+ ((v1 === 0 && v2 === 1) || (v1 === 1 && v2 === 0) ? 0 : 10 * scaleFactor) - d1);
if (strokeLineCap !== 'butt') {
const newCap = v1 === v2 ? 'butt' : strokeLineCap;
if (currentCap !== newCap) {
- // target.setAttribute('stroke-linecap', `${newCap}`);
- target.style.strokeLinecap = `${newCap}`; // Apply style instead of setAttribute to properly override stylesheet styles
+ target.style.strokeLinecap = `${newCap}`;
currentCap = newCap;
}
}
@@ -1556,6 +1576,7 @@ function createDrawableProxy($el, start, end) {
return Reflect.apply(value, target, args);
};
}
+
if (isFnc(value)) {
return (...args) => Reflect.apply(value, target, args);
} else {
@@ -1563,12 +1584,14 @@ function createDrawableProxy($el, start, end) {
}
}
});
+
if ($el.getAttribute('pathLength') !== `${pathLength}`) {
$el.setAttribute('pathLength', `${pathLength}`);
proxy.setAttribute('draw', `${start} ${end}`);
}
+
return /** @type {DrawableSVGGeometry} */(proxy);
-}
+};
/**
* Creates drawable proxies for multiple SVG elements.
diff --git a/src/svg.js b/src/svg.js
index 47c3bb33..8fe1628f 100644
--- a/src/svg.js
+++ b/src/svg.js
@@ -11,6 +11,7 @@ import {
round,
isSvg,
atan2,
+ sqrt,
PI,
isFnc,
} from './helpers.js';
@@ -67,6 +68,23 @@ const morphTo = (path2, precision = .33) => ($path1) => {
return [v1, v2];
}
+/**
+ * @param {SVGGeometryElement} [$el]
+ * @return {Number}
+ */
+const getScaleFactor = $el => {
+ let scaleFactor = 1;
+ if ($el && $el.getCTM) {
+ const ctm = $el.getCTM();
+ if (ctm) {
+ const scaleX = sqrt(ctm.a * ctm.a + ctm.b * ctm.b);
+ const scaleY = sqrt(ctm.c * ctm.c + ctm.d * ctm.d);
+ scaleFactor = (scaleX + scaleY) / 2;
+ }
+ }
+ return scaleFactor;
+}
+
/**
* @typedef {SVGGeometryElement & {
* setAttribute(name: 'draw', value: `${number} ${number}`): void;
@@ -76,15 +94,19 @@ const morphTo = (path2, precision = .33) => ($path1) => {
/**
* Creates a proxy that wraps an SVGGeometryElement and adds drawing functionality.
- * @param {SVGGeometryElement} $el - The SVG element to wrap
+ * @param {SVGGeometryElement} $el - The SVG element to transform into a drawable
* @param {number} start - Starting position (0-1)
* @param {number} end - Ending position (0-1)
* @return {DrawableSVGGeometry} - Returns a proxy that preserves the original element's type with additional 'draw' attribute functionality
*/
-function createDrawableProxy($el, start, end) {
- const strokeLineCap = getComputedStyle($el).strokeLinecap;
+const createDrawableProxy = ($el, start, end) => {
const pathLength = K;
+ const computedStyles = getComputedStyle($el);
+ const strokeLineCap = computedStyles.strokeLinecap;
+ // @ts-ignore
+ const $scalled = computedStyles.vectorEffect === 'non-scaling-stroke' ? $el : null;
let currentCap = strokeLineCap;
+
const proxy = new Proxy($el, {
get(target, property) {
const value = target[property];
@@ -100,16 +122,15 @@ function createDrawableProxy($el, start, end) {
// const spaceIndex = value.indexOf(' ');
// const v1 = round(+value.slice(0, spaceIndex), precision);
// const v2 = round(+value.slice(spaceIndex + 1), precision);
- const os = v1 * -pathLength;
- const d1 = (v2 * pathLength) + os;
- // Prevents linecap to smear by offsetting the dasharray length by 0.01% when v2 is not at max
- const d2 = (pathLength + ((v1 === 0 && v2 === 1) || (v1 === 1 && v2 === 0) ? 0 : 10) - d1);
- // Handle cases where the cap is still visible when the line is completly hidden
+ const scaleFactor = getScaleFactor($scalled);
+ const os = v1 * -pathLength * scaleFactor;
+ const d1 = (v2 * pathLength * scaleFactor) + os;
+ const d2 = (pathLength * scaleFactor +
+ ((v1 === 0 && v2 === 1) || (v1 === 1 && v2 === 0) ? 0 : 10 * scaleFactor) - d1);
if (strokeLineCap !== 'butt') {
const newCap = v1 === v2 ? 'butt' : strokeLineCap;
if (currentCap !== newCap) {
- // target.setAttribute('stroke-linecap', `${newCap}`);
- target.style.strokeLinecap = `${newCap}`; // Apply style instead of setAttribute to properly override stylesheet styles
+ target.style.strokeLinecap = `${newCap}`;
currentCap = newCap;
}
}
@@ -119,6 +140,7 @@ function createDrawableProxy($el, start, end) {
return Reflect.apply(value, target, args);
};
}
+
if (isFnc(value)) {
return (...args) => Reflect.apply(value, target, args);
} else {
@@ -126,10 +148,12 @@ function createDrawableProxy($el, start, end) {
}
}
});
+
if ($el.getAttribute('pathLength') !== `${pathLength}`) {
$el.setAttribute('pathLength', `${pathLength}`);
proxy.setAttribute('draw', `${start} ${end}`);
}
+
return /** @type {DrawableSVGGeometry} */(proxy);
}
diff --git a/tests/playground/sandbox/index.html b/tests/playground/sandbox/index.html
new file mode 100644
index 00000000..288f836d
--- /dev/null
+++ b/tests/playground/sandbox/index.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>Sandbox / Anime.js</title>
+ <meta charset="utf-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
+ <link href="../assets/css/styles.css" rel="stylesheet">
+ <style>
+ body {
+ display: flex;
+ align-items: center;
+ overflow: hidden;
+ position: absolute;
+ width: 100%;
+ height: 100dvh;
+ }
+ circle {
+ vector-effect: non-scaling-stroke;
+ }
+ </style>
+</head>
+<body>
+ <svg xmlns="http://www.w3.org/2000/svg" width="400" height="400" viewBox="0 0 400 400" preserveAspectRatio="none">
+ <circle cx="200" cy="200" r="200" fill="none" stroke="#FFF"/>
+ </svg>
+ <script type="module" src="./index.js"></script>
+</body>
+</html>
diff --git a/tests/playground/sandbox/index.js b/tests/playground/sandbox/index.js
new file mode 100644
index 00000000..7a824587
--- /dev/null
+++ b/tests/playground/sandbox/index.js
@@ -0,0 +1,11 @@
+import {
+ svg,
+ animate,
+} from '../../../lib/anime.esm.js';
+
+animate(svg.createDrawable('svg circle'), {
+ draw: '0 1',
+ alternate: true,
+ loop: true,
+ duration: 4000,
+});
\ No newline at end of file
diff --git a/types/index.js b/types/index.js
index e190a4d1..c49efeef 100644
--- a/types/index.js
+++ b/types/index.js
@@ -1111,6 +1111,22 @@ const morphTo = (path2, precision = .33) => ($path1) => {
$path1[morphPointsSymbol] = v2;
return [v1, v2];
};
+/**
+ * @param {SVGGeometryElement} [$el]
+ * @return {Number}
+ */
+const getScaleFactor = $el => {
+ let scaleFactor = 1;
+ if ($el && $el.getCTM) {
+ const ctm = $el.getCTM();
+ if (ctm) {
+ const scaleX = sqrt(ctm.a * ctm.a + ctm.b * ctm.b);
+ const scaleY = sqrt(ctm.c * ctm.c + ctm.d * ctm.d);
+ scaleFactor = (scaleX + scaleY) / 2;
+ }
+ }
+ return scaleFactor;
+};
/**
* @typedef {SVGGeometryElement & {
* setAttribute(name: 'draw', value: `${number} ${number}`): void;
@@ -1119,14 +1135,17 @@ const morphTo = (path2, precision = .33) => ($path1) => {
*/
/**
* Creates a proxy that wraps an SVGGeometryElement and adds drawing functionality.
- * @param {SVGGeometryElement} $el - The SVG element to wrap
+ * @param {SVGGeometryElement} $el - The SVG element to transform into a drawable
* @param {number} start - Starting position (0-1)
* @param {number} end - Ending position (0-1)
* @return {DrawableSVGGeometry} - Returns a proxy that preserves the original element's type with additional 'draw' attribute functionality
*/
-function createDrawableProxy($el, start, end) {
- const strokeLineCap = getComputedStyle($el).strokeLinecap;
+const createDrawableProxy = ($el, start, end) => {
const pathLength = K;
+ const computedStyles = getComputedStyle($el);
+ const strokeLineCap = computedStyles.strokeLinecap;
+ // @ts-ignore
+ const $scalled = computedStyles.vectorEffect === 'non-scaling-stroke' ? $el : null;
let currentCap = strokeLineCap;
const proxy = new Proxy($el, {
get(target, property) {
@@ -1144,16 +1163,15 @@ function createDrawableProxy($el, start, end) {
// const spaceIndex = value.indexOf(' ');
// const v1 = round(+value.slice(0, spaceIndex), precision);
// const v2 = round(+value.slice(spaceIndex + 1), precision);
- const os = v1 * -1e3;
- const d1 = (v2 * pathLength) + os;
- // Prevents linecap to smear by offsetting the dasharray length by 0.01% when v2 is not at max
- const d2 = (pathLength + ((v1 === 0 && v2 === 1) || (v1 === 1 && v2 === 0) ? 0 : 10) - d1);
- // Handle cases where the cap is still visible when the line is completly hidden
+ const scaleFactor = getScaleFactor($scalled);
+ const os = v1 * -1e3 * scaleFactor;
+ const d1 = (v2 * pathLength * scaleFactor) + os;
+ const d2 = (pathLength * scaleFactor +
+ ((v1 === 0 && v2 === 1) || (v1 === 1 && v2 === 0) ? 0 : 10 * scaleFactor) - d1);
if (strokeLineCap !== 'butt') {
const newCap = v1 === v2 ? 'butt' : strokeLineCap;
if (currentCap !== newCap) {
- // target.setAttribute('stroke-linecap', `${newCap}`);
- target.style.strokeLinecap = `${newCap}`; // Apply style instead of setAttribute to properly override stylesheet styles
+ target.style.strokeLinecap = `${newCap}`;
currentCap = newCap;
}
}
@@ -1176,7 +1194,7 @@ function createDrawableProxy($el, start, end) {
proxy.setAttribute('draw', `${start} ${end}`);
}
return /** @type {DrawableSVGGeometry} */ (proxy);
-}
+};
/**
* Creates drawable proxies for multiple SVG elements.
* @param {TargetsParam} selector - CSS selector, SVG element, or array of elements and selectors