alloy_crop.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 self = this;
  53. new AlloyFinger(this.croppingBox, {
  54. multipointStart: function () {
  55. self.curScale = self.img.scaleX;
  56. },
  57. pinch: function (evt) {
  58. self.img.scaleX = self.img.scaleY = self.curScale * evt.scale;
  59. var cr = self.img.getBoundingClientRect();
  60. if(self.img.scaleX >= self.initScale * self.maxZoom){
  61. self.img.scaleX = self.img.scaleY = self.initScale * self.maxZoom;
  62. }
  63. if(cr.width <= self.img_min_width){
  64. var scaleFn = function(){
  65. if( self.img.scaleX < self.initScale){
  66. self.img.scaleX = self.img.scaleY += 0.02;
  67. requestAnimationFrame(scaleFn);
  68. }else{
  69. self.img.scaleX = self.img.scaleY = self.initScale * 1;
  70. }
  71. }
  72. scaleFn();
  73. }
  74. },
  75. pressMove: function (evt) {
  76. //获得取景框的坐标
  77. var w = self.cover.width,
  78. h = self.cover.height,
  79. cw = self.canvas.width,
  80. ch = self.canvas.height,
  81. rx = w/2 - cw/2,
  82. ry = h/2 -ch/2;
  83. var cr = self.img.getBoundingClientRect();
  84. var tx = evt.deltaX,
  85. ty = evt.deltaY,
  86. sx = 0, sy = 0;
  87. if((cr.left + evt.deltaX) > rx){
  88. tx = rx - cr.left;
  89. } else if((cr.left + evt.deltaX + cr.width) < (rx + cw)){
  90. tx = (rx + cw) - (cr.left + cr.width);
  91. }
  92. if((cr.top + evt.deltaY) > ry){
  93. ty = ry - cr.top;
  94. } else if((cr.top + evt.deltaY + cr.height) < (ry +ch)){
  95. ty = (ry + ch) - (cr.top + cr.height);
  96. }
  97. self.img.translateX += tx;
  98. self.img.translateY += ty;
  99. evt.preventDefault();
  100. }
  101. });
  102. new AlloyFinger(this.cancel_btn, {
  103. tap: this._cancel.bind(this)
  104. });
  105. new AlloyFinger(this.ok_btn, {
  106. tap: this._ok.bind(this)
  107. });
  108. this.renderCover();
  109. this.setStyle();
  110. },
  111. _cancel: function () {
  112. this._css(this.croppingBox, {
  113. display: "none"
  114. });
  115. this.cancel();
  116. },
  117. _ok: function () {
  118. this.crop();
  119. this._css(this.croppingBox, {
  120. display: "none"
  121. });
  122. this.ok(this.canvas.toDataURL("image/" + this.type), this.canvas);
  123. },
  124. renderCover: function () {
  125. var ctx = this.cover_ctx,
  126. w = this.cover.width,
  127. h = this.cover.height,
  128. cw = this.canvas.width,
  129. ch = this.canvas.height;
  130. ctx.save();
  131. ctx.fillStyle = "black";
  132. ctx.globalAlpha = 0.7;
  133. ctx.fillRect(0, 0, this.cover.width, this.cover.height);
  134. ctx.restore();
  135. ctx.save();
  136. ctx.globalCompositeOperation = "destination-out";
  137. ctx.beginPath();
  138. if (this.circle) {
  139. ctx.arc(w / 2, h / 2, cw / 2 - 4, 0, Math.PI * 2, false);
  140. } else {
  141. ctx.rect(w / 2 - cw / 2, h / 2 - ch / 2, cw, ch)
  142. }
  143. ctx.fill();
  144. ctx.restore();
  145. ctx.save();
  146. ctx.beginPath();
  147. ctx.strokeStyle = "white";
  148. if (this.circle) {
  149. ctx.arc(w / 2, h / 2, cw / 2 - 4, 0, Math.PI * 2, false);
  150. } else {
  151. ctx.rect(w / 2 - cw / 2, h / 2 - ch / 2, cw, ch)
  152. }
  153. ctx.stroke();
  154. },
  155. setStyle: function () {
  156. this._css(this.cover, {
  157. position: "absolute",
  158. zIndex: "100",
  159. left: "0px",
  160. top: "0px"
  161. });
  162. this._css(this.croppingBox, {
  163. color: "white",
  164. textAlign: "center",
  165. fontSize: "18px",
  166. textDecoration: "none",
  167. visibility: "visible"
  168. });
  169. this._css(this.img, {
  170. position: "absolute",
  171. zIndex: "99",
  172. left: "50%",
  173. // error position in meizu when set the top 50%
  174. top: window.innerHeight / 2 + "px",
  175. marginLeft: this.img_width / -2 + "px",
  176. marginTop: this.img_height / -2 + "px"
  177. });
  178. this._css(this.ok_btn, {
  179. position: "fixed",
  180. zIndex: "101",
  181. width: "100px",
  182. right: "50px",
  183. lineHeight: "40px",
  184. height: "40px",
  185. bottom: "20px",
  186. borderRadius: "2px",
  187. backgroundColor: "#836FFF"
  188. });
  189. this._css(this.cancel_btn, {
  190. position: "fixed",
  191. zIndex: "101",
  192. width: "100px",
  193. height: "40px",
  194. lineHeight: "40px",
  195. left: "50px",
  196. bottom: "20px",
  197. borderRadius: "2px",
  198. backgroundColor: "#836FFF"
  199. });
  200. },
  201. crop: function () {
  202. this.calculateRect();
  203. var self = this;
  204. var x0 = self.crop_rect[0],
  205. y0 = self.crop_rect[1],
  206. w = self.crop_rect[2],
  207. h = self.crop_rect[3];
  208. // self.ctx.drawImage(self.img, x0, y0, w, h, 0, 0, self.canvas.width * window.devicePixelRatio, self.canvas.height * window.devicePixelRatio);
  209. self.ctx.drawImage(self.img, x0, y0, w, h, 0, 0, self.canvas.width, self.canvas.height);
  210. },
  211. calculateRect: function () {
  212. var cr = this.img.getBoundingClientRect();
  213. var c_left = (window.innerWidth - this.canvas.width).toFixed(2) / 2;
  214. var c_top = (window.innerHeight - this.canvas.height).toFixed(2)/2;
  215. var cover_rect = [c_left, c_top, this.canvas.width + c_left, this.canvas.height + c_top];
  216. var cleft = Math.round(cr.left),
  217. ctop = Math.round(cr.top),
  218. cwidth = Math.round(cr.width),
  219. cheight = Math.round(cr.height);
  220. var img_rect = [cleft, ctop, cwidth + cleft, cheight + ctop];
  221. var intersect_rect = this.getOverlap.apply(this, cover_rect.concat(img_rect));
  222. var left = Math.round((intersect_rect[0] - img_rect[0]) / this.img.scaleX);
  223. var top = Math.round((intersect_rect[1] - img_rect[1]) / this.img.scaleY);
  224. var width = Math.round(intersect_rect[2] / this.img.scaleX);
  225. var height = Math.round(intersect_rect[3] / this.img.scaleY);
  226. if (left < 0) left = 0;
  227. if (top < 0) top = 0;
  228. if (left + width > this.img_width) width = this.img_width - left;
  229. if (top + height > this.img_height) height = this.img_height - top;
  230. this.crop_rect = [left, top, width, height];
  231. },
  232. // top left (x1,y1) and bottom right (x2,y2) coordination
  233. getOverlap: function (ax1, ay1, ax2, ay2, bx1, by1, bx2, by2) {
  234. if (ax2 < bx1 || ay2 < by1 || ax1 > bx2 || ay1 > by2) return [0, 0, 0, 0];
  235. var left = Math.max(ax1, bx1);
  236. var top = Math.max(ay1, by1);
  237. var right = Math.min(ax2, bx2);
  238. var bottom = Math.min(ay2, by2);
  239. return [left, top, right - left, bottom - top]
  240. },
  241. _css: function (el, obj) {
  242. for (var key in obj) {
  243. if (obj.hasOwnProperty(key)) {
  244. el.style[key] = obj[key];
  245. }
  246. }
  247. }
  248. };
  249. if (typeof module !== 'undefined' && typeof exports === 'object') {
  250. module.exports = AlloyCrop;
  251. }else {
  252. window.AlloyCrop = AlloyCrop;
  253. }
  254. })();