]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#383] review cleanup, leaky vars, setInteval to setTimeout
authorScottDowne <sdowne@learn.senecac.on.ca>
Tue, 17 May 2011 01:27:23 +0000 (21:27 -0400)
committerScottDowne <sdowne@learn.senecac.on.ca>
Tue, 17 May 2011 01:27:23 +0000 (21:27 -0400)
plugins/gml/popcorn.gml.html
plugins/gml/popcorn.gml.js

index ad607fde4b6d3c78e9ab962e6f34d2e8ab01704f..9d46f7335cf1d2527a17dd9ba99c2137bdd331fa 100644 (file)
@@ -34,7 +34,7 @@
   </script>
 </head>
 <body>
-  <h1 id="qunit-header">Popcorn Attribution Plug-in Demo</h1>
+  <h1 id="qunit-header">Popcorn GML Plug-in Demo</h1>
   <p> A Processing canvas displaying the GML tag <a href="http://000000book.com/data/29582">29582</a> will appear at 0-5 seconds.</p>
   <p> A Processing canvas displaying the GML tag <a href="http://000000book.com/data/155">155</a> will appear at 10-20 seconds.</p>
   <p> A Processing canvas displaying the GML tag <a href="http://000000book.com/data/161">161</a> will appear at 20-40 seconds.</p>
index 5491afa0796e16fcc0297e61a1ee067076838bed..26512dccf364e6bf94ef057ed8d47861ba20aa44 100644 (file)
@@ -5,15 +5,15 @@
   var processingLoaded = false,
       gmlPlayer = function( $p ) {
 
-        var _stroke = 0,
-            onPt = 0, 
+        var _stroke  = 0,
+            onPt     = 0, 
             onStroke = 0,
-            x = null,
-            y = null,
+            x        = null,
+            y        = null,
             rotation = false,
-            strokes = 0,
-            play =  function() {},
-            reset = function() {
+            strokes  = 0,
+            play     = function() {},
+            reset    = function() {
 
               $p.background( 0 );
               onPt = onStroke = 0;
@@ -21,6 +21,8 @@
             },
             drawLine = function( x, y, x2, y2 ) {
 
+              var _x, _y, _x2, _y2;
+
               if ( rotation ) {
 
                 _x  = y * $p.height;
@@ -34,6 +36,7 @@
                 _x2 = x2 * $p.width;
                 _y2 = y2 * $p.height;
               }
+
               $p.stroke( 0 );
               $p.strokeWeight( 13 );
               $p.strokeCap( $p.SQUARE );
                 if ( !strokes ) {
                   return;
                 }
+
                 _stroke = strokes[ onStroke ] || strokes;
-                var pt = _stroke.pt[ onPt ];
-                var p = onPt;
-                x && drawLine( x, y, pt.x, pt.y );
+                var pt = _stroke.pt[ onPt ],
+                    p = onPt;
+                x != null && drawLine( x, y, pt.x, pt.y );
 
                 x = pt.x;
                 y = pt.y;
         $p.setup = function() {};
         $p.construct = function( media, data, options ) {
 
-          $p.size( 640,640 );
-          $p.frameRate( 60 );
-          $p.smooth();
-          reset();
-          $p.noLoop();
+          var dataReady = function() {
 
-          var si =  setInterval(function() {
+            if ( data ) {
 
-            if ( !data ) {
-              return;
-            }
-            clearInterval( si );
+              strokes = data.gml.tag.drawing.stroke;
 
-            strokes = data.gml.tag.drawing.stroke;
+              var drawingDur = ( options.end - options.start ) / ( strokes.pt || (function( strokes ) {
 
-            var drawingDur = ( options.end - options.start ) / ( strokes.pt || (function( strokes ) {
+                var rStrokes = [];
 
-              var rStrokes = [];
+                for ( var i = 0, sl = strokes.length; i < sl; i++ ) {
 
-              for ( var i = 0, sl = strokes.length; i < sl; i++ ) {
+                  rStrokes = rStrokes.concat( strokes[ i ].pt );
+                }
 
-                rStrokes = rStrokes.concat( strokes[ i ].pt );
-              }
+                return rStrokes;
+              })( strokes ) ).length;
 
-              return rStrokes;
-            })( strokes ) ).length;
+              var tag = data.gml.tag,
+                  app_name =  tag.header && tag.header.client && tag.header.client.name;
 
-            var tag = data.gml.tag;
-            var app_name =  tag.header && tag.header.client && tag.header.client.name;
-            rotation = app_name === 'Graffiti Analysis 2.0: DustTag' ||
-                       app_name === 'DustTag: Graffiti Analysis 2.0' ||
-                       app_name === 'Fat Tag - Katsu Edition';
+              rotation = app_name === 'Graffiti Analysis 2.0: DustTag' ||
+                         app_name === 'DustTag: Graffiti Analysis 2.0' ||
+                         app_name === 'Fat Tag - Katsu Edition';
 
-            play = function() {
+              play = function() {
 
-              if ( media.currentTime < options.endDrawing ) {
+                if ( media.currentTime < options.endDrawing ) {
 
-                seek( ( media.currentTime - options.start ) / drawingDur );
-              }
-            };
-          }, 50);
+                  seek( ( media.currentTime - options.start ) / drawingDur );
+                }
+              };
+
+              return;
+            }
+
+            setTimeout( dataReady, 5 );
+          };
+
+          $p.size( 640, 640 );
+          $p.frameRate( 60 );
+          $p.smooth();
+          reset();
+          $p.noLoop();
+
+          dataReady();
         };
       };
 
       document.getElementById( options.target ) && document.getElementById( options.target ).appendChild( options.container );
 
       // makes sure both processing.js and the gml data are loaded
-      var readyCheck = setInterval(function() {
+      var readyCheck = function() {
+
+        if ( processingLoaded ) {
+          Popcorn.getJSONP( "http://000000book.com/data/" + options.gmltag + ".json?callback=", function( data ) {
 
-        if ( !processingLoaded ) {
+            options.pjsInstance = new Processing( options.container, gmlPlayer );
+            options.pjsInstance.construct( self.media, data, options );
+            options._running && options.pjsInstance.loop();
+          }, false );
 
           return;
         }
 
-        clearInterval( readyCheck );
-        Popcorn.getJSONP( "http://000000book.com/data/" + options.gmltag + ".json?callback=", function( data ) {
+        setTimeout( readyCheck, 5 );
+      }
 
-          options.pjsInstance = new Processing( options.container, gmlPlayer );
-          options.pjsInstance.construct( self.media, data, options );
-          options._running && options.pjsInstance.loop();
-        }, false );
-      }, 5);
+      readyCheck();
     },
     /**
      * @member gml