Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
perfect-freehand
Advanced tools
Perfect freehand is a library for creating freehand paths by [@steveruizok](https://twitter.com/steveruizok).
The perfect-freehand npm package is designed to generate smooth, natural-looking strokes from a series of input points, which is particularly useful for drawing applications. It provides a way to create freehand drawings that look more polished and professional by smoothing out the input data.
Generate Smooth Strokes
This feature allows you to generate a smooth stroke from a series of input points. The code sample demonstrates how to use the `getStroke` function from the perfect-freehand package to create a stroke with specific options for size, thinning, smoothing, and streamline.
const { getStroke } = require('perfect-freehand');
const points = [
[0, 0],
[1, 1],
[2, 2],
[3, 3],
[4, 4]
];
const options = {
size: 8,
thinning: 0.5,
smoothing: 0.5,
streamline: 0.5
};
const stroke = getStroke(points, options);
console.log(stroke);
Paper.js is an open-source vector graphics scripting framework that runs on top of the HTML5 Canvas. It provides a powerful API for creating and manipulating vector graphics, including freehand drawing. Compared to perfect-freehand, Paper.js offers a broader range of vector graphics functionalities but may not be as specialized in generating smooth freehand strokes.
Rough.js is a library that allows you to create graphics that appear hand-drawn. It can be used to create freehand-like drawings, but its primary focus is on creating rough, sketchy graphics rather than smooth strokes. Compared to perfect-freehand, Rough.js is more about the aesthetic of roughness rather than the smoothness of freehand strokes.
Fabric.js is a powerful and simple JavaScript HTML5 canvas library. It provides an interactive object model on top of the canvas element, which includes support for freehand drawing. While Fabric.js offers a wide range of canvas manipulation features, perfect-freehand is more focused on the quality and smoothness of the stroke itself.
Perfect freehand is a library for creating freehand paths by @steveruizok.
🔗 Demo
npm install perfect-freehand
or
yarn add perfect-freehand
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 },
])
The options object is optional, as are its properties.
Property | Type | Default | Description |
---|---|---|---|
minSize | number | 2.5 | The thinnest size of the stroke. |
maxSize | number | 8 | The thickest size of the stroke. |
simulatePressure | boolean | true | Whether to simulate pressure based on velocity. |
pressure | boolean | true | Whether to apply pressure. |
streamline | number | .5 | How much to streamline the stroke. |
smooth | number | .5 | How much to soften the stroke's edges. |
clip | boolean | true | Whether to flatten the stroke into a single polygon. |
getPath(myPoints, {
minSize: 2.5,
maxSize: 8,
simulatePressure: true,
pressure: true,
streamline: 0.5,
smooth: 0.5,
clip: true,
})
import * as React from 'react'
import getPath from 'perfect-freehand'
export default function Example() {
const [currentMark, setCurrentMark] = React.useState()
function handlePointerDown(e) {
setCurrentMark({
type: e.pointerType,
points: [[e.pageX, e.pageY, e.pressure]],
})
}
function handlePointerMove(e) {
if (e.buttons === 1) {
setCurrentMark({
...currentMark,
points: [...currentMark.points, [e.pageX, e.pageY, e.pressure]],
})
}
}
return (
<svg
width={800}
height={600}
onPointerDown={handlePointerDown}
onPointerMove={handlePointerMove}
style={{ touchAction: 'none' }}
>
{currentMark && (
<path
d={getPath(currentMark.points, {
simulatePressure: currentMark.type !== 'pen',
})}
/>
)}
</svg>
)
}
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
Draw perfect pressure-sensitive freehand strokes.
The npm package perfect-freehand receives a total of 0 weekly downloads. As such, perfect-freehand popularity was classified as not popular.
We found that perfect-freehand demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.