arnoson / hobby-curve

Typescript implementation of the hobby curve algorithm

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

Typescript implementation of the hobby curve algorithm


Languages

Language:TypeScript 95.3%Language:JavaScript 4.7%