MWFRaphael.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. o2.widget = o2.widget || {};
  2. o2.widget.MWFRaphael = MWFRaphael = {
  3. load: function(callback){
  4. if (window.Raphael){
  5. if (callback) callback();
  6. }else{
  7. if(!window.Raphael && typeof require === 'function' && define.amd){
  8. require(["../o2_lib/raphael/raphael.js"], function(r){
  9. window.Raphael = r;
  10. this.expandRaphael();
  11. if (callback) callback();
  12. }.bind(this));
  13. }else{
  14. COMMON.AjaxModule.load("raphael", function(){
  15. this.expandRaphael();
  16. if (callback) callback();
  17. }.bind(this), true, true);
  18. }
  19. }
  20. },
  21. expandRaphael: function(){
  22. Raphael.fn.diamond = function(x, y, width, height, r1, r2){
  23. return MWFRaphael.diamond.call(this, x, y, width, height, r1, r2);
  24. };
  25. Raphael.fn.rectPath = function(x, y, width, height, r){
  26. return MWFRaphael.rectPath.call(this, x, y, width, height, r);
  27. };
  28. Raphael.fn.circlePath = function(x, y, r){
  29. return MWFRaphael.circlePath.call(this, x, y, r);
  30. };
  31. Raphael.fn.arrow = function(beginPoint, endPoint, l1, l2, aj){
  32. return MWFRaphael.arrow.call(this, beginPoint, endPoint, l1, l2, aj);
  33. };
  34. },
  35. rectPath: function(x, y, width, height, r){
  36. var path = MWFRaphael.getRectPath(x, y, width, height, r);
  37. return this.path(path);
  38. },
  39. getRectPath: function(x, y, width, height, r){
  40. x = x.toFloat();
  41. y = y.toFloat();
  42. width = width.toFloat();
  43. height = height.toFloat();
  44. r = r.toFloat();
  45. var beginPoint = {
  46. x: x+width-r,
  47. y: y+height-r
  48. };
  49. var arc_t_h = height-r-r;
  50. var arc_t_w = width-r-r;
  51. //先得到圆角path------------------------------------------------------
  52. // |
  53. //---------- 这个位置的圆角
  54. // | *
  55. var arcPath1 = Raphael.parsePathString(MWFRaphael.getArcPath(beginPoint.x, beginPoint.y, r));
  56. var r_c_p_x = beginPoint.x;
  57. var r_c_p_y = beginPoint.y;
  58. // | *
  59. //---------- 这个位置的圆角
  60. // |
  61. var arcPath2 = Raphael.transformPath(arcPath1, "r-90,"+r_c_p_x+","+r_c_p_y+"T0,-"+arc_t_h);
  62. //var arcPath2 = Raphael.transformPath(arcPath1, "r-90,"+r_c_p_x+","+r_c_p_y);
  63. // * |
  64. //---------- 这个位置的圆角
  65. // |
  66. r_c_p_y = r_c_p_y - arc_t_h;
  67. var arcPath3 = Raphael.transformPath(arcPath2, "r-90,"+r_c_p_x+","+r_c_p_y+"T-"+arc_t_w+",0");
  68. // |
  69. //---------- 这个位置的圆角
  70. // * |
  71. r_c_p_x = r_c_p_x - arc_t_w;
  72. var arcPath4 = Raphael.transformPath(arcPath3, "r-90,"+r_c_p_x+","+r_c_p_y+"T0,"+arc_t_h);
  73. //先得到圆角path------------------------------------------------------
  74. //得到四个边的path----------------------------------------------------
  75. // | |
  76. //----------|---- 右边
  77. // | |
  78. var h_l_f_x = x+width;
  79. var h_l_f_y = y+height-r;
  80. var h_l_t_x = x+width;
  81. var h_l_t_y = y+r;
  82. var hLine1 = Raphael.parsePathString("M"+h_l_f_x+","+h_l_f_y+"L"+h_l_t_x+","+h_l_t_y);
  83. // ————|————
  84. //-------------- 上边
  85. // |
  86. var w_l_f_x = x+width-r;
  87. var w_l_f_y = y;
  88. var w_l_t_x = x+r;
  89. var w_l_t_y = y;
  90. var wLine1 = Raphael.parsePathString("M"+w_l_f_x+","+w_l_f_y+"L"+w_l_t_x+","+w_l_t_y);
  91. // | |
  92. //--|----------- 左边
  93. // | |
  94. var hLine2 = Raphael.transformPath(hLine1, "t-"+width+",0"+"R180");
  95. // |
  96. //-------------- 下边
  97. // ————|————
  98. var wLine2 = Raphael.transformPath(wLine1, "t0,"+height+"R180");
  99. //得到四个边的path----------------------------------------------------
  100. //return ""+arcPath1+hLine1+arcPath2+wLine1+arcPath3+hLine2+arcPath4+wLine2+"Z";
  101. //return ""+arcPath1+hLine1+arcPath2+wLine1+arcPath3+hLine2+arcPath4+wLine2+"Z";
  102. return ""+arcPath1+hLine1[1]+arcPath2[1]+wLine1[1]+arcPath3[1]+hLine2[1]+arcPath4[1]+wLine2[1]+"Z";
  103. },
  104. circlePath: function(x, y, r){
  105. var path = MWFRaphael.getCirclePath(x, y, r);
  106. return this.path(path);
  107. },
  108. getArcPath: function(x, y, r){
  109. x = x.toFloat();
  110. y = y.toFloat();
  111. r = r.toFloat();
  112. var x0 = x;
  113. var y0 = y+r;
  114. var x1;
  115. var y1 = y0;
  116. var x2 = x+r;
  117. var y2;
  118. var x3 = x+r;
  119. var y3 = y;
  120. //三次贝塞尔曲线公式
  121. //B(t)=P0(1-t)*(1-t)*(1-t)+3P1t(1-t)*(1-t)+3P2t*t(1-t)+P3t*t*t; t属于[0,1]
  122. //假设t=0.5
  123. var b = Math.sqrt((r*r)/2);
  124. var Bx = x0+b;
  125. var By = y3+b;
  126. x1 = (Bx-0.125*x0-0.375*x2-0.125*x3)/0.375;
  127. y2 = (By-0.125*y0-0.375*y1-0.125*y3)/0.375;
  128. return "M"+x0+","+y0+"C"+x1+","+y1+" "+x2+","+y2+" "+x3+","+y3;
  129. },
  130. getCirclePath: function(x, y, r){
  131. var path1 = MWFRaphael.getArcPath(x, y, r);
  132. var path2 = Raphael.transformPath(path1, "R-90,"+x+","+y);
  133. var path3 = Raphael.transformPath(path2, "R-90,"+x+","+y);
  134. var path4 = Raphael.transformPath(path3, "R-90,"+x+","+y);
  135. var path = path1+path2[1]+path3[1]+path4[1]+"z";
  136. return path;
  137. },
  138. diamond: function(x, y, width, height, r1, r2){
  139. var path = MWFRaphael.getDiamondPath(x, y, width, height, r1, r2);
  140. return this.path(path);
  141. },
  142. getDiamondPath: function(x, y, width, height, r1, r2){
  143. var leftPoint = {
  144. "x": x,
  145. "y": y+height/2
  146. };
  147. var topPoint = {
  148. "x": x+width/2,
  149. "y": y
  150. };
  151. var rightPoint = {
  152. "x": x+width,
  153. "y": y+height/2
  154. };
  155. var bottomPoint = {
  156. "x": x+width/2,
  157. "y": y+height
  158. };
  159. var leftRightMinus = MWFRaphael.getMinus(width/2, height/2, r1);
  160. var topBottomMinus = MWFRaphael.getMinus(width/2, height/2, r2);
  161. var line1StartPoint = {
  162. "x": leftPoint.x+(leftRightMinus.x),
  163. "y": leftPoint.y+(leftRightMinus.y)
  164. };
  165. var line1Point = {
  166. "x": bottomPoint.x-(topBottomMinus.x),
  167. "y": bottomPoint.y-(topBottomMinus.y)
  168. };
  169. var line2StartPoint = {
  170. "x": bottomPoint.x+(topBottomMinus.x),
  171. "y": bottomPoint.y-(topBottomMinus.y)
  172. };
  173. var line2Point = {
  174. "x": rightPoint.x-(leftRightMinus.x),
  175. "y": rightPoint.y+(leftRightMinus.y)
  176. };
  177. var line3StartPoint = {
  178. "x": rightPoint.x-(leftRightMinus.x),
  179. "y": rightPoint.y-(leftRightMinus.y)
  180. };
  181. var line3Point = {
  182. "x": topPoint.x+(topBottomMinus.x),
  183. "y": topPoint.y+(topBottomMinus.y)
  184. };
  185. var line4StartPoint = {
  186. "x": topPoint.x-(topBottomMinus.x),
  187. "y": topPoint.y+(topBottomMinus.y)
  188. };
  189. var line4Point = {
  190. "x": leftPoint.x+(leftRightMinus.x),
  191. "y": leftPoint.y-(leftRightMinus.y)
  192. };
  193. path = "M"+line4Point.x+","+line4Point.y;
  194. path += "Q"+leftPoint.x+","+leftPoint.y+","+line1StartPoint.x+","+line1StartPoint.y;
  195. path += "L"+line1Point.x+","+line1Point.y;
  196. path += "Q"+bottomPoint.x+","+bottomPoint.y+","+line2StartPoint.x+","+line2StartPoint.y;
  197. path += "L"+line2Point.x+","+line2Point.y;
  198. path += "Q"+rightPoint.x+","+rightPoint.y+","+line3StartPoint.x+","+line3StartPoint.y;
  199. path += "L"+line3Point.x+","+line3Point.y;
  200. path += "Q"+topPoint.x+","+topPoint.y+","+line4StartPoint.x+","+line4StartPoint.y;
  201. path += "Z";
  202. return path;
  203. },
  204. getMinus: function(w,h,r){
  205. var c = Math.sqrt(w*w + h*h);
  206. var angle = h/(c/90);
  207. var y = (r/90)*angle;
  208. var x = Math.sqrt(r*r-y*y);
  209. return {x:x, y:y};
  210. },
  211. getPointDistance: function(a,b){
  212. var tmp1 = Math.abs(a.x-b.x);
  213. var tmp2 = Math.abs(a.y-b.y);
  214. return Math.sqrt((tmp1*tmp1)+(tmp2*tmp2));
  215. },
  216. getAngles: function(a,b,c){
  217. var cosA=(b*b+c*c-a*a)/(2*b*c);
  218. var cosB=(a*a+c*c-b*b)/(2*a*c);
  219. var cosC=(a*a+b*b-c*c)/(2*a*b);
  220. var A = Math.acos(cosA);
  221. var B = Math.acos(cosB);
  222. var C = Math.acos(cosC);
  223. return {A:A, B:B, C:C};
  224. },
  225. getMinDistance: function(p1, p2, p3){
  226. var lineA = this.getPointDistance(p1,p2);
  227. var lineB = this.getPointDistance(p1,p3);
  228. var lineC = this.getPointDistance(p2,p3);
  229. var angle = this.getAngles(lineA,lineB,lineC);
  230. var angleA = angle.A;
  231. var angleB = angle.B;
  232. var angleC = angle.C;
  233. var h = lineA*Math.sin(angleB);
  234. var pointLineC = Math.sqrt(lineA*lineA-h*h);
  235. var x = p3.x-p2.x;
  236. var y = p3.y-p2.y;
  237. var sinY = y*(1/lineC);
  238. var offy = sinY*pointLineC;
  239. var sinX = x*(1/lineC);
  240. var offx = sinX*pointLineC;
  241. // var offx = Math.sqrt(pointLineC*pointLineC-offy*offy);
  242. return {"h": h, "p": {"x": p2.x+offx, "y": p2.y+offy}};
  243. },
  244. arrow: function(beginPoint, endPoint, l1, l2, aj){
  245. var path = MWFRaphael.getArrowPath(beginPoint, endPoint, l1, l2, aj);
  246. return this.path(path);
  247. },
  248. getArrowPath: function(beginPoint, endPoint, l1, l2, aj){
  249. var endX = endPoint.x;
  250. var endY = endPoint.y;
  251. var beginX = beginPoint.x;
  252. var beginY = beginPoint.y;
  253. // var l1 = l1;
  254. // var l2 = l2;
  255. // var aj = aj;
  256. var p1 = endX - beginX;
  257. var p2 = endY - beginY;
  258. if (p1==0 && p2==0){
  259. return "";
  260. }
  261. var y = (p2/Math.sqrt((p1*p1)+(p2*p2)))*l1;
  262. var x = (p1/Math.sqrt((p1*p1)+(p2*p2)))*l1;
  263. var ag = (Math.asin(p2/Math.sqrt((p1*p1)+(p2*p2)))/Math.PI)*180;
  264. var ag1 = (Math.asin(p1/Math.sqrt((p1*p1)+(p2*p2)))/Math.PI)*180;
  265. if ((p1<=0) & (p2<=0)) {
  266. var x1 = Math.sin((270-(ag-aj))*(Math.PI/180))*l2;
  267. var y1 = Math.sin((ag-aj)*(Math.PI/180))*l2;
  268. var y2 = Math.sin((270-(ag1-aj))*(Math.PI/180))*l2;
  269. var x2 = Math.sin((ag1-aj)*(Math.PI/180))*l2;
  270. }else if((p1>0) & (p2<0)){
  271. var x1 = Math.sin((90-(ag+aj))*(Math.PI/180))*l2;
  272. var y1 = Math.sin((ag+aj)*(Math.PI/180))*l2;
  273. var y2 = Math.sin((ag-aj)*(Math.PI/180))*l2;
  274. var x2 = Math.sin((ag1-aj)*(Math.PI/180))*l2;
  275. }else if((p1<0) & (p2>0)){
  276. var x1 = Math.sin(((ag1+aj))*(Math.PI/180))*l2;
  277. var y1 = Math.sin((ag+aj)*(Math.PI/180))*l2;
  278. var y2 = Math.sin((90-(ag1-aj))*(Math.PI/180))*l2;
  279. var x2 = Math.sin((ag1-aj)*(Math.PI/180))*l2;
  280. }else{
  281. var x1 = Math.sin((90-(ag-aj))*(Math.PI/180))*l2;
  282. var y1 = Math.sin((ag-aj)*(Math.PI/180))*l2;
  283. var y2 = Math.sin((90-(ag1-aj))*(Math.PI/180))*l2;
  284. var x2 = Math.sin((ag1-aj)*(Math.PI/180))*l2;
  285. }
  286. x = (p1-x) + beginX;
  287. y = (p2-y) + beginY;
  288. x1 = (p1-x1) + beginX;
  289. y1 = (p2-y1) + beginY;
  290. x2 = (p1-x2) + beginX;
  291. y2 = (p2-y2) + beginY;
  292. return "M"+x+","+y+"L"+x1+","+y1+"L"+endX+","+endY+"L"+x2+","+y2+"Z";
  293. }
  294. };