Skip to content

Instantly share code, notes, and snippets.

@Maciek416
Created December 17, 2009 01:30
Show Gist options
  • Select an option

  • Save Maciek416/258425 to your computer and use it in GitHub Desktop.

Select an option

Save Maciek416/258425 to your computer and use it in GitHub Desktop.
/*
* This code searches for all the <script type="application/processing" target="canvasid">
* in your page and loads each script in the target canvas with the proper id.
* It is useful to smooth the process of adding Processing code in your page and starting
* the Processing.js engine.
*/
if ( window.addEventListener ) {
window.addEventListener("load", function() {
var scripts = document.getElementsByTagName("script");
for ( var i = 0; i < scripts.length; i++ ) {
if ( scripts[i].type == "application/processing" ) {
var src = scripts[i].getAttribute("target");
console.log("i=",i,"src = ",src);
var canvas = scripts[i].nextSibling;
if ( src && src.indexOf("#") > -1 ) {
canvas = document.getElementById( src.substr( src.indexOf("#") + 1 ) );
console.log("i=",i,"canvas=",canvas);
} else {
while ( canvas && canvas.nodeName.toUpperCase() != "CANVAS" )
canvas = canvas.nextSibling;
}
if ( canvas ) {
Processing(canvas, scripts[i].text);
}
}
}
}, false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment