使用处理对对象进行 lerpColor
我目前正在致力于创建一种带有处理功能的绘图工具,其中用鼠标指针绘制黑色椭圆,一段时间后完全消失为白色 - 即不再可见。整个事情看起来像是一种手指压力传感器......我已经有了第一种方法,可以找到此处。在第二种方法中(参见下面的代码),我使用了 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码中有一些问题:
Counter
是全局。因此每个笔触共享相同的计数器,这意味着:1-1。所有
椭圆
s的颜色相同1-2。
Counter = Counter + 0.01
正在同一变量上计算,使其迅速淡出。颜色
和tempc
在brush
中有点模棱两可。类变量tempc
被声明但未使用,而您将另一个tempc
作为参数。c
的值brush.display&gt;填充(c)
永远不会更改。一旦将tempc
作为参数,其使用的值就永远不会更改。我在下面解决了这些问题:
There are a few problems in your code:
counter
is global. So every brushstroke shares the same counter, meaning that:1-1. all the
ellipse
s will have the same color,1-2.
counter = counter + 0.01
is being calculated on the same variable, making it fade away super quickly.color
andtempC
are a bit ambiguous inBrush
. The class variabletempC
is declared but unused, while you take anothertempC
as an argument.The value of
c
inBrush.display > fill(c)
never changes. Once you taketempC
as the argument, its value used never changes.I fixed those problems below: