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?');
}