You might also want an ArrayList of PVectors to store mouse points for your curveVertex calls.
Here is a simple example of drawing curve points with a mouse. Note that the mouse points are so close together that it almost doesn’t matter that you are using a curve – you would get almost the same curvy effect from connecting them with straight lines.
// CurvePoints -- Processing 3.3.6 -- 2018-12-06
int maxPoints = 500;
ArrayList<PVector> points;
void setup() {
size(400, 400);
points = new ArrayList<PVector>();
noFill();
}
void draw() {
background(192);
// draw curve
beginShape();
if (points.size()>0) {
for (PVector p : points) {
curveVertex(p.x, p.y);
}
}
endShape();
}
void mouseMoved() {
points.add(new PVector(mouseX, mouseY));
// erase from back of line
if (points.size() > maxPoints) {
println(points.size());
points.remove(0);
}
}
Note also that there is a bit of extra code here that erases points from the back of the line. This is to prevent you from drawing so many points that the sketch bogs down and stops – which is even more important if you are adding points in draw()
every frame rather than in mouseMoved()
, as you will quickly have thousands of points at 60 points-per-second.
There are a lot of little things you could add to making curve drawling like this behave in a pleasing or intuitive way – deciding how new points are added (by distance, time, or both), figuring out how the anchors should work, making aspects of the curve adjustable, and so on. Here is a more complex implementation of the same approach along with a bit of code that flashes the points on and off.
Summary
ArrayList<PVector> points;
int maxPoints = 50;
int markDistance = 50;
void setup() {
size(400, 400);
points = new ArrayList<PVector>();
noFill();
}
void draw() {
background(192);
// draw curve
beginShape();
if (points.size()>0) {
// begin anchor
curveVertex(points.get(0).x, points.get(0).y);
// curve points
for (PVector p : points) {
curveVertex(p.x, p.y);
// turn on and off point markers
if (millis()/2000%2==0) {
ellipse(p.x, p.y, 3, 3);
}
}
// end anchor
curveVertex(points.get(points.size()-1).x, points.get(points.size()-1).y);
}
endShape();
}
void mouseMoved() {
// add point when new is distant from old
if (points.size()<2 || points.get(points.size()-2).dist(new PVector(mouseX, mouseY)) > markDistance) {
points.add(new PVector(mouseX, mouseY));
// erase from back of line
if (points.size() > maxPoints) {
println(points.size());
points.remove(0);
}
} else {
// nudge -- update last point if too close to previous
points.set(points.size()-1, new PVector(mouseX, mouseY));
}
}
void keyPressed(){
// delete previous points
if(points.size()>2 && key==DELETE || key==BACKSPACE){
points.remove(points.size()-2);
}
}
You may also be interested in this tutorial series, particularly the parts beginning at “Limited Trail”
http://www.science.smith.edu/dftwiki/index.php/Creating_a_trail_of_moving_object_in_Processing#Limited_Trail