main.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. window.onload = function () {
  2. 'use strict';
  3. var Cropper = window.Cropper;
  4. var URL = window.URL || window.webkitURL;
  5. var container = document.querySelector('.img-container');
  6. var image = container.getElementsByTagName('img').item(0);
  7. var download = document.getElementById('download');
  8. var actions = document.getElementById('actions');
  9. var dataX = document.getElementById('dataX');
  10. var dataY = document.getElementById('dataY');
  11. var dataHeight = document.getElementById('dataHeight');
  12. var dataWidth = document.getElementById('dataWidth');
  13. var dataRotate = document.getElementById('dataRotate');
  14. var dataScaleX = document.getElementById('dataScaleX');
  15. var dataScaleY = document.getElementById('dataScaleY');
  16. var dataX_text = document.getElementById('dataX_text');
  17. var dataY_text = document.getElementById('dataY_text');
  18. var dataHeight_text = document.getElementById('dataHeight_text');
  19. var dataWidth_text = document.getElementById('dataWidth_text');
  20. var dataRotate_text = document.getElementById('dataRotate_text');
  21. var dataScaleX_text = document.getElementById('dataScaleX_text');
  22. var dataScaleY_text = document.getElementById('dataScaleY_text');
  23. var options = {
  24. aspectRatio: aspectRatioValue,
  25. preview: '.img-preview',
  26. ready: function (e) {
  27. //console.log(e.type);
  28. var param = $('#param').val();
  29. if (param) {
  30. param = param.split('_');
  31. param[0] = parseFloat(param[0]);
  32. param[1] = parseFloat(param[1]);
  33. param[2] = parseFloat(param[2]);
  34. param[3] = parseFloat(param[3]);
  35. param[4] = parseFloat(param[4]);
  36. param[5] = parseFloat(param[5]);
  37. param[6] = parseFloat(param[6]);
  38. cropper.setData({"x":param[0],"y":param[1],"width":param[3],"height":param[2],"rotate":param[4],"scaleX":param[5],"scaleY":param[6]});
  39. }
  40. },
  41. cropstart: function (e) {
  42. //console.log(e.type, e.detail.action);
  43. },
  44. cropmove: function (e) {
  45. //console.log(e.type, e.detail.action);
  46. },
  47. cropend: function (e) {
  48. //console.log(e.type, e.detail.action);
  49. },
  50. crop: function (e) {
  51. var data = e.detail;
  52. //console.log(e.type);
  53. dataX.value = Math.round(data.x);
  54. dataY.value = Math.round(data.y);
  55. dataHeight.value = Math.round(data.height);
  56. dataWidth.value = Math.round(data.width);
  57. dataRotate.value = typeof data.rotate !== 'undefined' ? data.rotate : '';
  58. dataScaleX.value = typeof data.scaleX !== 'undefined' ? data.scaleX : '';
  59. dataScaleY.value = typeof data.scaleY !== 'undefined' ? data.scaleY : '';
  60. dataX_text.innerHTML = dataX.value;
  61. dataY_text.innerHTML = dataY.value;
  62. dataHeight_text.innerHTML = dataHeight.value;
  63. dataWidth_text.innerHTML = dataWidth.value;
  64. dataRotate_text.innerHTML = dataRotate.value;
  65. dataScaleX_text.innerHTML = dataScaleX.value;
  66. dataScaleY_text.innerHTML = dataScaleY.value;
  67. },
  68. zoom: function (e) {
  69. //console.log(e.type, e.detail.ratio);
  70. }
  71. };
  72. var cropper = new Cropper(image, options);
  73. var originalImageURL = image.src;
  74. var uploadedImageType = 'image/jpeg';
  75. var uploadedImageName = 'cropped.jpg';
  76. var uploadedImageURL;
  77. // Tooltip
  78. $('[data-toggle="tooltip"]').tooltip();
  79. // Buttons
  80. if (!document.createElement('canvas').getContext) {
  81. $('button[data-method="getCroppedCanvas"]').prop('disabled', true);
  82. }
  83. if (typeof document.createElement('cropper').style.transition === 'undefined') {
  84. $('button[data-method="rotate"]').prop('disabled', true);
  85. $('button[data-method="scale"]').prop('disabled', true);
  86. }
  87. // Download
  88. if (typeof download.download === 'undefined') {
  89. download.className += ' disabled';
  90. download.title = 'Your browser does not support download';
  91. }
  92. function aspectRatio(value)
  93. {
  94. if (!cropper) {
  95. return;
  96. }
  97. options.aspectRatio = value;
  98. // Restart
  99. cropper.destroy();
  100. cropper = new Cropper(image, options);
  101. }
  102. // Methods
  103. actions.querySelector('.docs-buttons').onclick = function (event) {
  104. var e = event || window.event;
  105. var target = e.target || e.srcElement;
  106. var cropped;
  107. var result;
  108. var input;
  109. var data;
  110. if (!cropper) {
  111. return;
  112. }
  113. while (target !== this) {
  114. if (target.getAttribute('data-method')) {
  115. break;
  116. }
  117. target = target.parentNode;
  118. }
  119. if (target === this || target.disabled || target.className.indexOf('disabled') > -1) {
  120. return;
  121. }
  122. data = {
  123. method: target.getAttribute('data-method'),
  124. value: target.getAttribute('data-value'),
  125. target: target.getAttribute('data-target'),
  126. option: target.getAttribute('data-option') || undefined,
  127. secondOption: target.getAttribute('data-second-option') || undefined
  128. };
  129. cropped = cropper.cropped;
  130. if (data.method) {
  131. if (data.method == 'save') {
  132. var option = {};
  133. option.param_x = dataX.value;
  134. option.param_y = dataY.value;
  135. option.param_h = dataHeight.value;
  136. option.param_w = dataWidth.value;
  137. option.param_r = dataRotate.value;
  138. option.param_sx = dataScaleX.value;
  139. option.param_sy = dataScaleY.value;
  140. option.pic = $('.img-container img').attr('src');
  141. var cas = cropper.getCroppedCanvas({ maxWidth: 4096, maxHeight: 4096 })
  142. option.img = cas.toDataURL('image/jpeg');//生成base64图片的格式
  143. /*
  144. cas.toBlob(function (e) {
  145. console.log(e); //生成Blob的图片格式
  146. })
  147. */
  148. option.key = $('#key').val();
  149. var url = config.host + 'save.cropper&json=1';
  150. $.post(url, option, function(t)
  151. {
  152. if (typeof t != 'object') {
  153. var t = eval('(' + t + ')');
  154. }
  155. var pic = t.url;
  156. var info = '';
  157. if (t.width) {
  158. info = t.width + '_' + t.height + '_' +t.size + '_' + t.fid;
  159. }
  160. var state = $('#state').val();
  161. window.parent.picSet(pic, state, info);
  162. })
  163. return;
  164. } else if (data.method == 'del') {
  165. var state = $('#state').val();
  166. window.parent.picDel(state);
  167. return;
  168. } else if (typeof data.target !== 'undefined') {
  169. input = document.querySelector(data.target);
  170. if (!target.hasAttribute('data-option') && data.target && input) {
  171. try {
  172. data.option = JSON.parse(input.value);
  173. } catch (e) {
  174. console.log(e.message);
  175. }
  176. }
  177. }
  178. switch (data.method) {
  179. case 'rotate':
  180. if (cropped && options.viewMode > 0) {
  181. cropper.clear();
  182. }
  183. break;
  184. case 'getCroppedCanvas':
  185. try {
  186. data.option = JSON.parse(data.option);
  187. } catch (e) {
  188. console.log(e.message);
  189. }
  190. if (uploadedImageType === 'image/jpeg') {
  191. if (!data.option) {
  192. data.option = {};
  193. }
  194. data.option.fillColor = '#fff';
  195. }
  196. break;
  197. }
  198. if (data.method != 'aspectRatio') {
  199. result = cropper[data.method](data.option, data.secondOption);
  200. }
  201. switch (data.method) {
  202. case 'aspectRatio':
  203. cropper.destroy();
  204. if (data.value.indexOf(':') == -1) {
  205. options.aspectRatio = data.value;
  206. } else {
  207. var tmp = data.value.split(':');
  208. options.aspectRatio = tmp[0]/tmp[1];
  209. }
  210. cropper = new Cropper(image, options);
  211. break;
  212. case 'rotate':
  213. if (cropped && options.viewMode > 0) {
  214. cropper.crop();
  215. }
  216. break;
  217. case 'scaleX':
  218. case 'scaleY':
  219. target.setAttribute('data-option', -data.option);
  220. break;
  221. case 'getCroppedCanvas':
  222. if (result) {
  223. // Bootstrap's Modal
  224. $('#getCroppedCanvasModal').modal().find('.modal-body').html(result);
  225. if (!download.disabled) {
  226. download.download = uploadedImageName;
  227. download.href = result.toDataURL(uploadedImageType);
  228. }
  229. }
  230. break;
  231. case 'destroy':
  232. cropper = null;
  233. if (uploadedImageURL) {
  234. URL.revokeObjectURL(uploadedImageURL);
  235. uploadedImageURL = '';
  236. image.src = originalImageURL;
  237. }
  238. break;
  239. }
  240. if (typeof result === 'object' && result !== cropper && input) {
  241. try {
  242. input.value = JSON.stringify(result);
  243. } catch (e) {
  244. console.log(e.message);
  245. }
  246. }
  247. }
  248. };
  249. document.body.onkeydown = function (event) {
  250. var e = event || window.event;
  251. if (e.target !== this || !cropper || this.scrollTop > 300) {
  252. return;
  253. }
  254. switch (e.keyCode) {
  255. case 37:
  256. e.preventDefault();
  257. cropper.move(-1, 0);
  258. break;
  259. case 38:
  260. e.preventDefault();
  261. cropper.move(0, -1);
  262. break;
  263. case 39:
  264. e.preventDefault();
  265. cropper.move(1, 0);
  266. break;
  267. case 40:
  268. e.preventDefault();
  269. cropper.move(0, 1);
  270. break;
  271. }
  272. };
  273. // Import image
  274. var inputImage = document.getElementById('inputImage');
  275. if (URL) {
  276. inputImage.onchange = function () {
  277. var files = this.files;
  278. var file;
  279. if (cropper && files && files.length) {
  280. file = files[0];
  281. if (/^image\/\w+/.test(file.type)) {
  282. uploadedImageType = file.type;
  283. uploadedImageName = file.name;
  284. if (uploadedImageURL) {
  285. URL.revokeObjectURL(uploadedImageURL);
  286. }
  287. image.src = uploadedImageURL = URL.createObjectURL(file);
  288. cropper.destroy();
  289. cropper = new Cropper(image, options);
  290. inputImage.value = null;
  291. } else {
  292. window.alert('Please choose an image file.');
  293. }
  294. }
  295. };
  296. } else {
  297. inputImage.disabled = true;
  298. inputImage.parentNode.className += ' disabled';
  299. }
  300. };