Recitation 6: Processing Animation
Recitation 6: Processing Animation
What I have made in recitation 5 is difficult to be animated, so I decided to make something new. When it comes to animation, I think of light and stars shining in the dark sky. So I decided to draw some rotating stars controlled by the keyboard and a light beam moving as the mouse.
Video:
Code:
color a =0; float b=0; float c=0; void setup(){ size(600,600); background(30); } void draw(){ background(30); //star fill(255, 204, 0); pushMatrix(); translate(width*0.2, height*0.5); if (keyPressed == true){ rotate(frameCount / -100.0); } star(30,30,15,35,5); star(-200,-300,15,35,5); star(300,50,15,35,5); star(250,200,30,70,5); star(-300,-350,15,35,5); star(-50,-250,15,35,5); popMatrix(); strokeWeight(8); stroke(random(100,255),random(0,255),random(0,255)); line(pmouseX, pmouseY, 300, 300); b=random(0,100); c=random(0,100); fill(random(100,255),random(0,255),random(0,255)); noStroke(); ellipse(pmouseX,pmouseY,b,c); } //star function void star(float x, float y, float radius1, float radius2, int npoints) { float angle = TWO_PI / npoints; float halfAngle = angle/2.0; beginShape(); for (float a = 0; a < TWO_PI; a += angle) { float sx = x + cos(a) * radius2; float sy = y + sin(a) * radius2; vertex(sx, sy); sx = x + cos(a+halfAngle) * radius1; sy = y + sin(a+halfAngle) * radius1; vertex(sx, sy); } endShape(CLOSE); }
Interesting functions:
-
star()
I find it hard to draw a star with simple and basic shape functions in Processing. So, I searched for a sample code on the references of Processing and fortunately found one. I used it to create several yellow stars.
-
rotate()
This function enables those yellow stars to rotate around a certain point, which makes the whole animation looks like a starry night.
-
keyPressed()
The keyPressed() function is called once every time a key is pressed. It gives users the experience of interaction, which makes the animation funnier.