From: Christopher De Cairos Date: Mon, 20 Jun 2011 19:30:05 +0000 (-0400) Subject: canvases now get unique ids, fixed leaking vars, moved around some functions and... X-Git-Tag: v0.7^2~31^2~2 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=65aea12261a5d1f9f2236bc14069659239065e76;p=popcorn-js.git canvases now get unique ids, fixed leaking vars, moved around some functions and fixed scoping issues --- diff --git a/plugins/processing/popcorn.processing.html b/plugins/processing/popcorn.processing.html index e4974f0..36ffe9d 100644 --- a/plugins/processing/popcorn.processing.html +++ b/plugins/processing/popcorn.processing.html @@ -6,31 +6,38 @@ @@ -60,5 +67,6 @@
+ diff --git a/plugins/processing/popcorn.processing.js b/plugins/processing/popcorn.processing.js index 3fee167..b972ea6 100644 --- a/plugins/processing/popcorn.processing.js +++ b/plugins/processing/popcorn.processing.js @@ -26,79 +26,58 @@ (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(); } @@ -111,6 +90,26 @@ 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 { @@ -132,18 +131,16 @@ }, _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 ) { diff --git a/plugins/processing/popcorn.processing.unit.html b/plugins/processing/popcorn.processing.unit.html index ab6a43a..a82a073 100644 --- a/plugins/processing/popcorn.processing.unit.html +++ b/plugins/processing/popcorn.processing.unit.html @@ -37,4 +37,7 @@
+
+ +
diff --git a/plugins/processing/popcorn.processing.unit.js b/plugins/processing/popcorn.processing.unit.js index 01cd2dc..42753aa 100644 --- a/plugins/processing/popcorn.processing.unit.js +++ b/plugins/processing/popcorn.processing.unit.js @@ -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;