frosting animation

I want to do a simple animation - cracks/frosts on mousePressed(), then the cracks increase in size and spread all over the screen, //to be done with line/ellipse cracks to look like this: Screen Shot 2018-02-26 at 4.18.57 PM

Answers

  • edited February 2018
    class Crack {
      float a, b, c, d;
      Crack(float ia, float ib, float ic, float id) {
        a=ia;
        b=ib;
        c=ic;
        d=id;
      }
      void draw() {
        line(a, b, c, d);
      }
    }
    
    ArrayList<Crack> cracks = new ArrayList();
    
    void setup() {
      size(600, 600);
    }
    
    void draw() {
      background(0);
      noFill();
      stroke(255);
      strokeWeight(2);
      rect(10, 10, 580, 580);
      for (Crack crack : cracks) crack.draw();
    }
    
    void mousePressed() {
      for (int i = 0; i < 4; i++) {
        float r = random(TWO_PI);
        float d = random(20, 500);
        cracks.add( new Crack(mouseX, mouseY, mouseX+d*cos(r), mouseY+d*sin(r)));
      }
    }
    

    I had to write this code from scratch because YOU POSTED NO CODE.

    I am unable to help you more because YOU HAVEN'T TOLD US WHAT YOU ARE HAVING TROUBLE WITH.

  • edited February 2018

    New to the community; apologies.

    Have achieved this much with a simpler code:

             if (mousePressed){
                      line(mouseX, mouseY, random(100,300), random(300,500));
                    }
    

    Want to increase the size of each line that is drawn.

  • What do you mean by a line's "size"? Its thickness? Its length?

    class Crack {
      float a, b, c, d;
      Crack(float ia, float ib, float ic, float id) {
        a=ia;
        b=ib;
        c=ic;
        d=id;
      }
      void draw() {
        line(a, b, c, d);
      }
    }
    
    int sw = 1;
    
    ArrayList<Crack> cracks = new ArrayList();
    
    void setup() {
      size(600, 600);
    }
    
    void draw() {
      background(0);
      noFill();
      stroke(255);
      strokeWeight(2);
      rect(10, 10, 580, 580);
      strokeWeight(sw);
      for (Crack crack : cracks) crack.draw();
    }
    
    void mousePressed() {
      for (int i = 0; i < 4; i++) {
        float r = random(TWO_PI);
        float d = random(20, 500);
        cracks.add( new Crack(mouseX, mouseY, mouseX+d*cos(r), mouseY+d*sin(r)));
      }
      sw++;
    }
    
  • Answer ✓

    Have achieved this much with a simpler code:

    Want to increase the size of each line that is drawn.

    then you haven't achieved this with that code

    if you use random the way you have then every frame will draw a different line. it won't grow, it'll just be random.

    you have to generate some random lines in setup, and draw a bit more of them in every draw loop.

  • edited February 2018 Answer ✓
    class Crack {
      float x, y, r, d;
      Crack(float ix, float iy, float ir, float id) {
        x=ix;
        y=iy;
        r=ir;
        d=id;
      }
      void draw() {
        line(x, y, x+d*cos(r), y+d*sin(r));
      }
      void longer() {
        d+=random(2, 5);
      }
    }
    
    ArrayList<Crack> cracks = new ArrayList();
    
    void setup() {
      size(600, 600);
    }
    
    void draw() {
      background(0);
      noFill();
      stroke(255);
      strokeWeight(2);
      rect(10, 10, 580, 580);
      for (Crack crack : cracks) crack.draw();
    }
    
    void mousePressed() {
      for (Crack crack : cracks) { 
        crack.longer();
      }
      for (int i = 0; i < 4; i++) {
        cracks.add(new Crack(mouseX, mouseY, random(TWO_PI), 5));
      }
    }
    
  • Thank you so much!

  • edited February 2018

    here you can just hold the mouse in one place, mouse button down to let it grow automatically

    or release button and press anew

    or drag....

    and you learned nothing from it but just got code from us.

    Chrisir

    boolean hold=false; 
    ArrayList<Crack> cracks = new ArrayList();
    int currentCracks=0; // how many of the last added cracks are growing  
    
    void setup() {
      //size(1200, 800);
      fullScreen();
      background(0);
    }
    
    void draw() {
      //  background(0);
    
      // frame 
      noFill();
      stroke(255);
      strokeWeight(2);
      rect(10, 10, width-20, height-20);
    
      for (Crack crack : cracks) { 
        crack.draw();
      }
    
      if (hold)
        growLast();
    }
    
    // ----------------------------------------------------------------------
    
    void mousePressed() {
      hold=true;
      init(mouseX, mouseY);
      currentCracks=0; // reset
    }
    
    void mouseDragged() {
      if (hold) {
        init(mouseX, mouseY);
        println("here");
      }
    }
    
    void mouseReleased() {
      hold=false;
      currentCracks=0; // reset
    }
    
    // ----------------------------------------------------------------------
    
    void keyPressed() {
      background(0);
      cracks.clear();
    }
    
    // ----------------------------------------------------------------------
    
    void init(float x, float y) {
      // currentCracks+=4;
      currentCracks=6;
      for (int i = 0; i < 4; i++) {
        cracks.add(new Crack(x, y, random(TWO_PI), random(3, 5)));
      }//for
    }
    
    void growLast() {
      //for (Crack crack : cracks) {
      //  crack.longer();
      //}
    
      //Crack crack = cracks.get(cracks.size()-1);
      //crack.longer();
    
      for (int i = cracks.size()-1; i > max(0, cracks.size() - currentCracks); i--) {
        /// cracks.add(new Crack(mouseX, mouseY, random(TWO_PI), 5));
        Crack crack = cracks.get(i);
        crack.longer();
      }
    }
    
    //  =====================================================
    
    class Crack {
      float x, y, 
        angle, 
        radius;
      color col;
    
      Crack(float ix, float iy, 
        float i_angle, 
        float i_radius) {
        x=ix;
        y=iy;
        angle  = i_angle;
        radius = i_radius;
        if (random(100)>50)
          col=color(random(210, 256), random(55, 99)); // white-ish 
        else col=color(random(22), random(33), random(210, 256), random(66, 99)); // blue-ish
      }
    
      void draw() {
        stroke(col); 
        line(x, y, 
          x+ radius*cos(angle), y+ radius*sin(angle));
      }
    
      void longer() {
        radius+=random(2, 6);
        if (random(100)>90) 
          init( x+ radius*cos(angle), y+ radius*sin(angle)  );
      }
    }
    //
    
Sign In or Register to comment.