Bezier.js

repository·master·Indexed 23 days ago

https://github.com/pomax/bezierjs

An ES Module based JavaScript library for performing quadratic and cubic Bezier curve operations in Node.js and browsers. Version 6.1.4 provides the Bezier class for curve manipulation—including point projection, splitting, length calculation, bounding boxes, and SVG path generation—and the PolyBezier class for managing sequences of Bezier segments.

Tokens
1.6K
Snippets
5
Records
18
Agent score
84%

What's inside bezier-js

  1. Install Bezier.js without a package manager

    master

    You can use the rolled-up version of the library located in the dist directory. Download the bezier.js file and include it directly in your JavaScript assets directory.

    https://raw.githubusercontent.com/Pomax/bezierjs/master/dist/bezier.js
  2. Use Bezier.js in Node.js as a dependency

    master

    Bezier.js supports both ES Modules and legacy CommonJS syntax in Node.js. Use the Bezier constructor to create new curve instances.

    // ES Module syntax
    import { Bezier } from "bezier-js";
    
    const b = new Bezier(...);
    // Legacy CommonJS syntax
    const Bezier = require("bezier-js");
    
    const b = new Bezier(...);
  3. Use Bezier.js in Node or the browser from a local file

    master

    You can manually include the library by copying the contents of the src directory or the rolled-up dist/bezier.js file to your project (e.g., /js or /vendor). Then, import the Bezier constructor directly from that file path.

    import { Bezier } from "/js/vendor/bezier.js";
    
    const b = new Bezier(...);
  4. Node.js compatibility matrix

    master

    Bezier.js supports various Node.js versions for both require (CommonJS) and import (ESM) syntax. Note that ESM support in older v12 versions may require experimental flags.

    | Node Version | Require Supported | Import Supported |
    | ------------ | ----------------- | ----------------- |
    | v12.0.0      | Yes               | Yes <sup>Experimental Flag</sup> |
    | v12.14.1     | Yes               | No <sup>Experimental Flag</sup> |
    | v12.17.0     | Yes               | Yes <sup>Experimental Warning</sup> |
    | v12.22.1     | Yes               | Yes |
    | v14.0.0      | Yes               | Yes |
    | v14.16.1     | Yes               | Yes |
  5. PolyBezier API Reference

    master

    Methods available on the PolyBezier class:

    • constructor(curves): Initializes the poly-bezier with an array of curves. If curves is provided, it inherits the _3d property from the first curve.
    • addCurve(curve): Appends a new curve to the sequence. Updates the _3d property if the new curve is 3D.
    • length(): Returns the sum of the lengths of all individual curves in the sequence.
    • curve(idx): Returns the curve at the specified index.
    • bbox(): Returns the bounding box that contains all curves in the sequence.
    • offset(d): Returns a new PolyBezier instance representing the offset of the sequence by distance d.
    • toString(): Returns a string representation of the points in all curves, formatted as [points1, points2, ...].
    • valueOf(): Returns the string representation of the poly-bezier.
  6. Create a Bezier curve

    master

    The Bezier class is the primary interface for creating and manipulating Bezier curves. You can instantiate a curve by passing an array of coordinate objects or a flat array of numbers.

    Coordinate Formats:

    • Object Array: [{x, y}, {x, y}, ...]
    • Flat Array: [x1, y1, x2, y2, ...]
    • 3D Support: If the input objects contain a z property or if the flat array has a length that implies 3D (multiples of 3), the curve will be treated as 3D.

    Order: The curve order is determined by the number of points: order = points.length - 1.

  7. Fit a cubic Bezier curve from points

    master

    Use the static method Bezier.cubicFromPoints(S, B, E, t, d1) to create a cubic curve.

    • S: Start point
    • B: Control point 1
    • E: Control point 2
    • t: Parameter (default 0.5)
    • d1: Distance for the first control point (if undefined, it is calculated based on the distance between B and the calculated point C).
  8. Fit a quadratic Bezier curve from points

    master
    Use the static method Bezier.quadraticFromPoints(p1, p2, p3, t) to create a quadratic curve that passes through or near specific points. The parameter t (defaulting to 0.5) allows you to specify a point on the curve to assist in the fitting process.
  9. Generate an outline for a curve

    master

    The .outline(d1, d2, d3, d4) method creates a PolyBezier representing the outline (offset) of the curve.

    • d1: Offset distance for the 'front' side.
    • d2: Offset distance for the 'back' side.
    • d3, d4: Optional graduated offset distances for the end of the curve.