Initial commit xmarticaims
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* Anime.js - svg - CJS
|
||||
* @version v4.5.0
|
||||
* @license MIT
|
||||
* @copyright 2026 - Julian Garnier
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
var consts = require('../core/consts.cjs');
|
||||
var helpers = require('../core/helpers.cjs');
|
||||
var targets = require('../core/targets.cjs');
|
||||
|
||||
/**
|
||||
* @import {
|
||||
* TargetsParam,
|
||||
* DrawableSVGGeometry,
|
||||
* } from '../types/index.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {SVGGeometryElement} [$el]
|
||||
* @return {Number}
|
||||
*/
|
||||
const getScaleFactor = $el => {
|
||||
let scaleFactor = 1;
|
||||
if ($el && $el.getCTM) {
|
||||
const ctm = $el.getCTM();
|
||||
if (ctm) {
|
||||
const scaleX = helpers.sqrt(ctm.a * ctm.a + ctm.b * ctm.b);
|
||||
const scaleY = helpers.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 = consts.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 === consts.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 (helpers.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 = targets.parseTargets(selector);
|
||||
return els.map($el => createDrawableProxy(
|
||||
/** @type {SVGGeometryElement} */($el),
|
||||
start,
|
||||
end
|
||||
));
|
||||
};
|
||||
|
||||
exports.createDrawable = createDrawable;
|
||||
@@ -0,0 +1,3 @@
|
||||
export function createDrawable(selector: TargetsParam, start?: number, end?: number): Array<DrawableSVGGeometry>;
|
||||
import type { TargetsParam } from '../types/index.js';
|
||||
import type { DrawableSVGGeometry } from '../types/index.js';
|
||||
@@ -0,0 +1,117 @@
|
||||
/**
|
||||
* Anime.js - svg - ESM
|
||||
* @version v4.5.0
|
||||
* @license MIT
|
||||
* @copyright 2026 - Julian Garnier
|
||||
*/
|
||||
|
||||
import { proxyTargetSymbol, K } from '../core/consts.js';
|
||||
import { isFnc, sqrt } from '../core/helpers.js';
|
||||
import { parseTargets } from '../core/targets.js';
|
||||
|
||||
/**
|
||||
* @import {
|
||||
* TargetsParam,
|
||||
* DrawableSVGGeometry,
|
||||
* } from '../types/index.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* @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
|
||||
));
|
||||
};
|
||||
|
||||
export { createDrawable };
|
||||
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Anime.js - svg - CJS
|
||||
* @version v4.5.0
|
||||
* @license MIT
|
||||
* @copyright 2026 - Julian Garnier
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
var helpers = require('../core/helpers.cjs');
|
||||
var targets = require('../core/targets.cjs');
|
||||
|
||||
/**
|
||||
* @import {
|
||||
* TargetsParam,
|
||||
* } from '../types/index.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {TargetsParam} path
|
||||
* @return {SVGGeometryElement|void}
|
||||
*/
|
||||
const getPath = path => {
|
||||
const parsedTargets = targets.parseTargets(path);
|
||||
const $parsedSvg = /** @type {SVGGeometryElement} */(parsedTargets[0]);
|
||||
if (!$parsedSvg || !helpers.isSvg($parsedSvg)) return console.warn(`${path} is not a valid SVGGeometryElement`);
|
||||
return $parsedSvg;
|
||||
};
|
||||
|
||||
exports.getPath = getPath;
|
||||
@@ -0,0 +1,2 @@
|
||||
export function getPath(path: TargetsParam): SVGGeometryElement | void;
|
||||
import type { TargetsParam } from '../types/index.js';
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Anime.js - svg - ESM
|
||||
* @version v4.5.0
|
||||
* @license MIT
|
||||
* @copyright 2026 - Julian Garnier
|
||||
*/
|
||||
|
||||
import { isSvg } from '../core/helpers.js';
|
||||
import { parseTargets } from '../core/targets.js';
|
||||
|
||||
/**
|
||||
* @import {
|
||||
* TargetsParam,
|
||||
* } from '../types/index.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {TargetsParam} path
|
||||
* @return {SVGGeometryElement|void}
|
||||
*/
|
||||
const getPath = path => {
|
||||
const parsedTargets = parseTargets(path);
|
||||
const $parsedSvg = /** @type {SVGGeometryElement} */(parsedTargets[0]);
|
||||
if (!$parsedSvg || !isSvg($parsedSvg)) return console.warn(`${path} is not a valid SVGGeometryElement`);
|
||||
return $parsedSvg;
|
||||
};
|
||||
|
||||
export { getPath };
|
||||
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Anime.js - svg - CJS
|
||||
* @version v4.5.0
|
||||
* @license MIT
|
||||
* @copyright 2026 - Julian Garnier
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
var motionpath = require('./motionpath.cjs');
|
||||
var drawable = require('./drawable.cjs');
|
||||
var morphto = require('./morphto.cjs');
|
||||
|
||||
|
||||
|
||||
exports.createMotionPath = motionpath.createMotionPath;
|
||||
exports.createDrawable = drawable.createDrawable;
|
||||
exports.morphTo = morphto.morphTo;
|
||||
@@ -0,0 +1,3 @@
|
||||
export { createMotionPath } from "./motionpath.js";
|
||||
export { createDrawable } from "./drawable.js";
|
||||
export { morphTo } from "./morphto.js";
|
||||
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* Anime.js - svg - ESM
|
||||
* @version v4.5.0
|
||||
* @license MIT
|
||||
* @copyright 2026 - Julian Garnier
|
||||
*/
|
||||
|
||||
export { createMotionPath } from './motionpath.js';
|
||||
export { createDrawable } from './drawable.js';
|
||||
export { morphTo } from './morphto.js';
|
||||
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Anime.js - svg - CJS
|
||||
* @version v4.5.0
|
||||
* @license MIT
|
||||
* @copyright 2026 - Julian Garnier
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
var helpers$1 = require('../core/helpers.cjs');
|
||||
var helpers = require('./helpers.cjs');
|
||||
|
||||
/**
|
||||
* @import {
|
||||
* TargetsParam,
|
||||
* FunctionValue
|
||||
* } from '../types/index.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {TargetsParam} path2
|
||||
* @param {Number} [precision]
|
||||
* @return {FunctionValue}
|
||||
*/
|
||||
const morphTo = (path2, precision = .33) => ($path1, index, total, prevTween) => {
|
||||
const tagName1 = ($path1.tagName || '').toLowerCase();
|
||||
if (!tagName1.match(/^(path|polygon|polyline)$/)) {
|
||||
throw new Error(`Can't morph a <${$path1.tagName}> SVG element. Use <path>, <polygon> or <polyline>.`);
|
||||
}
|
||||
const $path2 = /** @type {SVGGeometryElement} */(helpers.getPath(path2));
|
||||
if (!$path2) {
|
||||
throw new Error("Can't morph to an invalid target. 'path2' must resolve to an existing <path>, <polygon> or <polyline> SVG element.");
|
||||
}
|
||||
const tagName2 = ($path2.tagName || '').toLowerCase();
|
||||
if (!tagName2.match(/^(path|polygon|polyline)$/)) {
|
||||
throw new Error(`Can't morph a <${$path2.tagName}> SVG element. Use <path>, <polygon> or <polyline>.`);
|
||||
}
|
||||
const isPath = $path1.tagName === 'path';
|
||||
const separator = isPath ? ' ' : ',';
|
||||
const previousPoints = prevTween ? prevTween._value : null;
|
||||
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 + helpers$1.round(pointOnPath1.x, 3) + separator + pointOnPath1.y + ' ';
|
||||
v2 += prefix + helpers$1.round(pointOnPath2.x, 3) + separator + pointOnPath2.y + ' ';
|
||||
}
|
||||
}
|
||||
|
||||
return [v1, v2];
|
||||
};
|
||||
|
||||
exports.morphTo = morphTo;
|
||||
@@ -0,0 +1,3 @@
|
||||
export function morphTo(path2: TargetsParam, precision?: number): FunctionValue;
|
||||
import type { TargetsParam } from '../types/index.js';
|
||||
import type { FunctionValue } from '../types/index.js';
|
||||
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* Anime.js - svg - ESM
|
||||
* @version v4.5.0
|
||||
* @license MIT
|
||||
* @copyright 2026 - Julian Garnier
|
||||
*/
|
||||
|
||||
import { round } from '../core/helpers.js';
|
||||
import { getPath } from './helpers.js';
|
||||
|
||||
/**
|
||||
* @import {
|
||||
* TargetsParam,
|
||||
* FunctionValue
|
||||
* } from '../types/index.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {TargetsParam} path2
|
||||
* @param {Number} [precision]
|
||||
* @return {FunctionValue}
|
||||
*/
|
||||
const morphTo = (path2, precision = .33) => ($path1, index, total, prevTween) => {
|
||||
const tagName1 = ($path1.tagName || '').toLowerCase();
|
||||
if (!tagName1.match(/^(path|polygon|polyline)$/)) {
|
||||
throw new Error(`Can't morph a <${$path1.tagName}> SVG element. Use <path>, <polygon> or <polyline>.`);
|
||||
}
|
||||
const $path2 = /** @type {SVGGeometryElement} */(getPath(path2));
|
||||
if (!$path2) {
|
||||
throw new Error("Can't morph to an invalid target. 'path2' must resolve to an existing <path>, <polygon> or <polyline> SVG element.");
|
||||
}
|
||||
const tagName2 = ($path2.tagName || '').toLowerCase();
|
||||
if (!tagName2.match(/^(path|polygon|polyline)$/)) {
|
||||
throw new Error(`Can't morph a <${$path2.tagName}> SVG element. Use <path>, <polygon> or <polyline>.`);
|
||||
}
|
||||
const isPath = $path1.tagName === 'path';
|
||||
const separator = isPath ? ' ' : ',';
|
||||
const previousPoints = prevTween ? prevTween._value : null;
|
||||
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 + ' ';
|
||||
}
|
||||
}
|
||||
|
||||
return [v1, v2];
|
||||
};
|
||||
|
||||
export { morphTo };
|
||||
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* Anime.js - svg - CJS
|
||||
* @version v4.5.0
|
||||
* @license MIT
|
||||
* @copyright 2026 - Julian Garnier
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
var consts = require('../core/consts.cjs');
|
||||
var helpers$1 = require('../core/helpers.cjs');
|
||||
var helpers = require('./helpers.cjs');
|
||||
|
||||
/**
|
||||
* @import {
|
||||
* TargetsParam,
|
||||
* FunctionValue,
|
||||
* TweenObjectValue,
|
||||
* TweenModifier,
|
||||
* } from '../types/index.js'
|
||||
*/
|
||||
|
||||
// Motion path animation
|
||||
|
||||
/**
|
||||
* @param {SVGGeometryElement} $path
|
||||
* @param {Number} totalLength
|
||||
* @param {Number} progress
|
||||
* @param {Number} lookup
|
||||
* @param {Boolean} shouldClamp
|
||||
* @return {DOMPoint}
|
||||
*/
|
||||
const getPathPoint = ($path, totalLength, progress, lookup, shouldClamp) => {
|
||||
const point = progress + lookup;
|
||||
const pointOnPath = shouldClamp
|
||||
? Math.max(0, Math.min(point, totalLength)) // Clamp between 0 and totalLength
|
||||
: (point % totalLength + totalLength) % totalLength; // Wrap around
|
||||
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[consts.isSvgSymbol];
|
||||
const ctm = $path.getCTM();
|
||||
const shouldClamp = offset === 0;
|
||||
/** @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, shouldClamp);
|
||||
const p1 = getPathPoint($path, totalLength, newProgress, 1, shouldClamp);
|
||||
return helpers$1.atan2(p1.y - p0.y, p1.x - p0.x) * 180 / helpers$1.PI;
|
||||
} else {
|
||||
const p = getPathPoint($path, totalLength, newProgress, 0, shouldClamp);
|
||||
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 = helpers.getPath(path);
|
||||
if (!$path) return;
|
||||
return {
|
||||
translateX: getPathProgess($path, 'x', offset),
|
||||
translateY: getPathProgess($path, 'y', offset),
|
||||
rotate: getPathProgess($path, 'a', offset),
|
||||
}
|
||||
};
|
||||
|
||||
exports.createMotionPath = createMotionPath;
|
||||
@@ -0,0 +1,7 @@
|
||||
export function createMotionPath(path: TargetsParam, offset?: number): {
|
||||
translateX: FunctionValue;
|
||||
translateY: FunctionValue;
|
||||
rotate: FunctionValue;
|
||||
};
|
||||
import type { TargetsParam } from '../types/index.js';
|
||||
import type { FunctionValue } from '../types/index.js';
|
||||
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* Anime.js - svg - ESM
|
||||
* @version v4.5.0
|
||||
* @license MIT
|
||||
* @copyright 2026 - Julian Garnier
|
||||
*/
|
||||
|
||||
import { isSvgSymbol } from '../core/consts.js';
|
||||
import { atan2, PI } from '../core/helpers.js';
|
||||
import { getPath } from './helpers.js';
|
||||
|
||||
/**
|
||||
* @import {
|
||||
* TargetsParam,
|
||||
* FunctionValue,
|
||||
* TweenObjectValue,
|
||||
* TweenModifier,
|
||||
* } from '../types/index.js'
|
||||
*/
|
||||
|
||||
// Motion path animation
|
||||
|
||||
/**
|
||||
* @param {SVGGeometryElement} $path
|
||||
* @param {Number} totalLength
|
||||
* @param {Number} progress
|
||||
* @param {Number} lookup
|
||||
* @param {Boolean} shouldClamp
|
||||
* @return {DOMPoint}
|
||||
*/
|
||||
const getPathPoint = ($path, totalLength, progress, lookup, shouldClamp) => {
|
||||
const point = progress + lookup;
|
||||
const pointOnPath = shouldClamp
|
||||
? Math.max(0, Math.min(point, totalLength)) // Clamp between 0 and totalLength
|
||||
: (point % totalLength + totalLength) % totalLength; // Wrap around
|
||||
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();
|
||||
const shouldClamp = offset === 0;
|
||||
/** @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, shouldClamp);
|
||||
const p1 = getPathPoint($path, totalLength, newProgress, 1, shouldClamp);
|
||||
return atan2(p1.y - p0.y, p1.x - p0.x) * 180 / PI;
|
||||
} else {
|
||||
const p = getPathPoint($path, totalLength, newProgress, 0, shouldClamp);
|
||||
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),
|
||||
}
|
||||
};
|
||||
|
||||
export { createMotionPath };
|
||||
Reference in New Issue
Block a user