From a0a2948e5a71468477e8515be80ba4760e156b0d Mon Sep 17 00:00:00 2001 From: ScottDowne Date: Mon, 16 May 2011 16:13:18 -0400 Subject: [PATCH] [#t383] unit tests and major code cleanup --- plugins/gml/gmlplayer.js | 103 ----------------- plugins/gml/popcorn.gml.html | 36 +++--- plugins/gml/popcorn.gml.js | 179 +++++++++++++++++++++++++----- plugins/gml/popcorn.gml.unit.html | 43 +++++++ plugins/gml/popcorn.gml.unit.js | 67 +++++++++++ 5 files changed, 283 insertions(+), 145 deletions(-) delete mode 100644 plugins/gml/gmlplayer.js create mode 100644 plugins/gml/popcorn.gml.unit.html create mode 100644 plugins/gml/popcorn.gml.unit.js diff --git a/plugins/gml/gmlplayer.js b/plugins/gml/gmlplayer.js deleted file mode 100644 index 451cf30..0000000 --- a/plugins/gml/gmlplayer.js +++ /dev/null @@ -1,103 +0,0 @@ -var _stroke = 0; // current stroke -var onPt = 0; // current point on stroke -var onStroke = 0; // current point on stroke -var start = +(new Date()); -var x, y, rotation = false; -var strokes=0; -var data = {}; -var toggle = 0; -var ready = false; -var play = function(){}; -var dur = 0; -var options = {}; -var video = document.getElementById("video"); - -// empty on purpose -void setup() {} -void draw() { - - play(); -} - -void construct( data, options ) { - - size(640,640); - frameRate(60); - smooth(); - reset(); - noLoop(); - - dur = (options.end - options.start); - drawingDur = (options.endDrawing - options.start); - - this.options = options; - - var si = setInterval(function() { - - if (!data) return; - clearInterval(si); - - this.data = data; - strokes = this.data.gml.tag.drawing.stroke; - var tag = this.data.gml.tag; - var app_name = tag.header && tag.header.client && tag.header.client.name; - rotation = app_name == 'Graffiti Analysis 2.0: DustTag' || - app_name == 'DustTag: Graffiti Analysis 2.0' || - app_name == 'Fat Tag - Katsu Edition'; - - - play = function() { - - if ( video.currentTime < options.endDrawing ) { - seek( ( video.currentTime - options.start ) / dur * strokes.pt.length ); - } - - }; - }, 50); -} - - -void reset() { - background( 0 ); - onPt = onStroke = 0; - x = y = null; -} - -function drawLine(x,y,x2,y2) { - _x = rotation ? y*height : x*width; - _y = rotation ? width-(x*width) : y*height; - _x2 = rotation ? y2 * height : x2*width; - _y2 = rotation ? width - (x2 * width) : y2*height; - stroke(0); - strokeWeight(13); - strokeCap(SQUARE); - line(_x,_y,_x2,_y2); - stroke(255); - strokeWeight(12); - strokeCap(ROUND); - line(_x,_y,_x2,_y2); - //ol = { x: _x, y: _y, x2: _x2, y2: _y2 }; -} - -function seek( point ) { - if ( point < onPt ) reset(); - - var start; var hyp; - while (onPt <= point) { - if (!strokes) return; - _stroke = strokes.length ? strokes[onStroke] : strokes; - pt = _stroke.pt[onPt]; - var p = onPt; - if (x !== null ) drawLine(x,y,pt.x,pt.y); - /*if (onPt) { -if (++onPt >= _stroke.pt.length) { -//if (!strokes.length || ++onStroke >= strokes.length) return reset(); -//onPt = 0; -} -}*/ - x = pt.x; - y = pt.y; - if (onPt==p) onPt++; //went back to 0 - } -} - diff --git a/plugins/gml/popcorn.gml.html b/plugins/gml/popcorn.gml.html index 8a19bc9..ad607fd 100644 --- a/plugins/gml/popcorn.gml.html +++ b/plugins/gml/popcorn.gml.html @@ -1,7 +1,7 @@ - Popcorn attribution Plug-in Demo + Popcorn Grafiti Markup Language Plug-in Demo @@ -12,20 +12,22 @@ .volume(0) .gml({ start: 0, // seconds - end: 10, // seconds + end: 5, // seconds gmltag: '29582', - target: 'attribdiv' + target: 'gmldiv' + }) + .gml({ + start: 10, // seconds + end: 20, // seconds + gmltag: '155', + target: 'gmldiv' + }) + .gml({ + start: 20, // seconds + end: 40, // seconds + gmltag: '161', + target: 'gmldiv' }); - /*.attribution({ - start: 5, // seconds - end: 15, // seconds - nameofwork: "Internet", - nameofworkurl:"http://www.archive.org/details/CC1232_internet", - copyrightholder:"The Computer Chronicles", - license:"CC-BY-NC-ND", - licenseurl: "http://creativecommons.org/licenses/by-nc-nd/2.0/", - target: 'attribdiv' - } )*/ }, false); @@ -33,8 +35,9 @@

Popcorn Attribution Plug-in Demo

-

A attribution displaying 'A Shared Culture, Jesse Dylan, license: CC-BY-N6' will appear at 0 seconds and disappear at 10 seconds.

-

A attribution displaying 'Internet, The Computer Chronicles' will appear at 5 seconds and disappear at 15 seconds.

+

A Processing canvas displaying the GML tag 29582 will appear at 0-5 seconds.

+

A Processing canvas displaying the GML tag 155 will appear at 10-20 seconds.

+

A Processing canvas displaying the GML tag 161 will appear at 20-40 seconds.

-
-
+
diff --git a/plugins/gml/popcorn.gml.js b/plugins/gml/popcorn.gml.js index aa12d19..d9ea26b 100644 --- a/plugins/gml/popcorn.gml.js +++ b/plugins/gml/popcorn.gml.js @@ -2,18 +2,113 @@ (function (Popcorn) { - var processingLoaded = false; - - var ajax = function ajax(url) { - - var xhr = new XMLHttpRequest(); - xhr.open("GET", url, false); - xhr.setRequestHeader("If-Modified-Since", "Fri, 1 Jan 1960 00:00:00 GMT"); - xhr.send(null); - // failed request? - if (xhr.status !== 200 && xhr.status !== 0) { throw ("XMLHttpRequest failed, status code " + xhr.status); } - return xhr.responseText; - }; + var processingLoaded = false, + gmlPlayer = function( $p ) { + + var _stroke = 0, + onPt = 0, + onStroke = 0, + x = null, + y = null, + rotation = false, + strokes = 0, + play = function() {}, + reset = function() { + + $p.background( 0 ); + onPt = onStroke = 0; + x = y = null; + }, + drawLine = function( x, y, x2, y2 ) { + + if ( rotation ) { + + _x = y * $p.height; + _y = $p.width - ( x * $p.width ); + _x2 = y2 * $p.height; + _y2 = $p.width - ( x2 * $p.width ); + } else { + + _x = x * $p.width; + _y = y * $p.height; + _x2 = x2 * $p.width; + _y2 = y2 * $p.height; + } + $p.stroke( 0 ); + $p.strokeWeight( 13 ); + $p.strokeCap( $p.SQUARE ); + $p.line( _x, _y, _x2, _y2 ); + $p.stroke( 255 ); + $p.strokeWeight( 12 ); + $p.strokeCap( $p.ROUND ); + $p.line( _x, _y, _x2, _y2 ); + }, + seek = function( point ) { + + if ( point < onPt ) reset(); + + while ( onPt <= point ) { + + if ( !strokes ) return; + _stroke = strokes[ onStroke ] || strokes; + var pt = _stroke.pt[ onPt ]; + var p = onPt; + if (x !== null ) drawLine( x, y, pt.x, pt.y ); + + x = pt.x; + y = pt.y; + if ( onPt === p ) onPt++; + } + }; + + $p.draw = function() { + + play(); + }; + $p.setup = function() {}; + $p.construct = function( media, data, options ) { + + $p.size( 640,640 ); + $p.frameRate( 60 ); + $p.smooth(); + reset(); + $p.noLoop(); + + var si = setInterval(function() { + + if ( !data ) return; + clearInterval( si ); + + strokes = data.gml.tag.drawing.stroke; + + var drawingDur = ( options.end - options.start ) / ( strokes.pt || (function( strokes ) { + + var rStrokes = []; + + for ( var i = 0, sl = strokes.length; i < sl; i++ ) { + + rStrokes = rStrokes.concat( strokes[ i ].pt ); + } + + return rStrokes; + })( strokes ) ).length; + + var tag = data.gml.tag; + var app_name = tag.header && tag.header.client && tag.header.client.name; + rotation = app_name === 'Graffiti Analysis 2.0: DustTag' || + app_name === 'DustTag: Graffiti Analysis 2.0' || + app_name === 'Fat Tag - Katsu Edition'; + + play = function() { + + if ( media.currentTime < options.endDrawing ) { + + seek( ( media.currentTime - options.start ) / drawingDur ); + } + }; + }, 50); + }; + }; Popcorn.getScript( "http://processingjs.org/content/download/processing-js-1.1.0/processing-1.1.0.js", function() { @@ -21,28 +116,46 @@ }); /** -*/ + * Grafiti markup Language (GML) popcorn plug-in + * Renders a GML tag inside an HTML element + * Options parameter will need a mandatory start, end, target, gmltag. + * Optional parameters: none. + * Start is the time that you want this plug-in to execute + * End is the time that you want this plug-in to stop executing + * Target is the id of the document element that you wish to render the grafiti in + * gmltag is the numerical reference to a gml tag via 000000book.com + * @param {Object} options + * + * Example: + var p = Popcorn('#video') + .gml({ + start: 0, // seconds + end: 5, // seconds + gmltag: '29582', + target: 'gmldiv' + }); + * + */ Popcorn.plugin( "gml" , { - - /** -*/ + _setup : function( options ) { var self = this; options.endDrawing = options.endDrawing || options.end; + // create a canvas to put in the target div - options.container = document.createElement( 'canvas' ); + options.container = document.createElement( "canvas" ); options.container.style.display = "none"; - options.container.setAttribute( 'id', 'canvas' + options.gmltag ); + options.container.setAttribute( "id", "canvas" + options.gmltag ); if ( document.getElementById( options.target ) ) { document.getElementById( options.target ).appendChild( options.container ); } - // makes sure both processing.js and the gml player are loaded + // makes sure both processing.js and the gml data are loaded var readyCheck = setInterval(function() { if ( !processingLoaded ) { @@ -50,29 +163,45 @@ return; } - clearInterval(readyCheck); + clearInterval( readyCheck ); Popcorn.getJSONP( "http://000000book.com/data/" + options.gmltag + ".json?callback=", function( data ) { - new Processing( options.container, ajax( "gmlplayer.js" ) ); - options.pjsInstance = Processing.getInstanceById( 'canvas' + data.id ); - options.pjsInstance.construct( data, options ); + new Processing( options.container, gmlPlayer ); + options.pjsInstance = Processing.getInstanceById( "canvas" + data.id ); + options.pjsInstance.construct( self.media, data, options ); options._running && options.pjsInstance.loop(); }, false ); }, 5); }, /** -*/ + * @member gml + * The start function will be executed when the currentTime + * of the video reaches the start time provided by the + * options variable + */ start: function( event, options ) { options.pjsInstance && options.pjsInstance.loop(); options.container.style.display = "block"; }, /** -*/ + * @member gml + * The end function will be executed when the currentTime + * of the video reaches the end time provided by the + * options variable + */ end: function( event, options ) { options.pjsInstance && options.pjsInstance.noLoop(); options.container.style.display = "none"; + }, + _teardown: function( options ) { + + options.pjsInstance && options.pjsInstance.exit(); + + if ( document.getElementById( options.target ) ) { + document.getElementById( options.target ).removeChild( options.container ); + } } }); })( Popcorn ); diff --git a/plugins/gml/popcorn.gml.unit.html b/plugins/gml/popcorn.gml.unit.html new file mode 100644 index 0000000..c0761c5 --- /dev/null +++ b/plugins/gml/popcorn.gml.unit.html @@ -0,0 +1,43 @@ + + + + Popcorn GML Plugin + + + + + + + + + + +

Popcorn GML Plugin

+

+
+

+
    +
    + + +
    + + diff --git a/plugins/gml/popcorn.gml.unit.js b/plugins/gml/popcorn.gml.unit.js new file mode 100644 index 0000000..63676b9 --- /dev/null +++ b/plugins/gml/popcorn.gml.unit.js @@ -0,0 +1,67 @@ +test("Popcorn GML Plugin", function () { + + var popped = Popcorn("#video"), + expects = 8, + count = 0, + setupId, + gmldiv = document.getElementById('gmldiv'); + + expect(expects); + + function plus() { + if ( ++count===expects) { + start(); + } + } + + stop( 10000 ); + + ok ('gml' in popped, "gml is a method of the popped instance"); + plus(); + + equals ( gmldiv.childElementCount, 0, "initially, there is nothing inside the gmldiv" ); + plus(); + + popped.gml({ + start: 0, // seconds + end: 1, // seconds + gmltag: '29582', + target: 'gmldiv' + }) + .gml({ + start: 1, // seconds + end: 2, // seconds + gmltag: '155', + target: 'gmldiv' + }).volume( 0 ); + + setupId = popped.getLastTrackEventId(); + + popped.exec( 0, function() { + equals( gmldiv.children[0].style.display , "block", "first GML is visible on the page" ); + plus(); + }); + + popped.exec( 1, function() { + equals( gmldiv.children[1].style.display , "block", "second GML is visible on the page" ); + plus(); + }); + + popped.exec( 2, function() { + equals( gmldiv.children[1].style.display , "none", "second GML is no longer visible on the page" ); + plus(); + equals( gmldiv.children[0].style.display , "none", "first GML is no longer visible on the page" ); + plus(); + + popped.pause().removeTrackEvent( setupId ); + + ok( !Processing.getInstanceById( "canvas155" ), "Processing insteance was properly destroyed" ); + plus(); + + ok( !gmldiv.children[1], "canvas element was properly removed" ); + plus(); + }); + + popped.play(); + +}); -- 2.43.0