jquery.jWindowCrop.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. @licstart The following is the entire license notice for the
  3. JavaScript code in this page.
  4. Copyright (c) 2012 Tyler Brown
  5. Permission is hereby granted, free of charge, to any person obtaining
  6. a copy of this software and associated documentation files (the
  7. "Software"), to deal in the Software without restriction, including
  8. without limitation the rights to use, copy, modify, merge, publish,
  9. distribute, sublicense, and/or sell copies of the Software, and to
  10. permit persons to whom the Software is furnished to do so, subject to
  11. the following conditions:
  12. The above copyright notice and this permission notice shall be included
  13. in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  17. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  18. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  19. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  20. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. @licend The above is the entire license notice
  22. for the JavaScript code in this page.
  23. */
  24. (function($){
  25. function fillContainer(val, targetLength, containerLength) { // ensure that no gaps are between target's edges and container's edges
  26. if(val + targetLength < containerLength) val = containerLength-targetLength;
  27. if(val > 0) val = 0;
  28. return val;
  29. }
  30. $.jWindowCrop = function(image, options){
  31. var base = this;
  32. base.$image = $(image); // target image jquery element
  33. base.image = image; // target image dom element
  34. base.$image.data("jWindowCrop", base); // target frame jquery element
  35. base.namespace = 'jWindowCrop';
  36. base.originalWidth = 0;
  37. base.isDragging = false;
  38. base.init = function(){
  39. base.$image.css({display:'none'}); // hide image until loaded
  40. base.options = $.extend({},$.jWindowCrop.defaultOptions, options);
  41. if(base.options.zoomSteps < 2) base.options.zoomSteps = 2;
  42. base.$image.addClass('jwc_image').wrap('<div class="jwc_frame" />'); // wrap image in frame
  43. base.$frame = base.$image.parent();
  44. base.$frame.append('<div class="jwc_loader">' + base.options.loadingText + '</div>');
  45. base.$frame.append('<div class="jwc_controls" style="display:'+(base.options.showControlsOnStart ? 'block' : 'none')+';"><span>click to drag</span><a href="#" class="jwc_zoom_in"></a><a href="#" class="jwc_zoom_out"></a></div>');
  46. base.$frame.css({'overflow': 'hidden', 'position': 'relative', 'width': base.options.targetWidth, 'height': base.options.targetHeight});
  47. base.$image.css({'position': 'absolute', 'top': '0px', 'left': '0px'});
  48. initializeDimensions();
  49. base.$frame.find('.jwc_zoom_in').on('click.'+base.namespace, base.zoomIn);
  50. base.$frame.find('.jwc_zoom_out').on('click.'+base.namespace, base.zoomOut);
  51. base.$frame.on('mouseenter.'+base.namespace, handleMouseEnter);
  52. base.$frame.on('mouseleave.'+base.namespace, handleMouseLeave);
  53. base.$image.on('load.'+base.namespace, handeImageLoad);
  54. base.$image.on('mousedown.'+base.namespace+' touchstart.'+base.namespace, handleMouseDown);
  55. $(document).on('mousemove.'+base.namespace+' touchmove.'+base.namespace, handleMouseMove);
  56. $(document).on('mouseup.'+base.namespace+' touchend.'+base.namespace, handleMouseUp);
  57. };
  58. base.destroy = function() {
  59. base.$image.removeData("jWindowCrop"); // remove data
  60. // $(document).unbind(); // remove body binds
  61. base.$image.unbind(); // remove image binds
  62. base.$frame.unbind(); // remove frame binds
  63. base.$frame.find('.jwc_zoom_out').unbind(); // remove zoom triggers
  64. base.$frame.find('.jwc_zoom_in').unbind(); // remove zoom triggers
  65. $('.jwc_loader').remove(); // remove the added text
  66. $('.jwc_controls').remove(); // remove the added controls
  67. base.$image.removeAttr( 'style' ); // undo the style
  68. base.$image.unwrap(); // undo the wrap
  69. };
  70. base.setZoom = function(percent) {
  71. if(base.minPercent >= 1) {
  72. percent = base.minPercent;
  73. } else if(percent > 1.0) {
  74. percent = 1;
  75. } else if(percent < base.minPercent) {
  76. percent = base.minPercent;
  77. }
  78. base.$image.width(Math.ceil(base.originalWidth*percent));
  79. base.workingPercent = percent;
  80. focusOnCenter();
  81. updateResult();
  82. };
  83. base.zoomIn = function() {
  84. var zoomIncrement = (1.0 - base.minPercent) / (base.options.zoomSteps-1);
  85. base.setZoom(base.workingPercent+zoomIncrement);
  86. return false;
  87. };
  88. base.zoomOut = function() {
  89. var zoomIncrement = (1.0 - base.minPercent) / (base.options.zoomSteps-1);
  90. base.setZoom(base.workingPercent-zoomIncrement);
  91. return false;
  92. };
  93. function initializeDimensions() {
  94. if(base.originalWidth == 0) {
  95. base.originalWidth = base.$image[0].width;
  96. base.originalHeight = base.$image[0].height;
  97. }
  98. if(base.originalWidth > 0) {
  99. var widthRatio = base.options.targetWidth / base.originalWidth;
  100. var heightRatio = base.options.targetHeight / base.originalHeight;
  101. //base.minPercent = (widthRatio >= heightRatio) ? widthRatio : heightRatio;
  102. if(widthRatio >= heightRatio) {
  103. base.minPercent = (base.originalWidth < base.options.targetWidth) ? (base.options.targetWidth / base.originalWidth) : widthRatio;
  104. } else {
  105. base.minPercent = (base.originalHeight < base.options.targetHeight) ? (base.options.targetHeight / base.originalHeight) : heightRatio;
  106. }
  107. base.focalPoint = {'x': Math.round(base.originalWidth/2), 'y': Math.round(base.originalHeight/2)};
  108. base.setZoom(base.minPercent);
  109. base.$image.show(); //display image now that it has loaded
  110. }
  111. }
  112. function storeFocalPoint() {
  113. var x = (parseInt(base.$image.css('left'))*-1 + base.options.targetWidth/2) / base.workingPercent;
  114. var y = (parseInt(base.$image.css('top'))*-1 + base.options.targetHeight/2) / base.workingPercent;
  115. base.focalPoint = {'x': Math.round(x), 'y': Math.round(y)};
  116. }
  117. function focusOnCenter() {
  118. var left = fillContainer((Math.round((base.focalPoint.x*base.workingPercent) - base.options.targetWidth/2)*-1), base.$image.width(), base.options.targetWidth);
  119. var top = fillContainer((Math.round((base.focalPoint.y*base.workingPercent) - base.options.targetHeight/2)*-1), base.$image.height(), base.options.targetHeight);
  120. base.$image.css({'left': (left.toString()+'px'), 'top': (top.toString()+'px')})
  121. storeFocalPoint();
  122. }
  123. function updateResult() {
  124. base.result = {
  125. cropX: Math.floor(parseInt(base.$image.css('left'))/base.workingPercent*-1),
  126. cropY: Math.floor(parseInt(base.$image.css('top'))/base.workingPercent*-1),
  127. cropW: Math.round(base.options.targetWidth/base.workingPercent),
  128. cropH: Math.round(base.options.targetHeight/base.workingPercent),
  129. mustStretch: (base.minPercent > 1)
  130. };
  131. base.options.onChange.call(base.image, base.result);
  132. }
  133. function handeImageLoad() {
  134. initializeDimensions();
  135. }
  136. function handleMouseDown(event) {
  137. event.preventDefault(); //some browsers do image dragging themselves
  138. base.isDragging = true;
  139. base.dragMouseCoords = {x: event.pageX || event.originalEvent.touches[0].pageX, y: event.pageY || event.originalEvent.touches[0].pageY};
  140. base.dragImageCoords = {x: parseInt(base.$image.css('left')), y: parseInt(base.$image.css('top'))}
  141. }
  142. function handleMouseUp() {
  143. base.isDragging = false;
  144. }
  145. function handleMouseMove(event) {
  146. if(base.isDragging) {
  147. var xDif = (event.pageX || event.originalEvent.touches[0].pageX) - base.dragMouseCoords.x;
  148. var yDif = (event.pageY || event.originalEvent.touches[0].pageY) - base.dragMouseCoords.y;
  149. var newLeft = fillContainer((base.dragImageCoords.x + xDif), base.$image.width(), base.options.targetWidth);
  150. var newTop = fillContainer((base.dragImageCoords.y + yDif), base.$image.height(), base.options.targetHeight);
  151. base.$image.css({'left' : (newLeft.toString()+'px'), 'top' : (newTop.toString()+'px')});
  152. storeFocalPoint();
  153. updateResult();
  154. }
  155. }
  156. function handleMouseEnter() {
  157. if(base.options.smartControls) base.$frame.find('.jwc_controls').fadeIn('fast');
  158. }
  159. function handleMouseLeave() {
  160. if(base.options.smartControls) base.$frame.find('.jwc_controls').fadeOut('fast');
  161. }
  162. base.init();
  163. };
  164. $.jWindowCrop.defaultOptions = {
  165. targetWidth: 320,
  166. targetHeight: 180,
  167. zoomSteps: 10,
  168. loadingText: 'Loading...',
  169. smartControls: true,
  170. showControlsOnStart: true,
  171. onChange: function() {}
  172. };
  173. $.fn.jWindowCrop = function(options){
  174. return this.each(function(){
  175. (new $.jWindowCrop(this, options));
  176. });
  177. };
  178. $.fn.getjWindowCrop = function(){
  179. return this.data("jWindowCrop");
  180. };
  181. })(jQuery);