【问题标题】:Processing - Move circle with cursor position处理 - 用光标位置移动圆圈
【发布时间】:2018-03-29 08:30:52
【问题描述】:

我做了一个简单的绘图程序来绘制线条并增加/减少线条的粗细:

float strokeWeight = 2;

void setup() {
  size(640, 360);
  noSmooth();
  fill(126);
  background(255);
  strokeWeight(strokeWeight);
}

void draw() {
  background(0);
  ellipse(mouseX, mouseY, strokeWeight/2, strokeWeight/2);
  background(255);

  if (mousePressed) {
    stroke(0);
    line(mouseX, mouseY, pmouseX, pmouseY);
  }

  if (keyPressed) {
    if (key == '+') {
      strokeWeight = strokeWeight + 0.5;
      }
    if  (key == '-') {
      strokeWeight = strokeWeight - 0.5;
      }
     if (strokeWeight >= 0.5) {
      strokeWeight(strokeWeight);
     }
    }
}

现在我想用光标移动一个圆圈,指示当前线条的粗细。我试过这样的事情:

ellipse(mouseX, mouseY, strokeWeight/2, strokeWeight/2)

但是这种方式会一遍又一遍地绘制椭圆。有没有办法“抹掉”之前做的圈子?

【问题讨论】:

    标签: processing


    【解决方案1】:

    我不是 100% 确定我已经理解了您的问题,但您可能想使用PGrahics,一方面您保留线条,另一方面您绘制圆圈。

    float strokeWeight = 2;
    
    PGraphics canvas;
    PGraphics thickness_circle;
    
    void setup() {
      size(640, 360);
    
      canvas = createGraphics(width, height);
    
      thickness_circle = createGraphics(width, height);
    
      thickness_circle.beginDraw();
      thickness_circle.noFill();
      thickness_circle.strokeWeight(1);
      thickness_circle.stroke(255, 0, 0);
      thickness_circle.endDraw();
    }
    
    void draw() {
      background(255);
    
      if (keyPressed) {
        if (key == '+') {
          strokeWeight += 0.5;
        }
        if  (key == '-') {
          strokeWeight -= 0.5;
        }
        strokeWeight = strokeWeight >= 0.5 ? strokeWeight : 0.5;
      }
    
      if (mousePressed) {    
        canvas.beginDraw();
        canvas.strokeWeight(strokeWeight);
        canvas.line(mouseX, mouseY, pmouseX, pmouseY);
        canvas.endDraw();
      }
    
      image(canvas, 0, 0);
    
      thickness_circle.beginDraw();
      thickness_circle.clear();
      thickness_circle.ellipse(mouseX, mouseY, strokeWeight, strokeWeight);
      thickness_circle.endDraw();
    
      image(thickness_circle, 0, 0);
    }
    

    【讨论】:

    • 我不知道 PGraphics :)。非常感谢。你不必为我完成整个工作。但这完美无缺。
    • 今后请不要只转储完整的代码解决方案。引导 OP 完成解决问题的过程,并指出参考资料的适用部分。
    猜你喜欢
    • 2018-09-04
    • 2012-06-02
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多