public
Created

Tracking football in a html5 video

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
// Original source http://lusob.com/2012/02/tracking-a-football-match-with-html5-and-javascript/
(function( $ ){
 
function format(str) {
for (var i = 1; i < arguments.length; i++) {
str = str.replace('%' + (i - 1), arguments[i]);
}
return str;
}
 
function buildOptions(options) {
options = options || {};
options.image = options.image || "";
options.cycle = options.cycle || 1;
options.resetMargin = options.resetMargin || 0;
options.sensibility = options.sensibility || options.cycle * 0.35;
options.autoscrollspeed = options.autoscrollspeed || 500;
return options;
}
 
var methods = {
init : function( options ) {
 
methods.options = buildOptions(options);
var wrapper = null;
 
return this.each(function(){
 
var $pic = $(this);
data = $pic.data('360view');
 
// If the plugin hasn't been initialized yet
if ( ! data ) {
 
$(this).data('360view', {
target : $pic
});
}
 
$(function() {
wrapper = $('<div id="meta-product-img" style="background-position:0px 0px"></div>') ;
wrapper.css("background-image", format("url(%0)", methods.options.image);
wrapper.css( { height: $pic.height(), width: $pic.width(), overflow:"hidden", position:"relative" });
$pic.wrap(wrapper);
wrapper = $pic.parent();
 
$pic.hide();
 
wrapper.data("currentIndex",0).data("tempIndex",0);
var originalHeight = 440;
var originalWidth = 220;
var totalImgs = 16;
var imgPerCol = 4;
var imgPerRow = 4;
 
console.log(wrapper);
wrapper.mousemove( function(evt) {
if (!!wrapper.data("refTouchX") === false) {
wrapper.data("refTouchX",evt.pageX);
wrapper.data("refTouchY",evt.pageY);
wrapper.data("refLocX",parseInt($pic.css("left")));
wrapper.data("refLocY",parseInt($pic.css("top")));
}
 
evt.preventDefault()
 
var e = evt;
var distance = e.pageX - wrapper.data("refTouchX"); //distance hold the distance traveled with the finger so far..
stripeSize = Math.floor(originalWidth / totalImgs);
var newIndex = wrapper.data("currentIndex") + Math.floor(distance*options.sensibility/stripeSize)
if (newIndex < 0)
{
newIndex = totalImgs - 1;
wrapper.data("currentIndex",newIndex);
}
newIndex = newIndex % totalImgs;
if (newIndex == wrapper.data("currentIndex"))
return;
console.log("NewIndex post " + newIndex);
//wrapper.attr("src",imgArr[newIndex]);
var currentRow = Math.floor(newIndex/imgPerRow)
//currentCol = Math.floor((newIndex - 1)/imgPerRow)
var currentCol = newIndex % imgPerRow;
//console.log("Row " + currentRow + " Col " + currentCol);
 
if (currentRow > imgPerCol) currentRow = imgPerCol;
 
//console.log("In Mousemove " + format("%0 %1", -1 * originalWidth * currentCol, originalHeight * currentRow));
wrapper.css("background-position", format("%0px %1px", -1 * originalWidth * currentCol, originalHeight * currentRow));
wrapper.data("tempIndex",newIndex);
});
 
});
 
});
 
},
destroy : function( ) {
 
return this.each(function(){
 
var $this = $(this),
data = $this.data('360view');
 
$(window).unbind('.360view');
data.tooltip.remove();
$this.removeData('360view');
});
 
},
reposition : function( ) { // ...
},
show : function( ) { // ...
},
hide : function( ) { // ...
},
update : function( content ) { // ...
}
};
 
$.fn.productView = function( method ) {
 
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 2 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.product' );
}
 
};
 
})( jQuery );

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.