0.2.1 • Published 2 years ago

hobby-curve v0.2.1

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

Hobby Curve

A tiny (1.3kb gzip) typescript implementation of the hobby curve algorithm without dependencies.

Install

npm i hobby-curve

Usage

import { createHobbyCurve } from 'hobby-curve'

const points = [
  { x: 0, y: 0 },
  { x: 200, y: 133 },
  { x: 130, y: 300 },
  { x: 33, y: 233 },
  { x: 100, y: 167 },
]
const pathDescription = createHobbyCurve(points, { tension: 1, cyclic: true })
const svg = `<svg><path d="${pathDescription}" /></svg>`

// Use the svg path somewhere.
document.body.innerHtml = svg

If you don't need an svg path, you can create just the bézier points:

import { createHobbyBezier } from 'hobby-curve'

const points = [
  { x: 0, y: 0 },
  { x: 200, y: 133 },
  { x: 130, y: 300 },
  { x: 33, y: 233 },
  { x: 100, y: 167 },
]

const bezier = createHobbyBezier(points, { tension: 1, cyclic: true })
bezier.forEach(({ startControl, endControl, point }) =>
  // do something
)

Credits