Mouse Recording

Create a sketch in which you'll be recording the movements of the mouse in the canvas for 2 seconds, and then playing that recording back, forwards and backwards, forever.

You'll need 3 built-in mouse-related reference items:

mousePressed()  // this function will be called whenever you pressed the left mouse button -- you'll need to create this function to use it just once to start the recording process.

mouseX, mouseY are built-in global variables that tell you where the mouse is.

Here's the start of the code and a video of the sketch in action.


int[] xs = new int[120];
int[] ys = new int[120];
int recorded = 0;

int state = 0; // 0 = doing nothing yet, 1 = recording, 2 = displaying

int radius_recording = 4;
int radius_displaying = 10;
boolean display_forward = false;

void setup() {
  size(600,400);
  background(200);
}

void mousePressed() {
  if (state == 0) state = 1;
}

void draw() {
  if (state == 0) return;

// your excellent code here