positioning.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // wrapped by build app
  2. define("dojox/drawing/util/positioning", ["dijit","dojo","dojox"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.drawing.util.positioning");
  4. (function(){
  5. var textOffset = 4; // distance from line to text box
  6. var textYOffset = 20; // height of text box
  7. dojox.drawing.util.positioning.label = function(/*Object*/start, /*Object*/end){
  8. // summary:
  9. // Returns the optimal text positions for annotations.Label.
  10. // label at middle of vector
  11. var x = 0.5*(start.x+end.x);
  12. var y = 0.5*(start.y+end.y);
  13. // move label a set distance from the line
  14. var slope = dojox.drawing.util.common.slope(start, end);
  15. var deltay = textOffset/Math.sqrt(1.0+slope*slope);
  16. if(end.y>start.y && end.x>start.x || end.y<start.y && end.x<start.x){
  17. // Position depending on quadrant. Y offset
  18. // positions box aligned vertically from top
  19. deltay = -deltay;
  20. y -= textYOffset;
  21. }
  22. x += -deltay*slope;
  23. y += deltay;
  24. // want text to be away from start of vector
  25. // This will make force diagrams less crowded
  26. var align = end.x<start.x ? "end" : "start";
  27. return { x:x, y:y, foo:"bar", align:align}; // Object
  28. };
  29. dojox.drawing.util.positioning.angle = function(/*Object*/start, /*Object*/end){
  30. // summary:
  31. // Returns the optimal position for annotations.Angle.
  32. //
  33. // angle at first third of vector
  34. var x = 0.7*start.x+0.3*end.x;
  35. var y = 0.7*start.y+0.3*end.y;
  36. // move label a set distance from the line
  37. var slope = dojox.drawing.util.common.slope(start, end);
  38. var deltay = textOffset/Math.sqrt(1.0+slope*slope);
  39. if(end.x<start.x){deltay = -deltay;}
  40. x += -deltay * slope;
  41. y += deltay;
  42. // want text to be clockwise from vector
  43. // to match angle measurement from x-axis
  44. var align = end.y>start.y ? "end" : "start";
  45. // box vertical aligned from middle
  46. y += end.x > start.x ? 0.5*textYOffset : -0.5*textYOffset;
  47. return { x:x, y:y, align:align}; // Object
  48. }
  49. })();
  50. });