# AvalynxAnimate

[![npm version](https://jbs-newmedia.de/badge/npm/avalynx-animate/version.svg)](https://www.npmjs.com/package/avalynx-animate)
[![npm downloads](https://jbs-newmedia.de/badge/npm/avalynx-animate/download.svg)](https://www.npmjs.com/package/avalynx-animate)
[![Composer version](https://jbs-newmedia.de/badge/composer/avalynx/avalynx-animate/version.svg)](https://packagist.org/packages/avalynx/avalynx-animate)
[![Composer downloads](https://jbs-newmedia.de/badge/composer/avalynx/avalynx-animate/download.svg)](https://packagist.org/packages/avalynx/avalynx-animate)
[![jsDelivr](https://jbs-newmedia.de/badge/jsdelivr/avalynx-animate/hits.svg)](https://www.jsdelivr.com/package/npm/avalynx-animate)
[![License](https://jbs-newmedia.de/badge/npm/avalynx-animate/license.svg)](LICENSE)
[![Tests](https://jbs-newmedia.de/badge/github/avalynx/avalynx-animate/tests.svg)](https://github.com/avalynx/avalynx-animate/actions/workflows/tests.yml)
[![codecov](https://jbs-newmedia.de/badge/codecov/avalynx/avalynx-animate/coverage.svg)](https://codecov.io/gh/avalynx/avalynx-animate)
[![GitHub stars](https://jbs-newmedia.de/badge/github/avalynx/avalynx-animate/stars.svg)](https://github.com/avalynx/avalynx-animate)

AvalynxAnimate is a modern, lightweight, and easy-to-use animation library built with vanilla JavaScript — without any framework dependencies. Use scroll-position triggers for precise layout-based timing or visibility-observer triggers for animations that start when elements enter the viewport.

## Features

- **Two trigger modes**: `trigger: 'scroll'` calculates start points from layout position and `offset` (default `120`); `trigger: 'observer'` starts at viewport entry and accepts `rootMargin`.
- **Modern Engine**: one passive scroll handler coalesced per animation frame, `ResizeObserver` based recalculation when the layout changes, CSS custom properties for timing — and elements at the very end of a document still animate.
- **Progressive Enhancement**: without JavaScript nothing is hidden — initial states are class-based, not attribute-based.
- **New Animations**: `blur-in*`, `pop*`, `tilt-in-*`, `roll-in-*` and GPU-friendly `clip-path` masks (`mask-wipe-*`, `mask-circle`).
- **Reduced Motion**: respects `prefers-reduced-motion` and shows content immediately.
- **DOM Observation**: optional MutationObserver support animates elements added after init (`observeDom: true`).
- **Hover Animations**: micro-interactions like `shake`, `grow` or `pop` on mouse over, via `data-avalynx-animate-hover`.
- **Lightweight**: zero dependencies.

## Examples

* [Overview](https://avalynx-animate.jbs-newmedia.de/examples/index.html)
* [Basic animations](https://avalynx-animate.jbs-newmedia.de/examples/basic.html)
* [All animations](https://avalynx-animate.jbs-newmedia.de/examples/all-animations.html)
* [Options](https://avalynx-animate.jbs-newmedia.de/examples/options.html)

## Installation

Include AvalynxAnimate in your HTML file:

```html
<link rel="stylesheet" href="path/to/avalynx-animate.css">
<script src="path/to/avalynx-animate.js"></script>
```

## Installation via jsDelivr ([Link](https://cdn.jsdelivr.net/npm/avalynx-animate/))

```html
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/avalynx-animate@1.0.0/dist/css/avalynx-animate.css">
<script src="https://cdn.jsdelivr.net/npm/avalynx-animate@1.0.0/dist/js/avalynx-animate.js"></script>
```

## Installation via NPM ([Link](https://www.npmjs.com/package/avalynx-animate))

```bash
npm install avalynx-animate
```

```javascript
import { AvalynxAnimate } from 'avalynx-animate';
import 'avalynx-animate/dist/css/avalynx-animate.css';
```

## Installation via Symfony AssetMapper

```bash
php bin/console importmap:require avalynx-animate
```

Import the library and its stylesheet in your application entry point:

```javascript
import { AvalynxAnimate } from 'avalynx-animate';
import 'avalynx-animate/dist/css/avalynx-animate.css';
```

## Installation via Symfony AssetComposer

More information about the Symfony AssetComposer Bundle can be found [here](https://github.com/jbsnewmedia/asset-composer-bundle).

```twig
{% do addAssetComposer('avalynx/avalynx-animate/dist/css/avalynx-animate.css') %}
{% do addAssetComposer('avalynx/avalynx-animate/dist/js/avalynx-animate.js') %}
```

## Installation via Composer ([Link](https://packagist.org/packages/avalynx/avalynx-animate))

```bash
composer require avalynx/avalynx-animate
```

```html
<link rel="stylesheet" href="vendor/avalynx/avalynx-animate/dist/css/avalynx-animate.css">
<script src="vendor/avalynx/avalynx-animate/dist/js/avalynx-animate.js"></script>
```

## Usage

Auto init:

```javascript
AvalynxAnimate.init({ duration: 600 });
```

Re-scan and recalculate after DOM or layout changes:

```javascript
AvalynxAnimate.refresh();
```

Own instance with a custom selector:

```javascript
new AvalynxAnimate('.my-animated', { observeDom: true });
```

### Markup

```html
<!-- Animation namespace -->
<div data-avalynx-animate="blur-in-up" data-avalynx-animate-delay="200"></div>

<!-- Scroll trigger: when the center reaches the bottom of the viewport -->
<div data-avalynx-animate="zoom-in" data-avalynx-animate-anchor-placement="center-bottom"></div>

<!-- Observer trigger, selected per element -->
<div data-avalynx-animate="fade-up" data-avalynx-animate-trigger="observer"></div>
```

Per-element options use the `data-avalynx-animate-*` namespace: `duration`, `delay`, `easing`, `trigger`, `offset`, `anchor` (selector of the element whose position triggers), `anchor-placement`, `once` and `mirror`. The global `offset` is only applied when an element defines neither its own offset nor its own anchor placement.

Animated elements receive the `avalynx-animate-in` class — use it for custom styles of the animated state.

## Options

Defaults are optimized for layout-based scroll triggers.

- `duration`: (number) Default animation duration in milliseconds (default: `400`).
- `delay`: (number) Default animation delay in milliseconds, applied when animating in (default: `0`).
- `easing`: (string) Default CSS timing function or named easing such as `ease-out-back` (default: `'ease'`).
- `trigger`: (string) Trigger mode, `'scroll'` or `'observer'` (default: `'scroll'`). May be overridden by `data-avalynx-animate-trigger`.
- `rootMargin`: (string|number) Margin for the observer trigger in `px` or `%`; a single value (numbers are pixels) is applied to the bottom (default: `'0px'`).
- `offset`: (number) Offset in pixels from the original trigger point (default: `120`).
- `anchorPlacement`: (string) Which part of the element has to reach which line of the viewport: `top-bottom`, `center-bottom`, `bottom-bottom`, `top-center`, `center-center`, `bottom-center`, `top-top`, `center-top` or `bottom-top` (default: `'top-bottom'`).
- `once`: (boolean) Animate only once and keep elements visible after entering (default: `false`).
- `mirror`: (boolean) In observer mode, animate out after any exit; in scroll mode, animate out after passing the element (default: `false`, requires `once: false`).
- `disable`: (boolean|`'mobile'`|function) Disable animations entirely, on small screens or when the function returns `true` (default: `false`).
- `respectReducedMotion`: (boolean) Skip animations when the user prefers reduced motion (default: `true`).
- `observeDom`: (boolean) Watch the DOM for added or removed elements and refresh automatically (default: `false`).
- `onAnimate`: (function) Callback whenever an element has been animated in (default: `null`).

### Trigger modes

The default `'scroll'` mode calculates trigger points from the element's layout position, anchor placement and offset. It animates out while scrolling back above the trigger unless `once: true`; `mirror: true` also animates out after passing an element.

Choose `'observer'` when actual viewport visibility should control the effect. Observer mode uses `IntersectionObserver`, accepts `rootMargin`, and by default animates out only when leaving below or to the right of the observer area; `mirror: true` animates out after every exit. If `IntersectionObserver` is unavailable or rejects the `rootMargin`, elements are shown immediately.

```javascript
AvalynxAnimate.init({ trigger: 'observer', rootMargin: '0px 0px -20% 0px', once: true });
```

Content stays visible without JavaScript, reduced motion is respected, and the animated state uses the single `avalynx-animate-in` class. Layout changes are recalculated through `ResizeObserver`.

## Animations

**Classic set**: `fade`, `fade-up`, `fade-down`, `fade-left`, `fade-right`, `fade-up-right`, `fade-up-left`, `fade-down-right`, `fade-down-left`, `flip-left`, `flip-right`, `flip-up`, `flip-down`, `slide-up`, `slide-down`, `slide-left`, `slide-right`, `zoom-in`, `zoom-in-up`, `zoom-in-down`, `zoom-in-left`, `zoom-in-right`, `zoom-out`, `zoom-out-up`, `zoom-out-down`, `zoom-out-left`, `zoom-out-right`.

**New set**: `blur-in`, `blur-in-up`, `blur-in-down`, `blur-in-left`, `blur-in-right`, `pop`, `pop-up`, `pop-down`, `tilt-in-left`, `tilt-in-right`, `roll-in-left`, `roll-in-right`, `mask-wipe-left`, `mask-wipe-right`, `mask-wipe-up`, `mask-wipe-down`, `mask-circle`.

## Hover animations

Micro-interactions on mouse over — pure AvalynxAnimate, no extra CSS needed:

```html
<!-- hover the card, only the image reacts -->
<div class="card" data-avalynx-animate-hover="shake" data-avalynx-animate-hover-target=".card-img-top">
    <img src="..." class="card-img-top" alt="...">
</div>

<!-- without a target the element itself reacts -->
<img src="..." alt="..." data-avalynx-animate-hover="grow">
```

One-shot effects (`shake`, `jiggle`, `pop`, `bounce`, `tada`, `heartbeat`) re-arm on every new hover, held states (`grow`, `shrink`) stay while hovering. Like all animations they respect `prefers-reduced-motion`.

## Contributing

Contributions are welcome! If you'd like to contribute, please fork the repository and submit a pull request with your changes or improvements. We're looking for contributions in the following areas:

- Bug fixes
- Feature enhancements
- Documentation improvements

Before submitting your pull request, please ensure your changes are well-documented and follow the existing coding style of the project.

## License

AvalynxAnimate is open-sourced software licensed under the [MIT license](LICENSE).

## Contact

If you have any questions, feature requests, or issues, please open an issue on our [GitHub repository](https://github.com/avalynx/avalynx-animate/issues) or submit a pull request.
