Skip to content

armgit / anime

Public Imported from GitHub
0

JavaScript animation engine

Fix stroke-linecap round / square defined in a stylesheet conflicting with createDrawable attributes #1002

Julian Garnier2d52ab8265f9
diff --git a/examples/svg-line-drawing/index.html b/examples/svg-line-drawing/index.html
index 22b74425..727c5fdf 100644
--- a/examples/svg-line-drawing/index.html
+++ b/examples/svg-line-drawing/index.html
@@ -9,6 +9,11 @@
body {
display: flex;
}
+ line {
+ stroke-linecap: round;
+ stroke-linejoin: round;
+ stroke-width: 10;
+ }
</style>
</head>
<body>
diff --git a/examples/svg-line-drawing/index.js b/examples/svg-line-drawing/index.js
index 290f7bfc..d5c52f21 100644
--- a/examples/svg-line-drawing/index.js
+++ b/examples/svg-line-drawing/index.js
@@ -15,7 +15,7 @@ function generateLines(numberOfLines) {
<g id="lines" fill="none" fill-rule="evenodd">`;
for (let i = 0; i < numberOfLines; i++) {
const x = margin + i * spacing;
- svgContent += `<line x1="${x}" y1="${margin}" x2="${x}" y2="${svgHeight - margin}" class="line-v" stroke="#A4FF4F" stroke-linecap="butt" stroke-linejoin="round" stroke-width="10"></line>`;
+ svgContent += `<line x1="${x}" y1="${margin}" x2="${x}" y2="${svgHeight - margin}" class="line-v" stroke="#A4FF4F"></line>`;
}
svgContent += `</g></svg>`;
@@ -36,7 +36,7 @@ function generateCircles(numberOfCircles) {
for (let i = 0; i < numberOfCircles; i++) {
const radius = (i + 1) * step;
- svgContent += `<circle class="circle" stroke="#A4FF4F" stroke-linecap="butt" stroke-linejoin="round" stroke-width="10" cx="${centerX}" cy="${centerY}" r="${radius}"></circle>`;
+ svgContent += `<circle class="circle" stroke="#A4FF4F" stroke-linecap="round" stroke-linejoin="round" stroke-width="10" cx="${centerX}" cy="${centerY}" r="${radius}"></circle>`;
}
svgContent += `</g></svg>`;
diff --git a/lib/anime.esm.js b/lib/anime.esm.js
index 66d61535..29b8287c 100644
--- a/lib/anime.esm.js
+++ b/lib/anime.esm.js
@@ -37,6 +37,7 @@
/** @typedef {Timer|Renderable} Tickable */
/** @typedef {Timer&JSAnimation&Timeline} CallbackArgument */
/** @typedef {DraggableParams} DraggableParams */
+/** @typedef {DrawableSVGGeometry} DrawableSVGGeometry */
/** @typedef {Animatable|Tickable|Draggable|ScrollObserver|Scope} Revertible */
/**
@@ -1504,10 +1505,18 @@ const morphTo = (path2, precision = .33) => ($path1) => {
};
/**
- * @param {SVGGeometryElement} $el
- * @param {Number} start
- * @param {Number} end
- * @return {Proxy}
+ * @typedef {SVGGeometryElement & {
+ * setAttribute(name: 'draw', value: `${number} ${number}`): void;
+ * draw: `${number} ${number}`;
+ * }} DrawableSVGGeometry
+ */
+
+/**
+ * Creates a proxy that wraps an SVGGeometryElement and adds drawing functionality.
+ * @param {SVGGeometryElement} $el - The SVG element to wrap
+ * @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;
@@ -1518,20 +1527,16 @@ function createDrawableProxy($el, start, end) {
const value = target[property];
if (property === proxyTargetSymbol) return target;
if (property === 'setAttribute') {
- /** @param {any[]} args */
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 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
@@ -1540,7 +1545,8 @@ function createDrawableProxy($el, start, end) {
if (strokeLineCap !== 'butt') {
const newCap = v1 === v2 ? 'butt' : strokeLineCap;
if (currentCap !== newCap) {
- target.setAttribute('stroke-linecap', `${newCap}`);
+ // target.setAttribute('stroke-linecap', `${newCap}`);
+ target.style.strokeLinecap = `${newCap}`; // Apply style instead of setAttribute to properly override stylesheet styles
currentCap = newCap;
}
}
@@ -1551,7 +1557,6 @@ function createDrawableProxy($el, start, end) {
};
}
if (isFnc(value)) {
- /** @param {any[]} args */
return (...args) => Reflect.apply(value, target, args);
} else {
return value;
@@ -1562,19 +1567,23 @@ function createDrawableProxy($el, start, end) {
$el.setAttribute('pathLength', `${pathLength}`);
proxy.setAttribute('draw', `${start} ${end}`);
}
- return /** @type {typeof Proxy} */(/** @type {unknown} */(proxy));
+ return /** @type {DrawableSVGGeometry} */(proxy);
}
/**
- * @param {TargetsParam} selector
- * @param {Number} [start=0]
- * @param {Number} [end=0]
- * @return {Array.<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 = /** @type {Array.<Proxy>} */((/** @type {unknown} */(parseTargets(selector))));
- els.forEach(($el, i) => els[i] = createDrawableProxy(/** @type {SVGGeometryElement} */(/** @type {unknown} */($el)), start, end));
- return els;
+ const els = parseTargets(selector);
+ return els.map($el => createDrawableProxy(
+ /** @type {SVGGeometryElement} */($el),
+ start,
+ end
+ ));
};
// Motion path animation
diff --git a/src/svg.js b/src/svg.js
index 68086046..47c3bb33 100644
--- a/src/svg.js
+++ b/src/svg.js
@@ -68,10 +68,18 @@ const morphTo = (path2, precision = .33) => ($path1) => {
}
/**
- * @param {SVGGeometryElement} $el
- * @param {Number} start
- * @param {Number} end
- * @return {Proxy}
+ * @typedef {SVGGeometryElement & {
+ * setAttribute(name: 'draw', value: `${number} ${number}`): void;
+ * draw: `${number} ${number}`;
+ * }} DrawableSVGGeometry
+ */
+
+/**
+ * Creates a proxy that wraps an SVGGeometryElement and adds drawing functionality.
+ * @param {SVGGeometryElement} $el - The SVG element to wrap
+ * @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;
@@ -82,20 +90,16 @@ function createDrawableProxy($el, start, end) {
const value = target[property];
if (property === proxyTargetSymbol) return target;
if (property === 'setAttribute') {
- /** @param {any[]} args */
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 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
@@ -104,7 +108,8 @@ function createDrawableProxy($el, start, end) {
if (strokeLineCap !== 'butt') {
const newCap = v1 === v2 ? 'butt' : strokeLineCap;
if (currentCap !== newCap) {
- target.setAttribute('stroke-linecap', `${newCap}`);
+ // target.setAttribute('stroke-linecap', `${newCap}`);
+ target.style.strokeLinecap = `${newCap}`; // Apply style instead of setAttribute to properly override stylesheet styles
currentCap = newCap;
}
}
@@ -115,7 +120,6 @@ function createDrawableProxy($el, start, end) {
};
}
if (isFnc(value)) {
- /** @param {any[]} args */
return (...args) => Reflect.apply(value, target, args);
} else {
return value;
@@ -126,19 +130,23 @@ function createDrawableProxy($el, start, end) {
$el.setAttribute('pathLength', `${pathLength}`);
proxy.setAttribute('draw', `${start} ${end}`);
}
- return /** @type {typeof Proxy} */(/** @type {unknown} */(proxy));
+ return /** @type {DrawableSVGGeometry} */(proxy);
}
/**
- * @param {TargetsParam} selector
- * @param {Number} [start=0]
- * @param {Number} [end=0]
- * @return {Array.<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 = /** @type {Array.<Proxy>} */((/** @type {unknown} */(parseTargets(selector))));
- els.forEach(($el, i) => els[i] = createDrawableProxy(/** @type {SVGGeometryElement} */(/** @type {unknown} */($el)), start, end));
- return els;
+ const els = parseTargets(selector);
+ return els.map($el => createDrawableProxy(
+ /** @type {SVGGeometryElement} */($el),
+ start,
+ end
+ ));
};
// Motion path animation
diff --git a/src/types.js b/src/types.js
index 0a116390..8fb82ee1 100644
--- a/src/types.js
+++ b/src/types.js
@@ -43,6 +43,7 @@
/** @typedef {Timer|Renderable} Tickable */
/** @typedef {Timer&JSAnimation&Timeline} CallbackArgument */
/** @typedef {import('./draggable.js').DraggableParams} DraggableParams */
+/** @typedef {import('./svg.js').DrawableSVGGeometry} DrawableSVGGeometry */
/** @typedef {import('./animatable.js').Animatable|Tickable|Draggable|ScrollObserver|Scope} Revertible */
/**
diff --git a/tests/index.html b/tests/index.html
index 4d153870..73a542a6 100644
--- a/tests/index.html
+++ b/tests/index.html
@@ -219,6 +219,9 @@
position: relative;
width: 100%;
}
+ #tests svg path {
+ stroke-linecap: round;
+ }
#tests img {
vertical-align: middle;
}
diff --git a/tests/suites/svg.test.js b/tests/suites/svg.test.js
index adb33823..d2c711e6 100644
--- a/tests/suites/svg.test.js
+++ b/tests/suites/svg.test.js
@@ -106,6 +106,22 @@ suite('SVG', () => {
// });
// });
+ test('stroke-linecap "round" path should be set to "butt" when hidden', () => {
+ const [ $path ] = utils.$('#tests path')
+ const [ $drawablePath ] = svg.createDrawable('#tests path');
+ expect(getComputedStyle($path).strokeLinecap).to.equal('butt');
+ $drawablePath.setAttribute('draw', '0 .5');
+ expect(getComputedStyle($path).strokeLinecap).to.equal('round');
+ $drawablePath.setAttribute('draw', '0 1');
+ expect(getComputedStyle($path).strokeLinecap).to.equal('round');
+ $drawablePath.setAttribute('draw', '.5 1');
+ expect(getComputedStyle($path).strokeLinecap).to.equal('round');
+ $drawablePath.setAttribute('draw', '1 1');
+ expect(getComputedStyle($path).strokeLinecap).to.equal('butt');
+ $drawablePath.setAttribute('draw', '.25 .25');
+ expect(getComputedStyle($path).strokeLinecap).to.equal('butt');
+ });
+
test('SVG Filters', () => {
// Filters tests
/** @type {HTMLElement} */
diff --git a/types/index.d.ts b/types/index.d.ts
index 421a6aca..397cb529 100644
--- a/types/index.d.ts
+++ b/types/index.d.ts
@@ -206,13 +206,17 @@ declare namespace svg {
export { createMotionPath };
export { createDrawable };
}
+type DrawableSVGGeometry = SVGGeometryElement & {
+ setAttribute(name: "draw", value: `${number} ${number}`): void;
+ draw: `${number} ${number}`;
+};
declare function morphTo(path2: TargetsParam, precision?: number): FunctionValue;
declare function createMotionPath(path: TargetsParam): {
translateX: FunctionValue;
translateY: FunctionValue;
rotate: FunctionValue;
};
-declare function createDrawable(selector: TargetsParam, start?: number, end?: number): Array<ProxyConstructor>;
+declare function createDrawable(selector: TargetsParam, start?: number, end?: number): Array<DrawableSVGGeometry>;
declare function stagger(val: number | string | [
number | string,
number | string
@@ -578,6 +582,7 @@ declare class DOMProxy {
};
}
type DraggableParams = DraggableParams;
+type DrawableSVGGeometry = DrawableSVGGeometry;
declare class Animatable {
constructor(targets: TargetsParam, parameters: AnimatableParams);
targets: (HTMLElement | SVGElement | JSTarget)[];
@@ -1017,4 +1022,4 @@ type WAAPIAnimationOptions = {
};
type WAAPIAnimationParams = Record<string, WAAPIKeyframeValue | WAAPIAnimationOptions | boolean | ScrollObserver | WAAPICallback | EasingParam | WAAPITweenOptions> & WAAPIAnimationOptions;
declare function easingToLinear(fn: EasingFunction, samples?: number): string;
-export { engine, utils, svg, stagger, eases, DefaultsParams, Renderable, Tickable, CallbackArgument, DraggableParams, Revertible, EasingFunction, EaseStringParamNames, EasingParam, DOMTarget, JSTarget, Target, TargetSelector, DOMTargetSelector, DOMTargetsParam, DOMTargetsArray, JSTargetsParam, JSTargetsArray, TargetsParam, TargetsArray, FunctionValue, TweenModifier, ColorArray, Callback, TickableCallbacks, RenderableCallbacks, Tween, TweenDecomposedValue, TweenPropertySiblings, TweenLookups, TweenReplaceLookups, TweenAdditiveLookups, TimerOptions, TimerParams, TweenParamValue, TweenPropValue, TweenComposition, TweenParamsOptions, TweenValues, TweenKeyValue, ArraySyntaxValue, TweenOptions, TweenObjectValue, PercentageKeyframeOptions, PercentageKeyframeParams, PercentageKeyframes, DurationKeyframes, AnimationOptions, AnimationParams, TimelineOptions, TimelineParams, AnimatablePropertySetter, AnimatablePropertyGetter, AnimatableProperty, AnimatableObject, AnimatablePropertyParamsOptions, AnimatableParams, createTimer, Timer, animate, JSAnimation, createTimeline, Timeline, createAnimatable, Animatable, createDraggable, Draggable, createScope, Scope, onScroll, ScrollObserver, scrollContainers, createSpring, Spring, waapi, WAAPIAnimation };
+export { engine, utils, svg, stagger, eases, DefaultsParams, Renderable, Tickable, CallbackArgument, DraggableParams, DrawableSVGGeometry, Revertible, EasingFunction, EaseStringParamNames, EasingParam, DOMTarget, JSTarget, Target, TargetSelector, DOMTargetSelector, DOMTargetsParam, DOMTargetsArray, JSTargetsParam, JSTargetsArray, TargetsParam, TargetsArray, FunctionValue, TweenModifier, ColorArray, Callback, TickableCallbacks, RenderableCallbacks, Tween, TweenDecomposedValue, TweenPropertySiblings, TweenLookups, TweenReplaceLookups, TweenAdditiveLookups, TimerOptions, TimerParams, TweenParamValue, TweenPropValue, TweenComposition, TweenParamsOptions, TweenValues, TweenKeyValue, ArraySyntaxValue, TweenOptions, TweenObjectValue, PercentageKeyframeOptions, PercentageKeyframeParams, PercentageKeyframes, DurationKeyframes, AnimationOptions, AnimationParams, TimelineOptions, TimelineParams, AnimatablePropertySetter, AnimatablePropertyGetter, AnimatableProperty, AnimatableObject, AnimatablePropertyParamsOptions, AnimatableParams, createTimer, Timer, animate, JSAnimation, createTimeline, Timeline, createAnimatable, Animatable, createDraggable, Draggable, createScope, Scope, onScroll, ScrollObserver, scrollContainers, createSpring, Spring, waapi, WAAPIAnimation };
diff --git a/types/index.js b/types/index.js
index 4df837ac..e190a4d1 100644
--- a/types/index.js
+++ b/types/index.js
@@ -1112,10 +1112,17 @@ const morphTo = (path2, precision = .33) => ($path1) => {
return [v1, v2];
};
/**
- * @param {SVGGeometryElement} $el
- * @param {Number} start
- * @param {Number} end
- * @return {Proxy}
+ * @typedef {SVGGeometryElement & {
+ * setAttribute(name: 'draw', value: `${number} ${number}`): void;
+ * draw: `${number} ${number}`;
+ * }} DrawableSVGGeometry
+ */
+/**
+ * Creates a proxy that wraps an SVGGeometryElement and adds drawing functionality.
+ * @param {SVGGeometryElement} $el - The SVG element to wrap
+ * @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;
@@ -1127,7 +1134,6 @@ function createDrawableProxy($el, start, end) {
if (property === proxyTargetSymbol)
return target;
if (property === 'setAttribute') {
- /** @param {any[]} args */
return (...args) => {
if (args[0] === 'draw') {
const value = args[1];
@@ -1146,7 +1152,8 @@ function createDrawableProxy($el, start, end) {
if (strokeLineCap !== 'butt') {
const newCap = v1 === v2 ? 'butt' : strokeLineCap;
if (currentCap !== newCap) {
- target.setAttribute('stroke-linecap', `${newCap}`);
+ // target.setAttribute('stroke-linecap', `${newCap}`);
+ target.style.strokeLinecap = `${newCap}`; // Apply style instead of setAttribute to properly override stylesheet styles
currentCap = newCap;
}
}
@@ -1157,7 +1164,6 @@ function createDrawableProxy($el, start, end) {
};
}
if (isFnc(value)) {
- /** @param {any[]} args */
return (...args) => Reflect.apply(value, target, args);
}
else {
@@ -1169,18 +1175,19 @@ function createDrawableProxy($el, start, end) {
$el.setAttribute('pathLength', `${pathLength}`);
proxy.setAttribute('draw', `${start} ${end}`);
}
- return /** @type {typeof Proxy} */ ( /** @type {unknown} */(proxy));
+ return /** @type {DrawableSVGGeometry} */ (proxy);
}
/**
- * @param {TargetsParam} selector
- * @param {Number} [start=0]
- * @param {Number} [end=0]
- * @return {Array.<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 = /** @type {Array.<Proxy>} */ (( /** @type {unknown} */(parseTargets(selector))));
- els.forEach(($el, i) => els[i] = createDrawableProxy(/** @type {SVGGeometryElement} */ ( /** @type {unknown} */($el)), start, end));
- return els;
+ const els = parseTargets(selector);
+ return els.map($el => createDrawableProxy(
+ /** @type {SVGGeometryElement} */ ($el), start, end));
};
// Motion path animation
/**