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 Solution with objects
Published Nov 01, 2017

Animation Solution with objects

Nov 01, 2017



Main Code:

boolean click1 = false; ArrayList<Animator> myAnimators; int count; void setup() { size(640, 480); smooth(); stroke(200); fill(255); myAnimators = new ArrayList<Animator>(); } void draw() { background(51); for(int i = 0; i<myAnimators.size();i++) { myAnimators.get(i).drawAnimator(); } // count++; //if (count >= 15) { // 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 mousePressed() { if (click1 == false) { myAnimators.add(new Animator(mouseX, mouseY)); click1 = true; } else { myAnimators.get(myAnimators.size()-1).endClick(mouseX, mouseY); click1 = false; } println(click1); } void mouseMoved() { if (click1 == true) { myAnimators.get(myAnimators.size()-1).endPos.set(mouseX, mouseY); } }



Class code:

class Animator { PVector startPos = new PVector(); PVector endPos = new PVector(); int curTime = 0; int animSpeed = 5; int animDuration = 2000; boolean drawFlag=false; boolean 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) { endPos.set(X, Y, 0); } void drawAnimator() { // 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); } } }

 





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": 16, "requestCorrelationId": "4a3d152183e34825b9f1cba9775de231"}