Coder Social home page Coder Social logo

dulnan / lazy-brush Goto Github PK

View Code? Open in Web Editor NEW
772.0 7.0 23.0 173 KB

Smooth drawing with mouse, finger or other pointing device

Home Page: https://lazybrush.dulnan.net/

License: MIT License

JavaScript 2.62% HTML 10.93% TypeScript 86.45%
brush canvas drawing javascript lazy painting typescript

lazy-brush's Introduction

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.

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:

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:

// 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():

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:

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.

lazy-brush's People

Contributors

dulnan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

lazy-brush's Issues

question

Hi

thank you for this library, I tried other methods to make smooth paths but the result I get from your method is awesome, I have a question though if you don't mind explain it a bit:

In the lazyPoint file there is a line where you are rotating the angle like this:

// Rotate the angle based on the browser coordinate system ([0,0] in the top left)
const angleRotated = angle + Math.PI / 2

I am kind of confused why you did that?

i don't know how to run it

i just "npm install" and "npm run start",but it say error!
Can the author give me a description of the running process?

My English is not very good, please forgive me :)

Does this have support for things like force changes?

My app has pressure sensitivity, so I'm struggling to see where I can integrate this. It seems like you can specify the radius when it's initially defined, but if I need to change the current radius, or have different ones per-stroke, I'm not sure how I would do that.

Hi , is it possible to have a .min file.

Hi , is it possible to have a .min file ?
like a distribution file ready for production ?

I want to be able to download a single file , plug it in my page and start using it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.