/webportal/src/main/webapp/scripts/jquery.rotate.1-1.js

http://alageospatialportal.googlecode.com/ · JavaScript · 65 lines · 55 code · 9 blank · 1 comment · 15 complexity · 988e87b11d4f742c42d6471e4fb4c98a MD5 · raw file

  1. jQuery.fn.rotate = function(angle,whence) {
  2. var p = this.get(0);
  3. // we store the angle inside the image tag for persistence
  4. if (!whence) {
  5. p.angle = ((p.angle==undefined?0:p.angle) + angle) % 360;
  6. } else {
  7. p.angle = angle;
  8. }
  9. if (p.angle >= 0) {
  10. var rotation = Math.PI * p.angle / 180;
  11. } else {
  12. var rotation = Math.PI * (360+p.angle) / 180;
  13. }
  14. var costheta = Math.cos(rotation);
  15. var sintheta = Math.sin(rotation);
  16. if (document.all && !window.opera) {
  17. var canvas = document.createElement('img');
  18. canvas.src = p.src;
  19. canvas.height = p.height;
  20. canvas.width = p.width;
  21. canvas.style.filter = "progid:DXImageTransform.Microsoft.Matrix(M11="+costheta+",M12="+(-sintheta)+",M21="+sintheta+",M22="+costheta+",SizingMethod='auto expand')";
  22. } else {
  23. var canvas = document.createElement('canvas');
  24. if (!p.oImage) {
  25. canvas.oImage = new Image();
  26. canvas.oImage.src = p.src;
  27. } else {
  28. canvas.oImage = p.oImage;
  29. }
  30. canvas.style.width = canvas.width = Math.abs(costheta*canvas.oImage.width) + Math.abs(sintheta*canvas.oImage.height);
  31. canvas.style.height = canvas.height = Math.abs(costheta*canvas.oImage.height) + Math.abs(sintheta*canvas.oImage.width);
  32. var context = canvas.getContext('2d');
  33. context.save();
  34. if (rotation <= Math.PI/2) {
  35. context.translate(sintheta*canvas.oImage.height,0);
  36. } else if (rotation <= Math.PI) {
  37. context.translate(canvas.width,-costheta*canvas.oImage.height);
  38. } else if (rotation <= 1.5*Math.PI) {
  39. context.translate(-costheta*canvas.oImage.width,canvas.height);
  40. } else {
  41. context.translate(0,-sintheta*canvas.oImage.width);
  42. }
  43. context.rotate(rotation);
  44. context.drawImage(canvas.oImage, 0, 0, canvas.oImage.width, canvas.oImage.height);
  45. context.restore();
  46. }
  47. canvas.id = p.id;
  48. canvas.angle = p.angle;
  49. p.parentNode.replaceChild(canvas, p);
  50. }
  51. jQuery.fn.rotateRight = function(angle) {
  52. this.rotate(angle==undefined?90:angle);
  53. }
  54. jQuery.fn.rotateLeft = function(angle) {
  55. this.rotate(angle==undefined?-90:-angle);
  56. }