使用处理对对象进行 lerpColor

发布于 2025-01-20 14:22:12 字数 1224 浏览 4 评论 0原文

我目前正在致力于创建一种带有处理功能的绘图工具,其中用鼠标指针绘制黑色椭圆,一段时间后完全消失为白色 - 即不再可见。整个事情看起来像是一种手指压力传感器......我已经有了第一种方法,可以找到此处。在第二种方法中(参见下面的代码),我使用了 ArrayList 来表示椭圆,并与 PVector 结合使用 - 为了避免所有椭圆同时消失,我认为有必要将单个椭圆视为一个对象,其中褪色(使用 lerpColor)被包含在内。我仍然无法让它工作,鼠标指针处的“最重要的”椭圆始终保持黑色,而更远的椭圆会逐渐消失......所以,每个椭圆都会单独消失......我是什么丢失的?或者我可能扭曲了代码中的某些内容?为此我需要对象吗?

float counter;

ArrayList<PVector> positionsList;

Brush myBrush;

void setup() {
  size(600, 600);
  positionsList = new ArrayList<PVector>();

}


void draw() {
  background(255);
    for (PVector p : positionsList) {
  color from = color(0);
  color to = color(255);
  color faded = lerpColor(from, to, counter);
  myBrush = new Brush(faded, p.x, p.y); 
  myBrush.display();
  
}

     positionsList.add(new PVector(mouseX, mouseY));
    }

class Brush {
  color tempC;
  float xpos;
  float ypos;
  color c;

  Brush(color tempC, float tempXpos, float tempYpos) {
    c = tempC;
    xpos = tempXpos;
    ypos = tempYpos;
  }
  void display() {
    noStroke();
    fill(c);
    ellipse(xpos, ypos, 50, 50);
    counter = counter + 0.01;
  }
}

I am currently working on creating a kind of drawing tool with processing, in which black ellipses are drawn with the mouse pointer, which after some time fade away completely to white - i.e. are no longer visible. The whole thing could look like a kind of finger pressure sensor... I already had a first approach which can be found here. In the second approach (see code below) I used an ArrayList for the ellipses, in connection with PVector - to avoid that all ellipses fade away at the same time, I thought it was necessary to consider the single ellipse as an object, in which the fading away (with lerpColor) is contained. I still can't get it to work, that "the foremost" ellipse at the mouse pointer always stays black, and further back the ellipses fade away... So, that just every single ellipse fades away separately... What am I missing? Or have I perhaps twisted something in the code? Do I need objects for this?

float counter;

ArrayList<PVector> positionsList;

Brush myBrush;

void setup() {
  size(600, 600);
  positionsList = new ArrayList<PVector>();

}


void draw() {
  background(255);
    for (PVector p : positionsList) {
  color from = color(0);
  color to = color(255);
  color faded = lerpColor(from, to, counter);
  myBrush = new Brush(faded, p.x, p.y); 
  myBrush.display();
  
}

     positionsList.add(new PVector(mouseX, mouseY));
    }

class Brush {
  color tempC;
  float xpos;
  float ypos;
  color c;

  Brush(color tempC, float tempXpos, float tempYpos) {
    c = tempC;
    xpos = tempXpos;
    ypos = tempYpos;
  }
  void display() {
    noStroke();
    fill(c);
    ellipse(xpos, ypos, 50, 50);
    counter = counter + 0.01;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甲如呢乙后呢 2025-01-27 14:22:12

您的代码中有一些问题:

  1. Counter是全局。因此每个笔触共享相同的计数器,这意味着:

1-1。所有椭圆 s的颜色相同

1-2。 Counter = Counter + 0.01正在同一变量上计算,使其迅速淡出。

  1. 颜色tempcbrush中有点模棱两可。类变量tempc被声明但未使用,而您将另一个tempc作为参数。

  2. c的值brush.display&gt;填充(c)永远不会更改。一旦将tempc作为参数,其使用的值就永远不会更改。

我在下面解决了这些问题:

ArrayList<Brush> brushList;

void setup() {
    size(600, 600);
    brushList = new ArrayList<Brush>();
    
}


void draw() {
    background(255);
    
    float counter = 0;
    
    for (Brush brush : brushList) {
        brush.display();
    }
    
    brushList.add(new Brush(mouseX, mouseY));
}


class Brush {
    float xpos;
    float ypos;
    float counter;
    
    Brush(float tempXpos, float tempYpos) {
        xpos = tempXpos;
        ypos = tempYpos;
        counter = 0;
    }
    void display() {
        noStroke();
        fill(lerpColor(color(0), color(255), counter));
        ellipse(xpos, ypos, 50, 50);
        counter = counter + 0.01;
    }
}

There are a few problems in your code:

  1. counter is global. So every brushstroke shares the same counter, meaning that:

1-1. all the ellipses will have the same color,

1-2. counter = counter + 0.01 is being calculated on the same variable, making it fade away super quickly.

  1. color and tempC are a bit ambiguous in Brush. The class variable tempC is declared but unused, while you take another tempC as an argument.

  2. The value of c in Brush.display > fill(c) never changes. Once you take tempC as the argument, its value used never changes.

I fixed those problems below:

ArrayList<Brush> brushList;

void setup() {
    size(600, 600);
    brushList = new ArrayList<Brush>();
    
}


void draw() {
    background(255);
    
    float counter = 0;
    
    for (Brush brush : brushList) {
        brush.display();
    }
    
    brushList.add(new Brush(mouseX, mouseY));
}


class Brush {
    float xpos;
    float ypos;
    float counter;
    
    Brush(float tempXpos, float tempYpos) {
        xpos = tempXpos;
        ypos = tempYpos;
        counter = 0;
    }
    void display() {
        noStroke();
        fill(lerpColor(color(0), color(255), counter));
        ellipse(xpos, ypos, 50, 50);
        counter = counter + 0.01;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文