forked from NOAA-ORR-ERD/compass-rose-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compass-rose-ui.min.js
1 lines (1 loc) · 4.46 KB
/
compass-rose-ui.min.js
1
!function(t){t.fn.compassRoseUI=function(e,i){return this.each(function(){var n=this;if("reset"===e)return void this.resetCanvas();if("update"===e){if(!this.frontcanv)return;this.resetCanvas();var a=parseInt(i.direction),r=parseInt(i.speed),h=this.flipXY(this.polarToCartesian(a,r));return h.x*=this.frontcanv.px_per_unit,h.y*=-this.frontcanv.px_per_unit,h.x+=this.frontcanv.width/2,h.y+=this.frontcanv.height/2,n.drawArrow(h),void canvas.parentElement.settings.move(r,a)}this.settings=t.extend({"arrow-direction":"out","cardinal-name":function(t){var e=["N","NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"];return e[Math.floor((+t+11.25)/22.5%16)]},"cardinal-angle":function(t){var e=["N","NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"],i=e.indexOf(t.toUpperCase());return-1===i?null:22.5*i},move:null,change:null},e),this.resetCanvas=function(){this.frontcanv&&this.frontcanv.getContext("2d").clearRect(0,0,this.frontcanv.width,this.frontcanv.height)},this.polarToCartesian=function(t,e){var i=t*Math.PI/180;return{x:e*Math.cos(i),y:e*Math.sin(i)}},this.flipXY=function(t){var e=t.x;return t.x=t.y,t.y=e,t},this.drawArrow=function(t){var e=this.frontcanv,i=e.getContext("2d"),n=t.x-e.width/2,a=-(t.y-e.height/2);e.pmag=Math.sqrt(Math.pow(n,2)+Math.pow(a,2)),e.pmag/=e.px_per_unit,e.pangle=180*Math.atan2(n,a)/Math.PI,e.pangle<0&&(e.pangle+=360),i.lineWidth=2,i.clearRect(0,0,e.width,e.height),i.beginPath(),i.moveTo(e.width/2,e.height/2),i.lineTo(t.x,t.y),i.stroke(),i.closePath(),i.beginPath(),e.parentElement.settings&&"in"===e.parentElement.settings["arrow-direction"]?(i.translate(e.width/2,e.height/2),i.rotate((e.pangle+180)*Math.PI/180)):(i.translate(t.x,t.y),i.rotate(e.pangle*Math.PI/180)),i.moveTo(0,0),i.quadraticCurveTo(0,8,8,15),i.lineTo(0,8),i.moveTo(0,0),i.quadraticCurveTo(0,8,-8,15),i.lineTo(0,8),i.stroke(),i.closePath(),e.parentElement.settings&&"in"===e.parentElement.settings["arrow-direction"]?(i.rotate(-(e.pangle+180)*Math.PI/180),i.translate(-e.width/2,-e.height/2)):(i.rotate(-e.pangle*Math.PI/180),i.translate(-t.x,-t.y)),i.beginPath(),i.closePath(),e.parentElement.settings&&null!=e.parentElement.settings.move&&e.parentElement.settings.move(e.pmag,e.pangle)},t(this).load(function(t){}),t(this).css({visibility:"hidden",display:"block"}),void 0===this.backcanv&&(t(this).append('<canvas id="'+this.id+'-back"></canvas>'),this.backcanv=t("canvas#"+this.id+"-back")[0],void 0!=window.G_vmlCanvasManager&&G_vmlCanvasManager.initElement(this.backcanv));var s=this.backcanv;s.width=t(this).width(),s.height=t(this).height(),s.style.zIndex="0",s.style.position="absolute",void 0===this.frontcanv&&(t(this).append('<canvas id="'+this.id+'-front"></canvas>'),this.frontcanv=t("canvas#"+this.id+"-front")[0],void 0!=window.G_vmlCanvasManager&&G_vmlCanvasManager.initElement(this.frontcanv));var o=this.frontcanv;o.width=t(this).width(),o.height=t(this).height(),o.style.zIndex="1",o.style.position="absolute",t(this).removeAttr("style");var l=s.getContext("2d"),v=.75*(s.width>s.height?s.height/2-1:s.width/2-1);l.beginPath(),l.arc(s.width/2,s.height/2,v,0,2*Math.PI,!0),l.closePath(),l.fillStyle="rgba(255, 255, 255, .8)",l.fill(),o.px_per_unit=v/50,l.beginPath();for(var d=v/5;v>=d;d+=v/5)l.moveTo(s.width/2+d,s.height/2),l.arc(s.width/2,s.height/2,d,0,2*Math.PI);l.closePath(),l.stroke(),l.fillStyle="rgba(0, 0, 0, .8)",l.translate(s.width/2,s.height/2);var g=s.height/20+1,c=s.height/50;l.font=void 0!=window.G_vmlCanvasManager?"bold "+g+"px Optimer":"bold "+g+"px Times New Roman",l.fillText("N",-5,-(s.height/2-g)),l.fillText("S",-5,s.height/2-c),l.fillText("W",-(s.width/2-c),5),l.fillText("E",s.width/2-g,5);for(var p=8,d=0;p>d;d++){var f=360/p,m=(f*d).toString(),u=l.measureText(m).width;l.fillText(m,-u/2,-(s.height/2-2*g-c+1)),l.rotate(f*Math.PI/180)}l=o.getContext("2d"),l.fillRect(20,20,150,100),l.clearRect(20,20,150,100),o.pressed=!1,o.moved=!1,t(o).mousedown(function(t){this.pressed=!0;var e;e=void 0!=t.originalEvent.layerX?{x:t.originalEvent.layerX,y:t.originalEvent.layerY}:{x:t.originalEvent.x,y:t.originalEvent.y},n.drawArrow(e)}),t(o).mousemove(function(t){if(this.pressed){this.moved=!0;var e;e=void 0!=t.originalEvent.layerX?{x:t.originalEvent.layerX,y:t.originalEvent.layerY}:{x:t.originalEvent.x,y:t.originalEvent.y},n.drawArrow(e)}}),t(o).mouseup(function(){this.pressed&&this.moved&&this.parentElement.settings&&null!=this.parentElement.settings.change&&this.parentElement.settings.change(this.pmag,this.pangle),this.pressed=this.moved=!1})})}}(jQuery);