shape-points
Version:
Generate points for simple shapes and curves: arcs, rectangles, rounded rectangles, circles, ellipses, bezierCurveTo, bezierCurveThrough (i.e., bezier curves through specific points)
124 lines (100 loc) • 4.03 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: bezierCurveThrough.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: bezierCurveThrough.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>import Curve from 'fit-curve'
import { bezierCurveTo } from './bezierCurveTo'
/**
* Calculate points for smooth bezier curves passing through a series of points
* uses https://github.com/soswow/fit-curve/blob/master/src/fit-curve.js
* uses ShapePoints.curveError=50 for error value
* @module shape-points/bezierCurveThrough
* @param {(number|number[])} x1 - starting point or array of points [x1, y1, x2, y2, ... xn, yn]
* @param {number} [y1]
* @param {number} [x2]
* @param {number} [y2]
* @param {number} [xn] - ending point
* @param {number} [yn]
* @param {object} [options]
* @param {number} [options.pointsInArc=5]
* @param {number} [options.curveError=50]
* @returns {number[]} [x1, y1, x2, y2, ... xn, yn]
*/
export function bezierCurveThrough()
{
let pointsInArc = 5, curveError = 50
const points = []
if (Array.isArray(arguments[0]))
{
const array = arguments[0]
for (let i = 0; i < array.length; i += 2)
{
points.push([array[i], array[i + 1]])
}
if (arguments.length === 2)
{
pointsInArc = arguments[1].pointsInArc
curveError = arguments[1].curveError
}
}
else
{
let length = arguments.length
if (arguments.length % 2 === 1)
{
length--
pointsInArc = arguments[length].pointsInArc
curveError = arguments[length].curveError
}
for (let i = 0; i < length; i += 2)
{
points.push([arguments[i], arguments[i + 1]])
}
}
// two points creates a line
if (points.length === 2)
{
return [points[0][0], points[0][1], points[1][0], points[1][1]]
}
// not enough points
if (points.length < 4)
{
return []
}
const results = []
const curves = Curve(points, curveError)
for (let i = 0; i < curves.length; i++)
{
const c = curves[i]
results.push(...bezierCurveTo(c[0][0], c[0][1], c[1][0], c[1][1], c[2][0], c[2][1], c[3][0], c[3][1], pointsInArc))
}
return results
}</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-shape-points.html">shape-points</a></li><li><a href="module-shape-points_arc.html">shape-points/arc</a></li><li><a href="module-shape-points_bezierCurveThrough.html">shape-points/bezierCurveThrough</a></li><li><a href="module-shape-points_bezierCurveTo.html">shape-points/bezierCurveTo</a></li><li><a href="module-shape-points_circle.html">shape-points/circle</a></li><li><a href="module-shape-points_ellipse.html">shape-points/ellipse</a></li><li><a href="module-shape-points_line.html">shape-points/line</a></li><li><a href="module-shape-points_quadraticCurveTo.html">shape-points/quadraticCurveTo</a></li><li><a href="module-shape-points_rect.html">shape-points/rect</a></li><li><a href="module-shape-points_roundedRect.html">shape-points/roundedRect</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.10</a> on Tue Mar 29 2022 14:31:03 GMT-0700 (Pacific Daylight Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>