Socket
Socket
Sign inDemoInstall

@jiaminghi/bezier-curve

Package Overview
Dependencies
2
Maintainers
2
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.9 to 1.0.0

.babelrc.js

14

CHANGELOG.md

@@ -0,1 +1,7 @@

# 1.0.0 (2020-05-02)
### Perfect
- **typescript:** Rewrite with typescript.
# 0.0.9-alpha (2019-08-28)

@@ -35,3 +41,3 @@

* **note:** Update note
- **note:** Update note

@@ -42,3 +48,3 @@ # 0.0.3-alpha (2019-04-03)

* **add:** Add a readme file
- **add:** Add a readme file

@@ -49,3 +55,3 @@ # 0.0.2-alpha (2019-04-03)

* **optmization:** Add a polyline abstraction to a curve algorithm
- **optmization:** Add a polyline abstraction to a curve algorithm

@@ -56,2 +62,2 @@ # 0.0.1-alpha (2019-03-30)

* **optmization:** Improve the calcUniformPointsByIteration algorithm
- **optmization:** Improve the calcUniformPointsByIteration algorithm

@@ -1,37 +0,407 @@

"use strict";
'use strict';
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, '__esModule', { value: true });
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "bezierCurveToPolyline", {
enumerable: true,
get: function get() {
return _bezierCurveToPolyline.bezierCurveToPolyline;
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
function __spreadArrays() {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
}
var DEFAULT_SEGMENT_POINTS_NUM = 50;
function getBezierCurveStartPoint(bezierCurve) {
return bezierCurve[0];
}
function getBezierCurveEndPoint(bezierCurve) {
return bezierCurve.slice(-1)[0][2];
}
function getBezierCurveSegments(bezierCurve) {
return bezierCurve.slice(1);
}
/**
* @description Get the sum of the {number[]}
*/
function getNumSum(nums) {
return nums.reduce(function (sum, num) {
return sum + num;
}, 0);
}
/**
* @description Get the distance between two points
*/
function getTwoPointDistance(_a, _b) {
var ax = _a[0],
ay = _a[1];
var bx = _b[0],
by = _b[1];
return Math.sqrt(Math.pow(ax - bx, 2) + Math.pow(ay - by, 2));
}
function flatten(input) {
return input.reduce(function (_, __) {
return __spreadArrays(_, __);
}, []);
}
/**
* @description Generate a function to calculate the point coordinates at time t
* @param {Point} beginPoint BezierCurve begin point
* @param {Point} controlPoint1 BezierCurve controlPoint1
* @param {Point} controlPoint2 BezierCurve controlPoint2
* @param {Point} endPoint BezierCurve end point
*/
function createGetBezierCurveTPointFun(beginPoint, controlPoint1, controlPoint2, endPoint) {
var cache = new Map([]);
return function (t) {
if (cache.has(t)) return cache.get(t);
var subtractedT = 1 - t;
var subtractedTPow3 = Math.pow(subtractedT, 3);
var subtractedTPow2 = Math.pow(subtractedT, 2);
var tPow3 = Math.pow(t, 3);
var tPow2 = Math.pow(t, 2);
var point = [beginPoint[0] * subtractedTPow3 + 3 * controlPoint1[0] * t * subtractedTPow2 + 3 * controlPoint2[0] * tPow2 * subtractedT + endPoint[0] * tPow3, beginPoint[1] * subtractedTPow3 + 3 * controlPoint1[1] * t * subtractedTPow2 + 3 * controlPoint2[1] * tPow2 * subtractedT + endPoint[1] * tPow3];
cache.set(t, point);
return point;
};
}
/**
* @description Create {GetBezierCurveTPointFunction} for every segment of bezierCurve
*/
function createGetSegmentTPointFuns(bezierCurve) {
var segments = getBezierCurveSegments(bezierCurve);
var startPoint = getBezierCurveStartPoint(bezierCurve);
return segments.map(function (segment, i) {
var beginPoint = i === 0 ? startPoint : segments[i - 1][2];
return createGetBezierCurveTPointFun.apply(void 0, __spreadArrays([beginPoint], segment));
});
}
function getSegmentPointsByNum(getSegmentTPointFuns, segmentPointsNum) {
return getSegmentTPointFuns.map(function (getSegmentTPointFun, i) {
var tGap = 1 / (segmentPointsNum[i] - 1);
return new Array(segmentPointsNum[i]).fill(0).map(function (_, j) {
return getSegmentTPointFun(j * tGap);
});
});
}
function createSegmentPoints(getSegmentTPointFuns) {
var length = getSegmentTPointFuns.length; // Initialize the curve to a polyline
var segmentPointsNum = new Array(length).fill(DEFAULT_SEGMENT_POINTS_NUM);
return getSegmentPointsByNum(getSegmentTPointFuns, segmentPointsNum);
}
/**
* @description Get the distance of multiple sets of points
*/
function getPointsDistance(points) {
return new Array(points.length - 1).fill(0).map(function (_, j) {
return getTwoPointDistance(points[j], points[j + 1]);
});
}
function getSegmentPointsDistance(segmentPoints) {
return segmentPoints.map(getPointsDistance);
}
/**
* @description Get the sum of deviations between line segment and the average length
* @param {Array} segmentPointsDistance Segment length of polyline
* @param {Number} avgLength Average length of the line segment
* @return {Number} Deviations
*/
function getAllDeviations(segmentPointsDistance, avgLength) {
var calcDeviation = function calcDeviation(distance) {
return distance.map(function (d) {
return Math.abs(d - avgLength);
});
};
return getNumSum(segmentPointsDistance.map(calcDeviation).map(getNumSum));
}
function getSegmentPointsData(segmentPoints) {
var segmentPointsDistance = getSegmentPointsDistance(segmentPoints);
var lineSegmentNum = getNumSum(segmentPointsDistance.map(function (_a) {
var length = _a.length;
return length;
}));
var segmentLength = segmentPointsDistance.map(getNumSum);
var totalLength = getNumSum(segmentLength);
var avgDistance = totalLength / lineSegmentNum;
var deviation = getAllDeviations(segmentPointsDistance, avgDistance);
return {
totalLength: totalLength,
segmentLength: segmentLength,
avgDistance: avgDistance,
deviation: deviation
};
}
function getSegmentPointsCount(segmentPoints) {
return flatten(segmentPoints).length;
}
function reGetSegmentPoints(segmentPoints, getSegmentTPointFuns, _a, precision) {
var avgDistance = _a.avgDistance,
totalLength = _a.totalLength,
segmentLength = _a.segmentLength;
var pointsCount = getSegmentPointsCount(segmentPoints); // Add more points to enhance accuracy
pointsCount = Math.ceil(avgDistance / precision * pointsCount * 1.1); // Redistribution points acount
var segmentPointsNum = segmentLength.map(function (length) {
return Math.ceil(length / totalLength * pointsCount);
}) // At least need two points
.map(function (_) {
return _ > 1 ? _ : 2;
}); // Calculate the points after redistribution
return getSegmentPointsByNum(getSegmentTPointFuns, segmentPointsNum);
}
function recursiveCalcSegmentPoints(segmentPoints, getSegmentTPointFuns, _a, recursiveCount) {
var avgDistance = _a.avgDistance;
var pointsCount = getSegmentPointsCount(segmentPoints);
var stepSize = 1 / pointsCount / 10; // Recursively for each segment of the polyline
getSegmentTPointFuns.forEach(function (getSegmentTPointFun, i) {
var currentSegmentPointsNum = segmentPoints[i].length;
var tGap = 1 / (currentSegmentPointsNum - 1);
var t = new Array(currentSegmentPointsNum).fill(0).map(function (_, j) {
return j * tGap;
}); // Repeated recursive offset
for (var r = 0; r < recursiveCount; r++) {
var distance = getPointsDistance(segmentPoints[i]);
var deviations = distance.map(function (d) {
return d - avgDistance;
});
var offset = 0;
for (var j = 0; j < currentSegmentPointsNum; j++) {
if (j === 0) continue;
offset += deviations[j - 1];
t[j] -= stepSize * offset;
if (t[j] > 1) t[j] = 1;
if (t[j] < 0) t[j] = 0;
segmentPoints[i][j] = getSegmentTPointFun(t[j]);
}
}
});
return segmentPoints;
}
function calcUniformPointsByIteration(segmentPoints, getSegmentTPointFuns, precision, recursiveCount) {
console.warn('-------------start-------------');
var segmentPointsData = getSegmentPointsData(segmentPoints);
if (segmentPointsData.deviation <= precision) return flatten(segmentPoints);
segmentPoints = reGetSegmentPoints(segmentPoints, getSegmentTPointFuns, segmentPointsData, precision);
if (recursiveCount <= 0) return flatten(segmentPoints);
segmentPointsData = getSegmentPointsData(segmentPoints);
segmentPoints = recursiveCalcSegmentPoints(segmentPoints, getSegmentTPointFuns, segmentPointsData, recursiveCount);
return flatten(segmentPoints);
}
/**
* @description Convert bezierCurve to polyline.
* Calculation results cannot guarantee accuracy!
* Recusive calculation can get more accurate results
* @param {BezierCurve} bezierCurve bezierCurve data
* @param {number} precision calculation accuracy. Recommended for 1-20
* @param {number} recursiveCount Recursive count
*/
function convertBezierCurveToPolyline(bezierCurve, precision, recursiveCount) {
if (precision === void 0) {
precision = 5;
}
});
Object.defineProperty(exports, "getBezierCurveLength", {
enumerable: true,
get: function get() {
return _bezierCurveToPolyline.getBezierCurveLength;
if (recursiveCount === void 0) {
recursiveCount = 0;
}
});
Object.defineProperty(exports, "polylineToBezierCurve", {
enumerable: true,
get: function get() {
return _polylineToBezierCurve["default"];
var getSegmentTPointFuns = createGetSegmentTPointFuns(bezierCurve);
var segmentPoints = createSegmentPoints(getSegmentTPointFuns); // Calculate uniformly distributed points by iteratively
var polyline = calcUniformPointsByIteration(segmentPoints, getSegmentTPointFuns, precision, recursiveCount);
var endPoint = getBezierCurveEndPoint(bezierCurve);
polyline.push(endPoint);
return polyline;
}
/**
* @description Transform bezierCurve to polyline
* @param {BezierCurve} bezierCurve bezier curve
* @param {number} precision Wanted precision (Not always achieveable)
* @param {number} recursiveCount Recursive count
*/
function bezierCurveToPolyline(bezierCurve, precision, recursiveCount) {
if (precision === void 0) {
precision = 5;
}
});
exports["default"] = void 0;
var _bezierCurveToPolyline = require("./core/bezierCurveToPolyline");
if (recursiveCount === void 0) {
recursiveCount = 0;
}
var _polylineToBezierCurve = _interopRequireDefault(require("./core/polylineToBezierCurve"));
if (!(bezierCurve instanceof Array)) throw new Error("bezierCurveToPolyline: Invalid input of " + bezierCurve);
if (bezierCurve.length <= 1) throw new Error("bezierCurveToPolyline: The length of the bezierCurve should be greater than 1");
if (typeof precision !== 'number') throw new Error("bezierCurveToPolyline: Type of precision must be number");
return convertBezierCurveToPolyline(bezierCurve, precision, recursiveCount);
}
function getBezierCurveLength(bezierCurve, precision, recursiveCount) {
if (precision === void 0) {
precision = 5;
}
var _default = {
bezierCurveToPolyline: _bezierCurveToPolyline.bezierCurveToPolyline,
getBezierCurveLength: _bezierCurveToPolyline.getBezierCurveLength,
polylineToBezierCurve: _polylineToBezierCurve["default"]
if (recursiveCount === void 0) {
recursiveCount = 0;
}
var polyline = bezierCurveToPolyline(bezierCurve, precision, recursiveCount);
var pointsDistance = getPointsDistance(polyline);
return getNumSum(pointsDistance);
}
/**
* @description Get the control points of the Bezier curve
* @param {Point[]} polyline A set of points that make up a polyline
* @param {number} index The index of which get controls points's point in polyline
* @param {boolean} close Closed curve
* @param {number} offsetA Smoothness
* @param {number} offsetB Smoothness
* @return {Point[]|null} Control points
*/
function getBezierCurveLineControlPoints(polyline, index, close, offsetA, offsetB) {
if (close === void 0) {
close = false;
}
if (offsetA === void 0) {
offsetA = 0.25;
}
if (offsetB === void 0) {
offsetB = 0.25;
}
var pointNum = polyline.length;
if (pointNum < 3 || index >= pointNum) return null;
var beforePointIndex = index - 1;
if (beforePointIndex < 0) beforePointIndex = close ? pointNum + beforePointIndex : 0;
var afterPointIndex = index + 1;
if (afterPointIndex >= pointNum) afterPointIndex = close ? afterPointIndex - pointNum : pointNum - 1;
var afterNextPointIndex = index + 2;
if (afterNextPointIndex >= pointNum) afterNextPointIndex = close ? afterNextPointIndex - pointNum : pointNum - 1;
var pointBefore = polyline[beforePointIndex];
var pointMiddle = polyline[index];
var pointAfter = polyline[afterPointIndex];
var pointAfterNext = polyline[afterNextPointIndex];
return [[pointMiddle[0] + offsetA * (pointAfter[0] - pointBefore[0]), pointMiddle[1] + offsetA * (pointAfter[1] - pointBefore[1])], [pointAfter[0] - offsetB * (pointAfterNext[0] - pointMiddle[0]), pointAfter[1] - offsetB * (pointAfterNext[1] - pointMiddle[1])]];
}
/**
* @description Get the symmetry point
* @param {Point} point Symmetric point
* @param {Point} centerPoint Symmetric center
* @return {Point} Symmetric point
*/
function getSymmetryPoint(_a, _b) {
var px = _a[0],
py = _a[1];
var cx = _b[0],
cy = _b[1];
var minusX = cx - px;
var minusY = cy - py;
return [cx + minusX, cy + minusY];
}
/**
* @description Get the last curve of the closure
*/
function closeBezierCurve(bezierCurve, startPoint) {
var firstSubCurve = bezierCurve[0];
var lastSubCurve = bezierCurve.slice(-1)[0];
bezierCurve.push([getSymmetryPoint(lastSubCurve[1], lastSubCurve[2]), getSymmetryPoint(firstSubCurve[0], startPoint), startPoint]);
return bezierCurve;
}
/**
* @description Convert polyline to bezierCurve
* @param {Point[]} polyline A set of points that make up a polyline
* @param {boolean} close Closed curve
* @param {number} offsetA Smoothness
* @param {number} offsetB Smoothness
* @return {BezierCurve} A set of bezier curve (Invalid input will return false)
*/
function polylineToBezierCurve(polyline, close, offsetA, offsetB) {
if (close === void 0) {
close = false;
}
if (offsetA === void 0) {
offsetA = 0.25;
}
if (offsetB === void 0) {
offsetB = 0.25;
}
if (!(polyline instanceof Array)) throw new Error("polylineToBezierCurve: Invalid input of " + polyline);
if (polyline.length <= 2) throw new Error("polylineToBezierCurve: The length of the polyline should be greater than 2");
var startPoint = polyline[0];
var bezierCurveLineNum = polyline.length - 1;
var bezierCurvePoints = new Array(bezierCurveLineNum).fill(0).map(function (_, i) {
return __spreadArrays(getBezierCurveLineControlPoints(polyline, i, close, offsetA, offsetB), [polyline[i + 1]]);
});
if (close) closeBezierCurve(bezierCurvePoints, startPoint);
bezierCurvePoints.unshift(polyline[0]);
return bezierCurvePoints;
}
var index = {
bezierCurveToPolyline: bezierCurveToPolyline,
getBezierCurveLength: getBezierCurveLength,
polylineToBezierCurve: polylineToBezierCurve
};
exports["default"] = _default;
exports.bezierCurveToPolyline = bezierCurveToPolyline;
exports.default = index;
exports.getBezierCurveLength = getBezierCurveLength;
exports.polylineToBezierCurve = polylineToBezierCurve;
{
"name": "@jiaminghi/bezier-curve",
"version": "0.0.9",
"version": "1.0.0",
"author": "JiaMing <743192023@qq.com>",
"description": "Bezier curve extension",
"main": "lib/index.js",
"unpkg": "dist/index.js",
"module": "es/index.js",
"types": "types/index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/jiaming743/BezierCurve.git"
"url": "https://github.com/DataV-Team/BezierCurve.git"
},
"scripts": {
"build": "node build/index.js",
"prepublish": "npm run build",
"deploy": "node deploy/index.js",
"test": "mocha"
"clean": "rimraf lib dist es types",
"type:check": "tsc --noEmit",
"lint": "eslint --ext js,ts src test",
"format:check": "prettier --check .",
"format": "prettier --write .",
"build": "rollup -c",
"prepare": "npm run clean && npm run check && npm run build",
"test": "mocha",
"check": "npm run type:check && npm run lint && npm run format:check && npm run test"
},
"husky": {
"hooks": {
"pre-commit": "npm run check",
"pre-push": "npm run check"
}
},
"license": "MIT",
"bugs": {
"url": "https://github.com/jiaming743/BezierCurve/issues"
"url": "https://github.com/DataV-Team/BezierCurve/issues"
},

@@ -27,19 +41,33 @@ "keywords": [

],
"homepage": "https://github.com/jiaming743/BezierCurve#readme",
"homepage": "https://github.com/DataV-Team/BezierCurve#readme",
"dependencies": {
"@babel/runtime": "^7.5.5"
},
"devDependencies": {
"@babel/cli": "^7.4.4",
"@babel/core": "^7.4.5",
"@babel/preset-env": "^7.5.5",
"@babel/plugin-transform-runtime": "^7.5.5",
"@babel/register": "^7.4.4",
"@jiaminghi/fs": "^0.0.2",
"browserify": "^16.5.0",
"@babel/cli": "^7.8.4",
"@babel/core": "^7.8.6",
"@babel/plugin-transform-runtime": "^7.8.3",
"@babel/preset-env": "^7.8.6",
"@babel/preset-typescript": "^7.9.0",
"@babel/register": "^7.9.0",
"@types/chai": "^4.2.11",
"@types/mocha": "^7.0.2",
"@types/node": "^13.11.0",
"@typescript-eslint/eslint-plugin": "^2.27.0",
"@typescript-eslint/parser": "^2.27.0",
"chai": "^4.2.0",
"ftp": "^0.3.10",
"mocha": "^6.1.4",
"uglifyjs": "^2.4.11"
},
"dependencies": {
"@babel/runtime": "^7.5.5"
"eslint": "^6.8.0",
"husky": "^4.2.5",
"mocha": "^6.2.3",
"prettier": "^2.0.4",
"rimraf": "^3.0.2",
"rollup": "^2.6.1",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-terser": "^5.3.0",
"rollup-plugin-typescript2": "^0.27.0",
"ts-node": "^8.8.2",
"typescript": "^3.8.3"
}
}

@@ -6,5 +6,11 @@ [中文](./README.md)

<p align="center">
<a href="https://travis-ci.com/jiaming743/bezierCurve"><img src="https://img.shields.io/travis/com/jiaming743/bezierCurve.svg" alt="Travis CI"></a>
<a href="https://github.com/jiaming743/BezierCurve/blob/master/LICENSE"><img src="https://img.shields.io/github/license/jiaming743/bezierCurve.svg" alt="LICENSE" /> </a>
<a href="https://www.npmjs.com/package/@jiaminghi/bezier-curve"><img src="https://img.shields.io/npm/v/@jiaminghi/bezier-curve.svg" alt="LICENSE" /> </a>
<a href="https://travis-ci.com/DataV-Team/beziercurve">
<img src="https://img.shields.io/travis/com/DataV-Team/bezierCurve.svg" alt="Travis CI">
</a>
<a href="https://github.com/DataV-Team/beziercurve/blob/master/LICENSE">
<img src="https://img.shields.io/github/license/DataV-Team/beziercurve.svg" alt="LICENSE" />
</a>
<a href="https://www.npmjs.com/package/@jiaminghi/bezier-curve">
<img src="https://img.shields.io/npm/v/@jiaminghi/bezier-curve.svg" alt="NPM" />
</a>
</p>

@@ -16,3 +22,3 @@

Ability to abstract a Bezier curve into a polyline consisting of N **uniformly distributed** points.
Ability to abstract a Bezier curve into a polyline consisting of N points.

@@ -44,14 +50,13 @@ - **[getBezierCurveLength](#getBezierCurveLength)**

```html
<!--Resources are located on personal servers for experience and testing only, do not use in production environments-->
<!--Debug version-->
<script src="http://lib.jiaminghi.com/bezierCurve/bezierCurve.map.js"></script>
<script src="https://unpkg.com/@jiaminghi/color/dist/index.js"></script>
<!--Compression version-->
<script src="http://lib.jiaminghi.com/bezierCurve/bezierCurve.min.js"></script>
<script src="https://unpkg.com/@jiaminghi/color/dist/index.min.js"></script>
<script>
const { bezierCurveToPolyline, getBezierCurveLength, polylineToBezierCurve } = window.bezierCurve
// do something
const { bezierCurveToPolyline, getBezierCurveLength, polylineToBezierCurve } = window.BezierCurve
// do something
</script>
```
------
---

@@ -62,14 +67,23 @@ <h3 align="center">Examples</h3>

```javascript
// Bezier curve data structure
const bezierCurve = [
// Start point
[20, 20],
// Multiple sets of bezier curve
[
// controlPoint1,controlPoint2,endPoint
[100, 20],[100, 80],[180,80]
],
// The starting point of the next bezier curve is the end point of the previous bezier curve
// [...],[...]
```typescript
type Point = [number, number]
/**
* [controlPoint1, controlPoint2, endPoint]
*/
type BezierCurveSegment = [Point, Point, Point]
/**
* [start point, Multiple sets of bezier curve, ...]
* The starting point of the next bezier curve is the end point of the previous bezier curve
*/
type BezierCurve = [Point, BezierCurveSegment, ...BezierCurveSegment[]]
const bezierCurve: BezierCurve = [
[20, 20],
[
[100, 20],
[100, 80],
[180, 80],
],
]

@@ -86,16 +100,17 @@ ```

```javascript
```typescript
/**
* @description Get the polyline corresponding to the Bezier curve
* @param {Array} bezierCurve BezierCurve data
* @param {Number} precision Calculation accuracy. Recommended for 5-10. Default = 5
* @return {Array|Boolean} Point data that constitutes a polyline after calculation (Invalid input will return false)
* @description Transform bezierCurve to polyline
* @param {BezierCurve} bezierCurve bezier curve
* @param {number} precision Wanted precision
* @param {number} recursiveCount Recursive count
* @return {Point[]} Polyline
*/
function bezierCurveToPolyline (bezierCurve, precision = 5) {
// ...
}
type bezierCurveToPolyline = (
bezierCurve: BezierCurve,
precision = 5,
resursiveCount = 0
) => Point[]
const precision = 5
const polyline = bezierCurveToPolyline(bezierCurve, precision)
const polyline = bezierCurveToPolyline(bezierCurve)
// polyline = [

@@ -116,39 +131,32 @@ // [[20,20],

- The calculation result of *bezierCurveToPolyline* consists of N points, and N depends on the precision you set.
- The calculation result of _bezierCurveToPolyline_ consists of N points, and N depends on the precision you set.
- Ideally, the distance between two adjacent points in the calculation result is equal to the set accuracy (unit px).
- Recommended precision is 5-10.
- If the setting precision is less than 1 or too large, the calculation result may be abnormal.
- Sometimes it is **impossible** to achieve precision.
- The precision of the setting is usually not achieved, unless the higher the number of recursiveCount, the higher the calculation cost.
#### getBezierCurveLength
```js
```typescript
/**
* @description Get the bezier curve length
* @param {Array} bezierCurve bezierCurve data
* @param {Number} precision calculation accuracy. Recommended for 5-10. Default = 5
* @return {Number|Boolean} BezierCurve length (Invalid input will return false)
* @description Get the length of the bezier curve
* @param {BezierCurve} bezierCurve bezier curve
* @param {number} precision Wanted precision
* @param {number} recursiveCount Recursive count
* @return {number} The length
*/
export function getBezierCurveLength (bezierCurve, precision = 5) {
// ...
}
type getBezierCurveLength = (bezierCurve: BezierCurve, precision = 5, resursiveCount = 0) => number
// Normally the default precision can achieve better visual effects.
const length = bezierCurveToPolyline(bezierCurve)
const length = getBezierCurveLength(bezierCurve)
```
#### polyline
```javascript
// polyline data structure
const polyline = [
[20, 70],
[50, 30],
[100, 70],
[150, 30],
[180, 70]
```typescript
const polyline: Point[] = [
[20, 70],
[50, 30],
[100, 70],
[150, 30],
[180, 70],
]

@@ -163,18 +171,19 @@ ```

#### polylineToBezierCurve
```javascript
```typescript
/**
* @description Abstract the polyline formed by N points into a set of bezier curve
* @param {Array} polyline A set of points that make up a polyline
* @param {Boolean} close Closed curve
* @param {Number} offsetA Smoothness
* @param {Number} offsetB Smoothness
* @return {Array|Boolean} A set of bezier curve (Invalid input will return false)
* @description Convert polyline to bezierCurve
* @param {Point[]} polyline A set of points that make up a polyline
* @param {boolean} close Closed curve
* @param {number} offsetA Smoothness
* @param {number} offsetB Smoothness
* @return {BezierCurve} A set of bezier curve (Invalid input will return false)
*/
function polylineToBezierCurve (polyline, close = false, offsetA = 0.25, offsetB = 0.25) {
// ...
}
type polylineToBezierCurve = (
polyline: Point[],
close = false,
offsetA = 0.25,
offsetB = 0.25
) => BezierCurve

@@ -213,2 +222,1 @@ const bezierCurve = polylineToBezierCurve(polyline)

<p align="center"><i>closedBezierCurve</i> in <b>SVG</b></p>

@@ -6,5 +6,11 @@ [ENGLISH](./README_EN.md)

<p align="center">
<a href="https://travis-ci.com/jiaming743/bezierCurve"><img src="https://img.shields.io/travis/com/jiaming743/bezierCurve.svg" alt="Travis CI"></a>
<a href="https://github.com/jiaming743/BezierCurve/blob/master/LICENSE"><img src="https://img.shields.io/github/license/jiaming743/bezierCurve.svg" alt="LICENSE" /> </a>
<a href="https://www.npmjs.com/package/@jiaminghi/bezier-curve"><img src="https://img.shields.io/npm/v/@jiaminghi/bezier-curve.svg" alt="LICENSE" /> </a>
<a href="https://travis-ci.com/DataV-Team/beziercurve">
<img src="https://img.shields.io/travis/com/DataV-Team/bezierCurve.svg" alt="Travis CI">
</a>
<a href="https://github.com/DataV-Team/beziercurve/blob/master/LICENSE">
<img src="https://img.shields.io/github/license/DataV-Team/beziercurve.svg" alt="LICENSE" />
</a>
<a href="https://www.npmjs.com/package/@jiaminghi/bezier-curve">
<img src="https://img.shields.io/npm/v/@jiaminghi/bezier-curve.svg" alt="NPM" />
</a>
</p>

@@ -16,3 +22,3 @@

将贝塞尔曲线抽象成由N个**均匀分布**的点构成的折线
将贝塞尔曲线抽象成由 N 个点构成的折线

@@ -25,5 +31,5 @@ - **[getBezierCurveLength](#getBezierCurveLength)**

将由N个点构成的折线抽象成光滑的贝塞尔曲线
将由 N 个点构成的折线抽象成光滑的贝塞尔曲线
### npm安装
### npm 安装

@@ -45,14 +51,13 @@ ```shell

```html
<!--资源位于个人服务器仅供体验和测试,请勿在生产环境使用-->
<!--调试版-->
<script src="http://lib.jiaminghi.com/bezierCurve/bezierCurve.map.js"></script>
<script src="https://unpkg.com/@jiaminghi/color/dist/index.js"></script>
<!--压缩版-->
<script src="http://lib.jiaminghi.com/bezierCurve/bezierCurve.min.js"></script>
<script src="https://unpkg.com/@jiaminghi/color/dist/index.min.js"></script>
<script>
const { bezierCurveToPolyline, getBezierCurveLength, polylineToBezierCurve } = window.bezierCurve
// do something
const { bezierCurveToPolyline, getBezierCurveLength, polylineToBezierCurve } = window.BezierCurve
// do something
</script>
```
------
---

@@ -63,14 +68,23 @@ <h3 align="center">示例</h3>

```javascript
// 贝塞尔曲线数据结构
const bezierCurve = [
// 起始点
[20, 20],
// 多段贝塞尔曲线
[
// 控制点1,控制点2,结束点
[100, 20],[100, 80],[180,80]
],
// 下一段贝塞尔曲线的起始点是上一段的结束点
// [...],[...]
```typescript
type Point = [number, number]
/**
* [控制点1,控制点2,结束点]
*/
type BezierCurveSegment = [Point, Point, Point]
/**
* [起始点, 贝塞尔曲线段, ...]
* 下一段贝塞尔曲线的起始点是上一段的结束点
*/
type BezierCurve = [Point, BezierCurveSegment, ...BezierCurveSegment[]]
const bezierCurve: BezierCurve = [
[20, 20],
[
[100, 20],
[100, 80],
[180, 80],
],
]

@@ -87,16 +101,17 @@ ```

```javascript
```typescript
/**
* @description 通过贝塞尔曲线获取折线
* @param {Array} bezierCurve 贝塞尔曲线数据
* @param {Number} precision 计算精度 建议5-10 默认为5
* @return {Array|Boolean} 构成折线的点集 (无效输入将返回false)
* @description 将贝塞尔曲线转换为折线
* @param {BezierCurve} bezierCurve 贝塞尔曲线
* @param {number} precision 需要的计算精度
* @param {number} recursiveCount 迭代次数
* @return {Point[]} 转换的折线
*/
function bezierCurveToPolyline (bezierCurve, precision = 5) {
// ...
}
type bezierCurveToPolyline = (
bezierCurve: BezierCurve,
precision = 5,
resursiveCount = 0
) => Point[]
const precision = 5
const polyline = bezierCurveToPolyline(bezierCurve, precision)
const polyline = bezierCurveToPolyline(bezierCurve)
// polyline = [

@@ -117,39 +132,32 @@ // [[20,20],

- *bezierCurveToPolyline*的计算结果是由N个点构成的折线,N取决于设置的精度。
- 理想情况下,计算结果中相邻的两个点的距离等于设置的精度(单位px)。
- 建议精度5-10。
- 如果设置的精度过小或过大(小于1或大于10),可能导致计算异常。
- 设置的精度并不是每次都能达到。
- *bezierCurveToPolyline*的计算结果是由 N 个点构成的折线,N 取决于设置的精度。
- 理想情况下,计算结果中相邻的两个点的距离等于设置的精度(单位 px)。
- 建议精度 5-10。
- 设置的精度通常是达不到的,除非设置较高的迭代次数,计算成本较高。
#### getBezierCurveLength
```js
```typescript
/**
* @description 获取贝塞尔曲线长度
* @param {Array} bezierCurve 贝塞尔曲线数据
* @param {Number} precision 计算精度 建议5-10 默认为5
* @return {Number|Boolean} 贝塞尔曲线长度 (无效输入将返回false)
* @description 计算贝塞尔曲线的长度
* @param {BezierCurve} bezierCurve 贝塞尔曲线
* @param {number} precision 需要的计算精度
* @param {number} recursiveCount 迭代次数
* @return {number} 转换的折线
*/
export function getBezierCurveLength (bezierCurve, precision = 5) {
// ...
}
type getBezierCurveLength = (bezierCurve: BezierCurve, precision = 5, resursiveCount = 0) => Point[]
// 通常情况下,默认精度已经能够达到较好的视觉效果。
const length = bezierCurveToPolyline(bezierCurve)
const length = getBezierCurveLength(bezierCurve)
```
#### polyline
```javascript
// 折线数据结构
const polyline = [
[20, 70],
[50, 30],
[100, 70],
[150, 30],
[180, 70]
```typescript
const polyline: Point[] = [
[20, 70],
[50, 30],
[100, 70],
[150, 30],
[180, 70],
]

@@ -164,18 +172,19 @@ ```

#### polylineToBezierCurve
```javascript
```typescript
/**
* @description 将由N个点构成的折线抽象为光滑的贝塞尔曲线
* @param {Array} polyline 由N个点构成的折线数据
* @param {Boolean} close 是否闭合
* @param {Number} offsetA 光滑程度
* @param {Number} offsetB 光滑程度
* @return {Array|Boolean} 贝塞尔曲线数据 (无效输入将返回false)
* @description 将折线转换为贝塞尔曲线
* @param {Point[]} polyline 折线
* @param {boolean} close 转换的贝塞尔曲线是否需要闭合
* @param {number} offsetA 光滑程度
* @param {number} offsetB 光滑程度
* @return {BezierCurve} 转换的贝塞尔曲线
*/
function polylineToBezierCurve (polyline, close = false, offsetA = 0.25, offsetB = 0.25) {
// ...
}
type polylineToBezierCurve = (
polyline: Point[],
close = false,
offsetA = 0.25,
offsetB = 0.25
) => BezierCurve

@@ -214,2 +223,1 @@ const bezierCurve = polylineToBezierCurve(polyline)

<p align="center"><i>closedBezierCurve</i> in <b>SVG</b></p>

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc