Perfect Freehand
Draw perfect pressure-sensitive freehand lines.
README
perfect-freehand
Draw perfect pressure-sensitive freehand lines.
🔗 Curious? Try out a demo.
💅 Designer? Check out the Figma Plugin.
💕 Love this library? Consider becoming a sponsor.
Also available in:
- 🎯 Flutter / Dart
- 🐍 Python
Installation
- ```bash
- npm install perfect-freehand
- ```
or
- ```bash
- yarn add perfect-freehand
- ```
Introduction
This package exports a function named getStroke that will generate the points for a polygon based on an array of points.
To do this work, getStroke first creates a set of spline points (red) based on the input points (grey) and then creates outline points (blue). You can render the result any way you like, using whichever technology you prefer.
Usage
To use this library, import the getStroke function and pass it an array of input points, such as those recorded from a user's mouse movement. The getStroke function will return a new array of outline points. These outline points will form a polygon (called a "stroke") that surrounds the input points.
- ```js
- import { getStroke } from 'perfect-freehand'
- const inputPoints = [
- [0, 0],
- [10, 5],
- [20, 8],
- // ...
- ]
- const outlinePoints = getStroke(inputPoints)
- ```
You then can render your stroke points using your technology of choice. See the Rendering section for examples in SVG and HTML Canvas.
You can customize the appearance of the stroke shape by passing getStroke a second parameter: an options object containing one or more options. See the Options section for a full list of available options.
- ```js
- const stroke = getStroke(myPoints, {
- size: 32,
- thinning: 0.7,
- })
- ```
The appearance of a stroke is effected by the pressure associated with each input point. By default, the getStroke function will simulate pressure based on the distance between input points.
To use real pressure, such as that from a pen or stylus, provide the pressure as the third number for each input point, and set the simulatePressure option to false.
- ```js
- const inputPoints = [
- [0, 0, 0.5],
- [10, 5, 0.7],
- [20, 8, 0.8],
- // ...
- ]
- const outlinePoints = getStroke(inputPoints, {
- simulatePressure: false,
- })
- ```
In addition to providing points as an array of arrays, you may also provide your points as an array of objects as show in the example below. In both cases, the value for pressure is optional (it will default to .5).
- ```js
- const inputPoints = [
- { x: 0, y: 0, pressure: 0.5 },
- { x: 10, y: 5, pressure: 0.7 },
- { x: 20, y: 8, pressure: 0.8 },
- // ...
- ]
- const outlinePoints = getStroke(inputPoints, {
- simulatePressure: false,
- })
- ```
Note: Internally, the getStroke function will convert your object points to array points, which will have an effect on performance. If you're using this library ambitiously and want to format your points as objects, consider modifying this library's getStrokeOutlinePoints to use the object syntax instead (e.g. replacing all [0] with .x, [1] with .y, and [2] with .pressure).
Example
- ```jsx
- import * as React from 'react'
- import { getStroke } from 'perfect-freehand'
- import { getSvgPathFromStroke } from './utils'
- export default function Example() {
- const [points, setPoints] = React.useState([])
- function handlePointerDown(e) {
- e.target.setPointerCapture(e.pointerId)
- setPoints([[e.pageX, e.pageY, e.pressure]])
- }
- function handlePointerMove(e) {
- if (e.buttons !== 1) return
- setPoints([...points, [e.pageX, e.pageY, e.pressure]])
- }
- const stroke = getStroke(points, {
- size: 16,
- thinning: 0.5,
- smoothing: 0.5,
- streamline: 0.5,
- })
- const pathData = getSvgPathFromStroke(stroke)
- return (
- <svg
- onPointerDown={handlePointerDown}
- onPointerMove={handlePointerMove}
- style={{ touchAction: 'none' }}
- >
- {points && <path d={pathData} />}
- </svg>
- )
- }
- ```
Tip: For implementations in Typescript, see the example project included in this repository.