Skip to content

armgit / anime

Public Imported from GitHub
0

JavaScript animation engine

add option for svg createMotionPath with an offset between 0 and 1

Léandre PITONecd0a6f04a12
diff --git a/src/svg.js b/src/svg.js
index 273d9e1f..674e4405 100644
--- a/src/svg.js
+++ b/src/svg.js
@@ -170,20 +170,24 @@ const createDrawable = (selector, start = 0, end = 0) => {
/**
* @param {SVGGeometryElement} $path
+ * @param {Number} totalLength
* @param {Number} progress
* @param {Number}lookup
* @return {DOMPoint}
*/
-const getPathPoint = ($path, progress, lookup = 0) => {
- return $path.getPointAtLength(progress + lookup >= 1 ? progress + lookup : 0);
+const getPathPoint = ($path, totalLength, progress, lookup = 0) => {
+ const point = progress + lookup;
+ const pointOnPath = (point % totalLength + totalLength) % totalLength;
+ return $path.getPointAtLength(pointOnPath);
}
/**
* @param {SVGGeometryElement} $path
* @param {String} pathProperty
+ * @param {Number} [offset=0]
* @return {FunctionValue}
*/
-const getPathProgess = ($path, pathProperty) => {
+const getPathProgess = ($path, pathProperty, offset = 0) => {
return $el => {
const totalLength = +($path.getTotalLength());
const inSvg = $el[isSvgSymbol];
@@ -194,12 +198,14 @@ const getPathProgess = ($path, pathProperty) => {
to: totalLength,
/** @type {TweenModifier} */
modifier: progress => {
+ const offsetLength = offset * totalLength;
+ const newProgress = progress + offsetLength;
if (pathProperty === 'a') {
- const p0 = getPathPoint($path, progress, -1);
- const p1 = getPathPoint($path, progress, +1);
+ const p0 = getPathPoint($path, totalLength, newProgress, -1);
+ const p1 = getPathPoint($path, totalLength, newProgress, +1);
return atan2(p1.y - p0.y, p1.x - p0.x) * 180 / PI;
} else {
- const p = getPathPoint($path, progress, 0);
+ const p = getPathPoint($path, totalLength, newProgress, 0);
return pathProperty === 'x' ?
inSvg || !ctm ? p.x : p.x * ctm.a + p.y * ctm.c + ctm.e :
inSvg || !ctm ? p.y : p.x * ctm.b + p.y * ctm.d + ctm.f
@@ -211,14 +217,15 @@ const getPathProgess = ($path, pathProperty) => {
/**
* @param {TargetsParam} path
+ * @param {Number} [offset]
*/
-const createMotionPath = path => {
+const createMotionPath = (path, offset) => {
const $path = getPath(path);
if (!$path) return;
return {
- translateX: getPathProgess($path, 'x'),
- translateY: getPathProgess($path, 'y'),
- rotate: getPathProgess($path, 'a'),
+ translateX: getPathProgess($path, 'x', offset),
+ translateY: getPathProgess($path, 'y', offset),
+ rotate: getPathProgess($path, 'a', offset),
}
}
diff --git a/tests/suites/svg.test.js b/tests/suites/svg.test.js
index d2c711e6..957802b0 100644
--- a/tests/suites/svg.test.js
+++ b/tests/suites/svg.test.js
@@ -162,4 +162,37 @@ suite('SVG', () => {
expect(feTurbulenceEl.hasAttribute('base-frequency')).to.equal(false);
});
+ test('svg.createMotionPath with offset', resolve => {
+ const squareEl = document.querySelector('#square');
+ const [pathEl] = utils.$('#tests path');
+ const pathSelector = 'motion-path-offset-test';
+ pathEl.id = pathSelector;
+
+ const motionPath = svg.createMotionPath(`#${pathSelector}`);
+ const anim = animate(squareEl, {
+ ...motionPath,
+ duration: 10,
+ autoplay: false,
+ });
+ anim.seek(0);
+
+ const initialTransform = squareEl.style.transform;
+ const offset = 0.5;
+ const motionPathWithOffset = svg.createMotionPath(`#${pathSelector}`, offset);
+ const animWithOffset = animate(squareEl, {
+ ...motionPathWithOffset,
+ duration: 10,
+ autoplay: false,
+ });
+ animWithOffset.seek(0);
+
+ const offsetTransform = squareEl.style.transform;
+ expect(initialTransform).to.not.equal(offsetTransform);
+ animWithOffset.seek(animWithOffset.duration);
+ const finalOffsetTransform = squareEl.style.transform;
+ expect(finalOffsetTransform).to.equal(offsetTransform);
+
+ resolve();
+ });
+
});