jQueryRotate.2.2.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // JavaScript Document
  2. // VERSION: 2.2 LAST UPDATE: 13.03.2012
  3. /*
  4. * Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
  5. *
  6. * Made by Wilq32, wilq32@gmail.com, Wroclaw, Poland, 01.2009
  7. * Website: http://code.google.com/p/jqueryrotate/
  8. */
  9. // Documentation removed from script file (was kinda useless and outdated)
  10. (function ($) {
  11. var supportedCSS, styles = document.getElementsByTagName("head")[0].style,
  12. toCheck = "transformProperty WebkitTransform OTransform msTransform MozTransform".split(" ");
  13. for (var a = 0; a < toCheck.length; a++) if (styles[toCheck[a]] !== undefined) supportedCSS = toCheck[a];
  14. // Bad eval to preven google closure to remove it from code o_O
  15. // After compresion replace it back to var IE = 'v' == '\v'
  16. var IE = eval('"v"=="\v"');
  17. jQuery.fn.extend({
  18. rotate: function (parameters) {
  19. if (this.length === 0 || typeof parameters == "undefined") return;
  20. if (typeof parameters == "number") parameters = {angle: parameters};
  21. var returned = [];
  22. for (var i = 0, i0 = this.length; i < i0; i++) {
  23. var element = this.get(i);
  24. if (!element.Wilq32 || !element.Wilq32.PhotoEffect) {
  25. var paramClone = $.extend(true, {}, parameters);
  26. var newRotObject = new Wilq32.PhotoEffect(element, paramClone)._rootObj;
  27. returned.push($(newRotObject));
  28. } else {
  29. element.Wilq32.PhotoEffect._handleRotation(parameters);
  30. }
  31. }
  32. return returned;
  33. },
  34. getRotateAngle: function () {
  35. var ret = [];
  36. for (var i = 0, i0 = this.length; i < i0; i++) {
  37. var element = this.get(i);
  38. if (element.Wilq32 && element.Wilq32.PhotoEffect) {
  39. ret[i] = element.Wilq32.PhotoEffect._angle;
  40. }
  41. }
  42. return ret;
  43. },
  44. stopRotate: function () {
  45. for (var i = 0, i0 = this.length; i < i0; i++) {
  46. var element = this.get(i);
  47. if (element.Wilq32 && element.Wilq32.PhotoEffect) {
  48. clearTimeout(element.Wilq32.PhotoEffect._timer);
  49. }
  50. }
  51. }
  52. });
  53. // Library agnostic interface
  54. Wilq32 = window.Wilq32 || {};
  55. Wilq32.PhotoEffect = (function () {
  56. if (supportedCSS) {
  57. return function (img, parameters) {
  58. img.Wilq32 = {
  59. PhotoEffect: this
  60. };
  61. this._img = this._rootObj = this._eventObj = img;
  62. this._handleRotation(parameters);
  63. }
  64. } else {
  65. return function (img, parameters) {
  66. // Make sure that class and id are also copied - just in case you would like to refeer to an newly created object
  67. this._img = img;
  68. this._rootObj = document.createElement('span');
  69. this._rootObj.style.display = "inline-block";
  70. this._rootObj.Wilq32 =
  71. {
  72. PhotoEffect: this
  73. };
  74. img.parentNode.insertBefore(this._rootObj, img);
  75. if (img.complete) {
  76. this._Loader(parameters);
  77. } else {
  78. var self = this;
  79. // TODO: Remove jQuery dependency
  80. jQuery(this._img).bind("load", function () {
  81. self._Loader(parameters);
  82. });
  83. }
  84. }
  85. }
  86. })();
  87. Wilq32.PhotoEffect.prototype = {
  88. _setupParameters: function (parameters) {
  89. this._parameters = this._parameters || {};
  90. if (typeof this._angle !== "number") this._angle = 0;
  91. if (typeof parameters.angle === "number") this._angle = parameters.angle;
  92. this._parameters.animateTo = (typeof parameters.animateTo === "number") ? (parameters.animateTo) : (this._angle);
  93. this._parameters.step = parameters.step || this._parameters.step || null;
  94. this._parameters.easing = parameters.easing || this._parameters.easing || function (x, t, b, c, d) {
  95. return -c * ((t = t / d - 1) * t * t * t - 1) + b;
  96. };
  97. this._parameters.duration = parameters.duration || this._parameters.duration || 1000;
  98. this._parameters.callback = parameters.callback || this._parameters.callback || function () {
  99. };
  100. if (parameters.bind && parameters.bind != this._parameters.bind) this._BindEvents(parameters.bind);
  101. },
  102. _handleRotation: function (parameters) {
  103. this._setupParameters(parameters);
  104. if (this._angle == this._parameters.animateTo) {
  105. this._rotate(this._angle);
  106. } else {
  107. this._animateStart();
  108. }
  109. },
  110. _BindEvents: function (events) {
  111. if (events && this._eventObj) {
  112. // Unbinding previous Events
  113. if (this._parameters.bind) {
  114. var oldEvents = this._parameters.bind;
  115. for (var a in oldEvents) if (oldEvents.hasOwnProperty(a))
  116. // TODO: Remove jQuery dependency
  117. jQuery(this._eventObj).unbind(a, oldEvents[a]);
  118. }
  119. this._parameters.bind = events;
  120. for (var a in events) if (events.hasOwnProperty(a))
  121. // TODO: Remove jQuery dependency
  122. jQuery(this._eventObj).bind(a, events[a]);
  123. }
  124. },
  125. _Loader: (function () {
  126. if (IE)
  127. return function (parameters) {
  128. var width = this._img.width;
  129. var height = this._img.height;
  130. this._img.parentNode.removeChild(this._img);
  131. this._vimage = this.createVMLNode('image');
  132. this._vimage.src = this._img.src;
  133. this._vimage.style.height = height + "px";
  134. this._vimage.style.width = width + "px";
  135. this._vimage.style.position = "absolute"; // FIXES IE PROBLEM - its only rendered if its on absolute position!
  136. this._vimage.style.top = "0px";
  137. this._vimage.style.left = "0px";
  138. /* Group minifying a small 1px precision problem when rotating object */
  139. this._container = this.createVMLNode('group');
  140. this._container.style.width = width;
  141. this._container.style.height = height;
  142. this._container.style.position = "absolute";
  143. this._container.setAttribute('coordsize', width - 1 + ',' + (height - 1)); // This -1, -1 trying to fix ugly problem with small displacement on IE
  144. this._container.appendChild(this._vimage);
  145. this._rootObj.appendChild(this._container);
  146. this._rootObj.style.position = "relative"; // FIXES IE PROBLEM
  147. this._rootObj.style.width = width + "px";
  148. this._rootObj.style.height = height + "px";
  149. this._rootObj.setAttribute('id', this._img.getAttribute('id'));
  150. this._rootObj.className = this._img.className;
  151. this._eventObj = this._rootObj;
  152. this._handleRotation(parameters);
  153. };
  154. else
  155. return function (parameters) {
  156. this._rootObj.setAttribute('id', this._img.getAttribute('id'));
  157. this._rootObj.className = this._img.className;
  158. this._width = this._img.width;
  159. this._height = this._img.height;
  160. this._widthHalf = this._width / 2; // used for optimisation
  161. this._heightHalf = this._height / 2;// used for optimisation
  162. var _widthMax = Math.sqrt((this._height) * (this._height) + (this._width) * (this._width));
  163. this._widthAdd = _widthMax - this._width;
  164. this._heightAdd = _widthMax - this._height; // widthMax because maxWidth=maxHeight
  165. this._widthAddHalf = this._widthAdd / 2; // used for optimisation
  166. this._heightAddHalf = this._heightAdd / 2;// used for optimisation
  167. this._img.parentNode.removeChild(this._img);
  168. this._aspectW = ((parseInt(this._img.style.width, 10)) || this._width) / this._img.width;
  169. this._aspectH = ((parseInt(this._img.style.height, 10)) || this._height) / this._img.height;
  170. this._canvas = document.createElement('canvas');
  171. this._canvas.setAttribute('width', this._width);
  172. this._canvas.style.position = "relative";
  173. this._canvas.style.left = -this._widthAddHalf + "px";
  174. this._canvas.style.top = -this._heightAddHalf + "px";
  175. this._canvas.Wilq32 = this._rootObj.Wilq32;
  176. this._rootObj.appendChild(this._canvas);
  177. this._rootObj.style.width = this._width + "px";
  178. this._rootObj.style.height = this._height + "px";
  179. this._eventObj = this._canvas;
  180. this._cnv = this._canvas.getContext('2d');
  181. this._handleRotation(parameters);
  182. }
  183. })(),
  184. _animateStart: function () {
  185. if (this._timer) {
  186. clearTimeout(this._timer);
  187. }
  188. this._animateStartTime = +new Date;
  189. this._animateStartAngle = this._angle;
  190. this._animate();
  191. },
  192. _animate: function () {
  193. var actualTime = +new Date;
  194. var checkEnd = actualTime - this._animateStartTime > this._parameters.duration;
  195. // TODO: Bug for animatedGif for static rotation ? (to test)
  196. if (checkEnd && !this._parameters.animatedGif) {
  197. clearTimeout(this._timer);
  198. } else {
  199. if (this._canvas || this._vimage || this._img) {
  200. var angle = this._parameters.easing(0, actualTime - this._animateStartTime, this._animateStartAngle, this._parameters.animateTo - this._animateStartAngle, this._parameters.duration);
  201. this._rotate((~~(angle * 10)) / 10);
  202. }
  203. if (this._parameters.step) {
  204. this._parameters.step(this._angle);
  205. }
  206. var self = this;
  207. this._timer = setTimeout(function () {
  208. self._animate.call(self);
  209. }, 10);
  210. }
  211. // To fix Bug that prevents using recursive function in callback I moved this function to back
  212. if (this._parameters.callback && checkEnd) {
  213. this._angle = this._parameters.animateTo;
  214. this._rotate(this._angle);
  215. this._parameters.callback.call(this._rootObj);
  216. }
  217. },
  218. _rotate: (function () {
  219. var rad = Math.PI / 180;
  220. if (IE)
  221. return function (angle) {
  222. this._angle = angle;
  223. this._container.style.rotation = (angle % 360) + "deg";
  224. };
  225. else if (supportedCSS)
  226. return function (angle) {
  227. this._angle = angle;
  228. this._img.style[supportedCSS] = "rotate(" + (angle % 360) + "deg)";
  229. };
  230. else
  231. return function (angle) {
  232. this._angle = angle;
  233. angle = (angle % 360) * rad;
  234. // clear canvas
  235. this._canvas.width = this._width + this._widthAdd;
  236. this._canvas.height = this._height + this._heightAdd;
  237. // REMEMBER: all drawings are read from backwards.. so first function is translate, then rotate, then translate, translate..
  238. this._cnv.translate(this._widthAddHalf, this._heightAddHalf); // at least center image on screen
  239. this._cnv.translate(this._widthHalf, this._heightHalf); // we move image back to its orginal
  240. this._cnv.rotate(angle); // rotate image
  241. this._cnv.translate(-this._widthHalf, -this._heightHalf); // move image to its center, so we can rotate around its center
  242. this._cnv.scale(this._aspectW, this._aspectH); // SCALE - if needed ;)
  243. this._cnv.drawImage(this._img, 0, 0); // First - we draw image
  244. }
  245. })()
  246. };
  247. if (IE) {
  248. Wilq32.PhotoEffect.prototype.createVMLNode = (function () {
  249. document.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
  250. try {
  251. !document.namespaces.rvml && document.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
  252. return function (tagName) {
  253. return document.createElement('<rvml:' + tagName + ' class="rvml">');
  254. };
  255. } catch (e) {
  256. return function (tagName) {
  257. return document.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
  258. };
  259. }
  260. })();
  261. }
  262. })(jQuery);