front_idill/extern/fajran-tuiojs/examples/processingjs/init.js
changeset 26 858e90c7cbaa
equal deleted inserted replaced
25:a7b0e40bcab0 26:858e90c7cbaa
       
     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 
       
     8 if (window.addEventListener) {
       
     9   window.addEventListener("load", function() {
       
    10     var scripts = document.getElementsByTagName("script");
       
    11     var canvasArray = Array.prototype.slice.call(document.getElementsByTagName("canvas"));
       
    12     var canvas;
       
    13     for (var i = 0, j = 0; i < scripts.length; i++) {
       
    14       if (scripts[i].type == "application/processing") {
       
    15         var src = scripts[i].getAttribute("target");
       
    16         if (src && src.indexOf("#") > -1) {
       
    17           canvas = document.getElementById(src.substr(src.indexOf("#") + 1));
       
    18           if (canvas) {
       
    19             new Processing(canvas, scripts[i].text);
       
    20             for (var k = 0; k< canvasArray.length; k++)
       
    21             {
       
    22               if (canvasArray[k] === canvas) {
       
    23                 // remove the canvas from the array so we dont override it in the else
       
    24                 canvasArray.splice(k,1);
       
    25               }
       
    26             }
       
    27           }
       
    28         } else {
       
    29           if (canvasArray.length >= j) {
       
    30             new Processing(canvasArray[j], scripts[i].text);
       
    31           }
       
    32           j++;
       
    33         }
       
    34       }
       
    35     }
       
    36   }, false);
       
    37 }