# 随机的笔触粗细在draw()函数中始终较大 – Processing

go评论57阅读模式

Random stroke weight is always bigger in draw() - Processing

# 问题

``````void setup() {
size(600, 800);
frameRate(7);
background(137, 156, 199);
} // Acaba void setup

void draw() {
// 随机线条粗细的三角形
noFill();
stroke(225, 225, 225);
strokeWeight(random(20)); // 粗细不正确地变化
triangle(580, 780, 580, 750, 500, 780);
}
``````

I'm trying to paint a triangle with a random stroke weight.The problem is that the stroke weight only changes if the new value is bigger than the older value. Here's what I've tried. I don't know if I am supposed to redraw or refresh the display at the bottom of the draw void.

``````void setup() {
size(600, 800);
frameRate(7);
background(137, 156, 199);
} // Acaba void setup

void draw() {
// RANDOM WEIGHT TRIANGLE
noFill();
stroke(225, 225, 225);
strokeWeight(random(20)); // DOESN&#39;T CHANGE PROPERLY
triangle(580, 780, 580, 750, 500, 780);
}
``````

# 答案1

The image isn't magically disappearing at every loop of the `draw()` method, that's why you don't see when the triangle's line weight is lower than before. You would need to repaint everything for it to be obsious.

Which is nice, because it's really easy to do. Move this line `background(137, 156, 199);` in the `draw()` method as the first thing that you do. After this the drawing should behave as you expect:

Have fun!

# 答案2

`background`不仅仅设置背景颜色，它实际上还会清除背景。您必须在每一帧中清除背景（在`draw`中）：

``````void draw(){

background(137, 156, 199);

// [...]

}
``````

`background` does not only set the background color, it actually clears the background. You have to clear the background in every frame (in `draw`):

``````void draw(){

background(137, 156, 199);

// [...]

}

</details>

``````

• 本文由 发表于 2020年10月6日 22:57:57
• 转载请务必保留本文链接：https://go.coder-hub.com/64228430.html
• java
• processing

go 70

go 65

go 59

go 51