首页 > 技术文章 > 交互:用到鼠标的实时距离来定义渐变色

xxfcz 2020-10-26 21:44 原文

程序运行效果:

Processing代码:

// gradiant varied by distance to mouse

int w = 5; // width of each rectangle
int y; 

void setup(){
  size(640, 480);
  y = height/2;
  colorMode(RGB, width);
  rectMode(CENTER);
  noStroke();
}

void draw(){
  for(int x = w/2; x<width-w/2; x+=w){
    int dx = abs(mouseX-x);  // distance to mouse
    fill(dx);
    rect(x, y, w, height);
  }
}

 

推荐阅读