Socket
Socket
Sign inDemoInstall

perfect-freehand

Package Overview
Dependencies
Maintainers
1
Versions
56
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

perfect-freehand

Perfect freehand is a library for creating freehand paths by [@steveruizok](https://twitter.com/steveruizok).


Version published
Weekly downloads
89K
decreased by-0.35%
Maintainers
1
Weekly downloads
 
Created
Source

Perfect Freehand

Perfect freehand is a library for creating freehand paths by @steveruizok.

Screenshot

🔗 Demo

Installation

npm install perfect-freehand

or

yarn add perfect-freehand

Usage

The library exports a default function, getPath, that accepts an array of points and an (optional) options object and returns SVG path data for a stroke.

The array of points may be either an array of number pairs representing the point's x, y, and (optionally) pressure...

import getPath from 'perfect-freehand'

const path = getPath([
  [0, 0],
  [10, 5],
  [20, 8],
])

const path = getPath([
  [0, 0, 0],
  [10, 5, 0.5],
  [20, 8, 0.3],
])

...or an array of objects with x, y, and (optionally) pressure properties.

getPath([
  { x: 0, y: 0 },
  { x: 10, y: 5 },
  { x: 20, y: 8 },
])

getPath([
  { x: 0, y: 0, pressure: 0, },
  { x: 10, y: 5, pressure: 0.5 },
  { x: 20, y: 8, pressure: 0.3 },
])

Options

The options object is optional, as are its properties.

PropertyTypeDefaultDescription
minSizenumber2.5The thinnest size of the stroke.
maxSizenumber8The thickest size of the stroke.
pressurebooleantrueWhether to apply (or simulate) pressure from the input device.
streamlinenumber.5How much to streamline the stroke.
smoothnumber.5How much to soften the stroke's edges.
clipbooleantrueWhether to flatten the stroke into a single polygon.
getPath(myPoints, {
  type: 'pen',
  minSize: 2.5,
  maxSize: 8,
  simulatePressure: true,
  streamline: 0.5,
  smooth: 0.5,
})

Example

import * as React from 'react'
import getPath from 'perfect-freehand'

export default function Example() {
  const [currentType, setCurrentType] = React.useState('mouse')
  const [currentMark, setCurrentMark] = React.useState([])

  function handlePointerDown(e) {
    setCurrentType(e.pointerType)
    setCurrentMark([[e.pageX, e.pageY, e.pressure]])
  }

  function handlePointerMove(e) {
    if (e.buttons === 1 && e.pointerType === currentType) {
      setCurrentMark([...currentMark, [e.pageX, e.pageY, e.pressure]])
    }
  }

  return (
    <svg
      width={800}
      height={600}
      onPointerDown={handlePointerDown}
      onPointerMove={handlePointerMove}
    >
      <path d={getPath(currentMark)} />
    </svg>
  )
}

Edit perfect-freehand-example

Advanced Usage

For advanced usage, the library also exports smaller functions that getPath uses to generate its SVG data. While you can use getPath's data to render strokes with an HTML canvas (via the Path2D element) or with SVG paths, these new functions will allow you to create paths in other rendering technologies.

getStrokePoints

Accepts an array of points (formatted either as [x, y, pressure] or { x: number, y: number, pressure: number}) and returns a set of streamlined points as [x, y, pressure, angle, distance, length]. The path's total length will be the length of the last point in the array.

getStrokeOutlinePoints

Accepts an array of points (formatted as [x, y, pressure, angle, distance, length], i.e. the output of getStrokePoints) and returns an array of points ([x, y]) defining the outline of a pressure-sensitive stroke.

getShortStrokeOutlinePoints

Works like getStrokeOutlinePoints, but designed to work with short paths.

clipPath

Accepts a series of points (formatted as [x, y], i.e. the output of getStrokeOutlinePoints or getShortStrokeOutlinePoints) and returns a polygon (a series of faces) from the stroke.

FAQs

Package last updated on 20 Feb 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc