main.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. $(function () {
  2. 'use strict';
  3. var console = window.console || { log: function () {} },
  4. $alert = $('.docs-alert'),
  5. $message = $alert.find('.message'),
  6. showMessage = function (message, type) {
  7. $message.text(message);
  8. if (type) {
  9. $message.addClass(type);
  10. }
  11. $alert.fadeIn();
  12. setTimeout(function () {
  13. $alert.fadeOut();
  14. }, 3000);
  15. };
  16. // Demo
  17. // -------------------------------------------------------------------------
  18. (function () {
  19. var $image = $('.img-container > img'),
  20. $dataX = $('#dataX'),
  21. $dataY = $('#dataY'),
  22. $dataHeight = $('#dataHeight'),
  23. $dataWidth = $('#dataWidth'),
  24. $dataRotate = $('#dataRotate'),
  25. options = {
  26. aspectRatio: 16 / 9,
  27. preview: '.img-preview',
  28. crop: function (data) {
  29. $dataX.val(Math.round(data.x));
  30. $dataY.val(Math.round(data.y));
  31. $dataHeight.val(Math.round(data.height));
  32. $dataWidth.val(Math.round(data.width));
  33. $dataRotate.val(Math.round(data.rotate));
  34. }
  35. };
  36. $image.on({
  37. 'build.cropper': function (e) {
  38. console.log(e.type);
  39. },
  40. 'built.cropper': function (e) {
  41. console.log(e.type);
  42. },
  43. 'dragstart.cropper': function (e) {
  44. console.log(e.type, e.dragType);
  45. },
  46. 'dragmove.cropper': function (e) {
  47. console.log(e.type, e.dragType);
  48. },
  49. 'dragend.cropper': function (e) {
  50. console.log(e.type, e.dragType);
  51. },
  52. 'zoomin.cropper': function (e) {
  53. console.log(e.type);
  54. },
  55. 'zoomout.cropper': function (e) {
  56. console.log(e.type);
  57. }
  58. }).cropper(options);
  59. // Methods
  60. $(document.body).on('click', '[data-method]', function () {
  61. var data = $(this).data(),
  62. $target,
  63. result;
  64. if (data.method) {
  65. data = $.extend({}, data); // Clone a new one
  66. if (typeof data.target !== 'undefined') {
  67. $target = $(data.target);
  68. if (typeof data.option === 'undefined') {
  69. try {
  70. data.option = JSON.parse($target.val());
  71. } catch (e) {
  72. console.log(e.message);
  73. }
  74. }
  75. }
  76. result = $image.cropper(data.method, data.option);
  77. if (data.method === 'getCroppedCanvas') {
  78. $('#getCroppedCanvasModal').modal().find('.modal-body').html(result);
  79. }
  80. if ($.isPlainObject(result) && $target) {
  81. try {
  82. $target.val(JSON.stringify(result));
  83. } catch (e) {
  84. console.log(e.message);
  85. }
  86. }
  87. }
  88. }).on('keydown', function (e) {
  89. if (this.scrollTop > 300) {
  90. return;
  91. }
  92. switch (e.which) {
  93. case 37:
  94. e.preventDefault();
  95. $image.cropper('move', -1, 0);
  96. break;
  97. case 38:
  98. e.preventDefault();
  99. $image.cropper('move', 0, -1);
  100. break;
  101. case 39:
  102. e.preventDefault();
  103. $image.cropper('move', 1, 0);
  104. break;
  105. case 40:
  106. e.preventDefault();
  107. $image.cropper('move', 0, 1);
  108. break;
  109. }
  110. });
  111. // Import image
  112. var $inputImage = $('#inputImage'),
  113. URL = window.URL || window.webkitURL,
  114. blobURL;
  115. if (URL) {
  116. $inputImage.change(function () {
  117. var files = this.files,
  118. file;
  119. if (files && files.length) {
  120. file = files[0];
  121. if (/^image\/\w+$/.test(file.type)) {
  122. blobURL = URL.createObjectURL(file);
  123. $image.one('built.cropper', function () {
  124. URL.revokeObjectURL(blobURL); // Revoke when load complete
  125. }).cropper('reset', true).cropper('replace', blobURL);
  126. $inputImage.val('');
  127. } else {
  128. showMessage('Please choose an image file.');
  129. }
  130. }
  131. });
  132. } else {
  133. $inputImage.parent().remove();
  134. }
  135. // Options
  136. $('.docs-options :checkbox').on('change', function () {
  137. var $this = $(this);
  138. options[$this.val()] = $this.prop('checked');
  139. $image.cropper('destroy').cropper(options);
  140. });
  141. // Tooltips
  142. $('[data-toggle="tooltip"]').tooltip();
  143. }());
  144. // Examples
  145. // -------------------------------------------------------------------------
  146. // Example 1
  147. (function () {
  148. $('.cropper-example-1 > img').cropper({
  149. aspectRatio: 16 / 9,
  150. autoCropArea: 0.65,
  151. strict: false,
  152. guides: false,
  153. highlight: false,
  154. dragCrop: false,
  155. movable: false,
  156. resizable: false
  157. });
  158. })();
  159. // Example 2
  160. (function () {
  161. var $image = $('#cropper-example-2 > img'),
  162. canvasData,
  163. cropBoxData;
  164. $('#cropper-example-2-modal').on('shown.bs.modal', function () {
  165. $image.cropper({
  166. autoCropArea: 0.5,
  167. built: function () {
  168. $image.cropper('setCanvasData', canvasData);
  169. $image.cropper('setCropBoxData', cropBoxData);
  170. }
  171. });
  172. }).on('hidden.bs.modal', function () {
  173. canvasData = $image.cropper('getCanvasData');
  174. cropBoxData = $image.cropper('getCropBoxData');
  175. $image.cropper('destroy');
  176. });
  177. })();
  178. });