1

I'm trying to build a simple interaction where users input two numbers, have a gif loop a number of times equal to the sum of those numbers, and then have the gif freeze on the first frame (with an option to reset the process). I think I have figured out how to do almost all of this in p5, except for the gif looping.

Is there a way to control the loops of a gif in p5js? Is there a better way to approach this task? Animating a sprite or something would largely achieve the same goals if that was a wiser approach.

thank you!

let input, button, greeting;

function setup() {
  // create canvas
  createCanvas(710, 400);

  //creates the first value input box
  input1 = createInput();
  input1.position(20, 65);
  
  //creates the second value input box
  input2 = createInput();
  input2.position(200, 65);

  //creates the 'go' button
  go_button = createButton('calculate');
  go_button.position(input2.x + input2.width, 65);
  //pressing the button triggers the greet() function
  go_button.mousePressed(greet);
  
  //creates the reset button
  reset_button = createButton('reset');
  reset_button.position(20, height - 30);
  //immediately hides it 
  reset_button.hide();
  //press the button and reset the screen
  reset_button.mousePressed(reset);
  

  //initial text
  greeting = createElement('h2', 'what numbers do you want to add?');
  greeting.position(20, 5);

  textAlign(CENTER);
  textSize(50);
}

function greet() {
  //hides the inputs and the go button
  input1.hide();
  input2.hide();
  go_button.hide();
  
  //does the math
  const total = input1.value() + input2.value();
  //updates the text
  greeting.html('hello ' + total + '!');
  
  //resets the input values for next time
  input1.value('');
  input2.value('');
  
  //reveal the reset button
  reset_button.show();
  

  //just a thing to show the total
  for (let i = 0; i < 200; i++) {
    push();
    fill(random(255), 255, 255);
    translate(random(width), random(height));
    rotate(random(2 * PI));
    text(total, 0, 0);
    pop();
  }
}

function reset() {
  //hides the old stuff
  rect(-1,-1,width+5,height+5);
  
  //shows the inputs and the go button
  input1.show();
  input2.show();
  go_button.show();
  
  //hides the reset button
  reset_button.hide();
  
  //updates the text
  greeting.html('what numbers do you want to add?');  
  
}
mweinberg
  • 161
  • 11
  • I don't see any code related to an animated loop in your example so it will be hard to give you a precise answer. However you should definitely watch [this coding train video](https://youtu.be/nBKwCCtWlUg) where Daniel Shiffman shows how to create an animated loop with processing, I think that's what you want to do. – statox Feb 02 '21 at 14:58
  • Sorry about that. There isn't animated loop code because I haven't figured out how to do it. I can use play() to play and pause it and delay() to change the speed but nothing to control how many times it loops. Once I have it I would replace the for loop. The coding train video is great, but as far as I can tell it is about creating a loop, not controlling an existing one. – mweinberg Feb 03 '21 at 01:02
  • Oh I think I misunderstood your question. If you already have a gif file that you want to display the video won't help you indeed. There are two things: First thanks to [`image`](https://p5js.org/reference/#/p5/image) you can load a gif and show it (see [this example](https://editor.p5js.org/remarkability/sketches/yP869zQPV)). However if you want to control to many times it loops I don't think you can do that with p5 but you could use another library (see [this question](https://stackoverflow.com/q/2385203/4194289)) and you could imagine controlling the gif library based on some p5 variables. – statox Feb 03 '21 at 13:00
  • 1
    Thank you for the pointer! I'll dive into the other question to see what I can learn. I started with p5 because that's what I (sort of) know, but I'm not wedded to it in any way. If CSS can get me where I need to be I'm happy to use that. Thanks! – mweinberg Feb 03 '21 at 21:46
  • Personally I would first try with the JS libs from the other answers like libgif instead of CSS, but that's up to you. If you find a solution and get what you want don't hesitate to post an answer to your own question it could be useful to future readers. – statox Feb 04 '21 at 10:52

0 Answers0