main.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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.id = $('#id').val();
  149. option.group_id = $('#group_id').val();
  150. option.user_id = $('#user_id').val();
  151. option.name = $('#name').val();
  152. option.type = $('#type').val();
  153. var url = config.host;
  154. $.post(url, option, function(t)
  155. {
  156. if (typeof t != 'object') {
  157. var t = eval('(' + t + ')');
  158. }
  159. t = t.data
  160. var pic = t.url;
  161. var info = '';
  162. if (t.width) {
  163. info = t.width + '_' + t.height + '_' +t.size + '_' + t.fid;
  164. }
  165. var state = $('#state').val();
  166. //window.parent.picSet(pic, state, info);
  167. window.parent.postMessage({action: 'picSet', pic:pic, name:option.name, type:option.type}, '*')
  168. })
  169. return;
  170. } else if (data.method == 'del') {
  171. var state = $('#state').val();
  172. window.parent.picDel(state);
  173. return;
  174. } else if (typeof data.target !== 'undefined') {
  175. input = document.querySelector(data.target);
  176. if (!target.hasAttribute('data-option') && data.target && input) {
  177. try {
  178. data.option = JSON.parse(input.value);
  179. } catch (e) {
  180. console.log(e.message);
  181. }
  182. }
  183. }
  184. switch (data.method) {
  185. case 'rotate':
  186. if (cropped && options.viewMode > 0) {
  187. cropper.clear();
  188. }
  189. break;
  190. case 'getCroppedCanvas':
  191. try {
  192. data.option = JSON.parse(data.option);
  193. } catch (e) {
  194. console.log(e.message);
  195. }
  196. if (uploadedImageType === 'image/jpeg') {
  197. if (!data.option) {
  198. data.option = {};
  199. }
  200. data.option.fillColor = '#fff';
  201. }
  202. break;
  203. }
  204. if (data.method != 'aspectRatio') {
  205. result = cropper[data.method](data.option, data.secondOption);
  206. }
  207. switch (data.method) {
  208. case 'aspectRatio':
  209. cropper.destroy();
  210. if (data.value.indexOf(':') == -1) {
  211. options.aspectRatio = data.value;
  212. } else {
  213. var tmp = data.value.split(':');
  214. options.aspectRatio = tmp[0]/tmp[1];
  215. }
  216. cropper = new Cropper(image, options);
  217. break;
  218. case 'rotate':
  219. if (cropped && options.viewMode > 0) {
  220. cropper.crop();
  221. }
  222. break;
  223. case 'scaleX':
  224. case 'scaleY':
  225. target.setAttribute('data-option', -data.option);
  226. break;
  227. case 'getCroppedCanvas':
  228. if (result) {
  229. // Bootstrap's Modal
  230. $('#getCroppedCanvasModal').modal().find('.modal-body').html(result);
  231. if (!download.disabled) {
  232. download.download = uploadedImageName;
  233. download.href = result.toDataURL(uploadedImageType);
  234. }
  235. }
  236. break;
  237. case 'destroy':
  238. cropper = null;
  239. if (uploadedImageURL) {
  240. URL.revokeObjectURL(uploadedImageURL);
  241. uploadedImageURL = '';
  242. image.src = originalImageURL;
  243. }
  244. break;
  245. }
  246. if (typeof result === 'object' && result !== cropper && input) {
  247. try {
  248. input.value = JSON.stringify(result);
  249. } catch (e) {
  250. console.log(e.message);
  251. }
  252. }
  253. }
  254. };
  255. document.body.onkeydown = function (event) {
  256. var e = event || window.event;
  257. if (e.target !== this || !cropper || this.scrollTop > 300) {
  258. return;
  259. }
  260. switch (e.keyCode) {
  261. case 37:
  262. e.preventDefault();
  263. cropper.move(-1, 0);
  264. break;
  265. case 38:
  266. e.preventDefault();
  267. cropper.move(0, -1);
  268. break;
  269. case 39:
  270. e.preventDefault();
  271. cropper.move(1, 0);
  272. break;
  273. case 40:
  274. e.preventDefault();
  275. cropper.move(0, 1);
  276. break;
  277. }
  278. };
  279. // Import image
  280. var inputImage = document.getElementById('inputImage');
  281. if (URL) {
  282. inputImage.onchange = function () {
  283. var files = this.files;
  284. var file;
  285. if (cropper && files && files.length) {
  286. file = files[0];
  287. if (/^image\/\w+/.test(file.type)) {
  288. uploadedImageType = file.type;
  289. uploadedImageName = file.name;
  290. if (uploadedImageURL) {
  291. URL.revokeObjectURL(uploadedImageURL);
  292. }
  293. image.src = uploadedImageURL = URL.createObjectURL(file);
  294. cropper.destroy();
  295. cropper = new Cropper(image, options);
  296. inputImage.value = null;
  297. } else {
  298. window.alert('Please choose an image file.');
  299. }
  300. }
  301. };
  302. } else {
  303. inputImage.disabled = true;
  304. inputImage.parentNode.className += ' disabled';
  305. }
  306. };