]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
canvases now get unique ids, fixed leaking vars, moved around some functions and...
authorChristopher De Cairos <cadecairos@India.(none)>
Mon, 20 Jun 2011 19:30:05 +0000 (15:30 -0400)
committerChristopher De Cairos <cadecairos@India.(none)>
Mon, 20 Jun 2011 19:30:05 +0000 (15:30 -0400)
plugins/processing/popcorn.processing.html
plugins/processing/popcorn.processing.js
plugins/processing/popcorn.processing.unit.html
plugins/processing/popcorn.processing.unit.js

index e4974f0034a86e1b643d485ab91fd4a1948c509f..36ffe9d825d240238a8d9371393eed4911080679 100644 (file)
@@ -6,31 +6,38 @@
   <script src="popcorn.processing.js"></script>
   
   <script>
-    document.addEventListener('DOMContentLoaded', function () {
-      var p = Popcorn('#video')
+    document.addEventListener( "DOMContentLoaded", function () {
+      var p = Popcorn( "#video" )
+      .volume( 0 )
       .play()
       .processing({
         start: 2,
         end: 10,
-        target: 'processing-div',
-        sketch: 'test.pjs',
+        target: "processing-div",
+        sketch: "test.pjs",
         noPause: true
-      } )
+      })
       .processing({
         start: 10,
         end: 15,
-        target: 'processing-div2',
-        sketch: 'test2.pjs',
+        target: "processing-div2",
+        sketch: "test2.pjs",
         noPause: false
-      } )
+      })
      .processing({
         start: 15,
         end: 20,
-        sketch: 'test.pjs',
-        target: 'processing-div2',
+        sketch: "test.pjs",
+        target: "processing-div2",
         noPause: true
-      } )
-    window.popped = p;
+      })
+      .processing({
+        start: 5,
+        end: 15,
+        sketch: "test2.pjs",
+        target: "processing-canvas",
+        noPause: false
+      });
     }, false);
 
   </script>
@@ -60,5 +67,6 @@
   </div>
   <div id="processing-div2" width="50%" height="50%">
   </div>
+  <canvas id="processing-canvas"></canvas>
   </body>
 </html>
index 3fee16710865ed6c1ae33da3629bf66f370043cb..b972ea6c622ff6d111db845c95080878c58d6946 100644 (file)
 
 (function ( Popcorn ) {
 
-  var processingLoaded = false,
+  var processingLoaded = false;
 
-    toggle = function( on, options ) {
-      var instance = options.pjsInstance,
-          canvas = options.canvas;
-          
-      if ( canvas && options.isReady ) {
-        if ( on ) {
-          canvas.style.display = "inline";
-          !this.media.paused && instance.loop();
+      toggle = function( on, options ) {
+        var instance = options.pjsInstance,
+            canvas = options.canvas;
+            
+        if ( canvas && options.isReady ) {
+          if ( on ) {
+            canvas.style.display = "inline";
+            !this.media.paused && instance.loop();
+          } else {
+            canvas.style.display = "none";
+            instance.noLoop();
+          }
         } else {
-          canvas.style.display = "none";
-          instance.noLoop();
+          setTimeout (function() {
+            toggle.call( this, on, options );
+          }, 10 );
         }
-      } else {
-        setTimeout (function() {
-          toggle.call( this, on, options );
-        }, 10 );
-      }
-    },
+      },
 
-    load = function() {
-      Popcorn.getScript( "http://processingjs.org/content/download/processing-js-1.2.1/processing-1.2.1.js", function() {
-        processingLoaded = true;
-      });
-    };
+      load = function() {
+        Popcorn.getScript( "http://processingjs.org/content/download/processing-js-1.2.1/processing-1.2.1.js", function() {
+          processingLoaded = true;
+        });
+      };
 
   load();
 
   Popcorn.plugin( "processing" , function ( options ) {
 
     var init = function( context ) {
-      var popcorn = context,
-      initProcessing,
-      parentTarget = document.getElementById( options.target );
+    
+      var parentTarget = document.getElementById( options.target ),
       
-      if ( parentTarget.tagName === "CANVAS" ) {
-        options.canvas = parentTarget;
-      } else if ( parentTarget.tagName === "DIV" ) {
-        options.canvas = document.createElement( "canvas" );
-        // +new Date() is used here to create unique id's for canvas' within the same div.
-        options.canvas.id = options.target + "Sketch" + ( +new Date() );
-        options.canvas.setAttribute( "data-processing-sources", options.sketch );
-        parentTarget.appendChild( options.canvas );
-      }
-      options.canvas.style.display = "none";
-      Popcorn.xhr({
-        url: options.sketch,
-        dataType: "text",
-        success: function( responseCode ) {
-          codeReady = true;
-          processingCode = responseCode;
-          initProcessing();
-        }
-      });
-
       initProcessing = function() {
         var addListeners = function() {
-          popcorn.listen( "pause", function () {
+          context.listen( "pause", function () {
             if ( options.canvas.style.display === "inline" ) {
               options.pjsInstance.noLoop();
             }
           });
-          popcorn.listen( "play", function() {
+          context.listen( "play", function() {
             if ( options.canvas.style.display === "inline" ) {
               options.pjsInstance.loop();
             }
           });
         };
         
-        if ( codeReady && window.Processing ) {
-          options.pjsInstance = new Processing( options.canvas, processingCode );
-          popcorn.listen( "seeking", function() {
+        if ( options.codeReady && window.Processing ) {
+          options.pjsInstance = new Processing( options.canvas, options.processingCode );
+          context.listen( "seeking", function() {
             if ( options.canvas.style.display === "inline" && options.noPause ) {
               options.pjsInstance.loop();
             }
           setTimeout ( initProcessing, 10 );
         }
       };
+      
+      if ( parentTarget.tagName === "CANVAS" ) {
+        options.canvas = parentTarget;
+      } else if ( parentTarget.tagName === "DIV" ) {
+        options.canvas = document.createElement( "canvas" );
+        parentTarget.appendChild( options.canvas );
+      }    
+      options.canvas.id = Popcorn.guid( options.target + "-sketch-" );
+      options.canvas.setAttribute( "data-processing-sources", options.sketch );
+      options.canvas.style.display = "none";
+      
+      Popcorn.xhr({
+        url: options.sketch,
+        dataType: "text",
+        success: function( responseCode ) {
+          options.codeReady = true;
+          options.processingCode = responseCode;
+          initProcessing();
+        }
+      });
     };
 
     return {
       },
 
       _setup: function( options ) {
-
-        var processingCode,
-            codeReady = false,
-            self = this,
-
-        readyCheck = function() {
+        
+        options.codeReady = false;
+        
+        var readyCheck = function() {
           if ( !processingLoaded ) {
             load();
           }
         };
         readyCheck();
-        init( self );
+        init( this );
       },
 
       start: function( event, options ) {
index ab6a43abb66cb1a77bc52f9392b2b398259a7814..a82a073f2844f20ab2f73e96123a81c694f71079 100644 (file)
@@ -37,4 +37,7 @@
   <div id="processing-div" width="50%" height="50%"></div>
   <div id="processing-div-2" width="50%" height="50%"></div>
   <div id="processing-div-3" width="50%" height="50%"></div>
+  <div id="processing-canvas-div">
+  <canvas id="processing-canvas"></canvas>
+  </div>
 </html>
index 01cd2dc33ee77e7262e746ebe08cf9e7b674d026..42753aaf88ca25918d9f1c046ab6a39b0d881c66 100644 (file)
@@ -1,10 +1,11 @@
 test( "Processing plugin tests", function() {
   var popped = Popcorn( "#video" ),
-      expects = 18,
+      expects = 26,
       count = 0,
       processingDiv = document.getElementById( "processing-div" ),
       processingDiv2 = document.getElementById( "processing-div-2" ),
       processingDiv3 = document.getElementById( "processing-div-3" ),
+      processingCanvas = document.getElementById( "processing-canvas" ),
       canvases = [];
 
   expect( expects );
@@ -28,6 +29,9 @@ test( "Processing plugin tests", function() {
   
   equals( processingDiv3.innerHTML, "", "'processing-div-3' is empty" );
   plus();
+  
+  ok( processingCanvas, "'processing-canvas' is present in DOM" );
+  plus();
 
   popped.processing({
     start: 0,
@@ -47,9 +51,21 @@ test( "Processing plugin tests", function() {
     target: "processing-div-3",
     sketch: "test.pjs"
   })
+  .processing({
+    start: 3,
+    end: 4,
+    target: "processing-canvas",
+    sketch: "test2.pjs"
+  })
   .exec( 0.5, function() {
     equals( processingDiv.children[ 0 ].style.display, "inline", "canvas '" + processingDiv.children[ 0 ].id + "' is displayed" );
     plus();
+    equals( processingDiv2.children[ 0 ].style.display, "none", "canvas '" + processingDiv2.children[ 0 ].id + "' is hidden" );
+    plus();
+    equals( processingDiv3.children[ 0 ].style.display, "none", "canvas '" + processingDiv3.children[ 0 ].id + "' is hidden" );
+    plus();
+    equals( processingCanvas.style.display, "none", "canvas '" + processingCanvas.id + "' is hidden" );
+    plus();
     //TODO: David Humphrey has written code that allows for listening to events on Processing instances. when 
     // it lands In the next processing release we'll be able to detect if a processing instance is looping or not.
     // this is required in order to test the noPause functionality (automatically)
@@ -61,6 +77,8 @@ test( "Processing plugin tests", function() {
     plus();
     equals( processingDiv3.children[ 0 ].style.display, "none", "canvas '" + processingDiv3.children[ 0 ].id + "' is hidden" );
     plus();
+    equals( processingCanvas.style.display, "none", "canvas '" + processingCanvas.id + "' is hidden" );
+    plus();
   }).exec( 3.5, function() {
     equals( processingDiv.children[ 0 ].style.display, "none", "canvas '" + processingDiv.children[ 0 ].id + "' is hidden" );
     plus();
@@ -68,11 +86,13 @@ test( "Processing plugin tests", function() {
     plus();
     equals( processingDiv3.children[ 0 ].style.display, "inline", "canvas '" + processingDiv3.children[ 0 ].id + "' is displayed" );
     plus();
+    equals( processingCanvas.style.display, "inline", "canvas '" + processingCanvas.id + "' is displayed" );
+    plus();
   });
   
   //check that three canvases were created
   canvases = document.querySelectorAll( "canvas" ); 
-  equals( canvases.length, 3, "Three canvases are present" );
+  equals( canvases.length, 4, "four canvases are present" );
   plus();
 
   var called = 0;