positioning.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.drawing.util.positioning"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.drawing.util.positioning"] = true;
  8. dojo.provide("dojox.drawing.util.positioning");
  9. (function(){
  10. var textOffset = 4; // distance from line to text box
  11. var textYOffset = 20; // height of text box
  12. dojox.drawing.util.positioning.label = function(/*Object*/start, /*Object*/end){
  13. // summary:
  14. // Returns the optimal text positions for annotations.Label.
  15. // label at middle of vector
  16. var x = 0.5*(start.x+end.x);
  17. var y = 0.5*(start.y+end.y);
  18. // move label a set distance from the line
  19. var slope = dojox.drawing.util.common.slope(start, end);
  20. var deltay = textOffset/Math.sqrt(1.0+slope*slope);
  21. if(end.y>start.y && end.x>start.x || end.y<start.y && end.x<start.x){
  22. // Position depending on quadrant. Y offset
  23. // positions box aligned vertically from top
  24. deltay = -deltay;
  25. y -= textYOffset;
  26. }
  27. x += -deltay*slope;
  28. y += deltay;
  29. // want text to be away from start of vector
  30. // This will make force diagrams less crowded
  31. var align = end.x<start.x ? "end" : "start";
  32. return { x:x, y:y, foo:"bar", align:align}; // Object
  33. };
  34. dojox.drawing.util.positioning.angle = function(/*Object*/start, /*Object*/end){
  35. // summary:
  36. // Returns the optimal position for annotations.Angle.
  37. //
  38. // angle at first third of vector
  39. var x = 0.7*start.x+0.3*end.x;
  40. var y = 0.7*start.y+0.3*end.y;
  41. // move label a set distance from the line
  42. var slope = dojox.drawing.util.common.slope(start, end);
  43. var deltay = textOffset/Math.sqrt(1.0+slope*slope);
  44. if(end.x<start.x){deltay = -deltay;}
  45. x += -deltay * slope;
  46. y += deltay;
  47. // want text to be clockwise from vector
  48. // to match angle measurement from x-axis
  49. var align = end.y>start.y ? "end" : "start";
  50. // box vertical aligned from middle
  51. y += end.x > start.x ? 0.5*textYOffset : -0.5*textYOffset;
  52. return { x:x, y:y, align:align}; // Object
  53. }
  54. })();
  55. }