<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>
</div>
<div id="processing-div2" width="50%" height="50%">
</div>
+ <canvas id="processing-canvas"></canvas>
</body>
</html>
(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 ) {
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 );
equals( processingDiv3.innerHTML, "", "'processing-div-3' is empty" );
plus();
+
+ ok( processingCanvas, "'processing-canvas' is present in DOM" );
+ plus();
popped.processing({
start: 0,
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)
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();
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;