From: ScottDowne Date: Tue, 17 May 2011 01:27:23 +0000 (-0400) Subject: [#383] review cleanup, leaky vars, setInteval to setTimeout X-Git-Tag: v0.6~2^2~5^2~1 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=58eb92eb5e30e95cac90efdc314a5e8cb9e381c2;p=popcorn-js.git [#383] review cleanup, leaky vars, setInteval to setTimeout --- diff --git a/plugins/gml/popcorn.gml.html b/plugins/gml/popcorn.gml.html index ad607fd..9d46f73 100644 --- a/plugins/gml/popcorn.gml.html +++ b/plugins/gml/popcorn.gml.html @@ -34,7 +34,7 @@ -

Popcorn Attribution Plug-in Demo

+

Popcorn GML Plug-in Demo

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 5491afa..26512dc 100644 --- a/plugins/gml/popcorn.gml.js +++ b/plugins/gml/popcorn.gml.js @@ -5,15 +5,15 @@ var processingLoaded = false, gmlPlayer = function( $p ) { - var _stroke = 0, - onPt = 0, + var _stroke = 0, + onPt = 0, onStroke = 0, - x = null, - y = null, + x = null, + y = null, rotation = false, - strokes = 0, - play = function() {}, - reset = function() { + strokes = 0, + play = function() {}, + reset = function() { $p.background( 0 ); onPt = onStroke = 0; @@ -21,6 +21,8 @@ }, drawLine = function( x, y, x2, y2 ) { + var _x, _y, _x2, _y2; + if ( rotation ) { _x = y * $p.height; @@ -34,6 +36,7 @@ _x2 = x2 * $p.width; _y2 = y2 * $p.height; } + $p.stroke( 0 ); $p.strokeWeight( 13 ); $p.strokeCap( $p.SQUARE ); @@ -52,10 +55,11 @@ if ( !strokes ) { return; } + _stroke = strokes[ onStroke ] || strokes; - var pt = _stroke.pt[ onPt ]; - var p = onPt; - x && drawLine( x, y, pt.x, pt.y ); + var pt = _stroke.pt[ onPt ], + p = onPt; + x != null && drawLine( x, y, pt.x, pt.y ); x = pt.x; y = pt.y; @@ -70,47 +74,52 @@ $p.setup = function() {}; $p.construct = function( media, data, options ) { - $p.size( 640,640 ); - $p.frameRate( 60 ); - $p.smooth(); - reset(); - $p.noLoop(); + var dataReady = function() { - var si = setInterval(function() { + if ( data ) { - if ( !data ) { - return; - } - clearInterval( si ); + strokes = data.gml.tag.drawing.stroke; - strokes = data.gml.tag.drawing.stroke; + var drawingDur = ( options.end - options.start ) / ( strokes.pt || (function( strokes ) { - var drawingDur = ( options.end - options.start ) / ( strokes.pt || (function( strokes ) { + var rStrokes = []; - var rStrokes = []; + for ( var i = 0, sl = strokes.length; i < sl; i++ ) { - for ( var i = 0, sl = strokes.length; i < sl; i++ ) { + rStrokes = rStrokes.concat( strokes[ i ].pt ); + } - rStrokes = rStrokes.concat( strokes[ i ].pt ); - } + return rStrokes; + })( strokes ) ).length; - return rStrokes; - })( strokes ) ).length; + var tag = data.gml.tag, + app_name = tag.header && tag.header.client && tag.header.client.name; - 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'; + rotation = app_name === 'Graffiti Analysis 2.0: DustTag' || + app_name === 'DustTag: Graffiti Analysis 2.0' || + app_name === 'Fat Tag - Katsu Edition'; - play = function() { + play = function() { - if ( media.currentTime < options.endDrawing ) { + if ( media.currentTime < options.endDrawing ) { - seek( ( media.currentTime - options.start ) / drawingDur ); - } - }; - }, 50); + seek( ( media.currentTime - options.start ) / drawingDur ); + } + }; + + return; + } + + setTimeout( dataReady, 5 ); + }; + + $p.size( 640, 640 ); + $p.frameRate( 60 ); + $p.smooth(); + reset(); + $p.noLoop(); + + dataReady(); }; }; @@ -157,21 +166,23 @@ document.getElementById( options.target ) && document.getElementById( options.target ).appendChild( options.container ); // makes sure both processing.js and the gml data are loaded - var readyCheck = setInterval(function() { + var readyCheck = function() { + + if ( processingLoaded ) { + Popcorn.getJSONP( "http://000000book.com/data/" + options.gmltag + ".json?callback=", function( data ) { - if ( !processingLoaded ) { + options.pjsInstance = new Processing( options.container, gmlPlayer ); + options.pjsInstance.construct( self.media, data, options ); + options._running && options.pjsInstance.loop(); + }, false ); return; } - clearInterval( readyCheck ); - Popcorn.getJSONP( "http://000000book.com/data/" + options.gmltag + ".json?callback=", function( data ) { + setTimeout( readyCheck, 5 ); + } - options.pjsInstance = new Processing( options.container, gmlPlayer ); - options.pjsInstance.construct( self.media, data, options ); - options._running && options.pjsInstance.loop(); - }, false ); - }, 5); + readyCheck(); }, /** * @member gml