Skip to content

armgit / anime

Public Imported from GitHub
0

JavaScript animation engine

Update motionpath.js with offset param

Julian Garnier7e9d4a7d5c8e
diff --git a/dist/modules/svg/motionpath.cjs b/dist/modules/svg/motionpath.cjs
index 527c882f..0e69b7c7 100644
--- a/dist/modules/svg/motionpath.cjs
+++ b/dist/modules/svg/motionpath.cjs
@@ -24,20 +24,24 @@ var helpers = require('./helpers.cjs');
/**
* @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[consts.isSvgSymbol];
@@ -48,12 +52,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 helpers$1.atan2(p1.y - p0.y, p1.x - p0.x) * 180 / helpers$1.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
@@ -65,14 +71,15 @@ const getPathProgess = ($path, pathProperty) => {
/**
* @param {TargetsParam} path
+ * @param {Number} [offset=0]
*/
-const createMotionPath = path => {
+const createMotionPath = (path, offset = 0) => {
const $path = helpers.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/dist/modules/svg/motionpath.d.ts b/dist/modules/svg/motionpath.d.ts
index 0e9e666d..f90f7891 100644
--- a/dist/modules/svg/motionpath.d.ts
+++ b/dist/modules/svg/motionpath.d.ts
@@ -1,4 +1,4 @@
-export function createMotionPath(path: TargetsParam): {
+export function createMotionPath(path: TargetsParam, offset?: number): {
translateX: FunctionValue;
translateY: FunctionValue;
rotate: FunctionValue;
diff --git a/dist/modules/svg/motionpath.js b/dist/modules/svg/motionpath.js
index 7ea6ac98..8c8626ff 100644
--- a/dist/modules/svg/motionpath.js
+++ b/dist/modules/svg/motionpath.js
@@ -22,20 +22,24 @@ import { getPath } from './helpers.js';
/**
* @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];
@@ -46,12 +50,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
@@ -63,14 +69,15 @@ const getPathProgess = ($path, pathProperty) => {
/**
* @param {TargetsParam} path
+ * @param {Number} [offset=0]
*/
-const createMotionPath = path => {
+const createMotionPath = (path, offset = 0) => {
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/src/svg.js b/src/svg.js
deleted file mode 100644
index 2cbc431c..00000000
--- a/src/svg.js
+++ /dev/null
@@ -1,258 +0,0 @@
-/// <reference path='./types.js' />
-
-import {
- K,
- isSvgSymbol,
- morphPointsSymbol,
- proxyTargetSymbol,
-} from './consts.js';
-
-import {
- round,
- isSvg,
- atan2,
- sqrt,
- PI,
- isFnc,
-} from './helpers.js';
-
-import {
- parseTargets
-} from './targets.js';
-
-/**
- * @param {TargetsParam} path
- * @return {SVGGeometryElement|undefined}
- */
-const getPath = path => {
- const parsedTargets = parseTargets(path);
- const $parsedSvg = /** @type {SVGGeometryElement} */(parsedTargets[0]);
- if (!$parsedSvg || !isSvg($parsedSvg)) return;
- return $parsedSvg;
-}
-
-/**
- * @param {TargetsParam} path2
- * @param {Number} [precision]
- * @return {FunctionValue}
- */
-const morphTo = (path2, precision = .33) => ($path1) => {
- const $path2 = /** @type {SVGGeometryElement} */(getPath(path2));
- if (!$path2) return;
- const isPath = $path1.tagName === 'path';
- const separator = isPath ? ' ' : ',';
- const previousPoints = $path1[morphPointsSymbol];
- if (previousPoints) $path1.setAttribute(isPath ? 'd' : 'points', previousPoints);
-
- let v1 = '', v2 = '';
-
- if (!precision) {
- v1 = $path1.getAttribute(isPath ? 'd' : 'points');
- v2 = $path2.getAttribute(isPath ? 'd' : 'points');
- } else {
- const length1 = /** @type {SVGGeometryElement} */($path1).getTotalLength();
- const length2 = $path2.getTotalLength();
- const maxPoints = Math.max(Math.ceil(length1 * precision), Math.ceil(length2 * precision));
- for (let i = 0; i < maxPoints; i++) {
- const t = i / (maxPoints - 1);
- const pointOnPath1 = /** @type {SVGGeometryElement} */($path1).getPointAtLength(length1 * t);
- const pointOnPath2 = $path2.getPointAtLength(length2 * t);
- const prefix = isPath ? (i === 0 ? 'M' : 'L') : '';
- v1 += prefix + round(pointOnPath1.x, 3) + separator + pointOnPath1.y + ' ';
- v2 += prefix + round(pointOnPath2.x, 3) + separator + pointOnPath2.y + ' ';
- }
- }
-
- $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;
-}
-
-/**
- * Creates a proxy that wraps an SVGGeometryElement and adds drawing functionality.
- * @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
- */
-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];
- if (property === proxyTargetSymbol) return target;
- if (property === 'setAttribute') {
- return (...args) => {
- if (args[0] === 'draw') {
- const value = args[1];
- const values = value.split(' ');
- const v1 = +values[0];
- const v2 = +values[1];
- // TOTO: Benchmark if performing two slices is more performant than one split
- // const spaceIndex = value.indexOf(' ');
- // const v1 = round(+value.slice(0, spaceIndex), precision);
- // const v2 = round(+value.slice(spaceIndex + 1), precision);
- 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.style.strokeLinecap = `${newCap}`;
- currentCap = newCap;
- }
- }
- target.setAttribute('stroke-dashoffset', `${os}`);
- target.setAttribute('stroke-dasharray', `${d1} ${d2}`);
- }
- return Reflect.apply(value, target, args);
- };
- }
-
- if (isFnc(value)) {
- return (...args) => Reflect.apply(value, target, args);
- } else {
- return value;
- }
- }
- });
-
- 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.
- * @param {TargetsParam} selector - CSS selector, SVG element, or array of elements and selectors
- * @param {number} [start=0] - Starting position (0-1)
- * @param {number} [end=0] - Ending position (0-1)
- * @return {Array<DrawableSVGGeometry>} - Array of proxied elements with drawing functionality
- */
-const createDrawable = (selector, start = 0, end = 0) => {
- const els = parseTargets(selector);
- return els.map($el => createDrawableProxy(
- /** @type {SVGGeometryElement} */($el),
- start,
- end
- ));
-};
-
-// Motion path animation
-
-/**
- * @param {SVGGeometryElement} $path
- * @param {Number} totalLength
- * @param {Number} progress
- * @param {Number}lookup
- * @return {DOMPoint}
- */
-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, offset = 0) => {
- return $el => {
- const totalLength = +($path.getTotalLength());
- const inSvg = $el[isSvgSymbol];
- const ctm = $path.getCTM();
- /** @type {TweenObjectValue} */
- return {
- from: 0,
- to: totalLength,
- /** @type {TweenModifier} */
- modifier: progress => {
- const offsetLength = offset * totalLength;
- const newProgress = progress + offsetLength;
- if (pathProperty === 'a') {
- 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, 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
- }
- }
- }
- }
-}
-
-/**
- * @param {TargetsParam} path
- * @param {Number} [offset=0]
- */
-const createMotionPath = (path, offset = 0) => {
- const $path = getPath(path);
- if (!$path) return;
- return {
- translateX: getPathProgess($path, 'x', offset),
- translateY: getPathProgess($path, 'y', offset),
- rotate: getPathProgess($path, 'a', offset),
- }
-}
-
-// Check for valid SVG attribute
-
-const cssReservedProperties = ['opacity', 'rotate', 'overflow', 'color'];
-
-/**
- * @param {Target} el
- * @param {String} propertyName
- * @return {Boolean}
- */
-export const isValidSVGAttribute = (el, propertyName) => {
- // Return early and use CSS opacity animation instead (already better default values (opacity: 1 instead of 0)) and rotate should be considered a transform
- if (cssReservedProperties.includes(propertyName)) return false;
- if (el.getAttribute(propertyName) || propertyName in el) {
- if (propertyName === 'scale') { // Scale
- const elParentNode = /** @type {SVGGeometryElement} */(/** @type {DOMTarget} */(el).parentNode);
- // Only consider scale as a valid SVG attribute on filter element
- return elParentNode && elParentNode.tagName === 'filter';
- }
- return true;
- }
-}
-
-export const svg = {
- morphTo,
- createMotionPath,
- createDrawable,
-}
diff --git a/src/svg/motionpath.js b/src/svg/motionpath.js
index 2a1e6273..8238e88e 100644
--- a/src/svg/motionpath.js
+++ b/src/svg/motionpath.js
@@ -24,20 +24,24 @@ import {
/**
* @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];
@@ -48,12 +52,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
@@ -65,13 +71,14 @@ const getPathProgess = ($path, pathProperty) => {
/**
* @param {TargetsParam} path
+ * @param {Number} [offset=0]
*/
-export const createMotionPath = path => {
+export const createMotionPath = (path, offset = 0) => {
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),
}
}