开发者

How to make objects invisible in Processing

I am making rectangles on the sketch board using processing, but I want to make them invisible. H开发者_StackOverflow社区ow do I do that? I'd appreciate your help.

Amrita


You can either not draw them, either use the forth(alpha/transparency) parameter for the fill function

not drawing:

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");

drawing transparent(only strokes 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));
}

If it helps, here's a longer version of the same:

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);
  }
}

Bellow is a snippet you can run:

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>

How to make objects invisible in Processing


You could put in the draw function a var to control if it must show what you want.

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


Add noStroke(); and make the color the same as the background color?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜