Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


Main Code:

Code Block
languagejava
booleanlet clickFlag    click1 = false;
ArrayList<Animator>var myAnimators = [];
intlet count;

voidfunction setup()
{
  sizecreateCanvas(640, 480);
  smooth();
  stroke(200);
  fill(255);
  myAnimators = new ArrayList<Animator>Animator();

}
 void
function draw()
{
 
  background(51);
  for(intvar i = 0; i<myAnimators.size();i++) {<     myAnimators.get(i).drawAnimator();
  }
  
// count++;
//if (count >= 15length; i++) {
// myAnimators.add(new Animator(floor(random(0,width)), floor(random(0,height)))); // myAnimators.get(myAnimators.size()-1).endClick(floor(random(0,width)), floor(random(0,height)));
// count = 0;
//}

}

void i).drawAnimator();
  }

}
 
function mousePressed()
{
  if (click1clickFlag == false) {
    myAnimators.add( = new Animator(mouseX, mouseY));
    click1clickFlag = true;
  } else {
    myAnimators.get(myAnimators.size()-1).endClick(mouseX, mouseY);
    clickFlag click1 = false;
  }
  printlnprint(click1clickFlag);
}
 void
function mouseMoved() {
  if (click1clickFlag == true) {
   myAnimators.get(myAnimators.size()-1).endPos.setendClick(mouseX, mouseY);
   } }

Class code:

Code Block
languagejava
class
Animator { }
}
PVector

class Animator {
startPos
= new PVectorconstructor(x, y);{
  PVector     endPosthis.drawFlag = new PVector()true;
  int  this.curTime = 0;
    curTimethis.startPos = createVector(x,y,0);
  int  this.endPos= this.startPos;
     this.animSpeed = 5;
  int         this.animDuration = 2000;
  boolean     drawFlag=false;
  boolean     this.animEndFlag = false;


  Animator(int X, int Y) {

    drawFlag = true;
    curTime = 0;
    startPos.set(X, Y, 0);

   endPos = startPos.get();

  }


 void endClick(int X, int Y) {
    this.endPos.set(X, Y, 0);
  }

  void drawAnimator() {
    // calc. the anim time

    if (this.curTime >= this.animDuration) {
      this.animEndFlag = true;
    }

    if (this.curTime <= 0) {
      this.animEndFlag = false;
    }

    if (this.animEndFlag) {
      this.curTime -= this.animSpeed;
    } else {
      this.curTime += this.animSpeed;
    }

    // calc. the proportion of completion in the animation
    floatvar normTime = this.curTime * 1.0 / this.animDuration;

    if (this.drawFlag)
    {

      line(this.startPos.x, this.startPos.y, 
        this.endPos.x, this.endPos.y);

      // calculate the position of the circle on the line
      PVectorlet dir = PVectorp5.Vector.sub(this.endPos, this.startPos);
       PVectorlet pos = PVectorp5.Vector.add( this.startPos, PVectorp5.Veector.mult(dir, normTime));
      ellipse(pos.x, pos.y, 20, 20);
    }
  }
}

...