jQueryRotate.2.2.js 11 KB

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