annotate webapp/src/main/webapp/jquery/jquery.digilib.geometry.js @ 974:678313a989a9

first part of calibration dialog
author hertzhaft
date Thu, 26 Jan 2012 15:06:00 +0100
parents c4bd6f984aee
children dc09bccf8357
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
1 /** required digilib geometry plugin
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
2 */
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
3
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
4 (function($) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
5 //var dlGeometry = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
6 /*
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
7 * Size class
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
8 */
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
9 var size = function(w, h) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
10 var that;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
11 if (typeof w === "object") {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
12 // assume its size
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
13 that = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
14 width : w.width,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
15 height : w.height
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
16 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
17 } else {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
18 that = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
19 width : parseFloat(w),
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
20 height : parseFloat(h)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
21 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
22 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
23 that.equals = function(other) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
24 return (this.width === other.width && this.height === other.height);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
25 };
957
c4bd6f984aee don't set position of scaler div, just size.
robcast
parents: 953
diff changeset
26 // adjusts size of jQuery element "$elem" to this size
c4bd6f984aee don't set position of scaler div, just size.
robcast
parents: 953
diff changeset
27 that.adjustDiv = function($elem) {
c4bd6f984aee don't set position of scaler div, just size.
robcast
parents: 953
diff changeset
28 $elem.width(this.width).height(this.height);
c4bd6f984aee don't set position of scaler div, just size.
robcast
parents: 953
diff changeset
29 };
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
30 that.toString = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
31 return (this.width + "x" + this.height);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
32 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
33 return that;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
34 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
35
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
36 /*
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
37 * Position class
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
38 */
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
39 var position = function(x, y) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
40 var that;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
41 if (typeof x === "object") {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
42 if (x instanceof jQuery) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
43 // jQuery object
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
44 var pos = x.offset();
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
45 that = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
46 x : pos.left,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
47 y : pos.top
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
48 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
49 } else {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
50 if (x.x != null) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
51 // position object
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
52 that = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
53 x : x.x,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
54 y : x.y
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
55 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
56 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
57 if (x.pageX != null) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
58 // event object
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
59 that = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
60 x : x.pageX,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
61 y : x.pageY
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
62 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
63 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
64 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
65 } else {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
66 that = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
67 x : parseFloat(x),
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
68 y : parseFloat(y)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
69 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
70 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
71 that.equals = function(other) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
72 return (this.x === other.x && this.y === other.y);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
73 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
74 // add position other to this
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
75 that.add = function(other) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
76 this.x += other.x;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
77 this.y += other.y;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
78 return this;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
79 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
80 // returns negative position
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
81 that.neg = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
82 return position({
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
83 x : -this.x,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
84 y : -this.y
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
85 });
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
86 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
87 // returns new position that is the difference between this and other
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
88 that.delta = function(other) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
89 return position({
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
90 x : other.x - this.x,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
91 y : other.y - this.y
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
92 });
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
93 };
944
3916303b8f17 "preview" works now also for zoomIn/Out
robcast
parents: 903
diff changeset
94 // adjusts CSS position of $elem to this position
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
95 that.adjustDiv = function($elem) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
96 $elem.offset({
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
97 left : this.x,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
98 top : this.y
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
99 });
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
100 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
101 // returns distance of this position to pos (length if pos == null)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
102 that.distance = function(pos) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
103 if (pos == null) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
104 pos = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
105 x : 0,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
106 y : 0
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
107 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
108 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
109 var dx = pos.x - this.x;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
110 var dy = pos.y - this.y;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
111 return Math.sqrt(dx * dx + dy * dy);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
112 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
113 that.toString = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
114 return (this.x + "," + this.y);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
115 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
116 return that;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
117 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
118 /*
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
119 * Rectangle class
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
120 */
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
121 var rectangle = function(x, y, w, h) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
122 var that = {};
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
123 if (typeof x === "object") {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
124 if (x instanceof jQuery) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
125 // jQuery object
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
126 var pos = x.offset();
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
127 that = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
128 x : pos.left,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
129 y : pos.top,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
130 width : x.width(),
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
131 height : x.height()
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
132 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
133 } else if (y == null) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
134 // assume x is rectangle
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
135 that = {
974
678313a989a9 first part of calibration dialog
hertzhaft
parents: 957
diff changeset
136 x : x.x || 0,
678313a989a9 first part of calibration dialog
hertzhaft
parents: 957
diff changeset
137 y : x.y || 0,
678313a989a9 first part of calibration dialog
hertzhaft
parents: 957
diff changeset
138 width : x.width || 0,
678313a989a9 first part of calibration dialog
hertzhaft
parents: 957
diff changeset
139 height : x.height || 0
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
140 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
141 } else {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
142 // assume x and y are Position
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
143 that = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
144 x : Math.min(x.x, y.x),
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
145 y : Math.min(x.y, y.y),
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
146 width : Math.abs(y.x - x.x),
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
147 height : Math.abs(y.y - x.y)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
148 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
149 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
150 } else {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
151 that = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
152 x : parseFloat(x),
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
153 y : parseFloat(y),
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
154 width : parseFloat(w),
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
155 height : parseFloat(h)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
156 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
157 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
158 // returns a copy of this Rectangle
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
159 that.copy = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
160 return rectangle(this);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
161 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
162 // returns the position of this Rectangle
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
163 that.getPosition = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
164 return position(this);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
165 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
166 // returns the size of this Rectangle
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
167 that.getSize = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
168 return size(this);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
169 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
170 // returns the upper left corner position
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
171 that.getPt1 = that.getPosition;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
172 // returns the lower right corner position of this Rectangle
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
173 that.getPt2 = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
174 return position({
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
175 x : this.x + this.width,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
176 y : this.y + this.height
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
177 });
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
178 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
179 // sets the upper left corner position to pos
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
180 that.setPosition = function(pos) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
181 this.x = pos.x;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
182 this.y = pos.y;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
183 return this;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
184 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
185 // adds pos to the position
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
186 that.setPt1 = that.setPosition; // TODO: not really the same
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
187 that.addPosition = function(pos) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
188 this.x += pos.x;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
189 this.y += pos.y;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
190 return this;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
191 };
877
971b7122930f arrows: got basic functionality working
hertzhaft
parents: 847
diff changeset
192 // adds pos to the dimensions
971b7122930f arrows: got basic functionality working
hertzhaft
parents: 847
diff changeset
193 that.enlarge = function(pos) {
971b7122930f arrows: got basic functionality working
hertzhaft
parents: 847
diff changeset
194 this.width += pos.x;
971b7122930f arrows: got basic functionality working
hertzhaft
parents: 847
diff changeset
195 this.height += pos.y;
971b7122930f arrows: got basic functionality working
hertzhaft
parents: 847
diff changeset
196 return this;
971b7122930f arrows: got basic functionality working
hertzhaft
parents: 847
diff changeset
197 };
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
198 // sets the lower right corner to position pos
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
199 that.setPt2 = function(pos) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
200 this.width = pos.x - this.x;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
201 this.height = pos.y - this.y;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
202 return this;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
203 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
204 // returns the center position of this Rectangle
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
205 that.getCenter = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
206 return position({
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
207 x : this.x + this.width / 2,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
208 y : this.y + this.height / 2
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
209 });
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
210 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
211 // moves this rectangle's center to position pos
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
212 that.setCenter = function(pos) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
213 this.x = pos.x - this.width / 2;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
214 this.y = pos.y - this.height / 2;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
215 return this;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
216 };
944
3916303b8f17 "preview" works now also for zoomIn/Out
robcast
parents: 903
diff changeset
217 // returns true if both rectangles have equal position and size
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
218 that.equals = function(other) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
219 var eq = (this.x === other.x && this.y === other.y && this.width === other.width);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
220 return eq;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
221 };
944
3916303b8f17 "preview" works now also for zoomIn/Out
robcast
parents: 903
diff changeset
222 // returns a rectangle with the difference width, height and position
3916303b8f17 "preview" works now also for zoomIn/Out
robcast
parents: 903
diff changeset
223 that.delta = function(other) {
3916303b8f17 "preview" works now also for zoomIn/Out
robcast
parents: 903
diff changeset
224 return rectangle(other.x - this.x, other.y - this.y,
3916303b8f17 "preview" works now also for zoomIn/Out
robcast
parents: 903
diff changeset
225 other.width - this.width, other.height - this.height);
3916303b8f17 "preview" works now also for zoomIn/Out
robcast
parents: 903
diff changeset
226 };
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
227 // returns the area of this Rectangle
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
228 that.getArea = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
229 return (this.width * this.height);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
230 };
953
3d5e7458f9ae fixed birdseye region positioning after layout reflow.
robcast
parents: 944
diff changeset
231 // returns the aspect ratio of this Rectangle
3d5e7458f9ae fixed birdseye region positioning after layout reflow.
robcast
parents: 944
diff changeset
232 that.getAspect = function() {
3d5e7458f9ae fixed birdseye region positioning after layout reflow.
robcast
parents: 944
diff changeset
233 return (this.width / this.height);
3d5e7458f9ae fixed birdseye region positioning after layout reflow.
robcast
parents: 944
diff changeset
234 };
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
235 // eliminates negative width and height
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
236 that.normalize = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
237 var p = this.getPt2();
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
238 this.x = Math.min(this.x, p.x);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
239 this.y = Math.min(this.y, p.y);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
240 this.width = Math.abs(this.width);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
241 this.height = Math.abs(this.height);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
242 return this;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
243 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
244 // returns if Position "pos" lies inside of this rectangle
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
245 that.containsPosition = function(pos) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
246 var ct = ((pos.x >= this.x) && (pos.y >= this.y)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
247 && (pos.x <= this.x + this.width) && (pos.y <= this.y
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
248 + this.height));
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
249 return ct;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
250 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
251 // returns true if rectangle "rect" is contained in this rectangle
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
252 that.containsRect = function(rect) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
253 return (this.containsPosition(rect.getPt1()) && this
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
254 .containsPosition(rect.getPt2()));
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
255 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
256 // returns true if rectangle "rect" and this rectangle overlap
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
257 that.overlapsRect = function(rect) {
812
b484631f37c1 better intersect function
hertzhaft
parents: 807
diff changeset
258 return this.intersect(rect) != null;
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
259 };
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
260 // changes this rectangle's x/y values so it stays inside of rectangle
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
261 // "rect", keeping the proportions
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
262 that.stayInside = function(rect) {
812
b484631f37c1 better intersect function
hertzhaft
parents: 807
diff changeset
263 this.x = Math.max(this.x, rect.x);
b484631f37c1 better intersect function
hertzhaft
parents: 807
diff changeset
264 this.y = Math.max(this.y, rect.y);
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
265 if (this.x + this.width > rect.x + rect.width) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
266 this.x = rect.x + rect.width - this.width;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
267 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
268 if (this.y + this.height > rect.y + rect.height) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
269 this.y = rect.y + rect.height - this.height;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
270 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
271 return this;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
272 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
273 // clips this rectangle so it stays inside of rectangle "rect"
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
274 that.clipTo = function(rect) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
275 var p1 = rect.getPt1();
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
276 var p2 = rect.getPt2();
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
277 var this2 = this.getPt2();
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
278 this.setPosition(position(Math.max(this.x, p1.x), Math.max(this.y, p1.y)));
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
279 this.setPt2(position(Math.min(this2.x, p2.x), Math.min(this2.y, p2.y)));
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
280 return this;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
281 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
282 // returns the intersection of rectangle "rect" and this one
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
283 that.intersect = function(rect) {
847
c662bf335881 fixed a bug in geom.intersect
hertzhaft
parents: 812
diff changeset
284 var r = rect.copy();
c662bf335881 fixed a bug in geom.intersect
hertzhaft
parents: 812
diff changeset
285 var result = r.clipTo(this);
c662bf335881 fixed a bug in geom.intersect
hertzhaft
parents: 812
diff changeset
286 if (result.width < 0 || result.height < 0) result = null;
c662bf335881 fixed a bug in geom.intersect
hertzhaft
parents: 812
diff changeset
287 return result;
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
288 };
812
b484631f37c1 better intersect function
hertzhaft
parents: 807
diff changeset
289
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
290 // returns a copy of rectangle "rect" that fits into this one
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
291 // (moving it first)
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
292 that.fit = function(rect) {
847
c662bf335881 fixed a bug in geom.intersect
hertzhaft
parents: 812
diff changeset
293 var r = rect.copy();
c662bf335881 fixed a bug in geom.intersect
hertzhaft
parents: 812
diff changeset
294 r.x = Math.max(r.x, this.x);
c662bf335881 fixed a bug in geom.intersect
hertzhaft
parents: 812
diff changeset
295 r.y = Math.max(r.y, this.x);
c662bf335881 fixed a bug in geom.intersect
hertzhaft
parents: 812
diff changeset
296 if (r.x + r.width > this.x + this.width) {
c662bf335881 fixed a bug in geom.intersect
hertzhaft
parents: 812
diff changeset
297 r.x = this.x + this.width - r.width;
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
298 }
847
c662bf335881 fixed a bug in geom.intersect
hertzhaft
parents: 812
diff changeset
299 if (r.y + r.height > this.y + this.height) {
c662bf335881 fixed a bug in geom.intersect
hertzhaft
parents: 812
diff changeset
300 r.y = this.y + this.height - r.height;
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
301 }
847
c662bf335881 fixed a bug in geom.intersect
hertzhaft
parents: 812
diff changeset
302 return r.intersect(this);
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
303 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
304 // adjusts position and size of jQuery element "$elem" to this rectangle
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
305 that.adjustDiv = function($elem) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
306 $elem.offset({
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
307 left : this.x,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
308 top : this.y
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
309 });
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
310 $elem.width(this.width).height(this.height);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
311 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
312 // returns position and size of this rectangle in css-compatible format
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
313 that.getAsCss = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
314 return {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
315 left : this.x,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
316 top : this.y,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
317 width : this.width,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
318 height : this.height
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
319 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
320 };
807
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
321 // returns position and size of this rectangle formatted for SVG attributes
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
322 that.getAsSvg = function() {
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
323 return [this.x, this.y, this.width, this.height].join(" ");
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
324 };
1b1728926534 geometry: overlapsRect() plus some comment clarifications
hertzhaft
parents: 793
diff changeset
325 // returns size and position of this rectangle formatted for ??? (w x h@x,y)
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
326 that.toString = function() {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
327 return this.width + "x" + this.height + "@" + this.x + "," + this.y;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
328 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
329 return that;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
330 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
331
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
332 /*
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
333 * Transform class
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
334 *
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
335 * defines a class of affine transformations
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
336 */
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
337 var transform = function(spec) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
338 var that = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
339 m00 : 1.0,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
340 m01 : 0.0,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
341 m02 : 0.0,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
342 m10 : 0.0,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
343 m11 : 1.0,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
344 m12 : 0.0,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
345 m20 : 0.0,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
346 m21 : 0.0,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
347 m22 : 1.0
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
348 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
349 if (spec) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
350 jQuery.extend(that, spec);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
351 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
352 ;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
353 that.concat = function(trafA) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
354 // add Transform trafA to this Transform (i.e. this = trafC = trafA
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
355 // * this)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
356 var trafC = {};
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
357 for ( var i = 0; i < 3; i++) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
358 for ( var j = 0; j < 3; j++) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
359 var c = 0.0;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
360 for ( var k = 0; k < 3; k++) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
361 c += trafA["m" + i + k] * this["m" + k + j];
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
362 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
363 trafC["m" + i + j] = c;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
364 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
365 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
366 jQuery.extend(this, trafC);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
367 return this;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
368 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
369 that.transform = function(rect) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
370 // returns transformed Rectangle or Position with this Transform
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
371 // applied
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
372 var x = this.m00 * rect.x + this.m01 * rect.y + this.m02;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
373 var y = this.m10 * rect.x + this.m11 * rect.y + this.m12;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
374 var pt = position(x, y);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
375 if (rect.width) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
376 // transform the other corner point
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
377 var pt2 = this.transform(rect.getPt2());
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
378 return rectangle(pt, pt2);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
379 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
380 return pt;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
381 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
382 that.invtransform = function(rect) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
383 // returns transformed Rectangle or Position with the inverse of
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
384 // this Transform applied
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
385 var det = this.m00 * this.m11 - this.m01 * this.m10;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
386 var x = (this.m11 * rect.x - this.m01 * rect.y - this.m11
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
387 * this.m02 + this.m01 * this.m12)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
388 / det;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
389 var y = (-this.m10 * rect.x + this.m00 * rect.y + this.m10
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
390 * this.m02 - this.m00 * this.m12)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
391 / det;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
392 var pt = position(x, y);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
393 if (rect.width) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
394 // transform the other corner point
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
395 var pt2 = this.invtransform(rect.getPt2());
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
396 return rectangle(pt, pt2);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
397 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
398 return pt;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
399 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
400 that.toString = function(pretty) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
401 var s = '[';
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
402 if (pretty)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
403 s += '\n';
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
404 for ( var i = 0; i < 3; ++i) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
405 s += '[';
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
406 for ( var j = 0; j < 3; ++j) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
407 if (j)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
408 s += ',';
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
409 s += this['m' + i + j];
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
410 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
411 s += ']';
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
412 if (pretty)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
413 s += '\n';
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
414 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
415 s += ']';
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
416 if (pretty)
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
417 s += '\n';
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
418 return s;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
419 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
420 // add class methods to instance
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
421 that.getRotation = transform.getRotation;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
422 that.getRotationAround = transform.getRotationAround;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
423 that.getTranslation = transform.getTranslation;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
424 that.getMirror = transform.getMirror;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
425 that.getScale = transform.getScale;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
426
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
427 return that;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
428 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
429
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
430 transform.getRotation = function(angle) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
431 // returns a Transform that is a rotation by angle degrees around [0,0]
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
432 if (angle !== 0) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
433 var t = Math.PI * parseFloat(angle) / 180.0;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
434 var cost = Math.cos(t);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
435 var sint = Math.sin(t);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
436 var traf = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
437 m00 : cost,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
438 m01 : -sint,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
439 m10 : sint,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
440 m11 : cost
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
441 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
442 return transform(traf);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
443 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
444 return transform();
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
445 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
446
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
447 transform.getRotationAround = function(angle, pos) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
448 // returns a Transform that is a rotation by angle degrees around pos
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
449 var traf = transform.getTranslation(pos.neg());
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
450 traf.concat(transform.getRotation(angle));
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
451 traf.concat(transform.getTranslation(pos));
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
452 return traf;
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
453 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
454
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
455 transform.getTranslation = function(pos) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
456 // returns a Transform that is a translation by [pos.x, pos,y]
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
457 var traf = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
458 m02 : pos.x,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
459 m12 : pos.y
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
460 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
461 return transform(traf);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
462 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
463
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
464 transform.getMirror = function(type) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
465 // returns a Transform that is a mirror about the axis type
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
466 if (type === 'x') {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
467 var traf = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
468 m00 : 1,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
469 m11 : -1
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
470 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
471 } else {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
472 var traf = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
473 m00 : -1,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
474 m11 : 1
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
475 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
476 }
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
477 return transform(traf);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
478 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
479
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
480 transform.getScale = function(size) {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
481 // returns a Transform that is a scale by [size.width, size.height]
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
482 var traf = {
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
483 m00 : size.width,
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
484 m11 : size.height
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
485 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
486 return transform(traf);
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
487 };
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
488
786
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
489 // export constructor functions to digilib plugin
793
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
490 var geometry = {
786
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
491 size : size,
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
492 position : position,
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
493 rectangle : rectangle,
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
494 transform : transform
793
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
495 };
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
496 // install function called by digilib on plugin object
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
497 var install = function() {
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
498 // add constructor object to fn
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
499 this.fn.geometry = geometry;
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
500 };
793
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
501 // digilib plugin object
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
502 var plugin = {
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
503 name : 'geometry',
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
504 install : install,
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
505 fn : {},
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
506 // TODO: remove old init
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
507 init : init
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
508 };
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
509 // TODO: remove old version of init returning contructor
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
510 var init = function () {
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
511 return geometry;
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
512 };
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
513 // plug into digilib
786
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
514 if ($.fn.digilib == null) {
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
515 $.error("jquery.digilib.geometry must be loaded after jquery.digilib!");
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
516 } else {
793
63c1b33e38b1 documentation for new plugin api in jquery-digilib-plugin.txt.
robcast
parents: 788
diff changeset
517 $.fn.digilib('plugin', plugin);
786
868c2e795aca new plugin architecture.
robcast
parents: 785
diff changeset
518 }
785
b9a75079aece geometry as first digilib plugin.
robcast
parents:
diff changeset
519 })(jQuery);