该画笔当快速划过的时候,画笔的线条变粗,慢慢划过的时候,线条变细。
主要经过下面的speed实现,x是mouseX,y是mouseY,px是pmouseX,py是pmouseY, 也就是和当前这一帧鼠标的位置以及前一帧鼠标的位置。滑动地越快,两点距离越大,因此strokeWeight
也就越大。spa
float speed = abs(x-px) + abs(y-py); strokeWeight(speed);
全部代码:code
int count=0; int increment=1; void setup() { size(640, 360); background(255); fill (255,255,255); smooth(); } void draw() { drawLine(mouseX, mouseY, pmouseX, pmouseY); } void drawLine(int x, int y, int px, int py) { if (mousePressed==true) { count=count+increment; } if (count>25){ increment = -1; } if (count<=0){ increment = 1; } float speed = abs(x-px) + abs(y-py); strokeWeight(speed); line(x, y, px, py); stroke(count*3,count*8,count*10); } void keyPressed(){ if (key == ' '){ background(255); } }
另外,当按住鼠标的时候,有个count
变量一直在变化,影响到stroke
,也就是画笔颜色,当松开鼠标的时候,颜色就不会变化。rem
最后还添加了若是按下空格键,就清屏的代码:it
void keyPressed(){ if (key == ' '){ background(255); }