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