Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.DS_Store
.DS_Store
node_modules/
package-lock.json
209 changes: 165 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,73 +1,194 @@
# Leaflet Animated Marker
[![CDNJS](https://img.shields.io/cdnjs/v/leaflet.AnimatedMarker.svg)](https://cdnjs.com/libraries/leaflet.AnimatedMarker)

This is a Leaflet plugin for animating a marker along a polyline. Check out the [demo](http://openplans.github.com/Leaflet.AnimatedMarker/). Feedback appreciated!
[![npm version](https://img.shields.io/npm/v/leaflet.animatedmarker.svg)](https://www.npmjs.com/package/leaflet.animatedmarker)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

## How does it work?
A Leaflet plugin for animating a marker along a polyline.

It uses CSS3 animations to move the marker from point to point at a specific rate (meter per millisecond). For ancient browsers that don't support CSS3, the polyline is chunked into `distance` segments and moved per `interval` (not so great).
Check out the [demo](http://openplans.github.com/Leaflet.AnimatedMarker/).

## How can I use it?
## Requirements

The following code will create an AnimatedMarker that moves along `line`, assuming a `Leaflet.Map` called `map`.
- Leaflet ^1.0.0

var line = L.polyline([[40.68510, -73.94136],[40.68576, -73.94149],[40.68649, -73.94165]]),
animatedMarker = L.animatedMarker(line.getLatLngs());
## Installation

map.addLayer(animatedMarker);
### npm

## How do I change the rate?
```bash
npm install leaflet.animatedmarker
```

### CDN

```html
<script src="https://unpkg.com/leaflet.animatedmarker/src/AnimatedMarker.js"></script>
```

## Usage

### Basic

```javascript
const line = L.polyline([
[40.68510, -73.94136],
[40.68576, -73.94149],
[40.68649, -73.94165]
]);

const animatedMarker = L.animatedMarker(line.getLatLngs());
map.addLayer(animatedMarker);
```

### With ES Modules

```javascript
import L from 'leaflet';
import 'leaflet.animatedmarker';

const animatedMarker = L.animatedMarker(line.getLatLngs());
```

var animatedMarker = L.animatedMarker(line.getLatLngs(), {
distance: 300, // meters
interval: 2000, // milliseconds
});
### TypeScript

TypeScript definitions are included. The module augments Leaflet's types:

## What if I don't want it to animate right away? Or need to stop it halfway through?
```typescript
import L from 'leaflet';
import 'leaflet.animatedmarker';

var animatedMarker = L.animatedMarker(line.getLatLngs(), {
autoStart: false
});
const marker: L.AnimatedMarker = L.animatedMarker(line.getLatLngs(), {
distance: 300,
interval: 2000,
autoStart: false,
onEnd: () => console.log('Animation complete!')
});
```

// Start when you're ready
animatedMarker.start();
## Options

setTimeout(function() {
// Stop the animation
animatedMarker.stop();
}, 2000);
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `distance` | `number` | `200` | Distance in meters to travel per interval |
| `interval` | `number` | `1000` | Time in milliseconds to travel the distance |
| `autoStart` | `boolean` | `true` | Start animating immediately when added to the map |
| `onEnd` | `function` | `() => {}` | Callback when animation reaches the end |

## Can I give it a custom icon?
Plus all standard [Leaflet Marker options](https://leafletjs.com/reference.html#marker-option).

Yep! Just like a standard Leaflet.Marker layer.
## Methods

var myIcon = L.icon({
iconUrl: 'myicon.png'
});
### `start()`

var animatedMarker = L.animatedMarker(line.getLatLngs(), {
icon: myIcon
});
Start the animation from the beginning.

## Can I make the marker explode when it gets to the end of the line?
```javascript
animatedMarker.start();
```

Sure! Just use the `onEnd` callback.
### `stop()`

var animatedMarker = L.animatedMarker(line.getLatLngs(), {
onEnd: function() {
// TODO: blow up this marker
}
});

## Install via NPM
Stop the animation at the current position.

```javascript
animatedMarker.stop();
```
npm install -S leaflet.animatedmarker

### `setLine(latlngs)`

Set a new path for the marker to follow.

```javascript
animatedMarker.setLine(newPolyline.getLatLngs());
```

### Inclusion via npm
## Examples

### Custom Speed

```javascript
const animatedMarker = L.animatedMarker(line.getLatLngs(), {
distance: 300, // meters
interval: 2000 // milliseconds
});
```
require('leaflet.animatedmarker/src/AnimatedMarker');

### Manual Control

```javascript
const animatedMarker = L.animatedMarker(line.getLatLngs(), {
autoStart: false
});

// Start when ready
animatedMarker.start();

// Stop after 5 seconds
setTimeout(() => {
animatedMarker.stop();
}, 5000);
```

### Custom Icon

```javascript
const myIcon = L.icon({
iconUrl: 'myicon.png'
});

const animatedMarker = L.animatedMarker(line.getLatLngs(), {
icon: myIcon
});
```

### Callback on Complete

```javascript
const animatedMarker = L.animatedMarker(line.getLatLngs(), {
onEnd: function() {
console.log('Animation finished!');
map.removeLayer(this);
}
});
```

## Development

### Install dependencies

```bash
npm install
```

### Run tests

```bash
npm test
```

### Run tests in watch mode

```bash
npm run test:watch
```

## Changelog

### 1.1.0

- **Fixed:** Marker no longer floats to new position when zooming during animation
- **Added:** TypeScript type definitions
- **Added:** UMD module support (AMD, CommonJS, browser globals)
- **Added:** Vitest test suite
- **Changed:** Modernized to ES6 syntax
- **Changed:** Renamed `clickable` option to `interactive` (Leaflet 1.x naming)
- **Removed:** Legacy fallback for browsers without CSS3 transitions
- **Removed:** Bower support (deprecated)

### 1.0.0

- Initial release

## License

MIT License - [OpenPlans](https://www.openplans.org/)
23 changes: 0 additions & 23 deletions bower.json

This file was deleted.

Loading