如何在处理中使对象不可见

问题描述 投票:0回答:3

我正在使用处理在素描板上制作矩形,但我想让它们不可见。我该怎么做?

processing
3个回答
3
投票

您可以不绘制它们,或者使用fill函数的forth(alpha/transparency)参数

不画画:

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();并使颜色与背景颜色相同?

© www.soinside.com 2019 - 2024. All rights reserved.