p5.grain is a p5.js addon for conveniently adding grain and texture overlays to artworks.

Overview

🌾 p5.grain

p5.grain is a p5.js addon for conveniently adding grain and texture overlays to artworks.

p5.grain was also created with fxhash projects in mind that use the deterministic fxrand random function.

You can read more in detail about the different techniques to achieve grain in artworks in the article "All about that grain" by Gorilla Sun and meezwhite.

Getting started

Download the latest version from Releases and embed p5.grain.min.js (8 KB) in your project's HTML file after loading p5.js but before loading your sketch code.

<script src="./lib/p5.min.js"></script>
<!-- insert after p5.js -->
<script src="./lib/p5.grain.min.js"></script>
<script src="./sketch.js"></script>

Usage

The first step is to set up p5.grain according to your project's needs in the setup function of your sketch.

Non-deterministic setup

Suited for projects that don't have to be deterministic, don't use the pixel manipulation technique, or don't use methods for animating texture overlays.

function setup() {
    p5grain.setup();
}

Deterministic setup

Suited for projects that should be deterministic.

function setup() {
    // make Math.random be same as random
    Math.random = random;

    // set seeds (example)
    randomSeed(999999);
    noiseSeed(999999);

    p5grain.setup();
}

Deterministic setup (fxhash)

Suited for fxhash projects.

function setup() {
    // make Math.random be same as fxrand
    Math.random = fxrand;

    randomSeed(fxrand() * 999999);
    noiseSeed(fxrand() * 999999);

    // use fxrand as the internal random function
    p5grain.setup({ random: fxrand });
}

Ignoring errors and warnings

Initially, p5.grain will attempt to extend p5 core functionality by registering new methods. If a method cannot be registered because the method name is already in use, p5.grain will log a warning with a suggestion of an alternative usage. You can prevent warnings to be logged by passing ignoreWarnings: true to the config object when setting up p5.grain.

When using p5.grain methods, the library validates the parameters passed to the respective methods, and error messages are thrown in case of invalid parameters to attract attention during development. You can prevent errors to be thrown by passing ignoreErrors: true to the config object when setting up p5.grain.

Note: We recommend ignoring warnings and errors only when your sketch is final, and you've made sure that no p5.grain warnings or errors can occur.

function setup() {
    // ignore warnings and errors
    p5grain.setup({
        ignoreWarnings: true,
        ignoreErrors: true,
    });
}

Techniques

p5.grain currently supports the techniques: pixel manipulation, texture overlay and SVG filter. WebGL shader technique is coming soon.

Depending on how your artwork is created and whether you want to animate texture overlays, you would use p5.grain methods within the setup or draw functions of your sketch.

The best way to get you started with a technique is to checkout the provided standalone examples. There is an example for each technique currently supported by the library.

Go to standalone example:

Here are a few examples of a basic implementation for each respective technique. Note: the examples below are non-deterministic.

Pixel manipulation

function setup() {

    p5grain.setup();

    // draw your artwork here
    // ...

    granulateChannels(42);
}

Texture overlay inside canvas

let textureImage;

function preload() {
    textureImage = loadImage('./assets/texture.jpg');
}

function setup() {

    p5grain.setup();

    // draw your artwork here
    // ...

    textureOverlay(textureImage);
}

Texture overlay inside canvas with texture animation

let textureImage;

function preload() {
    textureImage = loadImage('./assets/texture.jpg');
}

function setup() {
    p5grain.setup();
}

function draw() {

    // draw your artwork here
    // ...

    textureOverlay(textureImage, { animate: true });
}

For more concrete use cases, please have a look at the provided examples.

API

p5.grain exposes the following API.

Note: p5.grain is still in the initial development phase and the API can still change. Always review the release notes.

The library initializes the global p5grain variable to a new P5Grain instance. You can directly access the fields and methods below from the p5grain variable. The library also attempts to register all p5.grain methods except setup with p5 by adding them to p5.prototype. This way, instead of calling, for example, p5grain.granulateSimple(42), you can conveniently call granulateSimple(42), although the former is also possible.

Fields

Field Type Description
version String Holds the p5.grain version in SemVer format.
ignoreWarnings Boolean Defines whether warnings should be ignored. (default: false)
ignoreErrors Boolean Defines whether errors should be ignored. (default: false)

Methods

Method Description
setup(config) Setup and configure certain p5.grain features.
granulateSimple(amount, alpha) Granulate the main canvas pixels by the given amount.
granulateChannels(amount, alpha) Granulate the main canvas pixels channels by the given amount.
granulateFuzzify(amount, fuzziness, alpha) Fuzzify and granulate the main canvas pixels by the given amount.
textureOverlay(textureImage, config) Blend the given texture image onto the canvas.
textureAnimate(textureElement, config) Animate the given texture element by randomly shifting its background position.

p5grain.setup(config)

Setup and configure certain p5grain features.

Property Type Description
config Object (optional) Config object to configure p5grain features.
config.random function (optional) The random function that should be used internally for pixel manipulation and texture animation.
config.ignoreWarnings Boolean (optional) Defines whether warnings should be ignored.
config.ignoreErrors Boolean (optional) Defines whether errors should be ignored.

granulateSimple(amount, alpha)

Granulate the main canvas pixels by the given amount.

This method generates one random value per pixel. The random value ranges from -amount to +amount and is added to every RGB(A) pixel channel.

Property Type Description
amount Number The amount of granularity that should be applied.
alpha Boolean (optional) Specifies whether the alpha channel should also be modified. When not specified the alpha channel will not be modified.

granulateChannels(amount, alpha)

Granulate the main canvas pixels channels by the given amount.

This method generates one random value per pixel channel. The random values range from -amount to +amount. Each random value is added to the respective RGB(A) channel of the pixel.

Property Type Description
amount Number The amount of granularity that should be applied.
alpha Boolean (optional) Specifies whether the alpha channel should also be modified. When not specified the alpha channel will not be modified.

granulateFuzzify(amount, fuzziness, alpha)

Fuzzify and granulate the main canvas pixels by the given amount.

This method modifies pixels in two steps:

  1. Selects a pixel $Pn$ that lies "width indices" + "2 pixel indices" further in the pixels array. The value of the current pixel $Pc$ is then calculated as follows: $Pc = (Pc + Pn) / 2$
  2. A random value per pixel channel is generated. The random values range from -amount to +amount. Each random value is added to the respective RGB(A) channel of the pixel.
Property Type Description
amount Number The amount of granularity that should be applied.
fuzziness Number (optional) The amount of fuzziness that should be applied or the amount of pixels the cavans should be fuzzified by. (default: 2)
alpha Boolean (optional) Specifies whether the alpha channel should also be modified. When not specified the alpha channel will not be modified. (default: false)

textureOverlay(textureImage, config)

Blend the given texture image onto the canvas.

The texture is repeated along the horizontal and vertical axes to cover the entire canvas (or context).

Property Type Description
texture p5.Image The texture image to blend over.
config Object (optional) Config object to configure the texture overlay.
config.width Number (optional) The width the texture image should have. When no width is specified, the width of the texture image is assumed.
config.height Number (optional) The height the texture image should have. When no height is specified, the height of the texture image is assumed.
config.mode Constant (optional) The blend mode that should be used to blend the texture over the canvas. Either BLEND, DARKEST, LIGHTEST, DIFFERENCE, MULTIPLY, EXCLUSION, SCREEN, REPLACE, OVERLAY, HARD_LIGHT, SOFT_LIGHT, DODGE, BURN, ADD or NORMAL. When no mode is specified, the blend mode MULTIPLY will be used.
config.context p5.Graphics (optional) The context on which the texture image should be drawn onto. When no context is specified, the main canvas will be used. Deprecated: Will be removed in favor of context.textureOverlay(arguments).
config.reflect Boolean (optional) Specifies whether the given texture image should reflect horizontally and vertically, in order to provide seamless continuity.
config.animate Boolean| Object (optional) Specifies whether the given texture image should be animated.
config.animate.atFrame Number (optional) When animation is activated, the frame at which the texture should be shifted. When atFrame isn't specified, the texture is shifted every 2nd frame.
config.animate.amount Number (optional) When animation is activated, the maximum amount of pixels by which the texture should be shifted. The actual amount of pixels which the texture is shifted by is generated randomly. When no amount is specified, the minimum of the main canvas width or height is used.

textureAnimate(textureElement, config)

Animate the given texture element by randomly shifting its background position.

Property Type Description
textureElement HTMLElement| SVGElement| p5.Element The texture element to be animated.
config Object (optional) Config object to configure the texture animation.
config.atFrame Number (optional) The frame at which the texture should be shifted. When atFrame isn't specified, the texture is shifted every 2nd frame.
config.amount Number (optional) The maximum amount of pixels by which the texture should be shifted. The actual amount of pixels which the texture is shifted by is generated randomly. When no amount is specified, the minimum of the main canvas width or height is used.

Note: Animation of SVGElement is currently unsupported. Will throw an error that cannot be ignored with ignoreErrors: true.

Limitations

  • p5.grain currently only works in p5's global mode.
  • granulate* methods currently only work on the main canvas pixels.
  • textureAnimate currently doesn't support animating SVG elements.

License

p5.grain is MIT licensed.

You might also like...

Simple & Quick Access Addon For Home Assistant

Simple & Quick Access Addon For Home Assistant

Home Assistant - Firefox Addon Quick Access Home Assistant - Firefox Addon Usage Create a Custom Dashboard With Quick Access Entity In Home Assistant

Dec 25, 2022

Simple & Quick Access Addon For Homer Dashboard

Simple & Quick Access Addon For Homer Dashboard

Homer Dashboard - Firefox Addon Quick Access Homer Dashboard - Firefox Addon Usage Install Addon [ Firefox ] Configure The Addon Add The Quick Access

Jan 22, 2022

L'addon qui sauveras les prochaines années

L'addon qui sauveras les prochaines années

GachaCleaner Une Extension Firefox pour purifier la lecture Download Changer "iel" 🤮 en : Permet de changer l'inexistant terme en quelque chose qui e

Jul 4, 2022

Jump to github urls (browser addon)

Jump to github urls (browser addon)

Currently a Firefox addon. Find GitHub locations quickly using your browser's history. Usage Use your mouse or keyboard. Use the filter to search for

Nov 28, 2022

Storybook Addon Root Attributes to switch html, body or some element attributes (multiple) at runtime for you story

Storybook Addon Root Attributes to switch html, body or some element attributes (multiple) at runtime for you story

Storybook Addon Root Attributes What is this This project was inspired by le0pard/storybook-addon-root-attribute The existing library received only on

Sep 6, 2022

An addon/plugin template for Zotero.

Zotero Addon Template This is an addon/plugin template for Zotero. Documentation(Chinese, provides English translation) 👍 You are currently in bootst

Jan 2, 2023

This project is about the awesome books we did during Microverse to build a website for adding and removing the books.Done using HTML and ES6 JAVASCRIPT and modules

Awesome-books-with-ES6 Description the project. this project is about the awesome books we did during Microverse to build a website for adding and rem

May 28, 2022

An interactive app that allows adding, editing and removing tasks of a to-do list. Drag-and-drop featured added. Webpack was used to bundle all the Js modules in to one main Js file.

An interactive app that allows adding, editing and removing tasks of a to-do list. Drag-and-drop featured added. Webpack was used to bundle all the Js modules in to one main Js file.

To-do List A to-do list app This app let you to set your own to-do list. Built With HTML CSS JavaScript WebPack Jest Live Page Page Link Getting Start

May 5, 2022

A simple app that helps a user monitor daily activities by adding, storing and deleting activities.Built with HTML,CSS and JavaScript

To-do-list A simple list app that allows a user to add and remove tasks. Built With HTML CSS JS Webpack Live Demo Click To-do-list to see the page. Ge

Apr 8, 2022
Releases(v0.6.1)
Owner
meezwhite
Digital nomad × Full-stack developer × Generative artist 🏝
meezwhite
Hashmat Noorani 4 Mar 21, 2023
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
The /r/place Atlas is a project aiming to catalog all the artworks created during Reddit's 2022 /r/place event.

The 2022 Place Atlas The /r/place Atlas is a project aiming to catalog all the artworks created during Reddit's 2022 /r/place event. This project was

Place Atlas 397 Dec 28, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
This is a Next.js app that powers my Twitch overlays. Good luck and godspeed!

This is a Next.js project bootstrapped with create-next-app. Getting Started First, run the development server: npm run dev # or yarn dev Open http://

Adam Elmore 17 Sep 27, 2022
Unstyled, dev error overlays for SolidJS

solid-error-overlay Unstyled, headless Error Overlay for SolidJS Install npm i solid-error-overlay yarn add solid-error-overlay pnpm add solid-error-o

Alexis H. Munsayac 20 Dec 29, 2022
The JavaScript library let’s you transform native tooltip’s into customizable overlays.

iTooltip The JavaScript library let’s you transform native tooltip’s into customizable overlays. Use: <script src="/path/to/iTooltip.js"></script> <sc

null 2 Dec 17, 2021
This is a dependency-free easy-to-use vanilla JavaScript addon allowing you to create HTML currency inputs with various different currencies and formattings.

intl-currency-input This is a dependency-free easy-to-use vanilla JavaScript addon allowing you to create HTML currency inputs with various different

null 6 Jan 4, 2023
Image addon for xterm.js

xterm-addon-image Image output in xterm.js. ⚠️ This is an experimental addon, that is still under construction. ⚠️ Install from npm (not yet released)

null 28 Dec 11, 2022