init.js 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. /*
  2. * This code searches for all the <script type="application/processing" target="canvasid">
  3. * in your page and loads each script in the target canvas with the proper id.
  4. * It is useful to smooth the process of adding Processing code in your page and starting
  5. * the Processing.js engine.
  6. */
  7. if ( window.addEventListener ) {
  8. window.addEventListener("load", function() {
  9. var scripts = document.getElementsByTagName("script");
  10. for ( var i = 0; i < scripts.length; i++ ) {
  11. if ( scripts[i].type == "application/processing" ) {
  12. var src = scripts[i].getAttribute("target");
  13. var canvas = scripts[i].nextSibling;
  14. if ( src && src.indexOf("#") > -1 ) {
  15. canvas = document.getElementById( src.substr( src.indexOf("#") + 1 ) );
  16. } else {
  17. while ( canvas && canvas.nodeName.toUpperCase() != "CANVAS" ) {
  18. canvas = canvas.nextSibling;
  19. }
  20. }
  21. if ( canvas ) {
  22. new Processing(canvas, scripts[i].text);
  23. }
  24. }
  25. }
  26. }, false);
  27. }