ENGLISH
Bezier Curve Extension
这是一个提供如下几个方法的三次贝塞尔曲线插件。
npm 安装
$ npm install @jiaminghi/bezier-curve
使用
import bezierCurve from '@jiaminghi/bezier-curve'
快速体验
<script src="https://unpkg.com/@jiaminghi/color/dist/index.js"></script>
<script src="https://unpkg.com/@jiaminghi/color/dist/index.min.js"></script>
<script>
const { bezierCurveToPolyline, getBezierCurveLength, polylineToBezierCurve } = window.BezierCurve
</script>
示例
bezierCurve
type Point = [number, number]
type BezierCurveSegment = [Point, Point, Point]
type BezierCurve = [Point, BezierCurveSegment, ...BezierCurveSegment[]]
const bezierCurve: BezierCurve = [
[20, 20],
[
[100, 20],
[100, 80],
[180, 80],
],
]
bezierCurve in SVG
bezierCurveToPolyline
type bezierCurveToPolyline = (
bezierCurve: BezierCurve,
precision = 5,
resursiveCount = 0
) => Point[]
const polyline = bezierCurveToPolyline(bezierCurve)
polyline in SVG
Notice
- bezierCurveToPolyline的计算结果是由 N 个点构成的折线,N 取决于设置的精度。
- 理想情况下,计算结果中相邻的两个点的距离等于设置的精度(单位 px)。
- 建议精度 5-10。
- 设置的精度通常是达不到的,除非设置较高的迭代次数,计算成本较高。
getBezierCurveLength
type getBezierCurveLength = (bezierCurve: BezierCurve, precision = 5, resursiveCount = 0) => Point[]
const length = getBezierCurveLength(bezierCurve)
polyline
const polyline: Point[] = [
[20, 70],
[50, 30],
[100, 70],
[150, 30],
[180, 70],
]
polyline in SVG
polylineToBezierCurve
type polylineToBezierCurve = (
polyline: Point[],
close = false,
offsetA = 0.25,
offsetB = 0.25
) => BezierCurve
const bezierCurve = polylineToBezierCurve(polyline)
const closedBezierCurve = polylineToBezierCurve(polyline, true)
bezierCurve in SVG
closedBezierCurve in SVG