【processing】小代码3

时间:2021-11-02 04:13:55

鼠标响应: mouseX, mouseY 鼠标的坐标

----------------------------------------------

void setup()
{
size(,);
smooth();
background();
} void draw()
{
fill(,,);
rectMode(CENTER);
rect(mouseX,mouseY,,);
}

随着鼠标的移动画方块

【processing】小代码3

-----------------------------------------------------

dist(x1,y1,x2,y2) 计算两点之间的距离

void setup()
{
size(,);
smooth();
} void draw()
{
background();
stroke();
float d = dist(mouseX, mouseY,,);
strokeWeight(d/);
fill();
ellipse(,,d*,d);
strokeWeight(d/);
fill(,,);
ellipse(,,d,d);
fill();
noStroke();
ellipse(,,d/,d/);
}

【processing】小代码3

眼睛的大小会随着鼠标位置改变

------------------------------------------------------------------

pmouseX,pmouseY: 鼠标的上一个坐标点

void setup()
{
size(,);
strokeWeight();
smooth();
stroke(,,,);
background();
}
void draw()
{
line(mouseX,mouseY,pmouseX,pmouseY);
}

【processing】小代码3

画出鼠标的轨迹

---------------------------------------------------------

鼠标响应事件

void mousePressed(){} 等价于 void draw(){if(mousePressed){}}

void mouseMoved(){}

void mouseDragged(){}

void setup()
{
size(,);
smooth();
background(,,,);
}
void draw()
{
if(mousePressed)
{
stroke();
fill(,,);
ellipse(mouseX,mouseY,,);
}
}

【processing】小代码3

【processing】小代码3

鼠标按下时画圆,一直按着不放就一直画圆 不要边框后更好看些

-------------------------------------------------------------------------------------------

void setup()
{
size(,);
smooth();
background(,,,);
}
void draw()
{
}
void mouseMoved()
{
stroke(,,,);
rectMode(CENTER);
fill(,,,);
rect(mouseX,mouseY,,);
}
void mouseDragged()
{
stroke(,,,);
fill(,,,);
ellipse(mouseX,mouseY,,);
}

【processing】小代码3

鼠标移动画方,鼠标拖动画圆

-------------------------------------------------------------

mouseButton  含 LEFT, RIGHT, CENTER

void setup()
{
size(,);
smooth();
background();
rectMode(CENTER);
} void draw()
{
if(mousePressed)
{
if(mouseButton == LEFT)
{
fill();
}
else if(mouseButton==CENTER)
{
fill();
}
else
{
fill();
}
rect(mouseX,mouseY,,);
}
}

【processing】小代码3

按下鼠标不同键颜色不同

--------------------------------------------------------

键盘响应:

keyPressed 按键响应 void keyPressed(){} 或 void draw(){if(keyPressed){}}

key 特定键响应 在keyPressed基础上加 if(key == 'a')

keyCode 功能键响应 包括 ALT, CONTROL, SHIFT,UP,DOWN,LEFT,RIGHT

int x = ;
int y = ;
void setup()
{
size(,);
smooth();
background();
rectMode(CENTER);
}
void draw()
{
if(keyPressed && (key == CODED))
{
if(keyCode == LEFT)
{
x--;
}
else if(keyCode == RIGHT)
{
x++;
}
else if(keyCode == UP)
{
y--;
}
else if(keyCode == DOWN)
{
y++;
}
}
ellipse(x,y,,);
}

【processing】小代码3

---------------------------------------------------------------

随机:random(10) 生成0-10的任意浮点数

int(random(6,10)), 生成6 7 8 9中任意一个数

void setup()
{
size(,);
smooth();
background();
stroke(,);
colorMode(HSB,,,);
}
void draw()
{
float d = random();
fill(random(),,,);
ellipse(random(),random(),d,d);
}

【processing】小代码3

-------------------------------------------------------

限制 constrain(value,min,max); 限制value值在min-max范围内

int x = ;
int y = ;
void setup()
{
size(,);
smooth();
}
void draw()
{
background();
fill();
rect(,,,);
fill();
rect(,,,);
float mx = constrain(mouseX,,);
float my = constrain(mouseY,,);
fill();
ellipse(mx,my,,);
}

【processing】小代码3