-// PLUGIN: Processing
/**
-* Processing popcorn plug-in
-*/
+ * 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.
+ * -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
+ * -noLoop [optional] specifies whether a sketch should continue to loop when the video is paused or seeking.
+ *
+ * @param {Object} options
+ *
+ * Example:
+ var p = Popcorn( "#video" )
+ .processing({
+ start: 5,
+ end: 10,
+ target: "processing-div",
+ sketch: "processingSketch.pjs",
+ noLoop: true
+ });
+ *
+ */
-(function (Popcorn) {
+(function ( Popcorn ) {
var processingLoaded = false,
toggle = function( on, options ) {
var instance = options.pjsInstance,
canvas = options.canvas;
+
if ( canvas && options.isReady ) {
if ( on ) {
canvas.style.display = "inline";
instance.noLoop();
}
} else {
- setTimeout ( function() {
+ setTimeout (function() {
toggle.call( this, on, options );
}, 10 );
}
var popcorn = context,
initProcessing,
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.id = options.target + "Sketch" + ( +new Date() );
options.canvas.setAttribute( "data-processing-sources", options.sketch );
parentTarget.appendChild( options.canvas );
}
if ( options.canvas.style.display === "inline" ) {
options.pjsInstance.noLoop();
}
- } );
+ });
popcorn.listen( "play", function() {
if ( options.canvas.style.display === "inline" ) {
options.pjsInstance.loop();
if ( codeReady && window.Processing ) {
options.pjsInstance = new Processing( options.canvas, processingCode );
- !options.noPause && addListeners();
+ popcorn.listen( "seeking", function() {
+ if ( options.canvas.style.display === "inline" && options.noPause ) {
+ options.pjsInstance.loop();
+ }
+ });
+
+ options.noPause = options.noPause || false;
+ !options.noPause && addListeners();
options.isReady = true;
} else {
setTimeout ( initProcessing, 10 );
expects = 18,
count = 0,
processingDiv = document.getElementById( "processing-div" ),
- processingDiv2= document.getElementById( "processing-div-2" ),
- processingDiv3= document.getElementById( "processing-div-3" ),
+ processingDiv2 = document.getElementById( "processing-div-2" ),
+ processingDiv3 = document.getElementById( "processing-div-3" ),
canvases = [];
expect( expects );
popped.processing({
start: 0,
- end: 3,
+ end: 1,
target: "processing-div",
sketch: "test.pjs"
})
.processing({
- start: 4,
- end: 7,
+ start: 1,
+ end: 3,
target: "processing-div-2",
sketch: "test2.pjs"
})
.processing({
- start: 4,
- end: 7,
+ start: 3,
+ end: 4,
target: "processing-div-3",
sketch: "test.pjs"
})
- .exec( 1.5, function() {
+ .exec( 0.5, function() {
equals( processingDiv.children[ 0 ].style.display, "inline", "canvas '" + processingDiv.children[ 0 ].id + "' is displayed" );
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)
})
- .exec( 5, function() {
+ .exec( 1.5, function() {
equals( processingDiv.children[ 0 ].style.display, "none", "canvas '" + processingDiv.children[ 0 ].id + "' is hidden" );
plus();
- equals( processingDiv2.children[ 0 ].style.display, "inline", "canvas '" + processingDiv.children[ 0 ].id + "' is displayed" );
+ equals( processingDiv2.children[ 0 ].style.display, "inline", "canvas '" + processingDiv2.children[ 0 ].id + "' is displayed" );
plus();
- equals( processingDiv3.children[ 0 ].style.display, "inline", "canvas '" + processingDiv.children[ 0 ].id + "' is displayed" );
+ equals( processingDiv3.children[ 0 ].style.display, "none", "canvas '" + processingDiv3.children[ 0 ].id + "' is hidden" );
plus();
- }).exec( 7.5, function() {
+ }).exec( 3.5, function() {
equals( processingDiv.children[ 0 ].style.display, "none", "canvas '" + processingDiv.children[ 0 ].id + "' is hidden" );
plus();
- equals( processingDiv2.children[ 0 ].style.display, "none", "canvas '" + processingDiv.children[ 0 ].id + "' is hidden" );
+ equals( processingDiv2.children[ 0 ].style.display, "none", "canvas '" + processingDiv2.children[ 0 ].id + "' is hidden" );
plus();
- equals( processingDiv3.children[ 0 ].style.display, "none", "canvas '" + processingDiv.children[ 0 ].id + "' is hidden" );
+ equals( processingDiv3.children[ 0 ].style.display, "inline", "canvas '" + processingDiv3.children[ 0 ].id + "' is displayed" );
plus();
});