/
Rect.js
80 lines (69 loc) · 1.73 KB
/
Rect.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
define(["dojo/_base/lang", "../util/oo", "../manager/_registry", "../stencil/Rect"],
function(lang, oo, registry, StencilRect){
//dojox.drawing.tools.Rect
var Rect = oo.declare(
StencilRect,
function(){
// summary:
// constructor
},
{
// summary:
// Class for a drawable rectangle
draws:true,
onDrag: function(/*EventObject*/obj){
var s = obj.start, e = obj;
var x = s.x < e.x ? s.x : e.x,
y = s.y < e.y ? s.y : e.y,
w = s.x < e.x ? e.x-s.x : s.x-e.x,
h = s.y < e.y ? e.y-s.y : s.y-e.y;
if(this.keys.shift){ w = h = Math.max(w,h); }
if(this.keys.alt){
x-=w; y-=h; w*=2; h*=2;
x = Math.max(x, 0);
y = Math.max(y, 0);
}
this.setPoints ([
{x:x, y:y}, // TL
{x:x+w, y:y}, // TR
{x:x+w, y:y+h}, // BR
{x:x, y:y+h} // BL
]);
this.render();
},
onUp: function(/*EventObject*/obj){
if(this.created || !this._downOnCanvas){ return; }
this._downOnCanvas = false;
//Default shape on single click
if(!this.shape){
var s = obj.start;
var e = this.minimumSize*4;
this.setPoints([
{x:s.x, y:s.y},
{x:s.x+e, y:s.y},
{x:s.x+e, y:s.y+e},
{x:s.x, y:s.y+e}
]);
this.render();
}else{
// if too small, need to reset
var o = this.data;
if(o.width<this.minimumSize && o.height < this.minimumSize){
this.remove(this.shape, this.hit);
return;
}
}
this.onRender(this);
}
}
);
lang.setObject("dojox.drawing.tools.Rect", Rect);
Rect.setup = {
name:"dojox.drawing.tools.Rect",
tooltip:'<span class="drawingTipTitle">Rectangle Tool</span><br/>'
+ '<span class="drawingTipDesc">SHIFT - constrain to square</span>',
iconClass:"iconRect"
};
registry.register(Rect.setup, "tool");
return Rect;
});