clip_photo.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="author" content="yihu.com" />
  6. <meta name="format-detection" content="telephone=no" />
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  8. <meta name="apple-mobile-web-app-capable" content="yes" />
  9. <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  10. <title>剪切图片</title>
  11. <link rel="stylesheet" type="text/css" href="../../../common/cross/css/cross.css"/>
  12. <link rel="stylesheet" type="text/css" href="../../../common/cross/css/cross.ui.css"/>
  13. <style type="text/css">
  14. .clip-img{
  15. position: absolute;
  16. left: 0;
  17. right: 0;
  18. bottom: 0;
  19. top: 0;
  20. width: 100%;
  21. height: 100%;
  22. margin: 0;
  23. }
  24. .clip-img img{
  25. position: absolute;
  26. width: auto;
  27. height: auto;
  28. max-width: 100%;
  29. max-height: 100%;
  30. }
  31. #clip{
  32. position: absolute;
  33. z-index: 100;
  34. /*border: 1px solid #ccc;*/
  35. outline: rgba(0, 0, 0, 0.8) solid 10000px;
  36. }
  37. nav{height: 40px;}
  38. nav > div:first-child{
  39. border-right: 1px solid #cbcbcb;
  40. }
  41. .bgc-000{
  42. background-color: #000;
  43. }
  44. .c-a8 {
  45. color: #a8a8a8;
  46. }
  47. </style>
  48. </head>
  49. <body class="bgc-000">
  50. <div class="h45">
  51. <div class="demo-comtop">
  52. <a class="mui-action-back"></a>
  53. <h1 class="c-f18">我的资料</h1>
  54. <div class="header-link" id="save">
  55. <a class="link" >完成</a>
  56. </div>
  57. </div>
  58. </div>
  59. <div class="mui-content">
  60. <div class="clip-img" id="clipContent">
  61. <canvas id="clipImg" width="" height="" style="position: absolute"></canvas>
  62. <div id="clip">
  63. </div>
  64. </div>
  65. </div>
  66. <nav>
  67. <div id="reduce" class="c-a8">
  68. <img src="../images/suoxiao_icon.png" width="18" class="mr10">
  69. <span class="c-f16 ">缩小</span>
  70. </div>
  71. <div id="enlarge" class="c-a8">
  72. <img src="../images/fangda_icon.png" width="18" class="mr10">
  73. <span class="c-f16 ">放大</span>
  74. </div>
  75. </nav>
  76. <script src="../../../js/jquery/2.1.3/jquery.js"></script>
  77. <script src="../../../js/mui.min.js"></script>
  78. <script type="text/javascript" src="../../../js/common_http.js"></script>
  79. <script src="../js/upload_photo_modify.js" type="text/javascript" charset="utf-8"></script>
  80. <script src="../../js/common.js" type="text/javascript" charset="utf-8"></script>
  81. <script src="../../../js/app.js" type="text/javascript" charset="utf-8"></script>
  82. <script type="text/javascript">
  83. var srcUrl, imgName;
  84. mui.init();
  85. mui.plusReady(function(){
  86. var self = plus.webview.currentWebview();
  87. srcUrl = self.srcUrl;
  88. imgName = self.imgName;
  89. // $("#img").attr('src', srcUrl);
  90. // console.log($("#img").width()+' '+ $("#img").height());
  91. // Clip.changeBase(0);
  92. // bindEvent();
  93. Clip.init();
  94. });
  95. var windowHeight = document.documentElement.clientHeight,
  96. windowWidth = document.documentElement.clientWidth,
  97. headerHeight = $(".h45").height(),
  98. footerHeight = $("nav").height(),
  99. contentHeight = windowHeight - headerHeight - footerHeight,
  100. imgHeight, imgWidth;
  101. var canvas = document.getElementById("clipImg");
  102. var Clip = {
  103. size: 200, //表示正方形的边长,乘上一个基数就是当前的像素值
  104. range: {}, //用来控制正方形的边界值
  105. topCss: (windowHeight - this.size) /2,
  106. leftCss: ($('body').width() - this.size)/2,
  107. touchX: 0,
  108. touchY: 0,
  109. timer: null,
  110. init: function(){
  111. //等比例缩放图片(如果图片宽高都比容器小,则绘制的图片宽高 = 原图片的宽高。)
  112. //如果图片的宽度或者高度比容器大,则宽度或者高度 = 容器的宽度或者高度,另一高度或者宽度则等比例缩放
  113. var cxt = canvas.getContext('2d');
  114. var img = new Image();
  115. img.src = srcUrl;
  116. img.onload = function(){
  117. if ( img.width < windowWidth && img.height < contentHeight) {
  118. imgWidth = img.width;
  119. imgHeight = img.height;
  120. } else {
  121. var pWidth = img.width / (img.height / contentHeight);
  122. var pHeight = img.height / (img.width / windowWidth);
  123. imgWidth = pWidth > windowWidth ? windowWidth : pWidth;
  124. imgHeight = pHeight > contentHeight ? contentHeight : pHeight;
  125. }
  126. canvas.height = imgHeight;
  127. canvas.width = imgWidth;
  128. canvas.style.left = (windowWidth - imgWidth) / 2 + 'px';;
  129. canvas.style.top = (contentHeight - imgHeight) / 2 + headerHeight + 'px';
  130. cxt.drawImage(img,0,0,imgWidth,imgHeight);
  131. Clip.changeBase(0);
  132. bindEvent();
  133. }
  134. },
  135. changeBase: function(index){
  136. //这个函数用来改变正方形的边长
  137. this.size += index*5;
  138. if(this.size > imgHeight || this.size > imgWidth){
  139. this.size = imgHeight>imgWidth ? imgWidth : imgHeight;
  140. }
  141. if((this.size + this.leftCss) > (parseFloat(canvas.style.left) + imgWidth)){
  142. var left = (this.size + this.leftCss) - (parseFloat(canvas.style.left) + imgWidth);
  143. this.leftCss = this.leftCss - left;
  144. }
  145. if((this.size + this.topCss) > (parseFloat(canvas.style.top) + imgHeight)){
  146. var top = (this.size + this.topCss) - (parseFloat(canvas.style.top) + imgHeight);
  147. this.topCss = this.topCss - top;
  148. }
  149. $("#clip").css({
  150. height : this.size+'px',
  151. width : this.size + 'px',
  152. lineHeight: this.size+'px'
  153. });
  154. this.changeRange();
  155. },
  156. changeRange: function(){
  157. //这个方法用来计算初始top,left以及边界值。相对clipContent而言
  158. if(isNaN(this.topCss)) {
  159. this.topCss = (contentHeight - this.size) / 2 + headerHeight;
  160. }
  161. if(isNaN(this.leftCss)) {
  162. this.leftCss = (windowWidth - this.size) / 2;
  163. }
  164. this.changeClipStyle(this.leftCss, this.topCss);
  165. //极限值,需控制不超出图片区域
  166. var minTop = (contentHeight - imgHeight)/2 + headerHeight;
  167. var maxTop = (contentHeight - imgHeight)/2+ headerHeight+ imgHeight - this.size;
  168. var minLeft = (windowWidth - imgWidth)/2;
  169. var maxLeft = (windowWidth - imgWidth)/2 + imgWidth - this.size;
  170. this.range = {
  171. minTop: minTop,
  172. maxTop: maxTop,
  173. minLeft: minLeft,
  174. maxLeft: maxLeft
  175. };
  176. },
  177. //设置新的样式,并改变当前的top和left值
  178. changeClipStyle: function(leftCss, topCss){
  179. $('#clip').css({
  180. left: leftCss + 'px',
  181. top: topCss + 'px'
  182. });
  183. Clip.leftCss = leftCss;
  184. Clip.topCss = topCss;
  185. }
  186. };
  187. function bindEvent(){
  188. var clip = document.getElementById('clip');
  189. clip.addEventListener('touchstart', function (event) {
  190. event.preventDefault();
  191. var x = event.touches ? event.touches[0].clientX : event.screenX;
  192. var y = event.touches ? event.touches[0].clientY : event.screenY;
  193. Clip.touchX = x;
  194. Clip.touchY = y;
  195. });
  196. clip.addEventListener('touchmove', function(event){
  197. if(Clip.timer != null){
  198. return;
  199. }
  200. Clip.timer = setTimeout(function(){
  201. var x = event.touches ? event.touches[0].clientX : event.screenX;
  202. var y = event.touches ? event.touches[0].clientY : event.screenY;
  203. var disX = x - Clip.touchX;
  204. var disY = y - Clip.touchY;
  205. var nowLeft = Clip.leftCss + disX;
  206. var nowTop = Clip.topCss + disY;
  207. if(nowLeft < Clip.range.minLeft){
  208. nowLeft = Clip.range.minLeft;
  209. }
  210. if(nowLeft > Clip.range.maxLeft){
  211. nowLeft = Clip.range.maxLeft;
  212. }
  213. if(nowTop < Clip.range.minTop){
  214. nowTop = Clip.range.minTop;
  215. }
  216. if(nowTop > Clip.range.maxTop){
  217. nowTop = Clip.range.maxTop;
  218. }
  219. Clip.changeClipStyle(nowLeft, nowTop);
  220. Clip.touchX = x;
  221. Clip.touchY = y;
  222. Clip.timer = null;
  223. }, 20);
  224. });
  225. //bind event
  226. mui('body').on('tap', '#enlarge', function(e){
  227. Clip.changeBase(1);
  228. });
  229. mui('body').on('tap', '#reduce', function(e){
  230. Clip.changeBase(-1);
  231. });
  232. mui('body').on('tap', '#save', function(e){
  233. // console.log('save picture srcUrl = '+srcUrl);
  234. // console.log('file name = '+imgName);
  235. // 获取width和height的百分比
  236. var widthPix = ((Clip.size / imgWidth).toFixed(2) * 100).toFixed(2);
  237. var heightPix = ((Clip.size / imgHeight).toFixed(2) * 100).toFixed(2);
  238. var topPix = (((Clip.topCss - Clip.range.minTop) / imgHeight).toFixed(2) * 100).toFixed(2);
  239. var leftPix = (((Clip.leftCss - Clip.range.minLeft) / imgWidth).toFixed(2) * 100).toFixed(2);
  240. var backToPre = function(){
  241. var old_back = mui.back;
  242. mui.back=function(){
  243. var gerenziliaoview = plus.webview.getWebviewById("gerenziliao");
  244. if(gerenziliaoview){
  245. mui.fire(gerenziliaoview,"getDocInfo");
  246. }
  247. old_back();
  248. };
  249. mui.back();
  250. };
  251. compressImg(topPix, leftPix, widthPix, heightPix, backToPre);
  252. });
  253. }
  254. </script>
  255. </body>
  256. </html>