2011-04-12 13 views

回答

3

您可以不畫它們,既可以使用如(阿爾法/ 透明度)參數爲fill功能

不拉絲:

int numVisible = 0; 
for(int i = 0 ; i < 20 ; i++) { 
    boolean visible = random(1) > .5; 
    if(visible) { 
    rect(random(100),random(100),random(10),random(10)); 
    numVisible++; 
    } 
} 
println(numVisible+" boxes are visible"); 

繪製透明(只有筆畫可見):

for(int i = 0 ; i < 20 ; i++) { 
    boolean visible = random(1) > .5; 
    fill(255,255,255,visible ? 255 : 0); 
    rect(random(100),random(100),random(10),random(10)); 
} 

如果有幫助,這是同樣的一個更長的版本:

void setup(){ 
    size(400,400,P2D); 
    smooth(); 
    noStroke(); 
    background(255); 
    for(int i = 0; i < 200 ; i++){ 
    Rect r = new Rect(random(width),random(height),random(10,20),random(10,20),color(random(255),random(255),random(255),random(1) > .5 ? 255 : 64)); 
    r.draw(); 
    } 
} 
class Rect{ 
    color c; 
    float w,h,x,y; 
    Rect(float x,float y,float w,float h,color c){ 
    this.c = c; 
    this.w = w; 
    this.h = h; 
    this.x = x; 
    this.y = y; 
    } 
    void draw(){ 
    fill(c); 
    rect(x,y,w,h); 
    } 
} 

貝婁是可以運行的代碼段:

function setup(){ 
 
    createCanvas(400,400); 
 
    smooth(); 
 
    noStroke(); 
 
    background(255); 
 
    for(var i = 0; i < 200 ; i++){ 
 
    var r = new Rect(random(width),random(height),random(10,20),random(10,20),color(random(255),random(255),random(255),random(1) > .5 ? 255 : 64)); 
 
    r.draw(); 
 
    } 
 
} 
 
function Rect(x,y,w,h,c){ 
 
    this.c = c; 
 
    this.w = w; 
 
    this.h = h; 
 
    this.x = x; 
 
    this.y = y; 
 
    
 
    this.draw = function(){ 
 
    fill(c); 
 
    rect(x,y,w,h); 
 
    } 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.4/p5.min.js"></script>

preview

1

你可以在繪製函數中放入一個var控制它是否必須顯示你想要的。

void draw() 
{ 
    if (showThis) 
    { 
    image(image); 
    } 
} 
0

添加noStroke();並使顏色與背景顏色相同?

+0

不要猜測(在答案的末尾添加'?') – 2017-10-25 17:59:12