@davepagurek/bezier-path

中文文档
0.0.8发布于 2025/11/18MITTypeScriptES Module零依赖近 30 天下载 33.4万

A library for efficiently querying points along cubic Bezier curves.

原始使用文档
文档目录7 节
  1. Why?
  2. Usage
  3. Adding the library
  4. Creating paths
  5. Drawing paths
  6. Morphing paths
  7. Full API

bezier-path

A library for efficiently querying points along cubic Bezier curves.

Why?

This implements two features of the native JS SVGPathElement API: path.getTotalLength(), and path.getPointAtLength(), but a lot faster than the native API.

Bezier curves aren't easy to sample by length. Normally, you sample in "Bezier space" for each segment via a number between 0 and 1, but evenly spaced values in Bezier space won't produce evenly spaced points in Cartesian coordinates. Finding a point at a specific distance normally means doing an integration of some kind, which can be slow if you are getting a lot of points. I need it to be fast, though, which is where this library comes in!

Usage

Adding the library

Add the library in a script tag:

<script src="https://cdn.jsdelivr.net/npm/@davepagurek/[email protected]"></script>

Or on OpenProcessing, add the CDN link as a library:

https://cdn.jsdelivr.net/npm/@davepagurek/[email protected]

[!IMPORTANT]
This package is ESM-only. It cannot be require()'d from CommonJS. Read More

Creating paths

Via the control point API

You can call create() with an array of control points. Each control point in an object including a pt property, which is the coordinate the curve will pass through. It may optionally include left and right properties, which control the handles going into and out of the point, respectively.

const myPath = BezierPath.create([
  {
    pt: { x: 10, y: 10 },
    right: { x: 50, y: 10 }
  },
  {
    left: { x: 20, y: 20 },
    pt: { x: 20, y: 30 },
    right: { x: 20, y: 40 }
  },
  {
    left: { x: 50, y: 50 },
    pt: { x: 10, y: 50 },
  },
])

image

image

https://editor.p5js.org/davepagurek/sketches/dg2o-sLeK

If you want a fully smooth curve, then the line between a control point's left and right coordinates must pass through its pt coordinate. A way to ensure this is to create just one side, e.g. right, and then mirror it for the other side. Here's an example resembling Catmull-Rom interpolation, using p5's p5.Vector class for the points:

const pts = []
for (let i = 0; i < 5; i++) {
  pts.push({
    pt: createVector(random(width), random(height))
  })
}

// Smooth tangents
for (let i = 0; i < 5; i++) {
  const prev = pts[i-1]
  const curr = pts[i]
  const next = pts[i+1]
  if (prev && next) {
    // Change the scaling of the tangent to adjust the curve tightness
    const tangent = next.pt.copy().sub(prev.pt).div(4)
    curr.right = curr.pt.copy().add(tangent)
    curr.left = curr.pt.copy().sub(tangent)
  }
}

const myPath = BezierPath.create(pts)

image

image

https://editor.p5js.org/davepagurek/sketches/g2GvhxcNj

Via an SVG <path> tag

You can also import a path from an SVG embedded in the page:

const myPath = BezierPath.createFromElement(document.querySelector('path'))

Drawing paths

You can draw a path as a polyline by querying points via getPointAtLength():

beginShape()
for (let i = 0; i <= 60; i++) {
  const pt = myPath.getPointAtLength(
    map(i, 0, 60, 0, myPath.getTotalLength())
  )
  vertex(pt.x, pt.y)
}
endShape()

Typically, you'll want to vary the number of sample points based on the length of the curve, e.g.:

beginShape()
const sampleRate = 3 // One point every 3px
const numSamples = ceil(myPath.getTotalLength / sampleRate)
for (let i = 0; i < numSamples; i++) {
  const pt = myPath.getPointAtLength(
    map(i, 0, numSamples-1, 0, myPath.getTotalLength())
  )
  vertex(pt.x, pt.y)
}
endShape()

Morphing paths

As long as you have the same number of sample points per path, you can morph between two paths by lerping each sample point between the two:

const mix = map(cos(frameCount / 120 * TWO_PI), -1, 1, 0, 1)
const numSamples = ceil(max(path1.getTotalLength(), path2.getTotalLength()) / 3)
beginShape()
for (let i = 0; i < numSamples; i++) {
  const pt1 = path1.getPointAtLength(
    map(i, 0, numSamples-1, 0, path1.getTotalLength())
  )
  const pt2 = path2.getPointAtLength(
    map(i, 0, numSamples-1, 0, path2.getTotalLength())
  )
  vertex(lerp(pt1.x, pt2.x, mix), lerp(pt1.y, pt2.y, mix))
}
endShape()

morph

https://editor.p5js.org/davepagurek/sketches/XvC5H7Zhi

Full API

BezierPath

A full path made of a number of segments.

  • getTotalLength(): number
    • Returns the total length of the path
  • getPointAtLength(length: number, approximate: boolean = false): Point
    • Returns a coordinate at the given length on the path
    • Specifying true for approximate will use linear interpolation between internal sample points. This will be faster, but will result in a more segmented line.
  • getTangentAtLength(length: number, approximate: boolean = false): Point
    • Returns the tangent vector at a given length on the path, normalized to have a length of 1
    • Specifying true for approximate will use linear interpolation between tangents at internal sample points for faster execution. The result will still be normalized, so this actually will still look pretty good, if not perfectly accurate.
  • getAngleAtLength(length: number, approximate: boolean = false): number
    • Like getTangentAtLength, but it will return the angle of the path in radians
    • This is equivalent to calling Math.atan2(y, x) on the tangent vector

版本与发布记录

@davepagurek/bezier-path 共发布 8 个版本,首次发布于 2024/10/03;过去 12 个月发布了 1 个版本。

版本发布日期距今
0.0.8latest2025/11/1811 个月前
0.0.72025/10/171.1 年前
0.0.62025/10/171.1 年前
0.0.52025/07/221.3 年前
0.0.42025/07/041.4 年前
0.0.32025/07/031.4 年前

关于 @davepagurek/bezier-path 的常见问题

@davepagurek/bezier-path 是什么?

A library for efficiently querying points along cubic Bezier curves.

如何安装 @davepagurek/bezier-path?

在项目目录运行 npm install @davepagurek/bezier-path,或使用 pnpm add / yarn add / bun add。

@davepagurek/bezier-path 支持 TypeScript 吗?

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

@davepagurek/bezier-path 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@davepagurek/bezier-path 可以商用吗?

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

@davepagurek/bezier-path 的下载量有多少?

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

@davepagurek/bezier-path 还在维护吗?

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