* Processing Popcorn Plug-In
*
* This plugin adds a Processing.js sketch to be added to a target div or canvas.
- *
+ *
* Options parameter needs to specify start, end, target and sketch attributes
- * -Start is the time [in seconds] that you want the sketch to display and start looping.
+ * -Start is the time [in seconds] that you want the sketch to display and start looping.
* -End is the time [in seconds] you want the sketch to become hidden and stop looping.
* -Target is the id of the div or canvas you want the target sketch to be displayed in. ( a target that is a div will have a canvas created and placed inside of it. )
* -Sketch specifies the filename of the Procesing code to be loaded into Processing.js
*/
(function ( Popcorn ) {
-
- var called = false;
Popcorn.plugin( "processing" , function ( options ) {
-
+
var init = function( context ) {
function scriptReady( options ) {
}
});
};
-
+
if ( options.sketch ) {
Popcorn.xhr({
url: options.sketch,
dataType: "text",
success: function( responseCode ) {
-
+
options.codeReady = false;
-
+
var s = Processing.compile( responseCode );
- s.onLoad = function() {
- console.log( "onLoad Called" );
- options.codeReady = true;
- ( options._running && !context.media.paused && options.pjsInstance.loop() ) || options.pjsInstance.noLoop() ;
- };
-
options.pjsInstance = new Processing( options.canvas, s );
options.seeking = false;
-
+ ( options._running && !context.media.paused && options.pjsInstance.loop() ) || options.pjsInstance.noLoop();
+
context.listen( "seeking", function() {
options._running && options.canvas.style.display === "inline" && options.noPause && options.pjsInstance.loop();
});
-
- options.noPause = options.noPause || false;
- !options.noPause && addListeners();
+ options.noPause = options.noPause || false;
+ !options.noPause && addListeners();
+ options.codeReady = true;
}
});
} else if ( Popcorn.plugin.debug ) {
-
- throw new Error( "options.sketch is undefined" );
+
+ throw new Error( "Popcorn.Processing: options.sketch is undefined" );
return;
}
-
- }
- if ( !window.Processing && !called ) {
+ }
+ if (!window.Processing) {
Popcorn.getScript( "http://processingjs.org/content/download/processing-js-1.3.0/processing-1.3.0.js", function() {
scriptReady( options )
});
- called = true;
} else {
- readyCheck = function() {
- if ( window.Processing ) {
- scriptReady( options );
- return;
- }
-
- setTimeout( readyCheck, 5 );
-
- };
-
- readyCheck();
-
+ scriptReady( options );
}
};
return {
_setup: function( options ) {
-
+
options.codeReady = false;
-
+
options.parentTarget = document.getElementById( options.target );
-
+
if ( !options.parentTarget && Popcorn.plugin.debug ) {
throw new Error( "target container doesn't exist" );
}
-
+
var canvas = document.createElement( "canvas" );
canvas.id = Popcorn.guid( options.target + "-sketch-" );
- //canvas[ "data-processing-sources" ] = options.sketch;
- canvas.style.display = "none";
+ canvas.style.display = "none";
options.canvas = canvas;
-
+
options.parentTarget && options.parentTarget.appendChild( options.canvas );
init( this );
},
end: function( event, options ) {
-
+
options.pjsInstance && options.pjsInstance.noLoop();
options.canvas.style.display = "none";
},
-
+
_teardown: function( options ) {
options.pjsInstance && options.pjsInstance.exit();
options.parentTarget && options.parentTarget.removeChild( options.canvas );