@elfalem/leaflet-curve

中文文档
0.9.2发布于 2023/03/08MITTypeScriptCommonJS零依赖近 30 天下载 3.8万

A Leaflet plugin for drawing Bézier curves and other complex shapes.

原始使用文档
文档目录5 节
  1. Usage
  2. Notes
  3. API
  4. License
  5. Contributions

Leaflet.curve

A Leaflet plugin for drawing Bézier curves and other complex shapes. (DEMO)

Usage

If using directly, include the plugin after Leaflet:

<script src="leaflet.js"></script>
<script src="leaflet.curve.js"></script>

(If rendering using canvas AND using animations, include tween.js as well.)

If using Node.js, install and import:

yarn add @elfalem/leaflet-curve
import L from 'leaflet'  // import { curve, Curve } from 'leaflet'; // for TypeScript
import '@elfalem/leaflet-curve'

Add a path that includes a Bézier curve and straight lines:

var path = L.curve(['M',[50.54136296522163,28.520507812500004],
                    'C',[52.214338608258224,28.564453125000004],
                        [48.45835188280866,33.57421875000001],
                        [50.680797145321655,33.83789062500001],
                    'V',[48.40003249610685],
                    'L',[47.45839225859763,31.201171875],
                        [48.40003249610685,28.564453125000004],'Z'],
                    {color:'red',fill:true}).addTo(map);

Notes

  • This plugin is based on Leaflet version 1.1.0.
  • SVG rendering is best supported. Canvas rendering is also possible.
  • Browser and device compatibility testing is very limited.

API

The first argument to L.curve() is an array of path data. This is composed of commands and coordinates. The commands are a subset of the SVG path specification.

CommandParametersDescription
M[lat,lng]+move to [lat,lng]
L[lat,lng]+line to [lat,lng]
H[lng]+horizontal line to [lng]
V[lat]+vertical line to [lat]
C([lat1,lng1],[lat2,lng2],[lat,lng])+cubic Bézier curve to [lat,lng] with control points at [lat1,lng1] and [lat2,lng2]
S([lat2,lng2],[lat,lng])+cubic bézier curve to [lat,lng] with control points at reflection of second control point of previous curve [lat1,lng1] and [lat2,lng2]
Q([lat1,lng1],[lat,lng])+quadratic Bézier curve to [lat,lng] with control point at [lat1,lng1]
T([lat,lng])+quadratic Bézier curve to [lat,lng] with control point at reflection of control point of previous curve [lat1,lng1]
Zclose path (line to M)

Note that only absolute commands (uppercase) are implemented. It's possible to approximate elliptical arcs (command 'A') with Bézier curves (the elliptical-arc branch implements this command if you're interested).

The L.Curve class extends L.Path so options, events, and methods inherited from L.Path are available. The following new option and methods are introduced by L.Curve.

Options

OptionTypeDefaultDescription
animateInteger or animate options objectundefinedAnimates the curve. It takes a parameter which is the same one provided to the options parameter of Element.animate() method. Note: Web Animations API support varies across browsers.

Methods

MethodReturnsDescription
setPath(pathData[])thisReplaces the current path with the given array of commands and coordinates.
getPath()pathData[]Returns array of the commands and coordinates in the path.
setLatLngs(pathData[])thisAlias to method this.setPath(pathData[]) using the naming convention of L.Polyline and other Leaflet components.
getLatLngs()pathData[] Alias to method this.getPath() using the naming convention of L.Polyline and other Leaflet components.
trace(samplingDistance[])latLng[]Returns array of points that lie on the curve at the given distances. Sampling distance is a decimal value between 0 and 1 inclusive and is applied to each segment (i.e. command) of the curve. See DEMO for example.

License

MIT

Contributions

  • Bug reports and pull requests are welcome!

版本与发布记录

@elfalem/leaflet-curve 共发布 15 个版本,首次发布于 2018/11/18;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.9.2latest2023/03/083 年前
0.9.12021/11/075 年前
0.9.02021/11/045 年前
0.8.22021/07/135 年前
0.8.12021/07/135 年前
0.8.02021/05/095 年前

关于 @elfalem/leaflet-curve 的常见问题

@elfalem/leaflet-curve 是什么?

A Leaflet plugin for drawing Bézier curves and other complex shapes.

如何安装 @elfalem/leaflet-curve?

在项目目录运行 npm install @elfalem/leaflet-curve,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:leaflet(^1.1.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@elfalem/leaflet-curve 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@elfalem/leaflet-curve 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@elfalem/leaflet-curve 可以商用吗?

@elfalem/leaflet-curve 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@elfalem/leaflet-curve 的下载量有多少?

根据 npm 官方下载统计,@elfalem/leaflet-curve 在 2026/10/14 – 2026/11/12 这 30 天内被下载 37,623 次,日均约 1,254 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@elfalem/leaflet-curve 还在维护吗?

最新版本 0.9.2 发布于 2023/03/08(3 年前);至今共发布 15 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/11/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。