alloy_crop.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* AlloyCrop v0.1.3
  2. * By dntzhang
  3. * Github: https://github.com/AlloyTeam/AlloyFinger/tree/master/alloy_crop
  4. */
  5. ;(function(){
  6. var AlloyCrop = function (option) {
  7. this.renderTo = document.body;
  8. this.canvas = document.createElement("canvas");
  9. this.canvas.width = option.width;
  10. this.canvas.height = option.height;
  11. this.circle = option.circle;
  12. if (option.width !== option.height && option.circle) {
  13. throw "can't set circle to true when width is not equal to height"
  14. }
  15. this.ctx = this.canvas.getContext("2d");
  16. this.croppingBox = document.createElement("div");
  17. this.croppingBox.style.visibility = "hidden";
  18. this.cover = document.createElement("canvas");
  19. this.type = option.type || "png";
  20. this.cover.width = window.innerWidth;
  21. this.cover.height = window.innerHeight;
  22. this.cover_ctx = this.cover.getContext("2d");
  23. this.img = document.createElement("img");
  24. this.img.crossOrigin = "Anonymous";
  25. this.cancel = option.cancel;
  26. this.ok = option.ok;
  27. this.ok_text = option.ok_text || "ok";
  28. this.cancel_text = option.cancel_text || "cancel";
  29. this.croppingBox.appendChild(this.img);
  30. this.croppingBox.appendChild(this.cover);
  31. this.renderTo.appendChild(this.croppingBox);
  32. this.img.onload = this.init.bind(this);
  33. this.img.src = option.image_src;
  34. this.cancel_btn = document.createElement('a');
  35. this.cancel_btn.innerHTML = this.cancel_text;
  36. this.ok_btn = document.createElement('a');
  37. this.ok_btn.innerHTML = this.ok_text;
  38. this.croppingBox.appendChild(this.ok_btn);
  39. this.croppingBox.appendChild(this.cancel_btn);
  40. this.maxZoom = 2;
  41. };
  42. AlloyCrop.prototype = {
  43. init: function () {
  44. this.img_width = this.img.naturalWidth;
  45. this.img_height = this.img.naturalHeight;
  46. this.initScale = window.innerWidth / this.img_width;
  47. this.img_max_width = this.img_width * this.maxZoom;
  48. this.img_min_width = this.canvas.width;
  49. Transform(this.img);
  50. this.curScale = this.initScale;
  51. this.img.scaleX = this.img.scaleY = this.curScale;
  52. var initCr = this.img.getBoundingClientRect();
  53. var self = this;
  54. new AlloyFinger(this.croppingBox, {
  55. multipointStart: function () {
  56. self.curScale = self.img.scaleX;
  57. },
  58. pinch: function (evt) {
  59. self.img.scaleX = self.img.scaleY = self.curScale * evt.scale;
  60. var cr = self.img.getBoundingClientRect();
  61. if(self.img.scaleX >= self.initScale * self.maxZoom){
  62. self.img.scaleX = self.img.scaleY = self.initScale * self.maxZoom;
  63. }
  64. if(cr.width <= self.img_min_width){
  65. var scaleFn = function(){
  66. if( self.img.scaleX < self.initScale){
  67. self.img.scaleX = self.img.scaleY += 0.02;
  68. requestAnimationFrame(scaleFn);
  69. }else{
  70. self.img.scaleX = self.img.scaleY = self.initScale * 1;
  71. }
  72. }
  73. scaleFn();
  74. }
  75. },
  76. pressMove: function (evt) {
  77. //获得取景框的坐标
  78. var w = self.cover.width,
  79. h = self.cover.height,
  80. cw = self.canvas.width,
  81. ch = self.canvas.height,
  82. rx = w/2 - cw/2,
  83. ry = h/2 -ch/2;
  84. var cr = self.img.getBoundingClientRect();
  85. var tx = evt.deltaX,
  86. ty = evt.deltaY,
  87. sx = 0, sy = 0;
  88. if((cr.left + evt.deltaX) > rx){
  89. tx = rx - cr.left;
  90. } else if((cr.left + evt.deltaX + cr.width) < (rx + cw)){
  91. tx = (rx + cw) - (cr.left + cr.width);
  92. }
  93. if((cr.top + evt.deltaY) > ry){
  94. ty = ry - cr.top;
  95. } else if((cr.top + evt.deltaY + cr.height) < (ry +ch)){
  96. ty = (ry + ch) - (cr.top + cr.height);
  97. }
  98. self.img.translateX += tx;
  99. self.img.translateY += ty;
  100. evt.preventDefault();
  101. }
  102. });
  103. // new AlloyFinger(this.cancel_btn, {
  104. // click: this._cancel.bind(this)
  105. // });
  106. this.cancel_btn.addEventListener("click", function(){
  107. self._cancel();
  108. });
  109. // new AlloyFinger(this.ok_btn, {
  110. // click: this._ok.bind(this)
  111. // });
  112. this.ok_btn.addEventListener("click", function(){
  113. self._ok();
  114. });
  115. this.renderCover();
  116. this.setStyle();
  117. },
  118. _cancel: function () {
  119. this._css(this.croppingBox, {
  120. display: "none"
  121. });
  122. this.clearFile();
  123. this.cancel();
  124. },
  125. _ok: function () {
  126. this.crop();
  127. this._css(this.croppingBox, {
  128. display: "none"
  129. });
  130. this.ok(this.canvas.toDataURL("image/" + this.type), this.canvas);
  131. },
  132. renderCover: function () {
  133. var ctx = this.cover_ctx,
  134. w = this.cover.width,
  135. h = this.cover.height,
  136. cw = this.canvas.width,
  137. ch = this.canvas.height;
  138. ctx.save();
  139. ctx.fillStyle = "black";
  140. ctx.globalAlpha = 0.7;
  141. ctx.fillRect(0, 0, this.cover.width, this.cover.height);
  142. ctx.restore();
  143. ctx.save();
  144. ctx.globalCompositeOperation = "destination-out";
  145. ctx.beginPath();
  146. if (this.circle) {
  147. ctx.arc(w / 2, h / 2, cw / 2 - 4, 0, Math.PI * 2, false);
  148. } else {
  149. ctx.rect(w / 2 - cw / 2, h / 2 - ch / 2, cw, ch)
  150. }
  151. ctx.fill();
  152. ctx.restore();
  153. ctx.save();
  154. ctx.beginPath();
  155. ctx.strokeStyle = "white";
  156. if (this.circle) {
  157. ctx.arc(w / 2, h / 2, cw / 2 - 4, 0, Math.PI * 2, false);
  158. } else {
  159. ctx.rect(w / 2 - cw / 2, h / 2 - ch / 2, cw, ch)
  160. }
  161. ctx.stroke();
  162. },
  163. setStyle: function () {
  164. this._css(this.cover, {
  165. position: "absolute",
  166. zIndex: "100",
  167. left: "0px",
  168. top: "0px"
  169. });
  170. this._css(this.croppingBox, {
  171. color: "white",
  172. textAlign: "center",
  173. fontSize: "18px",
  174. textDecoration: "none",
  175. visibility: "visible"
  176. });
  177. this._css(this.img, {
  178. position: "absolute",
  179. zIndex: "99",
  180. left: "50%",
  181. // error position in meizu when set the top 50%
  182. top: window.innerHeight / 2 + "px",
  183. marginLeft: this.img_width / -2 + "px",
  184. marginTop: this.img_height / -2 + "px"
  185. });
  186. this._css(this.ok_btn, {
  187. position: "fixed",
  188. zIndex: "101",
  189. width: "100px",
  190. right: "50px",
  191. lineHeight: "40px",
  192. height: "40px",
  193. bottom: "20px",
  194. borderRadius: "2px",
  195. backgroundColor: "#836FFF"
  196. });
  197. this._css(this.cancel_btn, {
  198. position: "fixed",
  199. zIndex: "101",
  200. width: "100px",
  201. height: "40px",
  202. lineHeight: "40px",
  203. left: "50px",
  204. bottom: "20px",
  205. borderRadius: "2px",
  206. backgroundColor: "#836FFF"
  207. });
  208. },
  209. crop: function () {
  210. this.calculateRect();
  211. var self = this;
  212. var x0 = self.crop_rect[0],
  213. y0 = self.crop_rect[1],
  214. w = self.crop_rect[2],
  215. h = self.crop_rect[3];
  216. // self.ctx.drawImage(self.img, x0, y0, w, h, 0, 0, self.canvas.width * window.devicePixelRatio, self.canvas.height * window.devicePixelRatio);
  217. self.ctx.drawImage(self.img, x0, y0, w, h, 0, 0, self.canvas.width, self.canvas.height);
  218. },
  219. calculateRect: function () {
  220. var cr = this.img.getBoundingClientRect();
  221. var c_left = (window.innerWidth - this.canvas.width).toFixed(2) / 2;
  222. var c_top = (window.innerHeight - this.canvas.height).toFixed(2)/2;
  223. var cover_rect = [c_left, c_top, this.canvas.width + c_left, this.canvas.height + c_top];
  224. var cleft = Math.round(cr.left),
  225. ctop = Math.round(cr.top),
  226. cwidth = Math.round(cr.width),
  227. cheight = Math.round(cr.height);
  228. var img_rect = [cleft, ctop, cwidth + cleft, cheight + ctop];
  229. var intersect_rect = this.getOverlap.apply(this, cover_rect.concat(img_rect));
  230. var left = Math.round((intersect_rect[0] - img_rect[0]) / this.img.scaleX);
  231. var top = Math.round((intersect_rect[1] - img_rect[1]) / this.img.scaleY);
  232. var width = Math.round(intersect_rect[2] / this.img.scaleX);
  233. var height = Math.round(intersect_rect[3] / this.img.scaleY);
  234. if (left < 0) left = 0;
  235. if (top < 0) top = 0;
  236. if (left + width > this.img_width) width = this.img_width - left;
  237. if (top + height > this.img_height) height = this.img_height - top;
  238. this.crop_rect = [left, top, width, height];
  239. },
  240. // top left (x1,y1) and bottom right (x2,y2) coordination
  241. getOverlap: function (ax1, ay1, ax2, ay2, bx1, by1, bx2, by2) {
  242. if (ax2 < bx1 || ay2 < by1 || ax1 > bx2 || ay1 > by2) return [0, 0, 0, 0];
  243. var left = Math.max(ax1, bx1);
  244. var top = Math.max(ay1, by1);
  245. var right = Math.min(ax2, bx2);
  246. var bottom = Math.min(ay2, by2);
  247. return [left, top, right - left, bottom - top]
  248. },
  249. _css: function (el, obj) {
  250. for (var key in obj) {
  251. if (obj.hasOwnProperty(key)) {
  252. el.style[key] = obj[key];
  253. }
  254. }
  255. },
  256. clearFile: function(){
  257. var str ='<input type="file" class="c-hide" id="file_head" accept="image/*" capture="camera" onchange="clip_photo()">';
  258. $("#file_head").remove();
  259. $("#chooseImage").append(str);
  260. }
  261. };
  262. if (typeof module !== 'undefined' && typeof exports === 'object') {
  263. module.exports = AlloyCrop;
  264. }else {
  265. window.AlloyCrop = AlloyCrop;
  266. }
  267. })();