Ellipse following mouse

I am new to processing and wanted to create a simple program where a circle follows the mouse.

This is the code that I have,

void setup () {
  size(400, 400, P3D);
  background(0);
}

void draw() {
  pushMatrix();
  fill(255);
  ellipse(mouseX, mouseY, 40, 40);
  popMatrix();
}

But the result is this,
image

Even though this looks cool, this is not what I wanted!
A new ellipse created every time I move the mouse, instead of the same one following it!

How to do this?

What’s actually happening is you are not removing any of the previously drawn circles from previous frames.

You need to paint over them!

In setup(), you call background(0). This draws black over the ENTIRE sketch. This is why your sketch has a black background.

If you move (or copy) that call to background() into draw(), then you will paint black over any previous circles… and then you can draw just one circle on top of that fresh, clean slate of black.

You don’t need to call pushMatrix() or popMatrix() at all.

void setup () {
  size(400, 400, P3D);
}

void draw() {
  background(0);
  fill(255);
  ellipse(mouseX, mouseY, 40, 40);
}
3 Likes