Mercurial > hg > digilib
annotate client/digitallibrary/jquery/dlGeometry.js @ 661:692b1191f89a jquery
make zoomarea and birdview work in embedded mode
| author | hertzhaft |
|---|---|
| date | Mon, 24 Jan 2011 09:48:27 +0100 |
| parents | b0c6cc4a0932 |
| children | dcd64ecdd64a |
| rev | line source |
|---|---|
| 599 | 1 /* digilib geometry classes |
| 2 * should be integrated into jquery.digilib.js | |
| 3 */ | |
| 4 | |
| 5 var dlGeometry = function() { | |
| 6 /* | |
| 7 * Size class | |
| 8 */ | |
| 9 var size = function (w, h) { | |
| 10 var that = { | |
| 11 width : parseFloat(w), | |
| 12 height : parseFloat(h) | |
| 13 }; | |
| 14 that.equals = function(other) { | |
| 15 return (this.width === other.width && this.height === other.height); | |
| 16 }; | |
| 17 that.toString = function() { | |
| 18 return (this.width + "x" + this.height); | |
| 19 }; | |
| 20 return that; | |
| 21 }; | |
| 22 | |
| 23 /* | |
| 24 * Position class | |
| 25 */ | |
| 26 var position = function (x, y) { | |
| 647 | 27 if (typeof x === "object") { |
| 28 if (x instanceof jQuery) { | |
| 29 // jQuery object | |
| 30 var pos = x.offset(); | |
| 31 var that = { | |
| 32 x : pos.left, | |
| 33 y : pos.top | |
| 34 }; | |
| 35 } else { | |
| 36 // event object(?) | |
| 37 var that = { | |
| 38 x : x.pageX, | |
| 39 y : x.pageY | |
| 40 }; | |
| 41 } | |
| 42 } else { | |
| 43 var that = { | |
| 44 x : parseFloat(x), | |
| 45 y : parseFloat(y) | |
| 46 }; | |
| 47 } | |
| 599 | 48 that.equals = function(other) { |
| 49 return (this.x === other.x && this.y === other.y); | |
| 50 }; | |
| 51 that.toString = function() { | |
| 52 return (this.x + "," + this.y); | |
| 53 }; | |
| 54 return that; | |
| 55 }; | |
| 56 /* | |
| 57 * Rectangle class | |
| 58 */ | |
| 59 var rectangle = function (x, y, w, h) { | |
| 60 var that = {}; | |
| 61 if (typeof x === "object") { | |
| 647 | 62 if (x instanceof jQuery) { |
| 63 // jQuery object | |
| 64 var pos = x.offset(); | |
| 65 that = { | |
| 66 x : pos.left, | |
| 67 y : pos.top, | |
| 68 width : x.width(), | |
| 69 height : x.height() | |
| 70 }; | |
| 71 } else { | |
| 72 // assume x and y are Position | |
| 73 that = { | |
| 74 x : x.x, | |
| 75 y : x.y, | |
| 76 width : y.x - x.x, | |
| 77 height : y.y - x.y | |
| 78 }; | |
| 79 } | |
| 599 | 80 } else { |
| 81 that = { | |
| 82 x : parseFloat(x), | |
| 83 y : parseFloat(y), | |
| 84 width : parseFloat(w), | |
| 85 height : parseFloat(h) | |
| 86 }; | |
| 87 } | |
| 88 // returns a copy of this Rectangle | |
| 89 that.copy = function() { | |
| 90 return rectangle(this.x, this.y, this.width, this.height); | |
| 91 }; | |
| 92 // returns the position of this Rectangle | |
| 93 that.getPosition = function() { | |
| 94 return position(this.x, this.y); | |
| 95 }; | |
| 96 // returns the upper left corner position | |
| 97 that.getPt1 = that.getPosition; | |
| 98 // returns the lower right corner position of this Rectangle | |
| 99 that.getPt2 = function() { | |
| 100 return position(this.x + this.width, this.y + this.height); | |
| 101 }; | |
| 102 // sets the upper left corner to position pos | |
| 103 that.setPt1 = function(pos) { | |
| 104 this.x = pos.x; | |
| 105 this.y = pos.y; | |
| 106 return this; | |
| 107 }; | |
| 108 that.setPt2 = function(pos) { | |
| 109 // sets the lower right corner to position pos | |
| 110 this.width = pos.x - this.x; | |
| 111 this.height = pos.y - this.y; | |
| 112 return this; | |
| 113 }; | |
| 114 that.getCenter = function() { | |
| 115 // returns the center position of this Rectangle | |
| 116 return position(this.x + this.width / 2, this.y + this.height / 2); | |
| 117 }; | |
| 118 that.setCenter = function(pos) { | |
| 119 // moves this Rectangle's center to position pos | |
| 120 this.x = pos.x - this.width / 2; | |
| 121 this.y = pos.y - this.height / 2; | |
| 122 return this; | |
| 123 }; | |
| 124 that.getSize = function() { | |
| 125 // returns the size of this Rectangle | |
| 126 return size(this.width, this.height); | |
| 127 }; | |
| 128 that.equals = function(other) { | |
| 129 // equal props | |
| 130 var eq = (this.x === other.x && this.y === other.y && | |
| 131 this.width === other.width); | |
| 132 return eq; | |
| 133 }; | |
| 134 that.getArea = function() { | |
| 135 // returns the area of this Rectangle | |
| 136 return (this.width * this.height); | |
| 137 }; | |
| 138 that.normalize = function() { | |
| 139 // eliminates negative width and height | |
| 140 var p = this.getPt2(); | |
| 141 this.x = Math.min(this.x, p.x); | |
| 142 this.y = Math.min(this.y, p.y); | |
| 143 this.width = Math.abs(this.width); | |
| 144 this.height = Math.abs(this.height); | |
| 145 return this; | |
| 146 }; | |
| 147 that.containsPosition = function(pos) { | |
| 148 // returns if Position "pos" lies inside of this rectangle | |
| 149 var ct = ((pos.x >= this.x) && (pos.y >= this.y) && | |
| 647 | 150 (pos.x <= this.x + this.width) && (pos.y <= this.y + this.height)); |
| 599 | 151 return ct; |
| 152 }; | |
| 153 that.containsRect = function(rect) { | |
| 154 // returns if rectangle "rect" is contained in this rectangle | |
| 155 return (this.containsPosition(rect.getPt1()) && this.containsPosition(rect.getPt2())); | |
| 156 }; | |
| 157 that.stayInside = function(rect) { | |
| 158 // changes this rectangle's x/y values so it stays inside of rectangle rect | |
| 159 // keeping the proportions | |
| 160 if (this.x < rect.x) { | |
| 161 this.x = rect.x; | |
| 162 } | |
| 163 if (this.y < rect.y) { | |
| 164 this.y = rect.y; | |
| 165 } | |
| 166 if (this.x + this.width > rect.x + rect.width) { | |
| 167 this.x = rect.x + rect.width - this.width; | |
| 168 } | |
| 169 if (this.y + this.height > rect.y + rect.height) { | |
| 170 this.y = rect.y + rect.height - this.height; | |
| 171 } | |
| 172 return this; | |
| 173 }; | |
| 174 that.clipTo = function(rect) { | |
| 175 // clips this rectangle so it stays inside of rectangle rect | |
| 176 var p1 = rect.getPt1(); | |
| 177 var p2 = rect.getPt2(); | |
| 178 var this2 = this.getPt2(); | |
| 179 this.setPt1(position(Math.max(this.x, p1.x), Math.max(this.y, p1.y))); | |
| 180 this.setPt2(position(Math.min(this2.x, p2.x), Math.min(this2.y, p2.y))); | |
| 181 return this; | |
| 182 }; | |
| 183 that.intersect = function(rect) { | |
| 184 // returns the intersection of the given Rectangle and this one | |
| 185 // FIX ME: not really, it should return null if there is no overlap | |
| 186 var sec = rect.copy(); | |
| 187 if (sec.x < this.x) { | |
| 188 sec.width = sec.width - (this.x - sec.x); | |
| 189 sec.x = this.x; | |
| 190 } | |
| 191 if (sec.y < this.y) { | |
| 192 sec.height = sec.height - (this.y - sec.y); | |
| 193 sec.y = this.y; | |
| 194 } | |
| 195 if (sec.x + sec.width > this.x + this.width) { | |
| 196 sec.width = (this.x + this.width) - sec.x; | |
| 197 } | |
| 198 if (sec.y + sec.height > this.y + this.height) { | |
| 199 sec.height = (this.y + this.height) - sec.y; | |
| 200 } | |
| 201 return sec; | |
| 202 }; | |
| 203 that.fit = function(rect) { | |
| 204 // returns a Rectangle that fits into this one (by moving first) | |
| 205 var sec = rect.copy(); | |
| 206 sec.x = Math.max(sec.x, this.x); | |
| 207 sec.y = Math.max(sec.y, this.x); | |
| 208 if (sec.x + sec.width > this.x + this.width) { | |
| 209 sec.x = this.x + this.width - sec.width; | |
| 210 } | |
| 211 if (sec.y + sec.height > this.y + this.height) { | |
| 212 sec.y = this.y + this.height - sec.height; | |
| 213 } | |
| 214 return sec.intersect(this); | |
| 215 }; | |
| 216 that.toString = function() { | |
| 217 return this.width+"x"+this.height+"@"+this.x+","+this.y; | |
| 218 }; | |
| 219 return that; | |
| 220 }; | |
| 221 | |
| 222 /* | |
| 223 * Transform class | |
| 224 * | |
| 225 * defines a class of affine transformations | |
| 226 */ | |
| 227 var transform = function (spec) { | |
|
657
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
228 var that = { |
| 620 | 229 m00 : 1.0, |
| 230 m01 : 0.0, | |
| 231 m02 : 0.0, | |
| 232 m10 : 0.0, | |
| 233 m11 : 1.0, | |
|
657
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
234 m12 : 0.0, |
| 620 | 235 m20 : 0.0, |
| 236 m21 : 0.0, | |
| 237 m22 : 1.0 | |
|
657
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
238 }; |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
239 if (spec) { |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
240 jQuery.extend(that, spec); |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
241 }; |
| 599 | 242 that.concat = function(traf) { |
| 243 // add Transform traf to this Transform | |
| 244 for (var i = 0; i < 3; i++) { | |
| 245 for (var j = 0; j < 3; j++) { | |
| 246 var c = 0.0; | |
| 247 for (var k = 0; k < 3; k++) { | |
|
657
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
248 c += traf["m"+i.toString()+k.toString()] * this["m"+k.toString()+j.toString()]; |
| 599 | 249 } |
|
657
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
250 this["m"+i.toString()+j.toString()] = c; |
| 599 | 251 } |
| 252 } | |
| 253 return this; | |
| 254 }; | |
| 255 that.transform = function(rect) { | |
| 256 // returns transformed Rectangle or Position with this Transform applied | |
| 257 var x = this.m00 * rect.x + this.m01 * rect.y + this.m02; | |
| 258 var y = this.m10 * rect.x + this.m11 * rect.y + this.m12; | |
| 259 if (rect.width) { | |
| 260 // transform the other corner points | |
| 261 var pt2 = rect.getPt2(); | |
| 262 var x2 = this.m00 * pt2.x + this.m01 * pt2.y + this.m02; | |
| 263 var y2 = this.m10 * pt2.x + this.m11 * pt2.y + this.m12; | |
| 264 var width = x2 - x; | |
| 265 var height = y2 - y; | |
| 266 return rectangle(x, y, width, height); | |
| 267 } | |
| 268 return position(x, y); | |
| 269 }; | |
| 270 that.invtransform = function(rect) { | |
| 271 // returns transformed Rectangle or Position with the inverse of this Transform applied | |
| 272 var det = this.m00 * this.m11 - this.m01 * this.m10; | |
| 273 var x = (this.m11 * rect.x - this.m01 * rect.y - this.m11 * this.m02 + this.m01 * this.m12) / det; | |
| 274 var y = (- this.m10 * rect.x + this.m00 * rect.y + this.m10 * this.m02 - this.m00 * this.m12) / det; | |
| 275 if (rect.width) { | |
| 276 // transform the other corner points | |
| 277 var pt2 = rect.getPt2(); | |
| 278 var x2 = (this.m11 * pt2.x - this.m01 * pt2.y - this.m11 * this.m02 + this.m01 * this.m12) / det; | |
| 279 var y2 = (- this.m10 * pt2.x + this.m00 * pt2.y + this.m10 * this.m02 - this.m00 * this.m12) / det; | |
| 280 var width = x2 - x; | |
| 281 var height = y2 - y; | |
| 282 return rectangle(x, y, width, height); | |
| 283 } | |
| 284 return position(x, y); | |
| 285 }; | |
|
657
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
286 that.toString = function (pretty) { |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
287 var s = '['; |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
288 if (pretty) s += '\n'; |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
289 for (var i = 0; i < 3; ++i) { |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
290 s += '['; |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
291 for (var j = 0; j < 3; ++j) { |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
292 if (j) s += ','; |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
293 s += this['m'+i+j]; |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
294 } |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
295 s += ']'; |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
296 if (pretty) s += '\n'; |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
297 } |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
298 s += ']'; |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
299 if (pretty) s += '\n'; |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
300 return s; |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
301 }; |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
302 // add class methods to instance |
| 620 | 303 that.getRotation = transform.getRotation; |
|
657
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
304 that.getRotationAround = transform.getRotationAround; |
| 620 | 305 that.getTranslation = transform.getTranslation; |
| 306 that.getScale = transform.getScale; | |
| 648 | 307 |
| 599 | 308 return that; |
| 309 }; | |
| 310 | |
|
657
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
311 transform.getRotation = function (angle) { |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
312 // returns a Transform that is a rotation by angle degrees around [0,0] |
| 599 | 313 if (angle !== 0) { |
|
657
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
314 var t = Math.PI * parseFloat(angle) / 180.0; |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
315 var cost = Math.cos(t); |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
316 var sint = Math.sin(t); |
| 599 | 317 var traf = { |
|
657
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
318 m00 : cost, |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
319 m01 : -sint, |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
320 m10 : sint, |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
321 m11 : cost |
| 599 | 322 }; |
| 323 return transform(traf); | |
| 324 } | |
| 325 return transform(); | |
| 326 }; | |
| 327 | |
| 328 transform.getTranslation = function (pos) { | |
| 329 // returns a Transform that is a translation by [pos.x, pos,y] | |
| 330 var traf = { | |
| 331 m02 : pos.x, | |
| 332 m12 : pos.y | |
| 333 }; | |
| 334 return transform(traf); | |
| 335 }; | |
| 336 | |
|
657
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
337 transform.getRotationAround = function (angle, pos) { |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
338 // returns a Transform that is a rotation by angle degrees around pos |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
339 var traf = transform.getTranslation({x : -pos.x, y : -pos.y}); |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
340 traf.concat(transform.getRotation(angle)); |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
341 traf.concat(transform.getTranslation(pos)); |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
342 return traf; |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
343 }; |
|
b0c6cc4a0932
work on getting transformation to work with rotation (currently still doesn't)
robcast
parents:
649
diff
changeset
|
344 |
| 599 | 345 transform.getScale = function (size) { |
| 346 // returns a Transform that is a scale by [size.width, size.height] | |
| 347 var traf = { | |
| 348 m00 : size.width, | |
| 349 m11 : size.height | |
| 350 }; | |
| 351 return transform(traf); | |
| 352 }; | |
| 353 | |
| 354 // export functions | |
| 355 var that = { | |
| 356 size : size, | |
| 357 position : position, | |
| 358 rectangle : rectangle, | |
| 359 transform : transform | |
| 360 }; | |
| 361 | |
| 362 return that; | |
| 363 }; |
