lazy-brush: 手写签名 JavaScript 库

2025年10月02日 16:02:56
6913
javscript 手写 签名
lazy-brush dulnan/lazy-brush

用于平滑绘图的 JavaScript 库,支持通过鼠标或手指流畅绘制画笔轨迹。采用“惰性画笔”算法,有效减少手抖、锯齿等问题,让线条更加自然顺滑,适用于画板、签名、手写等多种场景。

项目大小 173 Byte
涉及语言 TypeScript 86.45% HTML 10.93% JavaScript 2.62%
许可协议 LICENSE
仓库同步说明
  • • 同步需要仓库写入权限以创建目标仓库
  • • 使用平台账号授权登录后将同步到您平台下的个人仓库

lazy-brush - smooth drawing with a mouse, finger or any pointing device

lazy-brush banner

Demo - NPM - CodePen Examples

The demo app also uses
catenary-curve to draw the little
“rope” between mouse and brush.

This library provides the math required to implement a “lazy brush”.
It takes a radius and the {x,y} coordinates of a mouse/pointer and calculates
the position of the brush.

The brush will only move when the pointer is outside the “lazy area” of the
brush. With this technique it’s possible to freely draw smooth lines and curves
with just a mouse or finger.

How it works

When the position of the pointer is updated, the distance to the brush is
calculated. If this distance is larger than the defined radius, the brush will
be moved by distance - radius pixels in the direction where the pointer is.

Usage

lazy-brush is on npm so you can install it with your favorite package manager.

1
npm install --save lazy-brush

lazy-brush can be easily added in any canvas drawing scenario. It acts like a
“proxy” between user input and drawing.

It exports a LazyBrush class. Create a single instance of the class:

1
2
3
4
5
const lazy = new LazyBrush({
  radius: 30,
  enabled: true,
  initialPoint: { x: 0, y: 0 }
})

You can now use the update() method whenever the position of the mouse (or
touch) changes:

1
2
3
4
5
6
7
8
9
// Move mouse 20 pixels to the right.
lazy.update({ x: 20, y: 0 })
// Brush is not moved, because 20 is less than the radius (30).
console.log(lazy.getBrushCoordinates()) // { x: 0, y: 0 }
 
// Move mouse 40 pixels to the right.
lazy.update({ x: 40, y: 0 })
// Brush is now moved by 10 pixels because 40 (mouse X) - 30 (radius) = 10.
console.log(lazy.getBrushCoordinates()) // { x: 10, y: 0 }

The function returns a boolean to indicate whether any of the values (brush or
pointer) have changed. This can be used to prevent unneccessary canvas
redrawing.

If you need to know if the position of the brush was changed, you can get that
boolean via LazyBrush.brushHasMoved(). Use this information to decide if you
need to redraw the brush on the canvas.

To get the current brush coordinates, use LazyBrush.getBrushCoordinates().
For the pointer coordinates use LazyBrush.getPointerCoordinates(). This will
return a Point object with x and y properties.

The functions getBrush() and getPointer() will return a LazyPoint, which
has some additional functions like getDistanceTo, getAngleTo or equalsTo.

With Friction

You can also pass a friction value (number between 0 and 1) when calling update():

1
lazy.update({ x: 40, y: 0 }, { friction: 0.5 })

This will reduce the speed at which the brush moves towards the pointer. A
value of 0 means “no friction”, which is the same as not passing a value. 1
means “inifinte friction”, the brush won’t move at all.

You can define a constant value or make it dynamic, for example using a pressur
value from a touch event.

Updating both values

You can also update the pointer and the brush coordinates at the same time:

1
lazy.update({ x: 40, y: 0 }, { both: true })

This can be used when supporting touch events: On touch start you would update
both the pointer and the brush so that the pointer can be “moved” away from the
brush until the lazy radius is reached. This is how it’s implemented in the
demo page.

Examples

Check out the basic example for a simple starting point
on how to use this library.


                

                

免责声明 © 2026 - 虚宝阁

本站部分源码来源于网络,版权归属原开发者,用户仅获得使用权。依据《计算机软件保护条例》第十六条,禁止:

  • 逆向工程破解技术保护措施
  • 未经许可的分发行为
  • 去除源码中的原始版权标识

※ 本站源码仅用于学习和研究,禁止用于商业用途。如有侵权, 请及时联系我们进行处理。

侵权举报请提供: 侵权页面URL | 权属证明模板

响应时效:收到完整材料后48小时内处理

仓库下载

gitee

GitHub 下载代理

文件信息

文件名
文件大小
文件类型
代理耗时