--- /dev/null
- }
+ // PLUGIN: GML
+
+ (function (Popcorn) {
+
+ 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 ) {
+
+ var _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 ) {
+
+ ( point < onPt ) && reset();
+
+ while ( onPt <= point ) {
+
+ if ( !strokes ) {
+ return;
+ }
+
+ _stroke = strokes[ onStroke ] || strokes;
+ var pt = _stroke.pt[ onPt ],
+ p = onPt;
+ x != null && drawLine( x, y, pt.x, pt.y );
+
+ x = pt.x;
+ y = pt.y;
+ ( onPt === p ) && onPt++;
+ }
+ };
+
+ $p.draw = function() {
+
+ play();
+ };
+ $p.setup = function() {};
+ $p.construct = function( media, data, options ) {
+
+ var dataReady = function() {
+
+ if ( data ) {
+
+ 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,
+ 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 );
+ }
+ };
+
+ return;
+ }
+
+ setTimeout( dataReady, 5 );
+ };
+
+ $p.size( 640, 640 );
+ $p.frameRate( 60 );
+ $p.smooth();
+ reset();
+ $p.noLoop();
+
+ dataReady();
+ };
+ };
+
+ Popcorn.getScript( "http://processingjs.org/content/download/processing-js-1.1.0/processing-1.1.0.js", function() {
+
+ processingLoaded = true;
+ });
+
+ /**
+ * 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.style.display = "none";
+ options.container.setAttribute( "id", "canvas" + options.gmltag );
+
+ document.getElementById( options.target ) && document.getElementById( options.target ).appendChild( options.container );
+
+ // makes sure both processing.js and the gml data are loaded
+ var readyCheck = function() {
+
+ if ( processingLoaded ) {
+ Popcorn.getJSONP( "http://000000book.com/data/" + options.gmltag + ".json?callback=", function( data ) {
+
+ options.pjsInstance = new Processing( options.container, gmlPlayer );
+ options.pjsInstance.construct( self.media, data, options );
+ options._running && options.pjsInstance.loop();
+ }, false );
+
+ return;
+ }
+
+ setTimeout( readyCheck, 5 );
++ };
+
+ readyCheck();
+ },
+ /**
+ * @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();
+ document.getElementById( options.target ) && document.getElementById( options.target ).removeChild( options.container );
+ }
+ });
+ })( Popcorn );