【问题标题】:Making runs of a color when using mouse to draw in Processing使用鼠标在处理中绘制时运行颜色
【发布时间】:2011-12-28 02:07:59
【问题描述】:

我正在尝试在处理中创建代码,以允许在使用鼠标绘图时运行某种颜色。现在颜色是由一个随机函数选择的,该函数在 1 和 0 之间随机选择一个浮点数,然后根据该浮点数是高于还是低于 0.5 来选择颜色。我要运行一种颜色的方法是在下面有一个 for 循环,在该循环中选择随机浮点数,然后在其中有颜色函数,但是如果我把这个 for 循环放在 void draw() 中它不起作用,因为这是以帧速率调用的。如果我将它放在 void mouseDragged() 中,则会出现同样的问题,即每次拖动鼠标时它只调用一次。

基本上,我试图找出一种方法来随机选择两种颜色中的一种,然后为一定数量的帧绘制该颜色(当鼠标被拖动时),然后在该数量的帧之后随机选择一种再次使用两种颜色并重复。

这是我现在绘制的代码(每帧随机确定颜色):

void setup() {
  size(1000, 1000);
  background(000);
}

void draw() {  
}

void mouseDragged () {
  //assigns random float between 0 and 1 which is used for deciding which color to paint
  float x = random(0, 1);

  //assigns random stroke value
  stroke(random(100, 200));

  if (x <= .5) {
    fill(20, 255, 255);
    ellipse(mouseX, mouseY, mouseX/4, mouseY/4);
  }

  if (x > .5) {
    fill(random(220, 250), random(20, 50), random(220, 250));
    ellipse(mouseX, mouseY, mouseX/4, mouseY/4);
  }
}

感谢您的帮助。

【问题讨论】:

    标签: random colors drawing processing


    【解决方案1】:

    尝试使用用于切换“状态”的布尔变量(使用第一种颜色或第二种颜色)。如果你想根据帧数改变交换颜色,可以使用frameCount

    color a,b;
    boolean useFirst = true;
    
    void setup(){
      size(1000,1000);
      background(0);
      resetColors();
    }
    void resetColors(){
      a = color(20, 255, 255);
      b = color(random(220, 250), random(20, 50), random(220, 250));
    }
    void draw(){
      if(frameCount % 20 == 0) useFirst = !useFirst;//toggle colors every 20 frames
    }
    void mouseDragged () {
      if(useFirst) fill(a);
      else         fill(b);
      ellipse(mouseX, mouseY, mouseX/4, mouseY/4);
    }
    void keyPressed(){
      resetColors();
    }
    

    【讨论】:

    • 非常感谢;这正是我想要的。我是处理新手,所以我不知道 frameCount,但现在我知道了! (糟糕。)
    • 很高兴为您提供帮助!此外,如果您需要类似的功能但基于时间而不是帧,请查看millis() 函数。
    猜你喜欢
    • 2018-04-07
    • 1970-01-01
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    • 2020-09-19
    相关资源
    最近更新 更多