Computational Thinking

Joel Gethin Lewis

Lecture 6

What I'm going to talk about today:

  1. Repetition.
  2. Fixing necklaces.
  3. How to load a gif in p5.js.
  4. How to make a GUI (Graphical User Interface) in p5.js to display frames of a gif.

Repetition.

Thanks again to Rune Madsen for putting his course online:

http://printingcode.runemadsen.com/lecture-repetition/

Don't forget to click to the slides and press 's' Joel!

Fixing necklaces.

Thanks again to Ali Almossawi for writing his book.

Read the Fixing necklaces chapter of "Bad Choices" Joel!

How to load a gif in p5.js.

Next we'd like to get on with the work that we set for ourselves in lecture one - to make an Animated GIF maker.

So how do I load animated gifs in p5.js?

Google: "animated gif p5.js"

How to load a gif in p5.js.

https://github.com/antiboredom/p5.gif.js/tree/master looks great!

So lets download it.

And try it, by opening the file in the browser.

How to load a gif in p5.js.

It doesn't work. )-;

Don't panic! Lets look at the in JavaScript Console.

Hmmm. I don't know what that means, lets Google: "p5.js cross origin error"

How to load a gif in p5.js.

Looks like I have to install a server for p5.js.

Lets follow the instructions to install node.js.

CAUTION: COMMAND LINE INTERFACE WARNING!

Let's go to the folder that contains the file we are trying to get working and run the webserver inside it, by dragging and dropping the folder onto the Terminal icon and running: "http-server -c-1".

Of course, we can use any gif we like with this program! Demo using giphy.gif instead.

How to make a GUI (Graphical User Interface) in p5.js to display frames of a gif.

Wouldn't it be great to be able to slide a slider instead of having to hold my mouse in one place?

Let's use a GUI library to let us do that via the p5.js libraries page.

p5.dom looks good. Let's download it and try it.

How to make a GUI (Graphical User Interface) in p5.js to display frames of a gif.

The DOM examples look good. So lets start with the old gif example from p5.gif.js and go from there.

OK, lets add the most basic p5.dom to it, just with both running together in the same sketch.js. First look at index.html and then sketch.js from the Slider p5.DOM example and then duplicate them over.

How to make a GUI (Graphical User Interface) in p5.js to display frames of a gif.

So now we have both running together, we can disable the mouse movement code, as we want it to be controlled by the GUI.

We cam get rid of both the G and B sliders too.

We can edit R to be called something better...

How to make a GUI (Graphical User Interface) in p5.js to display frames of a gif.

So lets set it to myFrame, and change to display the correct frame.

But how do I know the correct range?

That doesn't seem to be working, lets add console.log to see what is going on.

It's saying 0? I guess the gif hasn't loaded at that point.

How to make a GUI (Graphical User Interface) in p5.js to display frames of a gif.

So lets keep checking until it is loaded, THEN add the GUI

Use a boolean value! (I use this all the time in my code).

We can even display a loading message while we are waiting. How do we do that? Google "displaying text in p5.js".

Thanks!