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

catenary-curve

v2.0.2

Published

Calculate the perfect catenary between two points

Downloads

92,452

Readme

Catenary Curve - Approximates the catenary curve between two points

catenary-curve banner

Demo - NPM - In use

One would assume it's easy to draw a hanging rope or chain between two points, but it's not, especially if you want it to look realistic.

This TypeScript/JavaScript library provides a function that, given two points and a chain length, returns an approximation of the catenary curve as quadratic curve points.

It also provides helper methods to draw the result to a 2D canvas.

How to use

npm install --save catenary-curve
import { getCatenaryCurve, drawResult, Point } from 'catenary-curve'

const p1: Point = { x: 200, y: 300 }
const p2: Point = { x: 250, y: 400 }

const context = canvas.getContext('2d')

context.beginPath()
context.lineWidth = 1
context.strokeStyle = 'black'

const result = getCatenaryCurve(p1, p2, 500)
drawResult(result, context)

context.stroke()

Exports

getCatenaryCurve

function getCatenaryCurve(
  point1: Point,
  point2: Point,
  chainLength: number,
  options?: CatenaryOptions
): CatenaryCurveResult

Provide the start and end point and the length of the chain. It returns an object with the approximated values.

{
  "type": "quadraticCurve",
  "start": [
    480.3333333333333,
    213.5920866272993
  ],
  "curves": [
    [
      485.2094017094017,
      237.1602015730591,
      490.0854700854701,
      258.6001522901396
    ],
    [
      846.0384615384615,
      202.65311976627885,
      850.9145299145299,
      175.99552022287116
    ],
    [
      855.7905982905983,
      149.33792067946348,
      860.6666666666666,
      120.05645761711651
    ]
  ]
}

This resulting object can be passed to the drawResult method:

const result = getCatenaryCurve(p1, p2, 500)
drawResult(result, context)

Alternatively you can also do the drawing yourself:

const result = getCatenaryCurve(p1, p2, 500)

context.moveTo(result.start[0], result.start[1])

for (let i = 0; i < result.curves.length; i++) {
  context.quadraticCurveTo(
    result.curves[i][0], // cpx
    result.curves[i][1], // cpy
    result.curves[i][2], // x
    result.curves[i][3], // y
  )
}

Straight lines

Here both the start and end points have the same position on the Y axias and their distance on the X axis is exactly 300, the same as the chain length. So there is no curve, only a straight line. In this case the curve is not calcuated and the line result type is returned:

const result = getCatenaryCurve({ x: 100, y: 300 }, { x: 400, y: 300 }, 300)
{
  "type": "line",
  "start": [
    100,
    300
  ],
  "lines": [
    [
      400,
      300
    ]
  ]
}

Options

options.segments?: number

The amount of segments used to approximate the curve. The higher the value the more accurate it will be, but will require more calculations.

A value of 25 is usually enough for a very realistic approximation. Values above that only show barely any noticeable differences.

options.iterationLimit?: number

The number of iterations used to determine the catenary parameter. A higher value will yield more accurate curves, but requires more calculations.

A value around 5 is usually enough.

Acknowledgement

The basis of this library is an ActionScript by poiasd, originally released on wonderfl.net, archived and preserved at http://wa.zozuar.org/code.php?c=8Bnl.

Unfortunately I wasn't able to find out who the original author was and ask them if and how they want to be mentioned/linked.