@hilux/ngx-motion 2.4.0 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -55,6 +55,7 @@ export class CardComponent {}
|
|
|
55
55
|
- **Pan and swipe:** `motionPan` reports pointer movement and swipe directions, with `whilePan` styles.
|
|
56
56
|
- **Reorder:** `motionReorderGroup` and `motionReorderItem` for drag-to-reorder lists, with keyboard support.
|
|
57
57
|
- **Motion values:** `motionSignal`, `mapSignal`, `springSignal`, `velocitySignal` and `scrollSignal`: values that change every frame, as Angular signals, bound to styles with `[motionStyle]` without change detection.
|
|
58
|
+
- **SVG line drawing:** `pathLength`, `pathOffset` and `pathSpacing` on any SVG shape, with every trigger (enter, hover, in view, scroll, `[motionStyle]`).
|
|
58
59
|
- **Layout animations:** `motionLayout` glides elements to their new place and size after any layout change, and `motionLayoutId` makes shared-element transitions.
|
|
59
60
|
- **Exit animations:** `*motionIf` and `*motionPresence` keep elements in the DOM until their exit animation finishes. If shown again mid-exit, they animate back in.
|
|
60
61
|
- **Variants:** named, reusable animation states.
|
|
@@ -368,6 +369,44 @@ export class Example {
|
|
|
368
369
|
- `[motionStyle]`: transform shortcuts `x`, `y`, `z` (px), `scale`, `scaleX`, `scaleY`, `rotate`, `rotateX/Y/Z`, `skewX/Y` (deg) are combined into `transform`, so don't animate the same element's transform another way. Other styles use camelCase names; numbers get px except unitless ones such as `opacity`. CSS custom properties (`'--progress'`) take any value.
|
|
369
370
|
- With reduced motion, and on the server, springs go straight to their target.
|
|
370
371
|
|
|
372
|
+
### SVG Line Drawing
|
|
373
|
+
Like Framer Motion's `pathLength`. Animate how much of a line is drawn, from 0 to 1, on any SVG shape: `path`, `circle`, `rect`, `line`, `polyline`, `polygon`, `ellipse`. It works with every trigger.
|
|
374
|
+
|
|
375
|
+
A simple hover draw is fine in plain CSS: give the shape `pathLength="1"`, set `stroke-dasharray: 1; stroke-dashoffset: 1` and transition `stroke-dashoffset`. Use ngx-motion when a line should draw when it scrolls into view, one part after another, as the page scrolls (in every browser), on exit, follow a value on a spring, or spring and loop on hover and settle smoothly when the pointer leaves (a CSS loop snaps back).
|
|
376
|
+
|
|
377
|
+
```html
|
|
378
|
+
<!-- Draws when scrolled into view, one part after another -->
|
|
379
|
+
<svg viewBox="0 0 100 100" fill="none" stroke="currentColor" stroke-width="6"
|
|
380
|
+
motionone runInView="once" [staggerChildren]="0.12" [initial]="{}" [animate]="{}">
|
|
381
|
+
<circle cx="50" cy="50" r="42"
|
|
382
|
+
motionone [initial]="{ pathLength: 0 }" [animate]="{ pathLength: 1 }" />
|
|
383
|
+
<path d="M30 52 L45 66 L72 36"
|
|
384
|
+
motionone [initial]="{ pathLength: 0 }" [animate]="{ pathLength: 1 }" />
|
|
385
|
+
</svg>
|
|
386
|
+
|
|
387
|
+
<!-- Draws as the page scrolls -->
|
|
388
|
+
<path d="…" [motionScroll]="{ pathLength: [0, 1] }" />
|
|
389
|
+
|
|
390
|
+
<!-- On hover: draws in and runs off the end, round and round; on leave it finishes forward -->
|
|
391
|
+
<path d="…" motionone
|
|
392
|
+
[initial]="{ pathLength: 0, pathOffset: 1 }"
|
|
393
|
+
[whileHover]="{ pathLength: [0, 1, 0], pathOffset: [0, 0, 1], transition: { duration: 1.6, repeat: Infinity } }" />
|
|
394
|
+
|
|
395
|
+
<!-- A dash running round a circle: dash 0.25 + gap 0.75 = 1, so it loops seamlessly -->
|
|
396
|
+
<circle cx="50" cy="50" r="40"
|
|
397
|
+
motionone [initial]="{ pathLength: 0.25, pathSpacing: 0.75, pathOffset: 0 }" [animate]="{ pathOffset: 1 }"
|
|
398
|
+
[transition]="{ duration: 1.2, ease: 'linear', repeat: Infinity }" />
|
|
399
|
+
|
|
400
|
+
<!-- Or follow a value on a spring (a gauge) -->
|
|
401
|
+
<path d="…" [motionStyle]="{ pathLength: length }" />
|
|
402
|
+
<!-- readonly length = mapSignal(springSignal(this.value), [0, 100], [0, 1]); -->
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
- `pathLength`: how much is drawn (0 to 1). `pathOffset`: where the drawn part starts. `pathSpacing`: the gap after each drawn part (default 1).
|
|
406
|
+
- They are fractions of the shape's own length: the shape gets `pathLength="1"`, and `stroke-dasharray` / `stroke-dashoffset` are set for you.
|
|
407
|
+
- With round or square line caps a line at `pathLength: 0` still shows a dot, so fade it in too (`opacity: 0` → `1`).
|
|
408
|
+
- Animating only `pathOffset` or `pathSpacing` keeps the dash length it had.
|
|
409
|
+
|
|
371
410
|
### Layout Animations
|
|
372
411
|
Like Framer Motion's `layout` and `layoutId`. Change the layout however you like (reorder or filter a list, switch flex to grid, toggle a class) and `motionLayout` animates each element from where it was drawn to where it is now. No positions to calculate.
|
|
373
412
|
|
|
@@ -504,6 +543,7 @@ The directives work seamlessly with Angular's new control flow syntax:
|
|
|
504
543
|
| `<Reorder.Group values onReorder>` / `<Reorder.Item value>` | `[(motionReorderGroup)]` / `[motionReorderItem]` |
|
|
505
544
|
| `useMotionValue`, `useTransform`, `useSpring`, `useVelocity`, `useScroll` | `motionSignal`, `mapSignal`, `springSignal`, `velocitySignal`, `scrollSignal` |
|
|
506
545
|
| `style={{ x, opacity }}` with motion values | `[motionStyle]="{ x: x, opacity: opacity }"` |
|
|
546
|
+
| `pathLength`, `pathOffset`, `pathSpacing` | the same keys in `[initial]`, `[animate]`, `[whileHover]`, `[motionScroll]`, `[motionStyle]` |
|
|
507
547
|
| `layout`, `layout="position"` | `motionLayout`, `motionLayout="position"` |
|
|
508
548
|
| `layoutId` | `motionLayoutId` |
|
|
509
549
|
| `<LayoutGroup>` | not needed: every layout element is measured after every render |
|
|
@@ -523,6 +563,7 @@ A lot of animation is plain CSS now, and where CSS is enough it is the better ch
|
|
|
523
563
|
| Sequences | ⚠️ Percentages in one `@keyframes`; several elements need hand-tuned delays | Timelines, `repeat` with `mirror` / `reverse`, per-property transitions |
|
|
524
564
|
| Play once when scrolled into view | ❌ No trigger yet (`animation-trigger` is experimental); needs IntersectionObserver | `runInView`, `whileInView` |
|
|
525
565
|
| Scroll-linked values | ✅ / ⚠️ `animation-timeline: scroll()` / `view()` in Chrome and Safari 26, not in Firefox by default | Same API everywhere (uses the native timeline where available), plus `progress()` and `velocity()` signals |
|
|
566
|
+
| SVG line drawing | ✅ / ⚠️ `pathLength="1"` plus a `stroke-dashoffset` transition or `@keyframes` covers simple hover, enter and looping spinners; no springs, a loop snaps back when hover ends, no in-view trigger, scroll-linked only with scroll timelines | Hover that springs or loops and settles on leave; draw when scrolled into view, staggered; scroll-linked in every browser; undraw on exit; follow a value on a spring |
|
|
526
567
|
| Magnetic, tilt, follow-pointer | ❌ CSS can't read the pointer position | `motionMagnetic`, `motionTilt` (sets `--motion-pointer-x/y` for CSS), `motionFollowPointer` |
|
|
527
568
|
| Drag | ❌ | Constraints, elastic edges, momentum, snap points |
|
|
528
569
|
| Pan and swipe | ❌ Only `touch-action` and scroll snap | Offset, delta, velocity and swipe direction |
|
|
@@ -536,6 +577,12 @@ A lot of animation is plain CSS now, and where CSS is enough it is the better ch
|
|
|
536
577
|
|
|
537
578
|
## Changelog
|
|
538
579
|
|
|
580
|
+
**2.5.0**
|
|
581
|
+
- SVG line drawing: `pathLength`, `pathOffset` and `pathSpacing` on any SVG shape, in every state (`initial`, `animate`, `whileHover`, `whileInView`, `exit`), in `[motionScroll]` and in `[motionStyle]`. See [SVG Line Drawing](#svg-line-drawing).
|
|
582
|
+
- Fixed: SVG shapes (path, circle…) with styles in `initial`, such as `opacity: 0`, stayed at those values. The starting values are now written as attributes, which is how Motion animates SVG, so the animation shows.
|
|
583
|
+
- Fixed: Motion's easing names (`'linear'`, `'easeOut'`, `'backOut'`, `'anticipate'`…) and easing functions were replaced by ease-in-out. They are now passed to Motion as given.
|
|
584
|
+
- Fixed: `springSignal` following a value that changes every frame (pointer, drag) no longer jitters: the spring keeps its position and speed, and only its target moves.
|
|
585
|
+
|
|
539
586
|
**2.4.0**
|
|
540
587
|
- Motion values as Angular signals: `motionSignal`, `mapSignal`, `springSignal`, `velocitySignal` and `scrollSignal`, and the `[motionStyle]` directive, which writes them to the element without change detection. See [Motion Values](#motion-values).
|
|
541
588
|
|
|
@@ -651,6 +698,8 @@ interface TransitionOptions {
|
|
|
651
698
|
- `'ease-in'` - CSS ease-in
|
|
652
699
|
- `'ease-out'` - CSS ease-out
|
|
653
700
|
- `'ease-in-out'` - CSS ease-in-out
|
|
701
|
+
- `'linear'`, `'easeIn'`, `'easeOut'`, `'easeInOut'`, `'circIn'`, `'circOut'`, `'circInOut'`, `'backIn'`, `'backOut'`, `'backInOut'`, `'anticipate'` - Motion's named easings
|
|
702
|
+
- `(t) => number` - An easing function
|
|
654
703
|
- `[0.4, 0, 0.2, 1]` - Custom cubic bezier
|
|
655
704
|
- `{ type: 'spring', stiffness: 400, damping: 17 }` - Spring physics
|
|
656
705
|
|
|
@@ -227,6 +227,69 @@ i0.ɵɵngDeclareClassMetadata({
|
|
|
227
227
|
{ type: NgZone }
|
|
228
228
|
]
|
|
229
229
|
});
|
|
230
|
+
const SVG_PATH_KEYS = /* @__PURE__ */ new Set([
|
|
231
|
+
"pathLength",
|
|
232
|
+
"pathOffset",
|
|
233
|
+
"pathSpacing"
|
|
234
|
+
]);
|
|
235
|
+
function isSvgPathKey(key) {
|
|
236
|
+
return SVG_PATH_KEYS.has(key);
|
|
237
|
+
}
|
|
238
|
+
function readSvgPath(el) {
|
|
239
|
+
if (!el.hasAttribute("pathLength")) return {};
|
|
240
|
+
const out = {};
|
|
241
|
+
const dash = el.getAttribute("stroke-dasharray")?.trim().split(/[\s,]+/).map(parseFloat);
|
|
242
|
+
if (dash && !isNaN(dash[0])) {
|
|
243
|
+
out.pathLength = dash[0];
|
|
244
|
+
if (dash.length > 1 && !isNaN(dash[1]) && dash[1] !== 1) out.pathSpacing = dash[1];
|
|
245
|
+
}
|
|
246
|
+
const offset = parseFloat(el.getAttribute("stroke-dashoffset") ?? "");
|
|
247
|
+
if (!isNaN(offset) && offset !== 0) out.pathOffset = -offset;
|
|
248
|
+
return out;
|
|
249
|
+
}
|
|
250
|
+
function applySvgPath(el, values) {
|
|
251
|
+
if (typeof SVGElement === "undefined" || !(el instanceof SVGElement)) return;
|
|
252
|
+
const current = readSvgPath(el);
|
|
253
|
+
for (const key of SVG_PATH_KEYS) {
|
|
254
|
+
if (!(key in values)) continue;
|
|
255
|
+
const v = values[key];
|
|
256
|
+
current[key] = v == null || v === "" ? void 0 : Number(v);
|
|
257
|
+
}
|
|
258
|
+
if (current.pathLength === void 0 && current.pathOffset === void 0 && current.pathSpacing === void 0) {
|
|
259
|
+
el.removeAttribute("pathLength");
|
|
260
|
+
el.removeAttribute("stroke-dasharray");
|
|
261
|
+
el.removeAttribute("stroke-dashoffset");
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
el.setAttribute("pathLength", "1");
|
|
265
|
+
el.setAttribute("stroke-dasharray", `${current.pathLength ?? 1} ${current.pathSpacing ?? 1}`);
|
|
266
|
+
el.setAttribute("stroke-dashoffset", `${-(current.pathOffset ?? 0)}`);
|
|
267
|
+
}
|
|
268
|
+
function withSvgPathContext(target, keyframes) {
|
|
269
|
+
if (typeof SVGElement === "undefined" || !(target instanceof SVGElement) || !keyframes || typeof keyframes !== "object") return keyframes;
|
|
270
|
+
if (!Object.keys(keyframes).some(isSvgPathKey)) return keyframes;
|
|
271
|
+
const current = readSvgPath(target);
|
|
272
|
+
const out = { ...keyframes };
|
|
273
|
+
const defaults = {
|
|
274
|
+
pathLength: 1,
|
|
275
|
+
pathOffset: 0,
|
|
276
|
+
pathSpacing: 1
|
|
277
|
+
};
|
|
278
|
+
for (const key of SVG_PATH_KEYS) {
|
|
279
|
+
const v = out[key];
|
|
280
|
+
if (v === void 0 || Array.isArray(v)) continue;
|
|
281
|
+
const k = key;
|
|
282
|
+
out[key] = [current[k] ?? defaults[k], v];
|
|
283
|
+
}
|
|
284
|
+
const hold = (key, fallback) => {
|
|
285
|
+
const v = current[key] ?? fallback;
|
|
286
|
+
if (!(key in out) && v !== void 0) out[key] = [v, v];
|
|
287
|
+
};
|
|
288
|
+
hold("pathLength", 1);
|
|
289
|
+
hold("pathSpacing");
|
|
290
|
+
hold("pathOffset");
|
|
291
|
+
return out;
|
|
292
|
+
}
|
|
230
293
|
const noopControls = () => ({ stop: () => {} });
|
|
231
294
|
const linear = (t) => t;
|
|
232
295
|
var MotionAnimationService = class MotionAnimationService {
|
|
@@ -236,7 +299,7 @@ var MotionAnimationService = class MotionAnimationService {
|
|
|
236
299
|
animate(target, keyframes, options) {
|
|
237
300
|
if (!this.isBrowser) return noopControls();
|
|
238
301
|
try {
|
|
239
|
-
return this.ngZone.runOutsideAngular(() => animate(target, keyframes, options));
|
|
302
|
+
return this.ngZone.runOutsideAngular(() => animate(target, withSvgPathContext(target, keyframes), options));
|
|
240
303
|
} catch (e) {
|
|
241
304
|
console.error("[ngx-motion] Error in animate:", e);
|
|
242
305
|
return noopControls();
|
|
@@ -303,6 +366,19 @@ i0.ɵɵngDeclareClassMetadata({
|
|
|
303
366
|
args: [{ providedIn: "root" }]
|
|
304
367
|
}]
|
|
305
368
|
});
|
|
369
|
+
const MOTION_EASINGS = /* @__PURE__ */ new Set([
|
|
370
|
+
"linear",
|
|
371
|
+
"easeIn",
|
|
372
|
+
"easeOut",
|
|
373
|
+
"easeInOut",
|
|
374
|
+
"circIn",
|
|
375
|
+
"circOut",
|
|
376
|
+
"circInOut",
|
|
377
|
+
"backIn",
|
|
378
|
+
"backOut",
|
|
379
|
+
"backInOut",
|
|
380
|
+
"anticipate"
|
|
381
|
+
]);
|
|
306
382
|
function getEasing(easingString, motionAnimation) {
|
|
307
383
|
if (!easingString) return motionAnimation.easeInOut;
|
|
308
384
|
if (Array.isArray(easingString)) return easingString;
|
|
@@ -310,11 +386,13 @@ function getEasing(easingString, motionAnimation) {
|
|
|
310
386
|
stiffness: easingString.stiffness || 100,
|
|
311
387
|
damping: easingString.damping || 10
|
|
312
388
|
});
|
|
389
|
+
if (typeof easingString === "function") return easingString;
|
|
313
390
|
if (typeof easingString === "string") {
|
|
314
391
|
if (easingString === "ease-in") return motionAnimation.easeIn;
|
|
315
392
|
if (easingString === "ease-out") return motionAnimation.easeOut;
|
|
316
393
|
if (easingString === "ease-in-out" || easingString === "ease") return motionAnimation.easeInOut;
|
|
317
394
|
if (easingString === "spring") return motionAnimation.spring();
|
|
395
|
+
if (MOTION_EASINGS.has(easingString)) return easingString;
|
|
318
396
|
}
|
|
319
397
|
return motionAnimation.easeInOut;
|
|
320
398
|
}
|
|
@@ -909,6 +987,7 @@ function transformFunction(key, value) {
|
|
|
909
987
|
default: return `${key}(${v})`;
|
|
910
988
|
}
|
|
911
989
|
}
|
|
990
|
+
const kebab = (key) => key.replace(/[A-Z]/g, (c) => "-" + c.toLowerCase());
|
|
912
991
|
function firstFrame(value) {
|
|
913
992
|
return Array.isArray(value) ? value[0] : value;
|
|
914
993
|
}
|
|
@@ -916,12 +995,21 @@ function applyInlineStyles(el, styles) {
|
|
|
916
995
|
if (!el?.style || !styles) return;
|
|
917
996
|
const transforms = [];
|
|
918
997
|
let rawTransform;
|
|
998
|
+
let path;
|
|
999
|
+
const svgShape = typeof SVGElement !== "undefined" && el instanceof SVGElement && !(el instanceof SVGSVGElement);
|
|
919
1000
|
for (const key of Object.keys(styles)) {
|
|
920
1001
|
const value = firstFrame(styles[key]);
|
|
921
1002
|
if (value === void 0 || value === null) continue;
|
|
922
1003
|
if (key === "transform") rawTransform = String(value);
|
|
923
|
-
else if (
|
|
1004
|
+
else if (isSvgPathKey(key)) (path ??= {})[key] = value;
|
|
1005
|
+
else if (!TRANSFORM_KEYS.has(key)) {
|
|
1006
|
+
if (svgShape && !key.startsWith("--")) {
|
|
1007
|
+
el.style.removeProperty(kebab(key));
|
|
1008
|
+
el.setAttribute(kebab(key), String(value));
|
|
1009
|
+
} else el.style[key] = value;
|
|
1010
|
+
}
|
|
924
1011
|
}
|
|
1012
|
+
if (path) applySvgPath(el, path);
|
|
925
1013
|
for (const key of TRANSFORM_ORDER) if (key in styles) {
|
|
926
1014
|
const value = firstFrame(styles[key]);
|
|
927
1015
|
if (value !== void 0 && value !== null) transforms.push(transformFunction(key, value));
|
|
@@ -4413,6 +4501,7 @@ var MotionStyleDirective = class MotionStyleDirective {
|
|
|
4413
4501
|
});
|
|
4414
4502
|
}
|
|
4415
4503
|
write(key, value) {
|
|
4504
|
+
if (isSvgPathKey(key)) return applySvgPath(this.el, { [key]: value });
|
|
4416
4505
|
if (TRANSFORMS.includes(key)) {
|
|
4417
4506
|
if (value == null) this.transform.delete(key);
|
|
4418
4507
|
else this.transform.set(key, value);
|