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。运行时大家可以多动动鼠标,会发现有趣的变化。

相关推荐
庸俗今天不摸鱼1 天前
Canvas进阶-4、边界检测(流光,鼠标拖尾)
开发语言·前端·javascript·计算机外设
bin91531 天前
DeepSeek 助力 Vue 开发:打造丝滑的 键盘快捷键(Keyboard Shortcuts)
前端·javascript·vue.js·计算机外设·ecmascript·deepseek
我有一棵树2 天前
overflow-x: auto 使用鼠标实现横向滚动,区分触摸板和鼠标滚动事件的方法
计算机外设
神仙别闹2 天前
基于MFC实现的键盘电子乐器演奏程序
c++·计算机外设·mfc
weixin_438150994 天前
新品发布:即插即用,8寸Type-C接口电脑副屏显示器发布!
计算机外设·电脑
ShyTan4 天前
罗技鼠标接收器丢了,怎么用另一个logi接收器重新配对?
计算机外设
陈老老老板7 天前
让编程变成一种享受-明基RD320U显示器
计算机外设
有点傻的小可爱9 天前
【matlab】大小键盘对应的Kbname
计算机外设
不想上班只想要钱9 天前
ECharts鼠标悬浮提示框数字设置鼠标在左侧时 tooltip 显示到右侧,鼠标在右侧时 tooltip 显示到左侧。
javascript·计算机外设·echarts
驱动小百科9 天前
电脑显示器无信号是什么原因?查看解决方法
计算机外设·电脑·电脑显示器无信号·电脑显示器显示无信号·电脑显示器无信号黑屏