Skip to content

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

Translate fixed

Alejandro9b625b4628fa
diff --git a/examples.js b/examples.js
index c91374d..b86cddb 100644
--- a/examples.js
+++ b/examples.js
@@ -44,16 +44,16 @@ brush.config({
// YOU CAN CREATE YOU OWN BRUSHES
brush.newBrush("watercolor", {
type: "image", // this is the TIP TYPE: choose standard / spray / marker / custom / image
- weight: 17, // Base weight of the brush tip
+ weight: 20, // Base weight of the brush tip
vibration: 2, // Vibration of the lines, spread
definition: 0.5, // Between 0 and 1
quality: 8, // + quality = more continuous line
- opacity: 10, // Base opacity of the brush (this will be affected by pressure)
+ opacity: 8, // Base opacity of the brush (this will be affected by pressure)
spacing: 1, // 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: {
type: "custom", // "standard" or "custom". Use "custom"" for custom pressure curves. Use standard for simple gauss bell curve
- //curve: [0.15,0.2], // If "standard", pick a and b values for the gauss curve.
+ //curve: [0.15,0.2], // If "standard", pick a and b values for the gauss curve. a is max horizontal mvt of the bell, b changes the slope
curve: function (x) {return 1-x}, // If "custom", define the curve function with a curve equation from x = 0 to x = 1, returning values from 0 to 1
min_max: [0.5,1.2] // For both cases, define min and max pressure (reverse for inverted presure)
},
@@ -66,7 +66,7 @@ brush.newBrush("watercolor", {
src: "./brush_tips/brush.jpg",
},
// For "custom" and "image" types, you can define the tip angle rotation here.
- rotate: "natural", // "none" disables rotation | "natural" follows the direction of the stroke | "random"
+ rotate: "random", // "none" disables rotation | "natural" follows the direction of the stroke | "random"
})
@@ -88,20 +88,23 @@ function setup () {
// USE BRUSHBOX LIBRARY
// Load BrushBox: load(BUFFER) -> Leave empty if you want to draw to the main p5 canvas
brush.load();
- brush.translate(-width/2,-height/2)
+ translate(-width/2,-height/2)
+
+ // STANDARD PALETTE TEST
+ /*
let i = 0
for (let b of brush.box()) {
- brush.set(b,random(palette),1)
- brush.line(30,30+i*10,200,30+i*10)
+ brush.set(b,random(palette),1)
+ brush.line(30,30+i*10,220,30+i*10)
i++
}
+ */
/*
// Set your brush name, color, and weight. There are 9 standard brushes to select from.
// Select colors with HEX codes, arrays of [r,g,b] or names
brush.set("marker","black",1)
- // Translate(x,y) - here I'm translating, so to draw like a 2D canvas
// Draw a line (x1,y1,x2,y2)
brush.line(20,20,200,20)
@@ -153,21 +156,24 @@ function setup () {
brush.circle(100,100,50)
brush.color("#fcd300")
brush.circle(102,100,50)
+
+ */
+
// 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.flowField("seabed")
- for (let j = 0; j < 4; j++) {
+ brush.pick("marker2")
+ brush.flowField("waves")
+ for (let j = 0; j < 5; j++) {
let plot = new brush.Plot("curve")
for (let i = 0; i < Math.floor(random(40,90)); i++) {
- plot.addSegment(0,1+i*4,random(1.2))
- plot.addSegment(90,2+i*4,random(1.2))
- plot.addSegment(180,3+i*4,random(1.2))
- plot.addSegment(270,4+i*4,random(1.2))
+ 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))
+ plot.addSegment(270,4+i*4,random(0.6,1.5))
}
plot.endPlot(0,1)
plot.rotate(Math.floor(random(0,180)))
- brush.color(random(palette))
- //brush.flowShape(plot,width*random(0.3,0.7),height*random(0.1,0.9),random(0.5,1))
+ brush.color(palette[j % (palette.length)])
+ brush.flowShape(plot,width*random(0.3,0.7),height*random(0.1,0.9),random(0.5,1))
}
brush.disableField()
@@ -180,9 +186,7 @@ function setup () {
// brush.spline(array_points, curvature) curvature is a value 0-1. Use 0 for a truncated line
// The curve will go from the first point to the last one, using the rest as control points
//brush.spline(points,1)
-
- brush.flowField("seabed")
- */
+
}
function draw() {
diff --git a/p5.brush.js b/p5.brush.js
index feb3f14..8f6cd61 100644
--- a/p5.brush.js
+++ b/p5.brush.js
@@ -67,9 +67,10 @@
R.c[i] = Math.cos(radians)
R.s[i] = Math.sin(radians)
}
- let trans = [0,0];
- function translate(x,y) {
- trans = [x,y]
+ let matrix = [0,0];
+ const trans = function () {
+ matrix = [_r._renderer.uMVMatrix.mat4[12],_r._renderer.uMVMatrix.mat4[13]]
+ return matrix;
}
//////////////////////////////////////////////////
@@ -89,7 +90,7 @@
blend (_c) {
this.pigment = [float(red(color(_c))),float(green(color(_c))),float(blue(color(_c)))];
_r.push();
- _r.translate(-_r.width/2,-_r.height/2)
+ _r.translate(-trans()[0],-trans()[1])
// Load shader and send data from canvas
_r.shader(this.shader);
this.shader.setUniform('addColor', this.pigment);
@@ -98,7 +99,7 @@
// Give geometry to shader
_r.fill(0,0,0,0);
_r.noStroke();
- _r.rect(0, 0, _r.width, _r.height);
+ _r.rect(-_r.width/2, -_r.height/2, _r.width, _r.height);
// Clear Mask to prepare for next colour
this.mask.draw(function () {_r.clear()})
_r.pop();
@@ -272,12 +273,12 @@
}
}
class Position {
- constructor (x,y) {this.update(x + trans[0],y + trans[1]), this.plotted = 0;}
+ constructor (x,y) {this.update(x, y), this.plotted = 0;}
update (x,y) {
this.x = x , this.y = y;
if (FF.isActive) {
- this.x_offset = this.x - FF.left_x;
- this.y_offset = this.y - FF.top_y;
+ this.x_offset = this.x - FF.left_x + trans()[0];
+ this.y_offset = this.y - FF.top_y + trans()[1];
this.column_index = Math.round(this.x_offset / FF.R);
this.row_index = Math.round(this.y_offset / FF.R);
}
@@ -287,7 +288,7 @@
return (FF.isActive) ? ((this.column_index >= 0 && this.row_index >= 0) && (this.column_index < FF.num_columns && this.row_index < FF.num_rows)) : this.isInCanvas();
}
isInCanvas() {
- return (this.x >= -0.55 * _r.width && this.x <= 0.55 * _r.width) && (this.y >= -0.55 * _r.height && this.y <= 0.55 * _r.height)
+ return (this.x >= -0.55 * _r.width - trans()[0] && this.x <= 0.55 * _r.width - trans()[0]) && (this.y >= -0.55 * _r.height - trans()[1] && this.y <= 0.55 * _r.height - trans()[1])
}
angle () {
if (this.isIn() && FF.isActive) {
@@ -377,13 +378,13 @@
},
push() {
B.p = B.list.get(B.name).param
- if (B.p.pressure.type !== "custom") {B.a = R.random(-1,1), B.b = R.random(1,1.5), B.cp = R.random(3,4.5);}
+ if (B.p.pressure.type !== "custom") {B.a = R.random(-1,1), B.b = R.random(1,1.5), B.cp = R.random(3,3.5);}
else {B.cp = R.random(-0.2,0.2)}
B.min = B.p.pressure.min_max[0], B.max = B.p.pressure.min_max[1];
if (S.on) B.pickBuffer(), B.buffer.beginShape();
- if (B.p.blend) Mix.mask.begin();
+ if (B.p.blend) trans(), Mix.mask.begin();
_r.push(), B.c = _r.color(B.c), _r.noStroke();
- if (B.p.blend) B.markerTip();
+ if (B.p.blend) _r.translate(matrix[0],matrix[1]), B.markerTip();
},
tip() {
if (S.on) B.buffer.vertex(B.position.x,B.position.y) // SVG
@@ -394,7 +395,7 @@
if (B.p.type === "spray") { // SPRAY TYPE BRUSHES
let vibration = (B.w * B.p.vibration * pressure) + B.w * randomGaussian() * B.p.vibration / 3;
let sw = B.p.weight * R.random(0.9,1.1);
- for (let j = 0; j < B.p.quality; j++) {
+ for (let j = 0; j < B.p.quality / pressure; j++) {
let r = R.random(0.9,1.1);
let rX = r * vibration * R.random(-1,1);
_r.circle(B.position.x + rX, B.position.y + R.random(-1, 1) * Math.sqrt(Math.pow(r * vibration,2) - Math.pow(rX,2)), sw);
@@ -597,14 +598,14 @@
//////////////////////////////////////////////////
// BASIC GEOMETRIES
- function rect(x,y,w,h,mode = false) {
+ function rectangulo(x,y,w,h,mode = false) {
if (mode) x = x - w / 2, y = y - h / 2;
B.line(x,y,x+w,y)
B.line(x+w,y,x+w,y+h)
B.line(x+w,y+h,x,y+h)
B.line(x,y+h,x,y)
}
- function circle(x,y,radius,field = false) {
+ function circulo(x,y,radius,field = false) {
let f = FF.isActive
FF.isActive = field;
let p = new Plot("curve")
@@ -631,19 +632,24 @@
let a1 = calcAngle(p1[0],p1[1],p2[0],p2[1]), a2 = calcAngle(p2[0],p2[1],p3[0],p3[1]);
let dd = Math.min(curvature * Math.min(d1,d2),0.5 * Math.min(d1,d2)), dmax = Math.max(d1,d2)
let s1 = d1 - dd, s2 = d2 - dd;
- let point1 = {x: p2[0] - dd * R.cos(-a1), y: p2[1] - dd * R.sin(-a1)}
- let point2 = {x: point1.x + dmax * R.cos(-a1+90), y: point1.y + dmax * R.sin(-a1+90)}
- let point3 = {x: p2[0] + dd * R.cos(-a2), y: p2[1] + dd * R.sin(-a2)}
- let point4 = {x: point3.x + dmax * R.cos(-a2+90), y: point3.y + dmax * R.sin(-a2+90)}
- let int = intersectar(point1,point2,point3,point4,true)
- let radius = dist(point1.x,point1.y,int.x,int.y)
- let disti = dist(point1.x,point1.y,point3.x,point3.y)/2
- let a3 = 2*asin(disti/radius)
- let s3 = 2 * Math.PI * radius * a3 / 360;
- p.addSegment(a1,s1-done, p1[2],true)
- p.addSegment(a1,s3, p1[2],true)
- p.addSegment(a2,i === array_points.length - 3 ? s2 : 0, p2[2],true)
- done = dd;
+ if (Math.floor(a1) === Math.floor(a2)) {
+ p.addSegment(a1,s1,p1[2],true)
+ p.addSegment(a2,d2,p2[2],true)
+ } else {
+ let point1 = {x: p2[0] - dd * R.cos(-a1), y: p2[1] - dd * R.sin(-a1)}
+ let point2 = {x: point1.x + dmax * R.cos(-a1+90), y: point1.y + dmax * R.sin(-a1+90)}
+ let point3 = {x: p2[0] + dd * R.cos(-a2), y: p2[1] + dd * R.sin(-a2)}
+ let point4 = {x: point3.x + dmax * R.cos(-a2+90), y: point3.y + dmax * R.sin(-a2+90)}
+ let int = intersectar(point1,point2,point3,point4,true)
+ let radius = dist(point1.x,point1.y,int.x,int.y)
+ let disti = dist(point1.x,point1.y,point3.x,point3.y)/2
+ let a3 = 2*asin(disti/radius)
+ let s3 = 2 * Math.PI * radius * a3 / 360;
+ p.addSegment(a1,s1-done, p1[2],true)
+ p.addSegment(a1,s3, p1[2],true)
+ p.addSegment(a2,i === array_points.length - 3 ? s2 : 0, p2[2],true)
+ done = dd;
+ }
if (i == array_points.length - 3) {
p.endPlot(a2,p2[2],true)
}
@@ -734,9 +740,12 @@
["2H", { weight: 0.2, vibration: 0.4, definition: 0.3, quality: 2, opacity: 150, spacing: 0.2, pressure: {curve: [0.15,0.2], min_max: [1.2,0.9]} }],
["cpencil", { weight: 0.4, vibration: 0.6, definition: 0.8, quality: 7, opacity: 120, spacing: 0.15, pressure: {curve: [0.15,0.2], min_max: [0.95,1.15]} }],
["charcoal", { weight: 0.45, vibration: 2, definition: 0.7, quality: 300, opacity: 110, spacing: 0.07, pressure: {curve: [0.15,0.2], min_max: [1.3,0.95]} }],
- ["hatch_brush", { weight: 0.2, vibration: 0.4, definition: 0.3, quality: 2, opacity: 150, spacing: 0.15, pressure: {curve: [0.93,0.7], min_max: [1,1.5]} }],
- ["spray", { type: "spray", weight: 0.3, vibration: 12, definition: 15, quality: 30, opacity: 120, spacing: 0.75, pressure: {curve: [0.3,0.15], min_max: [0.35,1.2]} }],
- ["marker", { type: "marker", weight: 2.5, vibration: 0.08, opacity: 17, spacing: 0.4, pressure: {curve: [0.15,0.2], min_max: [1.6,1]} }],
+ ["hatch_brush", { weight: 0.2, vibration: 0.4, definition: 0.3, quality: 2, opacity: 150, spacing: 0.15, pressure: {curve: [0.5,0.7], min_max: [1,1.5]} }],
+ ["spray", { type: "spray", weight: 0.3, vibration: 12, definition: 15, quality: 40, opacity: 120, spacing: 0.65, pressure: {curve: [0,0.1], min_max: [0.15,1.2]} }],
+ ["marker", { type: "marker", weight: 2.5, vibration: 0.08, opacity: 17, spacing: 0.4, pressure: {curve: [0.35,0.25], min_max: [1.35,1]} }],
+ ["marker2", { type: "custom", weight: 2.5, vibration: 0.08, opacity: 7, spacing: 0.6, pressure: {curve: [0.35,0.25], min_max: [1.35,1]},
+ tip: function () { _r.rect(-1.5,-1.5,3,3); _r.rect(1,1,1,1) }, rotate: "natural"
+ }],
];
for (let s of standard_brushes) {
B.add(s[0],s[1])
@@ -756,7 +765,6 @@
exports.preload = preload;
exports.flowField = flowField;
exports.disableField = disableField;
- exports.translate = translate;
exports.globalScale = globalScale;
// Export BRUSH functions
exports.newBrush = B.add;
@@ -770,8 +778,8 @@
exports.flowLine = B.flowLine;
exports.flowShape = B.flowShape;
exports.hatch = B.hatch;
- exports.rect = rect;
- exports.circle = circle;
+ exports.rect = rectangulo;
+ exports.circle = circulo;
exports.spline = spline;
// Classes
exports.Plot = Plot;