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.96 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),s=parseInt(i.speed),h=this.flipXY(this.polarToCartesian(a,s));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(this.frontcanv.parentElement.settings.move&&i.trigger_move===!0&&this.frontcanv.parentElement.settings.move(s,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.cartesianToPolar=function(t,e){return{magnitude:Math.sqrt(Math.pow(t,2)+Math.pow(e,2)),angle:180*Math.atan2(t,e)/Math.PI}},this.flipXY=function(t){var e=t.x;return t.x=t.y,t.y=e,t},this.getMousePosition=function(e){var i,n=t(this.frontcanv).offset();return i=void 0!=e.originalEvent.layerX?{x:e.originalEvent.pageX-n.left,y:e.originalEvent.pageY-n.top}:{x:e.originalEvent.x,y:e.originalEvent.y}},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;var s=e.pmag;e.pmag>30?e.pmag=30:e.pmag,e.pangle=180*Math.atan2(n,a)/Math.PI,e.pangle<0&&(e.pangle+=360),s>30&&(t=this.flipXY(this.polarToCartesian(e.pangle,30)),t.y=-t.y,t.x*=e.px_per_unit,t.y*=e.px_per_unit,t.x+=e.width/2,t.y+=e.height/2),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()},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 r=this.backcanv;r.width=t(this).width(),r.height=t(this).height(),r.style.zIndex="0",r.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=r.getContext("2d"),g=.75*(r.width>r.height?r.height/2-1:r.width/2-1);l.beginPath(),l.arc(r.width/2,r.height/2,g,0,2*Math.PI,!0),l.closePath(),l.fillStyle="rgba(255, 255, 255, .8)",l.fill(),o.px_per_unit=g/30,l.beginPath();for(var v=g/3;g>=v;v+=g/3)l.moveTo(r.width/2+v,r.height/2),l.arc(r.width/2,r.height/2,v,0,2*Math.PI);l.closePath(),l.stroke(),l.fillStyle="rgba(0, 0, 0, .8)",l.translate(r.width/2,r.height/2);var p=r.height/20+1,d=r.height/50;l.font=void 0!=window.G_vmlCanvasManager?"bold "+p+"px Arial":"bold "+p+"px Arial",l.fillText("N",-5,-(r.height/2-p)),l.fillText("S",-5,r.height/2-d),l.fillText("W",-(r.width/2-d),5),l.fillText("E",r.width/2-p,5);for(var c=8,v=0;c>v;v++){var f=360/c,m=(f*v).toString(),u=l.measureText(m).width;l.fillText(m,-u/2,-(r.height/2-2*p-d+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=n.getMousePosition(t);n.drawArrow(e),this.parentElement.settings&&null!=this.parentElement.settings.move&&this.parentElement.settings.move(this.pmag,this.pangle)}),t(o).mousemove(function(t){if(this.pressed){this.moved=!0;var e=n.getMousePosition(t);n.drawArrow(e),this.parentElement.settings&&null!=this.parentElement.settings.move&&this.parentElement.settings.move(this.pmag,this.pangle)}}),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);