We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have been watching a lot of Dan's videos on Processing and p5.js, and I have recently been working on a kind of doodle sketch program where you can draw things and then save them as images in Processing. I have most of it done already, but there is a feature that I would like to add but I'm not sure how to. Basically what I would like to do is have a "brush" object follow the mouse so that you can see things such as the color of the brush or the size of the brush before actually drawing anything, because as of right now you don't really have any other way to know than to draw something. I have limited knowledge of js, and even more limited knowledge of Processing, or any other OOP language, such as Java itself, so I don't have a clue as to how to actually make said "brush" object.
This is the code:
color rgb;
int R, G, B = 0;
int SW = 1;
int count = 1;
void setup() {
size(1000, 1000);
background(255);
strokeWeight(SW);
}
void keyPressed() {
//Backspace "clears" canvas
if (keyCode == 8) {
noStroke();
fill(255);
rect(0, 0, width, height);
}
if (keyCode == 38) {SW += 1; strokeWeight(SW);} //Increases stroke weight by 1
else if (keyCode == 40 && SW > 1) {SW -= 1; strokeWeight(SW);} //Decreases stroke weight by 1 if > 1
//Color control
if (keyCode == 155 && R < 255) {R+=1; rgb = color(R,G,B);} //Increases red value if < 255
else if (keyCode == 127 && R > 0) {R-=1; rgb = color(R,G,B);} //Decreases red value if > 0
if (keyCode == 36 && G < 255) {G+=1; rgb = color(R,G,B);} //Increases green value if < 255
else if (keyCode == 35 && G > 0) {G-=1; rgb = color(R,G,B);} //Decreases green value if > 0
if (keyCode == 33 && B < 255) {B+=1; rgb = color(R,G,B);} //Increases blue value if < 255
else if (keyCode == 34 && B > 0) {B-=1; rgb = color(R,G,B);} //Decreases blue value if > 0
//Color "presets"
if (keyCode == 48){R = 255; G = 255; B = 255; rgb = color(R,G,B);} //White
if (keyCode == 49){R = 0; G = 0; B = 0; rgb = color(R,G,B);} //Black
if (keyCode == 50){R = 255; G = 0; B = 0; rgb = color(R,G,B);} //Red
if (keyCode == 51){R = 0; G = 255; B = 0; rgb = color(R,G,B);} //Green
if (keyCode == 52){R = 0; G = 0; B = 255; rgb = color(R,G,B);} //Blue
if (keyCode == 53){R = 255; G = 127; B = 0; rgb = color(R,G,B);} //Orange
if (keyCode == 54){R = 255; G = 255; B = 0; rgb = color(R,G,B);} //Yellow
if (keyCode == 55){R = 0; G = 255; B = 255; rgb = color(R,G,B);} //Cyan
if (keyCode == 56){R = 255; G = 0; B = 255; rgb = color(R,G,B);} //Magenta
if (keyCode == 57){R = 127; G = 0; B = 255; rgb = color(R,G,B);} //Purple
//End color control
//Save with "S"
if (keyCode == 83){
save("Sketch_"+count+".jpg");
count++;
}
println("rgb("+R+","+G+","+B+")");
}
void draw() {
stroke(rgb);
//Draws a line with mouse
if (mousePressed == true) {
line(pmouseX, pmouseY, mouseX, mouseY);
}
}
Thanks in advance.
Answers
Yikes! Your code is scary looking.
Just draw the tip of your brush, simple.
Use
point(mouseX, mouseY);
.And use a buffer ArrayList to keep track of points.
This is a common problem in a lot of programs -- you want some elements that draw trails (the drawing) and some that do not (the brush). I would suggest investigation
createGraphics()
-- you can have the drawn elements there while "showing" the brush only on the main canvas. The separate offscreen graphics object then becomes the background. You'll also need to add some mouse interaction probably (draw only when clicked etc.) I realize this is confusing, it's a great topic for a video tutorial actually.I will certainly look in to that when I get a chance. If I'm understanding what you're saying correctly, then it will be exactly what I'm thinking of. If you make a video about it that would be really interesting and helpful. Thanks!
@the_metal_bug --
Here are two ways of approaching your problem.
PGraphics
As Dan Shiffman pointed out, in general you may want to draw things that persist (the drawing layer) and that are updated each frame (the icon / marker). You can adapt the official PGraphics example sketch from the reference and using a simple line from the continuous lines example:
Like this:
cursor()
Specifically, you may simply to want to change the mouse pointer into a special "brush" icon. There is already a function for this in Processing:
cursor()
(which can take use a pre-set icon or can take an image as an argument). Here is the continuous lines example again, this time with a cursor call added.Like this:
You can also combine the two methods -- draw the brush using any Processing drawing techniques into a
PGraphics pgcursor
, then load it into the mouse cursor withcursor(pgcursor)
.Of course. Should have thought of that.
Oh, I didn't realise it was the Shiffman who answered this question, as in the same guy who wrote Nature of Code. Cool.