Add natural freehand drawing to a web app by passing pointer coordinates through getStroke and rendering the result on an HTML Canvas.
Build a digital whiteboard or annotation tool where strokes look hand-drawn rather than mechanical.
Simulate pen pressure for mouse or touch input so drawings look natural even without a drawing tablet.
Use the Dart or Rust port to add freehand strokes to a Flutter or native app.
Perfect Freehand is a JavaScript library that converts a series of pointer coordinates into a smooth, variable-width stroke shape. If you record the x and y positions as someone draws with a mouse or stylus, the library produces the outline of a natural-looking line that gets thicker or thinner based on the speed or pressure of the movement. The core of the library is a single function called getStroke. You pass it an array of points, one per recorded position, and it returns a new array of points that trace the outline of a polygon representing the stroke. You then take those outline points and draw them yourself, using SVG, HTML Canvas, or whatever rendering method fits your project. Pressure can come from two sources. By default, the library simulates it based on how fast the pointer is moving: slow movement produces a thicker stroke, fast movement produces a thinner one. If you have real pressure data, such as from a drawing tablet or a touch device that reports stylus pressure, you can include that as a third value for each input point and turn simulation off. Several options let you tune the appearance of the stroke. Size sets the maximum diameter. Thinning controls how much pressure affects thickness. Smoothing and streamline affect how closely the stroke follows the raw input points versus averaging them out. You can also add tapering at the start and end of a stroke, and apply custom easing curves to the pressure. The library is written in TypeScript and published to npm. Community-maintained ports exist for Dart, Python, Rust, and other languages.
← steveruizok on gitmyhub — every repo by this author, as a profile.
Verify against the repo before relying on details.