Now, back to my original point, what makes this so important is optimization. When developing HTML5 canvas for mobile, it has been widely accepted that JavaScripts requestAnimationFrame() function offers huge advantages over traditional JS timing functions like setInterval() or setTimeout (see Paul Irish's post for more info as well as the polyfill). Unfortunately, because rAF wasn't around when PJS was first written, it uses setInterval() instead. The good people on the PJS team are aware of this and hard at work on a fix, but just isn't there yet. So, because of this, I found a work around! Because I had already bound PJS and JS together so I could do the interface, I also handed control of the looping to JS entirely! This way, I was able to write a rAF loop which calls pjs.draw(), as I will demonstrate below:
First, binding JavaScript and Processing.js together (see Pomax's guide for more details)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//at the top of your .pde or .pjs | |
void bindJavascript(JavaScript js) { | |
javascript = js; | |
} | |
JavaScript javascript; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Insert this script after processing.js is included --> | |
<script type="text/javascript"> | |
var bound = false; | |
var pjs; | |
function bindJavascript() { | |
//reference the ID of your canvas that will be rendering your processing sketch | |
pjs = Processing.getInstanceById('polybranch'); | |
if(pjs!=null) { | |
pjs.bindJavascript(this); | |
bound = true; | |
} | |
if(!bound) setTimeout(bindJavascript, 250); | |
} | |
bindJavascript(); | |
</script> |
Now that Processing and JavaScript can communicate with each other, we can use rAF to call Processing's draw function!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var playing = true; | |
//call this function to start the loop | |
function startRaf(){ | |
rafGo(); | |
} | |
function rafGo() { | |
//loop stops when playing is set to false | |
if(playing){ | |
//be sure to include Paul Irish's rAF shim! | |
requestAnimationFrame(rafGo); | |
pjs.draw(); | |
} | |
} | |
startRaf(); |
This is a very bare-bones demonstration of the draw loop. Be sure to check out Kushagra Agarwal's article on time based animations for best results!
As a disclaimer, I should note that I had moved over primarily to JavaScript development at this point in the process and was using this method so I could retain the logic I had written in Processing. I was no longer working within the Processing IDE! If you are using Processing.js as a JavaScript library, you can also check out this discussion from the PJS google group.
No comments:
Post a Comment