<html>
<head>
- <title>Popcorn processing Plug-in Demo</title>
+ <title>Popcorn processing Plug-in Simple Demo</title>
<!--<script src="./processing.js"></script> -->
<script src="../../popcorn.js"></script>
<script src="popcorn.processing.js"></script>
document.addEventListener('DOMContentLoaded', function () {
var p = Popcorn('#video')
.play()
- .processing({ // Div with sketch defined as a string
- start: 1, // seconds
- end: 5, // seconds
- control: 'pause',
+ .processing({
+ start: 2,
+ end: 10,
target: 'processing-div',
- sketch: 'test2.pjs'
+ sketch: 'test.pjs'
} )
- .processing({ // Div with sketch defined here
- start: 5, // seconds
- end: 10, // seconds
- control: 'pause',
+ .processing({
+ start: 10,
+ end: 15,
target: 'processing-div2',
- sketch: 'test.pjs'
+ sketch: 'test2.pjs'
} )
- .processing({ // Canvas with sketch hard-coded defined
- start: 10, // seconds
- end: 15, // seconds
- control: 'pause',
- sketch: 'test2.pjs',
- target: 'processing-canvas2', // we don't need a sketch because we are pausing a canvas that is hard coded
- } )
- .processing({ // Div with sketch defined here
- start: 20, // seconds
- end: 25, // seconds
- control: 'pause',
- sketch: 'test2.pjs',
- target: 'processing-div2', // Thest mulitple instructions for one sketch - Especially relevant here since the first time we are building a canvas
+ .processing({
+ start: 15,
+ end: 20,
+ sketch: 'test.pjs',
+ target: 'processing-div2',
} )
+ window.popped = p;
}, false);
</script>
</head>
<body>
- <h1 id="qunit-header">Popcorn processing Plug-in Demo</h1>
+ <h1 id="qunit-header">Popcorn processing Plug-in Simple Demo</h1>
<div>
<video id='video'
</video>
</div>
<div id="processing-div" width="50%" height="50%">
+ <hr />
</div>
<div id="processing-div2" width="50%" height="50%">
</div>
- <canvas id="processing-canvas2" data-processing-sources="test2.pjs" width="100" height="100"></canvas></body>
-
- <div id="processing-div3" width="50%" height="50%">
- </div>
+ </body>
</html>
(function (Popcorn) {
- var scriptLoadingFired = false,
- canvas,
- parentTarget,
- processingInstance,
- processingLoaded = false,
-
- loadProcessing = function() {
- if ( !scriptLoadingFired ) {
- if ( window.Processing ) {
- processingLoaded = true;
- return;
- }
- scriptLoadingFired = true;
- Popcorn.getScript ( "http://processingjs.org/content/download/processing-js-1.2.1/processing-1.2.1.js" );
- }
- };
-
- Popcorn.plugin( "processing" , function( options ) {
-
- var sketchInstance,
+ var processingLoaded = false,
- toggle = function( on ) {
- if ( canvas ) {
+ toggle = function( on, options ) {
+ var pcorn = options.popcornInstance,
+ instance = options.pjsInstance,
+ canvas = options.canvas;
+ if ( canvas && options.isReady ) {
if ( on ) {
canvas.style.display = "inline";
- processingInstance.loop();
+ !pcorn.media.paused && instance.loop();
} else {
canvas.style.display = "none";
- processingInstance.noLoop();
+ instance.noLoop();
}
} else {
setTimeout ( function() {
- toggle ( on );
+ toggle ( 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();
+ Popcorn.plugin( "processing" , function ( options ) {
+
+ var init = function( context ) {
+ 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.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() {
+ if ( codeReady && window.Processing ) {
+ options.pjsInstance = new Processing( options.canvas, processingCode );
+ popcorn.listen( "pause", function () {
+ if ( options.canvas.style.display === "inline" ) {
+ options.pjsInstance.noLoop();
+ }
+ } );
+ popcorn.listen( "play", function() {
+ if ( options.canvas.style.display === "inline" ) {
+ options.pjsInstance.loop();
+ }
+ });
+ options.isReady = true;
+ } else {
+ setTimeout ( initProcessing, 10 );
+ }
+ };
+ };
+
return {
manifest: {
},
_setup: function( options ) {
- var types = { "pause": true, "function": true },
- processingCode, codeReady = false, createCanvas,
-
- init = function() {
- if ( window.Processing ) {
- parentTarget = document.getElementById( options.target );
- if ( parentTarget.tagName === "CANVAS" ) {
- canvas = parentTarget;
- } else if ( parentTarget.tagName === "DIV" ) {
- canvas = document.createElement( "canvas" );
- // +new Date() is used here to create unique id's for canvas' within the same div.
- canvas.id = options.target + "Sketch" + +new Date();
- canvas.setAttribute( "data-processing-sources", options.sketch );
- canvas.style.display = "none";
- parentTarget.appendChild( canvas );
- }
- Popcorn.xhr({
- url: options.sketch,
- dataType: "text",
- success: function( responseCode ) {
- codeReady = true;
- processingCode = responseCode;
- }
- });
-
- createCanvas = function() {
- if ( codeReady ) {
- processingInstance = new Processing( canvas, processingCode );
- } else {
- setTimeout ( createCanvas, 10 );
- }
- };
- createCanvas();
- } else {
- !processingLoaded && loadProcessing();
- setTimeout( init, 10 );
- }
- };
-
- init();
+
+ var processingCode,
+ codeReady = false,
+ self = this;
+
+ readyCheck = function() {
+ if ( !processingLoaded ) {
+ load();
+ }
+ },
+ readyCheck();
+ init( self );
+ options.popcornInstance = this;
},
start: function( event, options ) {
- toggle ( true );
+ toggle ( true, options );
},
end: function( event, options ) {
- toggle ( false );
+ toggle ( false, options );
}
};
});
-// All Examples Written by Casey Reas and Ben Fry
-// unless otherwise stated.
-float a = 100;
+// All Examples Written by Casey Reas and Ben Fry
+// unless otherwise stated.
+ float baseX1, baseY1, baseX2, baseY2;
+float baseLength;
+float[] xCoords, yCoords;
+float ellipseX, ellipseY, ellipseRadius = 6;
+float directionX, directionY;
+float ellipseSpeed = 3.5;
+float velocityX, velocityY;
+
+void setup(){
+ size(200, 200);
+ frameRate(30);
+ fill(128);
+ smooth();
+ baseX1 = 0;
+ baseY1 = height-150;
+ baseX2 = width;
+ baseY2 = height;
+
+ // start ellipse at middle top of screen
+ ellipseX = width/2;
-void setup()
-{
- size(200, 200);
- stroke(255);
- frameRate(30);
-}
-
-void draw()
-{
- background(51);
- a = a - 1;
- if (a < 0) {
- a = height;
- }
- line(0, a, width, a);
-}
+ // calculate initial random direction
+ directionX = random(0.1, 0.99);
+ directionY = random(0.1, 0.99);
+
+ // normalize direction vector
+ float directionVectLength = sqrt(directionX*directionX +
+ directionY*directionY);
+ directionX /= directionVectLength;
+ directionY /= directionVectLength;
+}
+
+void draw(){
+ // draw background
+ fill(0, 12);
+ noStroke();
+ rect(0, 0, width, height);
+
+ // calculate length of base top
+ baseLength = dist(baseX1, baseY1, baseX2, baseY2);
+ xCoords = new float[ceil(baseLength)];
+ yCoords = new float[ceil(baseLength)];
+
+ // fill base top coordinate array
+ for (int i=0; i<xCoords.length; i++){
+ xCoords[i] = baseX1 + ((baseX2-baseX1)/baseLength)*i;
+ yCoords[i] = baseY1 + ((baseY2-baseY1)/baseLength)*i;
+ }
+
+ // draw base
+ fill(200);
+ quad(baseX1, baseY1, baseX2, baseY2, baseX2, height, 0, height);
+
+ // calculate base top normal
+ float baseDeltaX = (baseX2-baseX1)/baseLength;
+ float baseDeltaY = (baseY2-baseY1)/baseLength;
+ float normalX = -baseDeltaY;
+ float normalY = baseDeltaX;
+
+ // draw ellipse
+ noFill();
+ stroke(200);
+ ellipse(ellipseX, ellipseY, ellipseRadius*2, ellipseRadius*2);
+
+ // calculate ellipse velocity
+ velocityX = directionX * ellipseSpeed;
+ velocityY = directionY * ellipseSpeed;
+
+ // move elipse
+ ellipseX += velocityX;
+ ellipseY += velocityY;
+
+ // normalized incidence vector
+ float incidenceVectorX = -directionX;
+ float incidenceVectorY = -directionY;
+
+ // detect and handle collision
+ for (int i=0; i<xCoords.length; i++){
+ // check distance between ellipse and base top coordinates
+ if (dist(ellipseX, ellipseY, xCoords[i], yCoords[i]) < ellipseRadius){
+
+ // calculate dot product of incident vector and base top normal
+ float dot = incidenceVectorX*normalX + incidenceVectorY*normalY;
+
+ // calculate reflection vector
+ float reflectionVectorX = 2*normalX*dot - incidenceVectorX;
+ float reflectionVectorY = 2*normalY*dot - incidenceVectorY;
+
+ // assign reflection vector to direction vector
+ directionX = reflectionVectorX;
+ directionY = reflectionVectorY;
+
+ // draw base top normal at collision point
+ stroke(255, 128, 0);
+ line(ellipseX, ellipseY, ellipseX-normalX*100,
+ ellipseY-normalY*100);
+ }
+ }
+
+ // detect boundary collision
+ // right
+ if (ellipseX > width-ellipseRadius){
+ ellipseX = width-ellipseRadius;
+ directionX *= -1;
+ }
+ // left
+ if (ellipseX < ellipseRadius){
+ ellipseX = ellipseRadius;
+ directionX *= -1;
+ }
+ // top
+ if (ellipseY < ellipseRadius){
+ ellipseY = ellipseRadius;
+ directionY *= -1;
+ // randomize base top
+ baseY1 = random(height-100, height);
+ baseY2 = random(height-100, height);
+ }
+}
frequency[i]= random(-9, 9);
cellRadius[i] = random(16, 30);
}
- frameRate(30);
+ frameRate(15);
noLoop();
}