//trailing effect //by RJ Layton //description of how it works: /* The approach I took was to make a "trail" of ellipses behind the main ellipse that is drawn where the mouse is currently pointing. To do this, I made two arrays that store X and Y values of the current location as well as the PREVIOUS locations of the mouse. Visually, how this looks is like this: X: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] alpha: 255 215 185 165 125 095 075 055 025 005 The "main" ellipse that we're drawing is located at position [0]. The ones behind it are drawn after the main ellipse. The numbers for alpha aren't accurate, but they give the impression of what we're doing: We're lowering the alpha for each ellipse that is drawn behind the first one. So all we do is move the positions that we stored in the arrays down one position to give the impression of a trail. */ //variables int maxTrail = 10; //the maximum amount of ellipses we want to have int[] posX = new int[maxTrail]; //the array of X positions we want to store int[] posY = new int[maxTrail]; //the array of Y positions we want to store int eSize = 50; //the width and height of the ellipse int screenWidth = 500; //the width of the screen resolution int screenHeight = 500; //the height of the screen resolution //set up the essentials void setup() { size(screenWidth,screenHeight); //let's make the screen a bit bigger so we can tell what's going on background(0,0,0); //set the background to black noStroke(); //I didn't want a line around the ellipses, so I put this in smooth(); //the smoothing function //here we initialize the arrays by filling them with "0" values. this is //to avoid any issues we could run into by calling an array location that //has nothing (or garbage) stored in it on accident for(int i = 0; i < maxTrail; i++) { posX[i] = 0; posY[i] = 0; } } void draw() { //refresh the screen to be black (0) background(255); //to get the blur effect, I went with starting the alpha value very low (10) //this will be the value for the last ellipse in the trail. //alpha goes from 0-255, with 0 being transparent and 255 being opaque int currentAlpha = 255; //go through the array in reverse and store the numbers moveNumbers(); //based on where we are on the screen, we want to set a different "amount" of //motion blur. at the bottom we'll have more, at the top we'll have less. //this variable stores that based on our location. int drawLength = 1; //once we check to make sure the mouse is in range, we'll get a drawLength //based on its position if(mouseY > 0 && mouseY < screenHeight) { drawLength = mouseY/(screenHeight/maxTrail); } //let's make sure that drawLength isn't more than our max, or we'll get an error //also, check to make sure it's not zero, because you can't divide by zero! if(drawLength > maxTrail) { drawLength = maxTrail; } if(drawLength < 1) { drawLength = 1; } //to get a smooth alpha transition, i'm going to make a "alpha step" variable //that tells me how much to increase the alpha value for each ellipse in the chain int alphaStep = (currentAlpha)/drawLength; //run through the loop and draw the ellipses, in reverse //the reason we draw them in reverse is because what is drawn last is drawn //"on top," and we want that ellipse to be the "head" ellipse! for(int i = drawLength-1; i>-1; i--) { fill(100,100,255,currentAlpha); //set the alpha, with the color being light blue ellipse(posX[i],posY[i],eSize,eSize); //draw the ellipse at the set size currentAlpha += alphaStep; } println(drawLength); } void moveNumbers() { //here we are going to go in reverse through the arrays and store the value from the //location AHEAD of it in the trail. This will achieve a "trail" effect. //try drawing out the locations in boxes on paper to run it through your head for(int i = maxTrail-1; i > 0; i--) { posX[i] = posX[i-1]; posY[i] = posY[i-1]; } //the head location in the trail should be based on where the mouse is posX[0] = mouseX; posY[0] = mouseY; }