Processing圆圈随鼠标运动

一.案例代码如下:

import processing.pdf.*;

import java.util.Calendar;

boolean savePDF = false;

float tileCount =20;

color circleColor = color(0);

int circleAlpha = 180;

int actRandomSeed =0;

void setup(){

size(600,600);

}

void draw(){

if(savePDF)beginRecord(PDF,timestamp()+".pdf");

translate(width/tileCount/2,height/tileCount/2);

background(255);

smooth();

noFill();

randomSeed(actRandomSeed);

stroke(circleColor,circleAlpha);

strokeWeight(mouseY/60);

for(int gridY=0;gridY<tileCount;gridY++){

for(int gridX=0;gridX<tileCount;gridX++){

float posX=width/tileCount*gridX;

float posY =height/tileCount*gridY;

float shiftX=random(-mouseX,mouseX)/20;

float shiftY=random(-mouseX,mouseX)/20;

ellipse(posX+shiftX,posY+shiftY,mouseY/15,mouseY/15);

}

}

if(savePDF)

{savePDF=false;

endRecord();

}

}

void mousePressed(){

actRandomSeed=(int) random(100000);

}

void keyReleased(){

if(key=='s'||key=='S') saveFrame(timestamp()+"_##.png");

if(key=='p'||key=='P') savePDF=true;

}

String timestamp(){

Calendar now=Calendar.getInstance();

return String.format("%1ty%1tm%1td_%1tH%1tM%1tS",now);

}

保存并运行如图1

图1

注意:案例中按下大写P或者小写p保存PDF,如果按下大写S或者小写s可以保存PNG。运行时大家可以多动动鼠标,会发现有趣的变化。

相关推荐
sensor_WU2 天前
【Delphi】实现在多显示器时指定程序运行在某个显示器上
计算机外设·多显示器
碎碎思2 天前
打破延迟极限的 FPGA 机械键盘
fpga开发·计算机外设
Despacito0o4 天前
瀚文机械键盘固件开发详解:HWKeyboard.cpp文件解析与应用
数据库·mongodb·计算机外设
步达硬件4 天前
【原理解析】为什么显示器Fliker dB值越大,闪烁程度越轻?
计算机外设·显示器
感叹号的豆浆4 天前
使用qt 定义全局钩子 捕获系统的键盘事件
qt·计算机外设
Despacito0o4 天前
瀚文(HelloWord)智能键盘项目深度剖析:从0到1的全流程解读
计算机外设
开开心心就好4 天前
高效视频倍速播放插件推荐
python·学习·游戏·pdf·计算机外设·电脑·音视频
Despacito0o6 天前
APM32主控键盘全功能开发实战教程:软件部分
驱动开发·计算机外设
Java&Develop7 天前
Windows如何定制键盘按键
计算机外设
LAM LAB8 天前
【键盘说明书备份】ENERGYFORT
计算机外设