MWFRaphael.js 9.4 KB

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