jQueryRotate.js 14 KB

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