clip_photo.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. var windowHeight = document.documentElement.clientHeight,
  2. windowWidth = document.documentElement.clientWidth,
  3. footerHeight = $("nav").height(),
  4. headerHeight = $("#header").height(),
  5. contentHeight = windowHeight - footerHeight - headerHeight,
  6. canvas = document.getElementById('clipImg'),
  7. imgWidth ,
  8. imgHeight,
  9. clickCount = 0;
  10. var Clip = {
  11. size: 200, //表示正方形的边长,乘上一个基数就是当前的像素值
  12. range: {}, //用来控制正方形的边界值
  13. topCss: (windowHeight - this.size) / 2,
  14. leftCss: (windowWidth - this.size) / 2,
  15. touchX: 0,
  16. touchY: 0,
  17. timer: null,
  18. imgUrl:"",
  19. init: function(){
  20. //等比例缩放图片(如果图片宽高都比容器小,则绘制的图片宽高 = 原图片的宽高。)
  21. //如果图片的宽度或者高度比容器大,则宽度或者高度 = 容器的宽度或者高度,另一高度或者宽度则等比例缩放
  22. var cxt = canvas.getContext('2d');
  23. var img = new Image();
  24. img.src = this.imgUrl;
  25. // img.src = "../images/xing.jpeg";
  26. img.onload = function(){
  27. if ( img.width < windowWidth && img.height < contentHeight) {
  28. imgWidth = img.width;
  29. imgHeight = img.height;
  30. } else {
  31. var pWidth = img.width / (img.height / contentHeight);
  32. var pHeight = img.height / (img.width / windowWidth);
  33. imgWidth = pWidth > windowWidth ? windowWidth : pWidth;
  34. imgHeight = pHeight > contentHeight ? contentHeight : pHeight;
  35. }
  36. canvas.height = imgHeight;
  37. canvas.width = imgWidth;
  38. canvas.style.left = (windowWidth - imgWidth) / 2 + 'px';;
  39. canvas.style.top = (contentHeight - imgHeight) / 2 + headerHeight + 'px';
  40. cxt.drawImage(img,0,0,imgWidth,imgHeight);
  41. Clip.changeBase(0);
  42. bindClipEvent();
  43. }
  44. },
  45. changeBase: function(index) {
  46. //这个函数用来改变正方形的边长
  47. this.size += index * 5 ;
  48. if(this.size > imgHeight || this.size > imgWidth){
  49. this.size = imgHeight>imgWidth ? imgWidth : imgHeight;
  50. }
  51. if((this.size + this.leftCss) > (parseFloat(canvas.style.left) + imgWidth)){
  52. var left = (this.size + this.leftCss) - (parseFloat(canvas.style.left) + imgWidth);
  53. this.leftCss = this.leftCss - left;
  54. }
  55. if((this.size + this.topCss) > (parseFloat(canvas.style.top) + imgHeight)){
  56. var top = (this.size + this.topCss) - (parseFloat(canvas.style.top) + imgHeight);
  57. this.topCss = this.topCss - top;
  58. }
  59. $("#clip").css({
  60. height: this.size + 'px',
  61. width: this.size + 'px',
  62. lineHeight: this.size + 'px'
  63. });
  64. this.changeRange();
  65. },
  66. changeRange: function() {
  67. //这个方法用来计算初始top,left以及边界值。相对clipContent而言
  68. if(isNaN(this.topCss)) {
  69. this.topCss = (contentHeight - this.size) / 2 + headerHeight;
  70. }
  71. if(isNaN(this.leftCss)) {
  72. this.leftCss = (windowWidth - this.size) / 2;
  73. }
  74. this.changeClipStyle(this.leftCss, this.topCss);
  75. //极限值,需控制不超出图片区域
  76. var minTop = (contentHeight - imgHeight)/2 + headerHeight;
  77. var maxTop = (contentHeight - imgHeight)/2+ headerHeight+ imgHeight - this.size;
  78. var minLeft = (windowWidth - imgWidth)/2;
  79. var maxLeft = (windowWidth - imgWidth)/2 + imgWidth - this.size;
  80. this.range = {
  81. minTop: minTop,
  82. maxTop: maxTop,
  83. minLeft: minLeft,
  84. maxLeft: maxLeft
  85. };
  86. },
  87. //设置新的样式,并改变当前的top和left值
  88. changeClipStyle: function(leftCss, topCss) {
  89. $('#clip').css({
  90. left: leftCss + 'px',
  91. top: topCss + 'px'
  92. });
  93. Clip.leftCss = leftCss;
  94. Clip.topCss = topCss;
  95. }
  96. };
  97. var bindClipEvent = function(){
  98. var clip = document.getElementById('clip');
  99. clip.addEventListener('touchstart', function(event) {
  100. event.preventDefault();
  101. var x = event.touches ? event.touches[0].clientX : event.screenX;
  102. var y = event.touches ? event.touches[0].clientY : event.screenY;
  103. Clip.touchX = x;
  104. Clip.touchY = y;
  105. });
  106. clip.addEventListener('touchmove', function(event) {
  107. if(Clip.timer != null) {
  108. return;
  109. }
  110. Clip.timer = setTimeout(function() {
  111. var x = event.touches ? event.touches[0].clientX : event.screenX;
  112. var y = event.touches ? event.touches[0].clientY : event.screenY;
  113. var disX = x - Clip.touchX;
  114. var disY = y - Clip.touchY;
  115. var nowLeft = Clip.leftCss + disX;
  116. var nowTop = Clip.topCss + disY;
  117. if(nowLeft < Clip.range.minLeft) {
  118. nowLeft = Clip.range.minLeft;
  119. }
  120. if(nowLeft > Clip.range.maxLeft) {
  121. nowLeft = Clip.range.maxLeft;
  122. }
  123. if(nowTop < Clip.range.minTop) {
  124. nowTop = Clip.range.minTop;
  125. }
  126. if(nowTop > Clip.range.maxTop) {
  127. nowTop = Clip.range.maxTop;
  128. }
  129. Clip.changeClipStyle(nowLeft, nowTop);
  130. Clip.touchX = x;
  131. Clip.touchY = y;
  132. Clip.timer = null;
  133. }, 20);
  134. });
  135. //按钮的事件
  136. $("#reduce").on('tap', function(){
  137. Clip.changeBase(-1);
  138. });
  139. $("#enlarge").on('tap', function(){
  140. Clip.changeBase(1);
  141. });
  142. $("#saveBtn").on('click', function(){
  143. if(clickCount == 0){
  144. clickCount ++;
  145. saveImage();
  146. }
  147. });
  148. $("#cancelBtn").on('click', function(){
  149. $("#clipPanel").addClass("c-hide");
  150. clearFile();
  151. $("#content").removeClass("c-hide");
  152. });
  153. };
  154. var clearFile = function(){
  155. var str ='<input type="file" class="c-hide" id="file_head" accept="image/*;capture=camera" onchange="clip_photo()">';
  156. $("#file_head").remove();
  157. $("#chooseImage").append(str);
  158. }
  159. var saveImage = function(){
  160. var width = Clip.size,
  161. height = Clip.size,
  162. x = Clip.leftCss - Clip.range.minLeft,
  163. y = Clip.topCss - Clip.range.minTop;
  164. var canvas2=$('<canvas width="'+width+'" height="'+height+'"></canvas>')[0],
  165. ctx=canvas2.getContext('2d');
  166. var image = new Image();
  167. image.src = Clip.imgUrl;
  168. image.onload = function(){
  169. var widthPix = imgWidth/image.width,
  170. heightPix = imgHeight / image.height;
  171. ctx.drawImage(image,x/widthPix,y/heightPix,width/widthPix,height/heightPix,0,0,width,height);
  172. var data=canvas2.toDataURL();
  173. data=data.split(',')[1];
  174. data=window.atob(data);
  175. var ia = new Uint8Array(data.length);
  176. for (var i = 0; i < data.length; i++) {
  177. ia[i] = data.charCodeAt(i);
  178. }
  179. var blob=new Blob([ia],{type:"image/png",endings:'transparent'});
  180. var fd=new FormData();
  181. fd.append('file',blob,'image.png');
  182. var new_url = URL.createObjectURL(blob);
  183. appendFile(new_url);
  184. $.ajax(server + 'upload/image', {
  185. data: fd,
  186. dataType: 'json',
  187. contentType: false,
  188. cache: false,
  189. processData: false,
  190. beforeSend: function(request) {
  191. request.setRequestHeader("userAgent", userAgent);
  192. },
  193. type: 'post',
  194. error: function(res) {
  195. if(res.status == 999 || res.status == 998 || res.status == 997){
  196. loginUrl(res.status);
  197. return;
  198. }
  199. clickCount = 0;
  200. },
  201. success: function(res) {
  202. if(res.status == 999 || res.status == 998 || res.status == 997){
  203. loginUrl(res.status);
  204. return;
  205. }
  206. var params = {};
  207. params.photo = res.urls;
  208. var patientUrl = res.urls;
  209. sendPost('patient/save', params, 'json', 'post', submitFailed, submitSuccess);
  210. clickCount = 0;
  211. clearFile();
  212. }
  213. });
  214. };
  215. };