From e391ee3fc0c63fda1a615a09ed3728808e2b3408 Mon Sep 17 00:00:00 2001 From: Christopher De Cairos Date: Mon, 6 Jun 2011 17:24:13 -0400 Subject: [PATCH] fixed errors in plug-in... another re-write. sketches paus with the video now. --- plugins/processing/popcorn.processing.html | 47 +++---- plugins/processing/popcorn.processing.js | 145 +++++++++++---------- plugins/processing/test.pjs | 139 +++++++++++++++++--- plugins/processing/test2.pjs | 2 +- 4 files changed, 217 insertions(+), 116 deletions(-) diff --git a/plugins/processing/popcorn.processing.html b/plugins/processing/popcorn.processing.html index 8348c6c..7ccb928 100644 --- a/plugins/processing/popcorn.processing.html +++ b/plugins/processing/popcorn.processing.html @@ -1,7 +1,7 @@ - Popcorn processing Plug-in Demo + Popcorn processing Plug-in Simple Demo @@ -10,40 +10,31 @@ 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); -

Popcorn processing Plug-in Demo

+

Popcorn processing Plug-in Simple Demo

+
- - -
-
+ diff --git a/plugins/processing/popcorn.processing.js b/plugins/processing/popcorn.processing.js index 94128d2..3cab7dd 100644 --- a/plugins/processing/popcorn.processing.js +++ b/plugins/processing/popcorn.processing.js @@ -5,43 +5,81 @@ (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: { @@ -62,54 +100,27 @@ }, _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 ); } }; }); diff --git a/plugins/processing/test.pjs b/plugins/processing/test.pjs index 6729f19..4b3b6e3 100644 --- a/plugins/processing/test.pjs +++ b/plugins/processing/test.pjs @@ -1,20 +1,121 @@ -// 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 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); + } +} diff --git a/plugins/processing/test2.pjs b/plugins/processing/test2.pjs index 686692b..1a6ea55 100644 --- a/plugins/processing/test2.pjs +++ b/plugins/processing/test2.pjs @@ -30,7 +30,7 @@ void setup(){ frequency[i]= random(-9, 9); cellRadius[i] = random(16, 30); } - frameRate(30); + frameRate(15); noLoop(); } -- 2.43.0