- Notifications
You must be signed in to change notification settings - Fork35
Smooth drawing with mouse, finger or other pointing device
License
dulnan/lazy-brush
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
The demo app also usescatenary-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 calculatesthe position of the brush.
The brush will only move when the pointer is outside the "lazy area" of thebrush. With this technique it's possible to freely draw smooth lines and curveswith just a mouse or finger.
When the position of the pointer is updated, the distance to the brush iscalculated. If this distance is larger than the defined radius, the brush willbe moved bydistance - radius pixels in the direction where the pointer is.
lazy-brush is on npm so you can install it with your favorite package manager.
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 aLazyBrush class. Create a single instance of the class:
constlazy=newLazyBrush({radius:30,enabled:true,initialPoint:{x:0,y:0}})
You can now use theupdate() method whenever the position of the mouse (ortouch) changes:
// 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 orpointer) have changed. This can be used to prevent unneccessary canvasredrawing.
If you need to know if the position of the brush was changed, you can get thatboolean viaLazyBrush.brushHasMoved(). Use this information to decide if youneed to redraw the brush on the canvas.
To get the current brush coordinates, useLazyBrush.getBrushCoordinates().For the pointer coordinates useLazyBrush.getPointerCoordinates(). This willreturn aPoint object with x and y properties.
The functionsgetBrush() andgetPointer() will return aLazyPoint, whichhas some additional functions likegetDistanceTo,getAngleTo orequalsTo.
You can also pass a friction value (number between 0 and 1) when callingupdate():
lazy.update({x:40,y:0},{friction:0.5})
This will reduce the speed at which the brush moves towards the pointer. Avalue of 0 means "no friction", which is the same as not passing a value. 1means "inifinte friction", the brush won't move at all.
You can define a constant value or make it dynamic, for example using a pressurvalue from a touch event.
You can also update the pointer and the brush coordinates at the same time:
lazy.update({x:40,y:0},{both:true})
This can be used when supporting touch events: On touch start you would updateboth the pointer and the brush so that the pointer can be "moved" away from thebrush until the lazy radius is reached. This is how it's implemented in thedemo page.
Check out thebasic example for a simple starting pointon how to use this library.
About
Smooth drawing with mouse, finger or other pointing device
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
