-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.beziernav.min.js
11 lines (10 loc) · 4.23 KB
/
jquery.beziernav.min.js
1
2
3
4
5
6
7
8
9
10
11
/*! jquery-bezier-navigation - Copyright 2015 Andrey <qRoC.Work@gmail.com> Savitskiy */
(function(l){function n(a){return 1>=a?1:a*n(a-1)}function r(a,b){this.x=a;this.y=b}function h(a,b,c){for(var d=0;d<c;d++)a.push(b)}function p(a,b){this.points=[];this.pivot_points=a;for(var c=0;c<1+b;c+=b){1<c&&(c=1);for(var d=new r(0,0),e=0;e<this.pivot_points.length;e++){var g=this.getBasis(e,this.pivot_points.length-1,c);d.x+=this.pivot_points[e][0]*g;d.y+=this.pivot_points[e][1]*g}this.points.push(d)}}function k(a,b){var c=l.extend({$parent:a,bezier_points:[],bezier_step:.01,bezier_render:!1,
menu:[],menu_item_correction:function(a,b){},menu_padding:5,menu_active_padding:15,menu_default_active:void 0},b),d=new p(c.bezier_points,c.bezier_step);c.bezier_render&&d.debugRender(c.$parent);this.road_points=d.getPoints();this.menu_items=t(c.menu,c.menu_item_correction);this.active=void 0;this.tasks=[];this.menu_padding=c.menu_padding;this.menu_active_padding=c.menu_active_padding;"undefined"!==typeof c.menu_default_active?this.setActiveById(c.menu_default_active):this.setActive(0);for(d=0;d<
this.menu_items.length;d++)c.$parent.append(this.menu_items[d].$el)}function t(a,b){var c=l("<div class='menu_item'/>");c.css({position:"absolute",top:0,left:0});for(var d=[],e=0;e<a.length;e++){var g=c.clone(!1),f=a[e];b(g,f);d.push({id:f,$el:g,point_n:-1})}return d}function q(a,b,c){0>c||c>=b.lenght||(a.point_n=c,b=b[a.point_n],a.$el.css({left:b.x-a.$el.width()/2,top:b.y-a.$el.height()/2}))}function u(a,b,c,d,e){e.push(void 0);var g=a.point_n,f=d?-1:1;requestAnimationFrame(function v(){q(a,b,a.point_n+
f);d&&g-a.point_n===c[0]?(g=a.point_n,c.shift()):d||g+c[0]!==a.point_n||(g=a.point_n,c.shift());c.length?requestAnimationFrame(v):e.pop()})}p.prototype.getBasis=function(a,b,c){return n(b)/(n(a)*n(b-a))*Math.pow(c,a)*Math.pow(1-c,b-a)};p.prototype.getPoints=function(){return this.points};p.prototype.debugRender=function(a){var b=l("<div />");b.css({position:"absolute",top:0,left:0,width:"1px",height:"1px",backgroundColor:"#000"});for(var c=0;c<this.points.length;c++){var d=b.clone(!1);d.css({left:this.points[c].x,
top:this.points[c].y});a.append(d)}for(c=0;c<this.pivot_points.length;c++)d=b.clone(!1),d.css({left:this.pivot_points[c][0],top:this.pivot_points[c][1],width:"4px",height:"4px",backgroundColor:"#ff0000"}),a.append(d)};k.prototype.menuSearchPosById=function(a){for(var b=0;b<this.menu_items.length;b++)if(this.menu_items[b].id==a)return b;throw Error("Bad element id");};k.prototype.$innerSampleMove=function(a){var b=Math.floor(this.road_points.length/2),c=b-this.menu_active_padding,d=b+this.menu_active_padding;
q(this.menu_items[a],this.road_points,b);for(b=a-1;0<=b;b--)q(this.menu_items[b],this.road_points,c),c-=this.menu_padding;for(b=a+1;b<this.menu_items.length;b++)q(this.menu_items[b],this.road_points,d),d+=this.menu_padding};k.prototype.$innerAnimationMove=function(a){a-=this.active;var b=!1,c=this.menu_items,d=this.active;0<a?b=!0:(a=Math.abs(a),c=this.menu_items.slice().reverse(),d=this.menu_items.length-d-1);for(var e=0;e<c.length;e++){var g=c[e],f=[];e<d?h(f,this.menu_padding,a):e==d?(h(f,this.menu_active_padding,
1),h(f,this.menu_padding,a-1)):e-a==d?(h(f,this.menu_padding,a-1),h(f,this.menu_active_padding,1)):e-a<d?(h(f,this.menu_padding,e-d-1),h(f,this.menu_active_padding,2),h(f,this.menu_padding,a-(e-d)-1)):h(f,this.menu_padding,a);u(g,this.road_points,f,b,this.tasks)}};k.prototype.setActiveById=function(a){this.setActive(this.menuSearchPosById(a))};k.prototype.setActive=function(){var a=!0;return function(b){if(!this.tasks.length){if("undefined"!==typeof this.active){if(this.active==b)return;this.menu_items[this.active].$el.removeClass("active")}a?
(this.$innerSampleMove(b),a=!1):this.$innerAnimationMove(b);this.active=b;this.menu_items[this.active].$el.addClass("active")}}}();k.prototype.next=function(){this.setActive(this.active==this.menu_items.length-1?0:this.active+1)};k.prototype.prev=function(){this.setActive(0==this.active?this.menu_items.length-1:this.active-1)};var m={};l.fn.bezierNav=function(a){var b=this.attr("id");b||(b="bezier_nav_"+Date.now(),this.attr("id",b));if(m.hasOwnProperty(b)){if(a in m[b]&&"$"!=a[0])return m[b][a].apply(m[b],
Array.prototype.slice.call(arguments,1));l.error("Bad method name: "+a)}else m[b]=new k(this,a)}})(jQuery);