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-spline

v0.2.0

Published

Leaflet plugin for drawing bezier splines from polylines and polygons

Downloads

563

Readme

Leaflet-spline is a small plugin for leafletjs that transforms polylines and polygons into bezier splines. Built on top of leaflet.curve, leaflet-spline transforms polylines and polygons into cubic svg bezier curves.

Install

npm install leaflet-spline

Use

You can import leaflet-spline, and L.spline becomes available:

import L from "leaflet";
import "leaflet-spline";

const map = L.map("mapdiv", mapOptions);

const latLngs = [
  [-5.9765, 2.9542],
  [-6.1523, 2.5918],
  [-5.8337, 2.4052],
  [-5.5743, 2.4821],
  [-5.2294, 2.4492],
  [-4.9545, 2.1308],
  [-4.3286, 2.0544],
];

const mySpline = L.spline(latLngs);
mySpline.addTo(map);

You can also import spline directly:

import { spline } from "leaflet-spline";

const mySpline = spline(latLngs);

Note if you are using synthetic default imports with TypeScript, you must import spline in this fashion:

import * as L from "leaflet";
import { spline } from "leaflet-spline";

const mySpline: L.Spline = spline(latLngs);

Options

L.spline takes an array of L.LatLng objects, or an array of LatLng tuples ([number, number]) as its primary argument.

L.spline inherits all options from L.PathOptions. It also offers the smoothing option, which determines where to place the control points used to shape the bezier curves. All options are not required. The smoothing defaults to 0.15, but can be adjusted down for sharper corners. Too high a smoothing value will yield some strange shapes.

const mySpline = L.spline(latLngs, {
  color: "black",
  weight: 5,
  smoothing: 0.1,
});

Methods

An L.Spline inherits all methods from L.Polyline, as well as .trace from L.Curve. Most methods are forwarded to the underlying L.Curve, and all L.Curve methods are available in the underlying ._curve property of an L.Spline.

Closed shapes

leaflet-spline draws polylines by default (as opposed to polygons). If you want the appearance of a closed-polygon shape, you must ensure that your set of points has the exact same coordinate for the first and last entries:

const latLngs = [
  [5.1, 2.9], // First entry \
  [6.1, 2.5], //              \
  [6.2, 2.7], //                -> Must be identical
  [5.8, 2.4], //              /
  [5.1, 2.9], // Last entry  /
];

With a circular point set, you can set the fill to true to create the appearance of a closed shape spline:

const mySpline = L.spline(latLngs, { fill: true });

See the demo for examples.

Alternatives

TurfJS has a bezierSpline module that can be used to similar effect. However, their module works by transforming the original pointset into another pointset with more points interpolated along a bezier spline. This plugin leverages leaflet's use of svgs to not calculate intermediate points, but rather use svg path commands to draw perfectly smooth beziers. For comparison:

TurfJS implementation:

leaflet-spline implementation: