The Nature Of Overfishing with ProcessingJS and JQuery
For Dan Shiffman’s incredible Nature of Code class at NYU ITP, I decided to build a Web-based aquarium that would tell the tale of the world’s fish populations over the last 100 years. See it in action at: http://athinkingsam.com/aquarium/
People hear how fish stocks are being depleted due to overfishing, but it’s difficult to understand the real magnitude of this decline. My project — the Nature of Overfishing — is a Web aquarium that tells the tale of the world’s fish populations over the last 100 years. While there was once big “predatory” fish and small “prey” fish in healthy proportions, overfishing has decimated our ocean’s big fish (i.e. the ones humans catch and eat) and left small fish populations overly abundant. The decline was modest until 1970, and then took a sharp and drastic turn for the worst. This digital aquarium is a data story, presenting the information visually. The data comes directly from a regression analysis by Dr. Villy Christensen and the renowned UBC Fisheries Centre.
Above all, I wanted to make this overfishing data more accessible by building a fun and visually catching Web aquarium. I chose to build it for the Web using ProcessingJS, JQuery, and some custom Javascript.
- You can see the full code here: https://github.com/sslover/the-nature-of-overfishing. You’ll want to primarily look at index.html, aquarium.pde, and application.js in the JS folder.
- You can find Dan Shiffman’s entire Nature of Code book here (this book is approachable and lots of fun… highly recommended!): http://natureofcode.com/
I am going to use this post to talk about some best practices in working in ProcessingJS and bringing in other Javascript elements (such as Jquery). All of this is easier then one might expect, although there are some ground rules that you basically have to abide by:
- You cannot use Processing libraries. If you need a library, explore what Javascript and Jquery have to offer. There’s many comparable libraries that could be intertwined with your Processing sketch (however, this is more for supplemental aspects like audio or UI elements; libraries needed for the actual animation of your sketch are more tricky on the Web and I find are best avoided).
- You’ll want to set all sizing and placement based on the browser Window size. No worries — Javascript makes this easy.
- You have to be very conscious of how much you are drawing. I find it’s best to use Arraylists to easily remove elements of the sketch when they are no longer needed (and add new elements).
- You should take advantage of Javascript and Jquery! They can bring lots of interactivity to your animation.
- You should give the user a prompt if their browser can’t display the animation properly. Again using JQuery, you can detect browsers that don’t have the needed attributes to properly display a ProcessingJS sketch, and let the user know accordingly (for example, for browsers that don’t support Canvas or are very tiny).
Before I talk a bit about each of the above ground rules, I first want to address the question of “Why ProcessingJS?”. In fact, when I started on this project, I initially considered using either Paper.js or D3.js. While both of these Javascript libraries are wonderful in their own right, I decided to go with ProcessingJS mainly due to how much easier it is to do complex animations and drawings. It is true that these other libraries CAN do complex animations as well (for example: http://paperjs.org/examples/tadpoles/), but the amount of work needed to produce a similar quality is a tad burdensome. Quite simply, ProcessingJS makes animations a cinch. You just need to be careful about the speed and functionality of your animation in the Web canvas-based environment. And with that, the following should hopefully help
Rule 1: No Libraries
Processing comes with some amazing libraries. Unfortunately, these won’t work in a Web-based environment. So if you’re making a sketch for the Web, don’t even think about using them. Instead, think about why you’d want to use a library in the first place. Usually, you’ll be able to 1. accomplish a similar objective without a library altogether (even if it means simplifying your concept a tad) or 2. find a Javascript or Jquery equivalent that can work with your Processing sketch (especially if you need something supplemental like audio or UI elements). But really, for most things on the Web, you likely won’t need libraries.
Rule 2: Size EVERYTHING based on the detected screensize
This is an important one. When you’re developing a Processing sketch natively on your computer, there’s lots in your control. Perhaps most importantly, you control which system your sketch will run on. And as a result, you have some idea of the size of the screen. Unfortunately, life is not so easy on the Web. Computer screens come in all sorts of sizes, and you’ll probably want your animation to work on tablets too. But fear not! There’s actually a great solution here. Using Javascript, you can detect the height and width of the browser screen, set those detected sizes as global variables, and then set ALL sizings and placements in your sketch based on the detected sizes.
Check out this chunk of code to see what I mean:
Rule 3: Monitor How Much You Draw
This is more of a thought process than anything else. There’s times we all want to go crazy and animate everything we can think of, but in a Web canvas environment, that won’t always work (in my aquarium, I initially animated some pretty badass sea plants, before realizing this would render the animation painfully slow on the Web). You’ll need to manage what you’re drawing somewhat meticulously. Using the Processing ArrayList, you can easily add and remove elements as needed. In particular, if an item leaves the screen or is otherwise no longer needed in your sketch, best to remove it. Pro tip: Println() the size of your Arrays and ArrayList until you push to production. That way, you keep track of how much you are exactly drawing.
Rule 4: Take advantage of Javascript and JQuery
And here we finally get to why ProcessingJS is so darn useful and awesome: it can be combined elegantly with other elements of the Web, particularly Javascript and JQuery. This, my friends, is a powerful thing. For example, in my aquarium, I use a JQuery slider to allow the user to toggle between years. When the slider is changed, the new value is passed to the ProcessingJS sketch (via a function), and the sketch is updated accordingly (in my case, fish fade in or fade out). While I certainly COULD have implemented my own slider in ProcessingJS, it would be akin to re-inventing the wheel. JQuery already has amazing UI elements, and these elements (like any other Javascript component), can talk to your ProcessingJS sketch quite easily.
Before I go on, I must reference the amazing (and definititive) guide by Pomax: http://processingjs.org/articles/PomaxGuide.html#jstosketch. Seriously, read it.
Now, let’s jump into the code in my aquarium that made the magic happen. It involves 1. a function in ProcessingJS (aquarium.pde) that can receive an argument and 2. a function in Javascript (application.js) that can communicate to the ProcessingJS function. The former is super simple, and the latter is what you’ll need to get a handle on. Check it out:
Rule 5: Prompt the User if their browser won’t properly show the sketch
Finally, we must accept that the Web is a whole different beast. The fact of the matter is that some users are going to try to access your animation with old browsers that lack Canvas (which is needed to display a ProcessingJS animation (and almost all Web animations for that matter)), or on mobile devices that are teeny tiny and may not be suitable in displaying your work (for example, while my aquarium works on mobile browsers, it lacks the same visual effectiveness). In these scenarios, it’s a good idea to create a pop-up modal window letting the user know that the visualization is not compatible with his or her browser.
Check out this tutorial for a very easy way to create a pop-up modal window with JQuery. Note that the window is hidden at first and ONLY shows if certain conditions are met (in our case, the browser lacks canvas or is below a certain size).
Here’s the code I created to trigger the modal:
And that’s it. ProcessingJS with JQuery and Javascript can be a great way to get your animations to the Web for the world to see. Follow the 5 rules above, and you’ll be in business.

