Git Product home page Git Product logo

lazy-brush's Introduction

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

Demo drawing app

Example repository

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 pointer 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.

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

const lazy = new LazyBrush({
  radius: 30,
  enabled: true,
  initialPoint: { x: 0, y: 0 }
}) // default

lazy.update({ x: 50, y: 0 })
console.log(lazy.getBrushCoordinates()) // { x: 0, y: 0 }

lazy.update({ x: 200, y: 50 })
console.log(lazy.getBrushCoordinates()) // { x: 100, y: 0 }

Use the LazyBrush.update() function to update the pointer position. That would be when the mouse is moved. 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().

To get the coordinates , use LazyBrush.getBrushCoordinates() or LazyBrush.getPointerCoordinates(). This will return an object with x and y properties.

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

Performance

For performance reasons it's best to decouple calculations and canvas rendering from mousemove/touchmove events: One way is to store the current coordinates in a variable. Then, using an animation loop (typically requestAnimationFrame), call the LazyBrush.update() function on every frame with the latest coordinates from the variable.

The library will only do calculations if the pointer or brush values changed.

lazy-brush's People

Contributors

dulnan avatar

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.