source: Dev/branches/rest-dojo-ui/client/dojox/drawing/util/positioning.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

  • Property svn:executable set to *
File size: 1.8 KB
Line 
1dojo.provide("dojox.drawing.util.positioning");
2
3(function(){
4       
5        var textOffset = 4;  // distance from line to text box
6        var textYOffset = 20;  // height of text box
7       
8       
9        dojox.drawing.util.positioning.label = function(/*Object*/start, /*Object*/end){
10                // summary:
11                //              Returns the optimal text positions for annotations.Label.
12               
13                // label at middle of vector
14                var x = 0.5*(start.x+end.x);
15                var y = 0.5*(start.y+end.y);
16               
17                // move label a set distance from the line
18                var slope = dojox.drawing.util.common.slope(start, end);
19                var deltay = textOffset/Math.sqrt(1.0+slope*slope);
20               
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               
30                // want text to be away from start of vector
31                // This will make force diagrams less crowded
32                var align = end.x<start.x ? "end" : "start";
33               
34                return { x:x, y:y, foo:"bar", align:align}; // Object
35        };
36       
37        dojox.drawing.util.positioning.angle = function(/*Object*/start, /*Object*/end){
38                // summary:
39                //              Returns the optimal position for annotations.Angle.
40                //
41                // angle at first third of vector
42                var x = 0.7*start.x+0.3*end.x;
43                var y = 0.7*start.y+0.3*end.y;
44                // move label a set distance from the line
45                var slope = dojox.drawing.util.common.slope(start, end);
46                var deltay = textOffset/Math.sqrt(1.0+slope*slope);
47               
48                if(end.x<start.x){deltay = -deltay;}
49                x += -deltay * slope;
50                y += deltay;
51               
52                // want text to be clockwise from vector
53                // to match angle measurement from x-axis
54                var align = end.y>start.y ? "end" : "start";
55                // box vertical aligned from middle
56                y += end.x > start.x ? 0.5*textYOffset :  -0.5*textYOffset;
57               
58                return { x:x, y:y, align:align}; // Object
59        }
60       
61})();
62
63
64
Note: See TracBrowser for help on using the repository browser.