Skip to content

Unlock custom brushes, natural fill effects and intuitive hatching in p5.js

Bugs fixed

Alejandro027045d4baaf
diff --git a/p5.brush.js b/p5.brush.js
index 0f5c9dd..9b1ccf7 100644
--- a/p5.brush.js
+++ b/p5.brush.js
@@ -391,6 +391,7 @@
this.mask = createGraphics(_r.width,_r.height)
this.mask.pixelDensity(_r.pixelDensity())
this.mask.clear()
+ this.mask.angleMode(DEGREES)
exports.mask = this.mask;
// Load the spectral.js shader code only once
@@ -405,6 +406,13 @@
this.noBlend = createGraphics(_r.width,_r.height);
this.noBlend.pixelDensity(_r.pixelDensity())
this.noBlend.noSmooth()
+ this.noBlend.angleMode(DEGREES)
+
+ // WEBGL buffer for img brushes (image() is much quicker like this)
+ this.mask2 = createGraphics(_r.width,_r.height, WEBGL)
+ this.mask2.pixelDensity(_r.pixelDensity())
+ this.mask2.clear()
+ this.mask2.angleMode(DEGREES)
},
/**
@@ -422,17 +430,39 @@
return colorArray;
},
+ color1: new Float32Array(3),
+ color2: new Float32Array(3),
+ blending1: false,
+ blending2: false,
+
/**
* Applies the blend shader to the current rendering context.
* @param {string} _c - The color used for blending, as a p5.Color object.
* @param {boolean} _isLast - Indicates if this is the last blend after setup and draw.
+ * @param {boolean} _isLast - Indicates if this is the last blend after setup and draw.
*/
- blend (_color = false, _isLast = false) {
+ blend (_color = false, _isLast = false, webgl_mask = false) {
+
+ // Select between the two options:
+ this.isBlending = webgl_mask ? this.blending1 : this.blending2;
+ this.currentColor = webgl_mask ? this.color1 : this.color2;
+
// Check if blending is initialised
if(!this.isBlending) {
// If color has been provided, we initialise blending
- if (_color) this.currentColor = this.getPigment(_color), this.isBlending = true;
- else return
+ if (_color) {
+ this.currentColor = this.getPigment(_color);
+ if (webgl_mask) this.blending1 = true, this.color1 = this.currentColor;
+ else this.blending2 = true, this.color2 = this.currentColor;
+ }
+ else {
+ _r.push();
+ _r.translate(-_trans()[0],-_trans()[1])
+ _r.image(this.noBlend,-_r.width/2, -_r.height/2)
+ this.noBlend.clear()
+ _r.pop();
+ return;
+ }
}
// Checks if newColor is the same than the cadhedColor
@@ -448,30 +478,31 @@
this.noBlend.clear()
// Use the blend shader for rendering
_r.shader(this.shader);
-
// Set shader uniforms: color to add, source texture, and mask texture
this.shader.setUniform('addColor', this.currentColor);
this.shader.setUniform('source', _r._renderer);
- this.shader.setUniform('mask', this.mask);
+ let mask = webgl_mask ? this.mask2: this.mask;
+ this.shader.setUniform('mask', mask);
// Draw a rectangle covering the whole canvas to apply the shader
_r.fill(0,0,0,0);
_r.noStroke();
_r.rect(-_r.width/2, -_r.height/2, _r.width, _r.height);
_r.pop();
// Clear the mask after drawing
- this.mask.clear()
+ mask.clear()
// We cache the new color here
- if (!_isLast) this.currentColor = this.getPigment(_color)
+ if (!_isLast) {
+ this.currentColor = this.getPigment(_color);
+ if (webgl_mask) this.color1 = this.currentColor;
+ else this.color2 = this.currentColor;
+ }
}
- if (this.isBlending == false && _isLast) {
- _r.push();
- _r.translate(-_trans()[0],-_trans()[1])
- _r.image(this.noBlend,-_r.width/2, -_r.height/2)
- _r.pop();
+ if (_isLast) {
+ this.isBlending = false;
+ if (webgl_mask) this.blending1 = this.isBlending
+ else this.blending2 = this.isBlending
}
-
- if (_isLast) this.isBlending = false;
},
// Vertex shader source code
@@ -487,7 +518,9 @@
* Register methods after setup() and post draw() for belding last buffered color
*/
p5.prototype.registerMethod('afterSetup', () => Mix.blend(false, true));
+ p5.prototype.registerMethod('afterSetup', () => Mix.blend(false, true, true));
p5.prototype.registerMethod('post', () => Mix.blend(false, true));
+ p5.prototype.registerMethod('post', () => Mix.blend(false, true, true));
// =============================================================================
// Section: FlowField
@@ -1066,14 +1099,15 @@
// Blend Mode
this.c = _r.color(this.c);
// Select mask buffer for blend mode
- this.mask = this.p.blend ? Mix.mask : Mix.noBlend;
+ this.mask = this.p.blend ? ((this.p.type === "image") ? Mix.mask2 : Mix.mask) : Mix.noBlend;
+ _trans()
// Set the blender
this.mask.push();
this.mask.noStroke();
- _trans()
- this.mask.translate(_matrix[0] + _r.width/2,_matrix[1] + _r.height/2);
+ (this.p.type === "image") ? this.mask.translate(_matrix[0],_matrix[1]) : this.mask.translate(_matrix[0] + _r.width/2,_matrix[1] + _r.height/2);
if (this.p.blend) {
- Mix.blend(this.c);
+ if (this.p.type !== "image") Mix.blend(this.c);
+ else Mix.blend(this.c,false,true)
if (!isTip) this.markerTip()
}
},
@@ -1219,7 +1253,7 @@
let vibration = vibrate ? this.w * this.p.vibration : 0;
let rx = vibrate ? vibration * R.random(-1,1) : 0;
let ry = vibrate ? vibration * R.random(-1,1) : 0;
- this.mask.translate(this.position.x + rx, this.position.y + ry),
+ this.mask.translate(this.position.x + rx, this.position.y + ry);
this.adjustSizeAndRotation(this.w * pressure, alpha)
this.p.tip();
this.mask.pop();
@@ -1245,8 +1279,10 @@
this.mask.scale(pressure);
if (this.p.type === "image") (this.p.blend) ? this.mask.tint(255, 0, 0, alpha / 2) : this.mask.tint(this.mask.red(this.c), this.mask.green(this.c), this.mask.blue(this.c), alpha);
if (this.p.rotate === "random") this.mask.rotate(R.randInt(0,360));
- if (this.p.rotate === "natural") {
- this.mask.rotate(((this.plot) ? - this.plot.angle(this.position.plotted) : - this.dir) + (this.flow ? this.position.angle() : 0))
+ else if (this.p.rotate === "natural") {
+ let angle = ((this.plot) ? - this.plot.angle(this.position.plotted) : - this.dir) + (this.flow ? this.position.angle() : 0)
+ angle = (this.plot) ? - this.plot.angle(this.position.plotted) : - this.dir
+ this.mask.rotate(angle)
}
},
@@ -1371,7 +1407,7 @@
*/
let _isHatching = false;
- let _hatchingParams;
+ let _hatchingParams = [5,45,{}];
let _hatchingBrush = false;
/**
@@ -2106,7 +2142,7 @@
isActive: false,
isAnimated: false,
b: 0.07,
- t: 1,
+ t: 0,
/**
* Fills the given polygon with a watercolor effect.
* @param {Object} polygon - The polygon to fill.
@@ -2220,7 +2256,7 @@
fill (color, intensity, tex) {
// Precalculate stuff
- const numLayers = 18;
+ const numLayers = 20;
const intensityThird = intensity / 3;
const intensityQuarter = intensity / 4;
const intensityHalf = intensity / 2;
@@ -2239,7 +2275,7 @@
let pol4 = this.grow(0.6)
for (let i = 0; i < numLayers; i ++) {
- if (i === Math.floor(numLayers / 3) || i === Math.floor(2 * numLayers / 3)) {
+ if (i === Math.floor(numLayers / 4) || i === Math.floor(2 * numLayers / 4) || i === Math.floor(3 * numLayers / 4)) {
// Grow the polygon objects once per third of the process
pol = pol.grow();
pol2 = pol2.grow(0.1);
@@ -2287,7 +2323,7 @@
const halfSize = this.size / 2;
const minSizeFactor = 0.025 * this.size;
const maxSizeFactor = 0.19 * this.size;
- Mix.mask.erase(5 * texture);
+ Mix.mask.erase(4 * texture);
for (let i = 0; i < numCircles; i++) {
const x = this.midP.x + randomGaussian(0, halfSize);
const y = this.midP.y + randomGaussian(0, halfSize);
diff --git a/sketch.js b/sketch.js
index 4a6155f..bdae1bc 100644
--- a/sketch.js
+++ b/sketch.js
@@ -37,7 +37,7 @@ brush.add("watercolor", {
vibration: 2, // Vibration of the lines, spread
definition: 0.5, // Between 0 and 1
quality: 8, // + quality = more continuous line
- opacity: 30, // Base opacity of the brush (this will be affected by pressure)
+ opacity: 20, // Base opacity of the brush (this will be affected by pressure)
spacing: 1.5, // Spacing between the points that compose the brush stroke
blend: true, // Activate / Disable realistic color mixing. By default, this is active for marker-custom-image brushes
pressure: {
@@ -79,13 +79,13 @@ function setup () {
// STANDARD PALETTE TEST
+ /*
let i = 0
for (let b of brush.box()) {
brush.set(b,random(palette),1)
brush.flowLine(30,30+i*10,200,0)
i++
}
-
// Set your brush name, color, and weight. There are 9 standard brushes to select from.
@@ -123,8 +123,10 @@ function setup () {
// You can also atch these polygons .hath(distance_between_lines, angle_of_lines, 0-1.0 for % precission)
//polygon.hatch(2,45,0.05)
// You can also hatch arrays of polygons brush.hatch(ARRAY WITH POLS, distance_between_lines, angle_of_lines, 0-1.0 for % precission)
- brush.stroke("#080f15")
-
+
+*/
+
+
let pol_array = []
for (let i = 0; i < 5; i++) {
pol_array.push(new brush.Polygon([
@@ -134,18 +136,32 @@ function setup () {
[random(width),random(height)]
]));
}
- brush.hatch(pol_array,3,0)
+
+ brush.stroke(150)
+ for (let p of pol_array) {
+ p.draw()
+ }
+
+ brush.set("marker2",random(palette),1)
+ brush.hatch(1,30,{gradient: 0.4, random: false, continuous: true})
+ brush.hatchArray(pol_array)
// You can draw rectangles (x,y,width,height,mode) - mode is optional, you can set it to "center"
- brush.set("charcoal","#6b9404",1)
+ brush.set("marker2",random(palette),1)
+ brush.hatch(1,130,{gradient: 0.3, random: false, continuous: true})
+ brush.hatchArray(pol_array)
//brush.rect(100,100,100,100,"center")
// You can draw ricles (x,y,radius)
+
+
+ /*
+
// You can create curves/plots (here I'm creating 4 spirals). This offers full control of the brush pressure, though it's a tad more complicated
- brush.pick("watercolor")
- brush.field("seabed")
- for (let j = 0; j < 5; j++) {
+ brush.pick("marker2")
+ brush.field("curved")
+ for (let j = 0; j < 1; j++) {
let plot = new brush.Plot("curve")
- for (let i = 0; i < Math.floor(random(40,90)); i++) {
+ for (let i = 0; i < Math.floor(random(50,110)); i++) {
plot.addSegment(0,1+i*4,random(0.6,1.5))
plot.addSegment(90,2+i*4,random(0.6,1.5))
plot.addSegment(180,3+i*4,random(0.6,1.5))
@@ -154,9 +170,11 @@ function setup () {
plot.endPlot(0,1)
plot.rotate(Math.floor(random(0,180)))
brush.stroke(palette[j % (palette.length)])
- //brush.flowShape(plot,width*random(0.3,0.7),height*random(0.1,0.9),random(0.5,1))
+ brush.plot(plot,width*random(0.3,0.7),height*random(0.1,0.9),random(0.5,1))
}
+
+
brush.noField()
brush.pick("2B")
// A different way of creating curves, by an array of points [x, y, brush pressure at that point]
@@ -197,21 +215,23 @@ function setup () {
brush.noField()
brush.noStroke()
+ brush.bleed(0.3)
+ brush.fill("#002185",120)
+ brush.circle(150,100,50)
brush.fill("#fcd300",100)
brush.bleed(0.25)
brush.circle(100,100,50)
- brush.fill("#002185",120)
- brush.circle(150,100,50)
+*/
-
+ /*
brush.set("2B","#9c2128",1)
- let num_cols = 5
- let num_rows = 3
+ let num_cols = 1
+ let num_rows = 1
let col_size = (width - 40) / num_cols
let row_size = (height - 40) / num_rows
//brush.noStroke()
@@ -228,8 +248,8 @@ function setup () {
brush.rect(20 + col_size * j, 20 + row_size * i, col_size, row_size)
}
}
-
*/
+