npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

leaflet.marker-motion-react

v0.1.5

Published

<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --> [![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors-) <!-- ALL-CONTRIBUTORS-BADGE:END -->

Downloads

6

Readme

Leaflet.MarkerMotion React Wrapper

All Contributors

Leaflet.MarkerMotion is a powerful open-source plugin for Leaflet that enables smooth marker animation along predefined paths. This documentation covers the React wrapper for Leaflet.MarkerMotion, which allows easy integration with React-Leaflet projects.

Demo

Check out our live demo: https://leaflet-marker-motion-react-5jbe.vercel.app

Installation

Install Leaflet.MarkerMotion and its React wrapper via npm:

npm install leaflet.marker-motion-react

Usage

Here's a basic example of how to use the Leaflet.MarkerMotion React wrapper:

import { useRef, useState } from "react";
import L from "leaflet";
import MarkerMotion from "leaflet.marker-motion-react";
import { MapContainer, TileLayer, Polyline } from "react-leaflet";
import Control from "react-leaflet-custom-control";
import "leaflet/dist/leaflet.css";

const icon = L.icon({
  iconUrl: "./car.png",
  iconSize: [38, 38],
  iconAnchor: [19, 19],
});

const points = [
  [22.614407, -103.009848],
  [22.622247, -103.006986],
  // ... more points ...
  [22.616452, -102.997295],
];

function App() {
  const markerMotionRef = useRef(null);
  const [speed, setSpeed] = useState(4000);

  return (
    <MapContainer
      center={[22.634087, -102.983227]}
      zoom={14}
      style={{ height: "100vh", width: "100%" }}
    >
      <TileLayer
        attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
        url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
      />
      <Polyline pathOptions={{ color: "blue" }} positions={points} />
      <MarkerMotion
        ref={markerMotionRef}
        path={points}
        speedInKmH={speed}
        options={{
          icon,
          rotation: true,
          autoplay: true,
          loop: true,
        }}
        eventHandlers={{
          "motion.start": () => {
            console.log("motion start");
          },
          "motion.pause": () => {
            console.log("motion pause");
          },
          "motion.reset": () => {
            console.log("motion reset");
          },
          "motion.end": () => {
            console.log("motion end");
          },
          "motion.segment": (data) => {
            console.log("motion segment", data.index);
          },
        }}
      />
      <Control prepend position="topright">
        {/* Control buttons */}
      </Control>
    </MapContainer>
  );
}

export default App;

API Reference

MarkerMotion Component

The MarkerMotion component is the main component provided by the React wrapper.

Props

  • path: Array of [lat, lng] points defining the path.
  • speedInKmH: Speed of the marker in kilometers per hour.
  • options: Optional Leaflet marker options and MarkerMotion-specific options.
    • rotation (boolean): Updates the rotation angle of the marker based on its current position and next point in the path.
    • autoplay (boolean): Starts animation automatically when added to the map.
    • loop (boolean): Restarts the animation from the beginning when it reaches the end of the path.
    • All standard Leaflet marker options are also supported like icon
  • eventHandlers: Object containing event handler functions for MarkerMotion events.

Ref Methods

The MarkerMotion component can be controlled using a ref. The following methods are available:

  • start(): Starts or resumes the motion of the marker along the path.
  • pause(): Pauses the motion of the marker.
  • reset(): Stops the motion of the marker and resets it to the starting position.
  • setProgress(index): Sets the progress of the marker to a specific segment of the path.

Events

The following events can be handled using the eventHandlers prop:

  • motion.start: Fired when the motion starts or resumes.
  • motion.pause: Fired when the motion is paused.
  • motion.reset: Fired when the marker is reset to its starting position.
  • motion.end: Fired when the marker reaches the end of the path.
  • motion.segment: Fired when the marker enters a new segment of the path. Returns an object with the current segment index.

Example: Controlling MarkerMotion

Here's an example of how to control the MarkerMotion component using buttons:

<Control position="topright">
  <div className="leaflet-bar leaflet-control">
    <button onClick={() => markerMotionRef.current.start()}>
      Start
    </button>
    <button onClick={() => markerMotionRef.current.pause()}>
      Pause
    </button>
    <button onClick={() => markerMotionRef.current.reset()}>
      Reset
    </button>
    <button
      onClick={() => {
        const randomIndex = Math.floor(Math.random() * points.length);
        markerMotionRef.current.setProgress(randomIndex);
      }}
    >
      Random progress
    </button>
    <button onClick={() => setSpeed((s) => Math.max(s - 500, 1000))}>
      Slower
    </button>
    <button onClick={() => setSpeed((s) => Math.min(s + 1000, 10000))}>
      Faster
    </button>
  </div>
</Control>

This example demonstrates how to use the ref methods to control the marker's motion and how to adjust the speed dynamically.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!