npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

points-on-curve

v1.0.1

Published

Estimate points on a bezier curve or a set of connexted bezier curves

Downloads

885,518

Readme

points-on-curve

This package calculate the points on a curve with a certain tolerance. It can also simplify the shape to use fewer points. This can really be useful when estimating lines/polygons for curves in WebGL or for Hit/Collision detections.

Install

From npm

npm install --save points-on-curve

The package is distributed as an ES6 module.

API

pointsOnBezierCurves(points: Point[], tolerance?: number, distance?: number): Point[]

You pass in the points representing a bezier curve. Each point is an array of two numbers e.g. [100, 123].

The points can also be a set of continuous curves where the last poing on the Nth curve acts as the first point of the next.

import { pointsOnBezierCurves } from 'points-on-curve';

const curve = [[70,240],[145,60],[275,90],[300,230]];
const points = pointsOnBezierCurves(curve);
// plotPoints(points);

points on bezier

Same can be rendered with more tolerance (default value is 0.15):

const points = pointsOnBezierCurves(curve, 0.7);

points on bezier with 0.7 tolerance

Note that this method does not accept the number of points to render, but takes in a tolerance level which allows for better distribution of points.

The value of tolerance can be between 0 and 1. It is used to decide how many points are needed in a section of the curve. The algorithm determined the flatness of a section of the curve and compares it to the tolerance level, if less flat, the segment gets further divided into 2 segments.

Simplifying path

Based on the tolerance alone, this algorithm nicely provides enough points to represent a curve. It does not, however, efficiently get rid of unneeded points. The second optional argument in function, distance helps with that. If a distance value is provided, the method uses the Ramer–Douglas–Peucker algorithm to reduce the points.

const points = pointsOnBezierCurves(curve, 0.2, 0.15);

Following are the points generated with distance values of 0.15, 0.75, 1.5, and 3.0

points with 0.15d points with 0.75d points with 1.5d points with 3.0d

curveToBezier(pointsIn: Point[]): Point[]

Sometimes it's hard to think of shape as a set of cubic bezier curves, each curve with 2 controls points. It is simple to just think of them as a curve passing through a set of points.

This method turns those set of points to a set of points representing bezier curves.

import { curveToBezier } from 'points-on-curve/lib/curve-to-bezier.js';

const curvePoints = [
  [20, 240],
  [95, 69],
  [225, 90],
  [250, 180],
  [290, 220],
  [380, 80],
];
const bcurve = curveToBezier(curvePoints);
// .. Plot bcurve

Curve through points

Now that we have bezier points, these could be passed to pointsOnBezierCurves function to get the points on the curve

Curve through points

License

MIT License