acamposuribe / p5.brush

Unlock custom brushes, natural fill effects and intuitive hatching in p5.js
https://p5-brush.cargo.site/
MIT License
442 stars 22 forks source link
brush brushes children flowfield hatching p5 p5-library p5js watercolor

Teaser

p5.brush.js

p5.brush.js is a versatile library for the p5.js ecosystem, tailored for artists, designers, and hobbyists who wish to explore natural textures in generative art. This library extends the drawing capabilities of p5.js by introducing a rich set of tools that allow for the creation of dynamic and customizable brushes, vector-fields, and fill modes.

With p5.brush.js, you can easily configure and manage intricate drawing systems, inject life into your sketches with organic movements, and apply complex vector fields to control the flow and form of strokes. The library is designed with texture quality in mind, and may only be suitable for high-resolution artworks, not real-time interactive pieces.

Whether you're looking to simulate natural media, create patterned backgrounds, or design intricate particle systems, p5.brush.js offers the functionalities to turn your vision into reality. The API is straightforward and modular, providing both high-level functions for quick setup and in-depth customization options for advanced users.

Embrace the full potential of your creative coding projects with p5.brush.js, where every stroke is a brush with possibility.

Visit the library website here! (more examples soon)

Table of Contents

Installation

Local Installation

To set up your project, add p5.min.js p5.brush.js to your HTML file. You can download the last version of the p5.brush.js library in the dist folder. Place the script tags in the following order:

<!-- Commented version of p5.brush.js, with a Spectral.js dependency -->
<script src="https://github.com/acamposuribe/p5.brush/raw/main/path_to/p5.min.js"></script>
<script src="https://github.com/acamposuribe/p5.brush/raw/main/path_to/p5.brush.js"></script>

Replace path_to with the actual path to the script in your project directory or the URL if you are using a CDN.

Use a hosted version of the p5.brush.js library

Alternatively, you can link to a p5.brush.js file hosted online. All versions are stored in a CDN (Content Delivery Network). You can find a history of these versions in the p5.js CDN. In this case you can change the link to:

<!-- Online version of p5.brush -->
<script src="https://cdn.jsdelivr.net/npm/p5.brush@1.1.1/dist/p5.brush.js"></script>

Install with NPM and other modular-based apps

Install the npm package. p5.brush requires p5.js as a peer dependency.

npm install p5.brush --save

After that, import p5.brush functions to your sketch:

import * as brush from 'p5.brush'

If you are using p5 and p5.brush as modules, you will need to use instance mode. Read below.

Note for p5 instance mode

By default, all p5.js functions are in the global namespace (i.e. bound to the window object), meaning you can call them simply ellipse(), fill(), etc. However, this might be inconvenient if you are mixing with other JS libraries (synchronously or asynchronously) or writing long programs of your own. p5.js currently supports a way around this problem called "instance mode". In instance mode, all p5 functions are bound up in a single variable instead of polluting your global namespace.

If you plan to use p5 instance mode, you need to load p5.brush in a specific way:

- Use `brush.instance()` before the setup and draw functions, pointing to your sketch id and the p5 function argument
Example:
```javascript
  let sketch = function(p) {
    let x = 100;
    let y = 100;

    // Register instance method here, sending your function arg p
    brush.instance(p)

    p.setup = function() {
      // Important to create the canvas in WEBGL mode
      p.createCanvas(700, 410, p.WEBGL);
      // Don't forget to load the library after canvas is created
      brush.load()
    };

    p.draw = function() {
      p.background(0);
      brush.fill("red", 75);
      brush.rect(x, y, 50, 50);
    };
  };

  let myp5 = new p5(sketch);
  ```

Features

p5.brush.js enhances the p5.js framework with a set of tools that allow for sophisticated drawing and rendering techniques.

With p5.brush.js, your digital canvas becomes a playground for innovation and expression, where each tool is fine-tuned to complement your unique creative process.

.

Reference

p5.brush.js provides a comprehensive API for creating complex drawings and effects. Below are the categorized functions and classes available in the library.

Table of Functions

Section Functions Section Functions
Utility brush.push() Hatch Operations brush.hatch()
brush.pop() brush.noHatch()
brush.rotate() brush.setHatch()
brush.scale() Geometry brush.line()
brush.reDraw() brush.flowLine()
brush.reBlend() brush.beginStroke()
Vector-Fields brush.field() brush.segment()
brush.noField() brush.endStroke()
brush.refreshField() brush.spline()
brush.listFields() brush.plot()
brush.addField() brush.rect()
Brush Management brush.box() brush.circle()
brush.add() brush.beginShape()
brush.clip() brush.vertex()
brush.noClip() brush.endShape()
Stroke Operations brush.set() brush.polygon()
brush.pick() Configuration brush.seed()
brush.stroke() brush.load()
brush.noStroke() brush.preload()
brush.strokeWeight() brush.colorCache()
Fill Operations brush.fill() brush.scaleBrushes()
brush.noFill() brush.remove()
brush.bleed() brush.instance()
brush.fillTexture() Classes brush.Polygon()
brush.Plot()
brush.Position()

back to table

Utility Functions






back to table

Vector Fields

Vector Fields allow for dynamic control over brush stroke behavior, enabling the creation of complex and fluid motion within sketches.

Basic vector-field functions






Advanced vector-field functions



back to table

Brush Management

Functions for managing brush behaviors and properties.






back to table

Stroke Operations

Stroke Operations encompass methods for manipulating and applying brushes to strokes (aka lines), providing artists with precise control over their brushwork.







back to table

Fill Operations

The Fill Management section focuses on managing fill properties for shapes, enabling complex fill operations with effects like bleeding to simulate watercolor-like textures. These methods set fill colors with opacity, control bleed intensity, and manage fill operations. The watercolor fill effect is inspired by Tyler Hobbs' generative art techniques.

IMPORTANT: At the moment, fill operations expect an array of vertices in the clockwise direction. Otherwise, the fill will "bleed" to the inside, destroying the effect. I'll try to fix this on a forthcoming update.







back to table

Hatch Operations

The Hatching section focuses on creating and drawing hatching patterns, which involves drawing closely spaced parallel lines. These functions offer control over the hatching style and application.





In essence, the hatching system activates hatches for subsequent shapes, similarly to stroke and fill operations. However, you can also directly hatch multiple objects at once (and their intersections), if you proceed as described below .


back to table

Geometry

This section details the functions for creating various shapes and strokes with the set brush, fill, and hatch parameters.

Lines, Strokes, Splines, and Plots

The following functions are only affected by stroke() operations, completely ignoring fill() and hatch().




These three functions provide advanced control over the creation of strokes/paths, allowing for custom pressure and direction at different points along the path. This is a strange way of defining strokes, but intuitive when you think of them as bodily movements performed with the hands. You can create two types of strokes: "curve" or "segments". For curved strokes, the curvature at any point of the stroke is lerped between the nearest control points.

These functions allow for the creation of strokes with varied pressures and directions, mimicking the organic nature of hand-drawn strokes. For an application of these principles, see: Enfantines II




Shapes and Polygons

The following functions are affected by stroke(), fill() and hatch() operations.




These three functions perform similarly to the p5.js beginShape(), vertex(), and endShape() functions, althouh curvature calculation is very different. These allow you to draw custom shapes, with fine control over brush pressure at the different points of the perimeter.



back to table

Optional: Configuration

This section covers functions for initializing the drawing system, preloading required assets, and configuring system behavior. By default, the library works without executing these functions, but you might want to configure them to your liking.









back to table

Exposed Classes

Exposed Classes provide foundational elements for creating and manipulating shapes and paths, as well as interacting with vector-fields in a more advanced manner.


Class: brush.Polygon


Class: brush.Plot


Class: brush.Position

Examples

Contributing

We welcome contributions from the community. If you find a bug or have a feature request, please open an issue on Github.

License

p5.brush.js is released under the MIT License. See the LICENSE file for details.

Acknowledgements