How to fix gif exportation ?

Hello, I'm a beginner in programmation, and I have made a small animation based on an existing one found in a book. I am now looking for exporting this animation into an animated gif, so I found this method :

http://p5art.tumblr.com/post/76979034572/exporting-sketches-as-animated-gifs-in-processing

The problem is, it doesn't seem to work. (I've already dowloaded the gifAnimation library for processing 3.0, it works on another sketch but not on this one) I keep getting, " unreachable code" and I can't get where I made a mistake. I suppose the GifExport lines are in the wrong place but I don't know how to fix it. So I am looking for some help on this forum haha :) I post the code here, if anyone can help :

import java.util.Calendar;
import processing.pdf.*;
import gifAnimation.*;

GifMaker gifExport;


int NORTH = 0;
int EAST = 1;
int SOUTH = 2;
int WEST = 3;

float posX, posY;
float posXcross, posYcross;

int direction = SOUTH;
float angleCount =1;   
float angle = getRandomAngle(direction);
float stepSize = 2; 
int minLength = 7; 
int dWeight = 100; 
int dStroke = 7;

int drawMode = 1;

void setup() {

  frameRate(60);

  size(1200, 700);
  colorMode(HSB, 360, 100, 100, 100);
  smooth();
  background(360);

  posX = int(random(0, width));
  posY = 0;
  posXcross = posX;
  posYcross = posY;

  noStroke();
  fill(359);
  rect(450, 250, 300, 200);

  gifExport = new GifMaker(this, “export.gif”);
  gifExport.setRepeat(0);
}

void draw() {
  for (int i=40; i<=mouseX; i++) {


    // ------ make step ------
    posX += cos(radians(angle)) * stepSize;
    posY += sin(radians(angle)) * stepSize;

    // ------ check if agent is near one of the display borders ------
    boolean reachedBorder = false;

    if (posY <= 1) {
      direction = SOUTH;
      reachedBorder = true;
    } else if (posX >= width-1) {
      direction = WEST;
      reachedBorder = true;
    } else if (posY >= height-1) {
      direction = NORTH;
      reachedBorder = true;
    } else if (posX <= 1) {
      direction = EAST;
      reachedBorder = true;
    }

    // ------ if agent is crossing his path or border was reached ------
    int px = (int) posX;
    int py = (int) posY;
    if (get(px, py) != color(360) || reachedBorder ) {
      angle = getRandomAngle(direction);
      float distance = dist(posX, posY, posXcross, posYcross);
      if (distance >= minLength) {
        strokeWeight(dWeight/50);
        if (drawMode == 1) stroke(0, 0, 0, 20);
        if (drawMode == 2) stroke( distance/dStroke, 70, 150);
        if (drawMode == 3) stroke(192, 100, 64, distance/dStroke);
        line(posX, posY, posXcross, posYcross);
      }
      posXcross = posX;
      posYcross = posY;
    }
  }
}

float getRandomAngle(int theDirection) {
  float a = (floor(random(-angleCount, angleCount)) + 0.5) * 90.0/angleCount;

  if (theDirection == NORTH) return (a - 90);
  if (theDirection == EAST) return (a);
  if (theDirection == SOUTH) return (a + 90);
  if (theDirection == WEST) return (a + 180);

  return 0;

  if (frameCount >=0 && frameCount<121) {
    gifExport.setDelay(5); // = speed of the animated GIF
    gifExport.addFrame();
  }
  if (frameCount>121) gifExport.finish();
}



void keyReleased() {
  if (key == DELETE || key == BACKSPACE) {
    background(360);
    noStroke();
    fill(359);
    rect(450, 250, 300, 200);
  }

  if (key == 's' || key == 'S') saveFrame(timestamp()+"_##.png");
  if (key == '1') drawMode = 1;
  if (key == '2') drawMode = 2;
  if (key == '3') drawMode = 3;
}

// timestamp
String timestamp() {
  Calendar now = Calendar.getInstance();
  return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", now);
}

Thanks for your advice !

Answers

  • edited December 2017 Answer ✓

    Do not use nor for strings, just "". L-) You can use those inside "" though. O:-)

    "Unreachable code"

    @ line #100, you've got return 0;, which unconditionally & prematurely finishes the function getRandomAngle(): https://Processing.org/reference/return.html

    Obviously, the rest of the statements after that return 0; are unreachable! 8-X

  • Thank you ! I didn't really understood the return method. As for the "" , I think I blindly copy pasted the internet page without wondering if there was any unvalid character...

  • edited December 2017 Answer ✓

    I didn't really understood the return method.

    Technically, return isn't a method, but a Java command. ~O)
    It's also a reserved keyword: https://Docs.Oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.9

  • Thank you for your help ! I've managed to create the gif but now I can't open it ! :'( The file looks like corrupted or damaged...

  • When you copy and paste, the character encoding could be different between your source (your website) and your destination. Hence, you get some characters that look alike but the system interprets them differently. You were lucky as your " character looks a bit different. I had to deal with an hyphen problem ( - ) as those lines are hard to tell apart when the encoding is screwed up, so yeah, it took me a while to figure the problem.

    Kf

Sign In or Register to comment.