Atlassian uses cookies to improve your browsing experience, perform analytics and research, and conduct advertising. Accept all cookies to indicate that you agree to our use of cookies on your device. Atlassian cookies and tracking notice, (opens new window)
Confluence
/
Animation-2 Answer

Animation-2 Answer

Oct 31, 2017
PVector     startPos = new PVector();
PVector     endPos = new PVector();
int         curTime = 0;
int         animSpeed = 5;
int         animDuration = 2000;
boolean     drawFlag=false;
boolean     animEndFlag = false;
boolean     click1 = false;

void setup()
{
  size(640, 480);
  smooth();
}

void draw()
{
  background(51);

  // calc. the anim time

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

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

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

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

  if (drawFlag)
  {

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

    // calculate the position of the circle on the line
    PVector dir = PVector.sub(endPos, startPos);

    PVector pos = PVector.add( startPos, PVector.mult(dir, normTime));
    ellipse(pos.x, pos.y, 20, 20);
  }
}

void mousePressed()
{
  if (click1 == false) {
    stroke(255, 0, 0);
    fill(255,0,0);
    drawFlag = true;
    curTime = 0;
    startPos.set(mouseX, mouseY, 0);
    endPos = startPos.get();
    click1 = true;
  } else {
    stroke(255);
    fill(255);
    endPos.set(mouseX, mouseY, 0);
    click1 = false;
  }
  println(click1);
}

void mouseMoved() {
  if (click1 == true) {
    endPos.set(mouseX, mouseY, 0);
  }
}

, multiple selections available,
For you

Programming
Results will update as you type.
  • Tiling and Repetition
  • Reactors
  • Programming Basics: Parametric and Generative Graphic Design 2016
  • Archive
  • High Scores
  • Artificial Neural Network
  • Alternatives to the Processing IDE
  • p5.js Programming
  • Programming in Processing (java)
    • Starting with Processing (en)
    • Variables (en)
    • Classes and Objects (en)
    • Events und Functions (en)
    • Writing our own Functions (en)
    • Random Numbers (en)
    • Conditionals (en)
    • Loops (en)
    • Nested Loops (en)
    • Coordinates (en)
    • Arrays and Lists (en)
    • SVG + Images (en)
    • Motion and Temporality
    • Gesture Interactions
    • Using bitmaps as modifiers
    • Vectors
    • Animation
    • Animation 2
      • Animation Solution with objects
      • Animation-2 Answer
    • Simple Collision Detection
    • Sound
    • Typography

{"serverDuration": 12, "requestCorrelationId": "6b9642376d0648318e5b7706a71d328b"}