From 3dd0e3cd8466d641fb5a0cc4739a6d5b44394d4b Mon Sep 17 00:00:00 2001 From: David Humphrey Date: Wed, 16 Feb 2011 14:07:09 -0500 Subject: [PATCH] Added code plugin [ticket #346]. --- plugins/code/popcorn.code.html | 82 +++++++++++++ plugins/code/popcorn.code.js | 173 ++++++++++++++++++++++++++++ plugins/code/popcorn.code.unit.html | 47 ++++++++ plugins/code/popcorn.code.unit.js | 74 ++++++++++++ 4 files changed, 376 insertions(+) create mode 100644 plugins/code/popcorn.code.html create mode 100644 plugins/code/popcorn.code.js create mode 100644 plugins/code/popcorn.code.unit.html create mode 100644 plugins/code/popcorn.code.unit.js diff --git a/plugins/code/popcorn.code.html b/plugins/code/popcorn.code.html new file mode 100644 index 0000000..1533564 --- /dev/null +++ b/plugins/code/popcorn.code.html @@ -0,0 +1,82 @@ + + + + Popcorn Code Plugin Demo + + + + + + +

Popcorn Code Plugin Demo

+

Arbitrary JavaScript functions are run.

+
+ +
+
+
Code Plugin Output
+
+
+ + diff --git a/plugins/code/popcorn.code.js b/plugins/code/popcorn.code.js new file mode 100644 index 0000000..a9456ae --- /dev/null +++ b/plugins/code/popcorn.code.js @@ -0,0 +1,173 @@ +// PLUGIN: Code + +(function (Popcorn) { + + /** + * Code Popcorn Plug-in + * + * Adds the ability to run arbitrary code (JavaScript functions) according to video timing. + * + * @param {Object} options + * + * Required parameters: start, end, template, data, and target. + * Optional parameter: static. + * + * start: the time in seconds when the mustache template should be rendered + * in the target div. + * + * end: the time in seconds when the rendered mustache template should be + * removed from the target div. + * + * onStart: the function to be run when the start time is reached. + * + * onFrame: [optional] a function to be run on each paint call + * (e.g., called ~60 times per second) between the start and end times. + * + * onEnd: [optional] a function to be run when the end time is reached. + * + * Example: + var p = Popcorn('#video') + + /* onStart function only + .code({ + start: 1, + end: 4, + onStart: function( options ) { + // called on start + } + }) + + /* onStart + onEnd only + .code({ + start: 6, + end: 8, + onStart: function( options ) { + // called on start + }, + onEnd: function ( options ) { + // called on end + } + }) + + /* onStart, onEnd, onFrame + .code({ + start: 10, + end: 14, + onStart: function( options ) { + // called on start + }, + onFrame: function ( options ) { + // called on every paint frame between start and end. + // uses mozRequestAnimationFrame, webkitRequestAnimationFrame, + // or setTimeout with 16ms window. + }, + onEnd: function ( options ) { + // called on end + } + }); + * + */ + + Popcorn.plugin( "code" , function() { + + function get( name, options ) { + return options._instance[name]; + } + + function set( name, options, value ) { + options._instance[name] = value; + } + + // Setup a proper frame interval function (60fps), favouring paint events. + var step = ( function() { + + var buildFrameRunner = function( runner ) { + var _runner = runner; + + return function( f, options ) { + + var _options = options; + var _f = function() { + f(); + if ( get( 'running', _options ) ) { + _runner( _f ); + } + }; + + _f(); + + }; + }; + + // Figure out which level of browser support we have for this + if ( window.webkitRequestAnimationFrame ) { + return buildFrameRunner( window.webkitRequestAnimationFrame ); + } else if ( window.mozRequestAnimationFrame ) { + return buildFrameRunner( window.mozRequestAnimationFrame ); + } else { + return buildFrameRunner( function( f ) { + window.setTimeout( f, 16 ); + } ); + } + + } )(); + + + return { + manifest: { + about: { + name: "Popcorn Code Plugin", + version: "0.1", + author: "David Humphrey (@humphd)", + website: "http://vocamus.net/dave" + }, + options: { + start: {elem:'input', type:'text', label:'In'}, + end: {elem:'input', type:'text', label:'Out'}, + // TODO: how to deal with functions, eval strings? + onStart: {elem:'input', type:'text', label:'onStart'}, + onFrame: {elem:'input', type:'text', label:'onFrame'}, + onEnd: {elem:'input', type:'text', label:'onEnd'}, + } + }, + + _setup : function( options ) { + if ( !options.onStart || !( typeof options.onStart === 'function' ) ) { + throw "Popcorn Code Plugin Error: onStart must be a function."; + } + + if ( options.onEnd && !( typeof options.onEnd === 'function' ) ) { + throw "Popcorn Code Plugin Error: onEnd must be a function."; + } + + if ( options.onFrame && !( typeof options.onFrame === 'function' ) ) { + throw "Popcorn Code Plugin Error: onFrame must be a function."; + } + + options._instance = { running: false }; + }, + + start: function( event, options ) { + options.onStart( options ); + + if ( options.onFrame ) { + set( 'running', options, true ); + step( options.onFrame, options ); + } + }, + + end: function( event, options ) { + if ( options.onFrame ) { + set( 'running', options, false ); + } + + if ( options.onEnd ) { + options.onEnd( options ); + } + } + + }; + + }); + +})( Popcorn ); diff --git a/plugins/code/popcorn.code.unit.html b/plugins/code/popcorn.code.unit.html new file mode 100644 index 0000000..d67e8dc --- /dev/null +++ b/plugins/code/popcorn.code.unit.html @@ -0,0 +1,47 @@ + + + + Popcorn API + + + + + + + + + + + + +

Popcorn Code Plugin

+

+
+

+
    +
    + + +
    + + diff --git a/plugins/code/popcorn.code.unit.js b/plugins/code/popcorn.code.unit.js new file mode 100644 index 0000000..89d1cbd --- /dev/null +++ b/plugins/code/popcorn.code.unit.js @@ -0,0 +1,74 @@ +test('Popcorn Code Plugin', function () { + + var popped = Popcorn('#video'), + expects = 8, + count = 0, + frames = 0, + codeDiv = document.getElementById('code-div'); + + expect( expects ); + + function plus() { + if ( ++count === expects) { + start(); + } + } + + stop(); + + ok('code' in popped, 'code is a method of the popped instance'); + plus(); + + equals ( codeDiv.innerHTML, '', 'initially, there is nothing inside the code-div' ); + plus(); + + popped.code({ + start: 1, + end: 3, + onStart: function( options ) { + codeDiv.innerHTML = 'Test 1 - onStart (no onEnd)'; + ok(true, 'Test 1 onStart was run.'); + plus(); + } + }); + + popped.code({ + start: 5, + end: 8, + onStart: function( options ) { + codeDiv.innerHTML = 'Test 2 - onStart'; + ok(true, 'Test 2 onStart was run.'); + plus(); + }, + onEnd: function ( options ) { + codeDiv.innerHTML = 'Test 2 - onEnd'; + ok(true, 'Test 2 onEnd was run.'); + plus(); + } + }); + + popped.code({ + start: 10, + end: 14, + onStart: function( options ) { + codeDiv.innerHTML = 'Test 3 - onStart [Frames: '; + ok(true, 'Test 3 onStart was run.'); + plus(); + }, + onFrame: function ( options ) { + codeDiv.innerHTML += '.'; + frames++; + }, + onEnd: function ( options ) { + codeDiv.innerHTML += '] Test 3 - onEnd'; + ok(true, 'Test 3 onEnd was run.'); + plus(); + ok(frames > 100, 'Test 3 onFrames was run.'); + plus(); + popped.pause(); + } + }); + + popped.volume(0); + popped.play(); +}); -- 2.43.0