lazy-brush

repository·master·Indexed 22 days ago

https://github.com/dulnan/lazy-brush

A mathematical utility library for smooth, fluid drawing using imprecise input devices like mice or fingers. It implements a 'lazy area' around the brush, calculating a proxy position based on a defined radius and pointer input to create a dampened drawing effect. The library includes the LazyBrush class for managing smoothing logic and the LazyPoint class for coordinate manipulation, distance, and angle calculations.

Tokens
1.4K
Snippets
3
Records
11
Agent score
79%

What's inside lazy-brush

  1. How lazy-brush works

    master

    The library implements a 'lazy area' around the brush. When the pointer moves, the distance between the pointer and the brush is calculated.

    If the distance is greater than the defined radius, the brush moves by distance - radius pixels in the direction of the pointer. This creates a smooth, dampened drawing effect that allows for fluid lines even with imprecise input devices like a mouse.

  2. Retrieve brush and pointer coordinates

    master

    Use the following methods to access coordinate data:

    • getBrushCoordinates(): Returns a Point object { x, y } representing the current brush position.
    • getPointerCoordinates(): Returns a Point object { x, y } representing the current pointer position.
    • getBrush(): Returns a LazyPoint object for the brush.
    • getPointer(): Returns a LazyPoint object for the pointer.

    LazyPoint objects include additional utility methods:

    • getDistanceTo(point)
    • getAngleTo(point)
    • equalsTo(point)
  3. Update brush position with update()

    master

    Call update() whenever the pointer (mouse or touch) position changes.

    update(pointer, options) takes two arguments:

    1. pointer: A { x, y } object representing the current pointer position.
    2. options (optional): An object containing:
      • friction (number): A value between 0 and 1. 0 is no friction (default). 1 is infinite friction (brush won't move). This reduces the speed at which the brush moves towards the pointer.
      • both (boolean): If true, the brush position is updated to match the pointer position simultaneously. This is useful for touch events to prevent the pointer from being 'stuck' inside the radius on touch start.

    Returns a boolean indicating if either the brush or the pointer coordinates changed.

  4. Check if the brush has moved

    master
    To optimize canvas rendering, use brushHasMoved() to determine if the brush position has changed since the last update. This allows you to skip unnecessary redraws if the pointer is still within the radius area.
  5. Initialize the LazyBrush class

    master

    To use the library, instantiate the LazyBrush class. It acts as a proxy between user input (pointer) and your drawing logic.

    Pass an options object to the constructor with the following properties:

    • radius (number): The distance the pointer must move away from the brush before the brush starts moving.
    • enabled (boolean): Whether the brush logic is active.
    • initialPoint (object): An object with { x, y } coordinates to set the starting position.
    const lazy = new LazyBrush({
      radius: 30,
      enabled: true,
      initialPoint: { x: 0, y: 0 }
    })
  6. Control LazyBrush enabled state and radius

    master

    You can dynamically toggle the smoothing effect or adjust the smoothing radius during runtime.

    • enable(): Activates lazy brush calculations.
    • disable(): Deactivates lazy brush calculations. When disabled, the brush will follow the pointer exactly.
    • isEnabled(): Returns whether the brush is currently enabled.
    • setRadius(radius: number): Updates the smoothing radius.
    • getRadius(): Returns the current smoothing radius.
  7. Retrieve brush and pointer state

    master

    After calling update(), use these methods to retrieve the calculated coordinates and state for rendering your brush stroke.

    Coordinate Methods

    • getBrushCoordinates(): Returns the current brush position as a simple { x, y } object.
    • getPointerCoordinates(): Returns the current raw pointer position as a simple { x, y } object.
    • getBrush(): Returns the brush as a LazyPoint instance.
    • getPointer(): Returns the pointer as a LazyPoint instance.

    State and Metrics

    • brushHasMoved(): Returns true if the brush position changed during the last update() call.
    • getAngle(): Returns the angle (in radians) between the pointer and the brush.
    • getDistance(): Returns the distance (in pixels) between the pointer and the brush.
  8. Initialize LazyBrush

    master

    Create a new LazyBrush instance to manage smooth drawing calculations. You can configure the smoothing radius, whether the brush is enabled by default, and the starting position.

    Configuration Options

    OptionTypeDefaultDescription
    radiusnumber30The lazy radius used for smoothing calculations.
    enabledbooleantrueWhether lazy brush calculations are active.
    initialPointPoint{ x: 0, y: 0 }The starting coordinates for both the pointer and the brush.
  9. Use the LazyPoint class for coordinate manipulation

    master

    The LazyPoint class implements the Point interface and provides utility methods for calculating distances, angles, and moving points. It is designed to facilitate smooth movement and coordinate math.

    Key methods:

    • update(point: Point): Updates the current coordinates to match the provided Point.
    • moveByAngle(angle: number, distance: number, friction?: number): Moves the point based on a radian angle and distance. If friction is provided (a value where 1 is full distance), it reduces the movement to create a 'sluggish' or smoothed effect.
    • equalsTo(point: Point): Returns true if the coordinates are identical.
    • getDifferenceTo(point: Point): Returns a new LazyPoint representing the delta between the current point and the target.
    • getDistanceTo(point: Point): Returns the Euclidean distance to the target point.
    • getAngleTo(point: Point): Returns the angle (in radians) to the target point.
    • toObject(): Returns a plain Point object { x, y }.