Mercurial > hg > digilib-old
comparison client/digitallibrary/jquery/dlGeometry.js @ 664:d5a5ee4cbf04 jquery
work on getting transformation to work with rotation (currently still doesn't)
author | robcast |
---|---|
date | Mon, 24 Jan 2011 00:13:16 +0100 |
parents | 436e10669df8 |
children | a53c3e12995a |
comparison
equal
deleted
inserted
replaced
663:9791a2cec863 | 664:d5a5ee4cbf04 |
---|---|
223 * Transform class | 223 * Transform class |
224 * | 224 * |
225 * defines a class of affine transformations | 225 * defines a class of affine transformations |
226 */ | 226 */ |
227 var transform = function (spec) { | 227 var transform = function (spec) { |
228 var that = jQuery.extend({ | 228 var that = { |
229 m00 : 1.0, | 229 m00 : 1.0, |
230 m01 : 0.0, | 230 m01 : 0.0, |
231 m02 : 0.0, | 231 m02 : 0.0, |
232 m10 : 0.0, | 232 m10 : 0.0, |
233 m11 : 1.0, | 233 m11 : 1.0, |
234 m12 : 0.0, | |
234 m20 : 0.0, | 235 m20 : 0.0, |
235 m12 : 0.0, | |
236 m21 : 0.0, | 236 m21 : 0.0, |
237 m22 : 1.0 | 237 m22 : 1.0 |
238 }, spec); | 238 }; |
239 if (spec) { | |
240 jQuery.extend(that, spec); | |
241 }; | |
239 that.concat = function(traf) { | 242 that.concat = function(traf) { |
240 // add Transform traf to this Transform | 243 // add Transform traf to this Transform |
241 for (var i = 0; i < 3; i++) { | 244 for (var i = 0; i < 3; i++) { |
242 for (var j = 0; j < 3; j++) { | 245 for (var j = 0; j < 3; j++) { |
243 var c = 0.0; | 246 var c = 0.0; |
244 for (var k = 0; k < 3; k++) { | 247 for (var k = 0; k < 3; k++) { |
245 c += traf["m"+i+k] * this["m"+k+j]; | 248 c += traf["m"+i.toString()+k.toString()] * this["m"+k.toString()+j.toString()]; |
246 } | 249 } |
247 this["m"+i+j] = c; | 250 this["m"+i.toString()+j.toString()] = c; |
248 } | 251 } |
249 } | 252 } |
250 return this; | 253 return this; |
251 }; | 254 }; |
252 that.transform = function(rect) { | 255 that.transform = function(rect) { |
278 var height = y2 - y; | 281 var height = y2 - y; |
279 return rectangle(x, y, width, height); | 282 return rectangle(x, y, width, height); |
280 } | 283 } |
281 return position(x, y); | 284 return position(x, y); |
282 }; | 285 }; |
286 that.toString = function (pretty) { | |
287 var s = '['; | |
288 if (pretty) s += '\n'; | |
289 for (var i = 0; i < 3; ++i) { | |
290 s += '['; | |
291 for (var j = 0; j < 3; ++j) { | |
292 if (j) s += ','; | |
293 s += this['m'+i+j]; | |
294 } | |
295 s += ']'; | |
296 if (pretty) s += '\n'; | |
297 } | |
298 s += ']'; | |
299 if (pretty) s += '\n'; | |
300 return s; | |
301 }; | |
302 // add class methods to instance | |
283 that.getRotation = transform.getRotation; | 303 that.getRotation = transform.getRotation; |
304 that.getRotationAround = transform.getRotationAround; | |
284 that.getTranslation = transform.getTranslation; | 305 that.getTranslation = transform.getTranslation; |
285 that.getScale = transform.getScale; | 306 that.getScale = transform.getScale; |
286 | 307 |
287 return that; | 308 return that; |
288 }; | 309 }; |
289 | 310 |
290 transform.getRotation = function (angle, pos) { | 311 transform.getRotation = function (angle) { |
291 // returns a Transform that is a rotation by angle degrees around [pos.x, pos.y] | 312 // returns a Transform that is a rotation by angle degrees around [0,0] |
292 if (angle !== 0) { | 313 if (angle !== 0) { |
293 var t = 2.0 * Math.PI * parseFloat(angle) / 360.0; | 314 var t = Math.PI * parseFloat(angle) / 180.0; |
315 var cost = Math.cos(t); | |
316 var sint = Math.sin(t); | |
294 var traf = { | 317 var traf = { |
295 m00 : Math.cos(t), | 318 m00 : cost, |
296 m01 : - Math.sin(t), | 319 m01 : -sint, |
297 m10 : Math.sin(t), | 320 m10 : sint, |
298 m11 : Math.cos(t), | 321 m11 : cost |
299 m02 : pos.x - pos.x * Math.cos(t) + pos.y * Math.sin(t), | |
300 m12 : pos.y - pos.x * Math.sin(t) - pos.y * Math.cos(t) | |
301 }; | 322 }; |
302 return transform(traf); | 323 return transform(traf); |
303 } | 324 } |
304 return transform(); | 325 return transform(); |
305 }; | 326 }; |
311 m12 : pos.y | 332 m12 : pos.y |
312 }; | 333 }; |
313 return transform(traf); | 334 return transform(traf); |
314 }; | 335 }; |
315 | 336 |
337 transform.getRotationAround = function (angle, pos) { | |
338 // returns a Transform that is a rotation by angle degrees around pos | |
339 var traf = transform.getTranslation({x : -pos.x, y : -pos.y}); | |
340 traf.concat(transform.getRotation(angle)); | |
341 traf.concat(transform.getTranslation(pos)); | |
342 return traf; | |
343 }; | |
344 | |
316 transform.getScale = function (size) { | 345 transform.getScale = function (size) { |
317 // returns a Transform that is a scale by [size.width, size.height] | 346 // returns a Transform that is a scale by [size.width, size.height] |
318 var traf = { | 347 var traf = { |
319 m00 : size.width, | 348 m00 : size.width, |
320 m11 : size.height | 349 m11 : size.height |