想要关闭淡出效果,我们要用到keyPressed()这个方法,它会在每次键盘有按键动作时被调用。如果用户按了**f** 键,系统就切换一次fade的真假值。
void keyPressed(){
if (key == 'f') { fade = !fade; }
}
运行程序后你就能看到之前的线条都慢慢淡出背景,试一下f键关闭或启用淡出效果。 源码:
折叠Java 代码复制内容到剪贴板
1. float radius = 40; 2. float x,y; 3. float prevX,prevY; 4. Boolean fade = true; 5. Boolean saveOne = false; 6. void setup(){ 7. size(450,400); 8. background(0); 9. stroke(255); 10. x = width/2; 11. y = height/2; 12. prevX = x; 13. prevY = y; 14. stroke(255); 15. strokeWeight(2); 16. point(x,y); 17. 18. }
19. void draw(){ 20.
21. if(fade){ 22. noStroke(); 23. fill(0,4);
24. // fill(random(204),random(100),random(20),4); 25. rect(0,0,width,height); 26. }
27. float angle = (TWO_PI/6) * floor(random(6)); 28. x += cos(angle) * radius; 29. y += sin(angle) * radius; 30.
31. if(x<0||x>width){ 32. x= prevX; 33. y= prevY; 34. } 35.
36. if(y<0||y>height){ 37. x = prevX; 38. y = prevY; 39. }
40. // stroke(255,64); 41. stroke(255,0,0,100); 42. strokeWeight(1); 43. line(x,y,prevX,prevY); 44. strokeWeight(3); 45. point(x,y); 46. prevX = x; 47. prevY = y; 48. if(saveOne){
49. saveFrame(\+ second() + \ 50. saveOne = false; 51. }
52. delay(50); 53. }
54. void keyPressed(){ 55. if(key=='f'){
56. fade =!fade;
57. } 58.
59. if(key=='s'){ 60. saveOne = true; 61. } 62. }
效果显示
floor(x)函数:计算最接近的小于或等于X的整数值 Namefloor() Examples
float x = 2.88;
int a = floor(x); // Sets 'a' to 2
DescriptionCalculates the closest int value that is less than or equal to the value of the parameter. Syntax floor(n)
Parameters n float: number to round down Returns int
ceil(x)函数:计算最接近的大于或等于X的整数值 Name ceil() Examples
float x = 8.22;
int a = floor(x); // Sets 'a' to 9
DescriptionCalculates the closest int value that is greater than or equal to the value of the parameter. For example, ceil(9.03)returns the value 10. Syntax floor(n)
详细的processing学习笔记



