qf-image-cropper.wxs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. /**
  2. * 图片编辑器-手势监听
  3. * 1. wxs 暂不支持 es6 语法
  4. * 2. 支持编译到微信小程序、QQ小程序、app-vue、H5上(uni-app 2.2.5及以上版本)
  5. */
  6. /** 图片偏移量 */
  7. var offset = { x: 0, y: 0 };
  8. /** 图片缩放比例 */
  9. var scale = 1;
  10. /** 图片最小缩放比例 */
  11. var minScale = 1;
  12. /** 图片旋转角度 */
  13. var rotate = 0;
  14. /** 触摸点 */
  15. var touches = [];
  16. /** 图片布局信息 */
  17. var img = {};
  18. /** 系统信息 */
  19. var sys = {};
  20. /** 裁剪区域布局信息 */
  21. var area = {};
  22. /** 触摸行为类型 */
  23. var touchType = '';
  24. /** 操作角的位置 */
  25. var activeAngle = 0;
  26. /** 裁剪区域布局信息偏移量 */
  27. var areaOffset = { left: 0, right: 0, top: 0, bottom: 0 };
  28. /** 容错值 */
  29. var fault = 0.000001;
  30. /**
  31. * 获取a、b两数中的最小正数
  32. * @param a
  33. * @param b
  34. */
  35. function minimum(a, b) {
  36. if (a > 0 && b < 0) return a;
  37. if (a < 0 && b > 0) return b;
  38. if (a > 0 && b > 0) return Math.min(a, b);
  39. return 0;
  40. }
  41. /**
  42. * 在容错访问内获取n近似值
  43. * @param n
  44. */
  45. function num(n) {
  46. var m = parseFloat((n).toFixed(6));
  47. return m === fault || m === -fault ? 0 : m;
  48. }
  49. /**
  50. * 比较a值在容错值范围内是否等于b值
  51. * @param a
  52. * @param b
  53. */
  54. function equalsByFault(a, b) {
  55. return Math.abs(a - b) <= fault;
  56. }
  57. /**
  58. * 比较a值在容错值范围内是否小于b值
  59. * @param a
  60. * @param b
  61. */
  62. function lessThanByFault(a, b) {
  63. var c = a - b;
  64. return c < 0 ? c < -fault : c < fault;
  65. }
  66. /**
  67. * 验证并获取有效最大值
  68. * @param v
  69. * @param max
  70. * @param isInclude
  71. * @param x
  72. * @param y
  73. * @param rate
  74. * @returns
  75. */
  76. function validMax(v, max, isInclude, x, y, rate) {
  77. if(typeof max === 'number') {
  78. if(isInclude && equalsByFault(max, y)) { // 宽高不等时,x轴用y轴值要做等比例转换
  79. var n = num(max * rate);
  80. if (n <= x) return n; // 转化后值在x轴最大值范围内
  81. return x; // 转化后值超出x轴最大值范围则用最大值
  82. }
  83. return max;
  84. }
  85. return v;
  86. }
  87. /**
  88. * 计算两点间距
  89. * @param {Object} touches 触摸点信息
  90. */
  91. function getDistanceByTouches(touches) {
  92. // 根据勾股定理求两点间距离
  93. var a = touches[1].pageX - touches[0].pageX;
  94. var b = touches[1].pageY - touches[0].pageY;
  95. var c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
  96. // 求两点间的中点坐标
  97. // 1. a、b可能为负值
  98. // 2. 在求a、b时,如用touches[1]减touches[0],则求中点坐标也得用touches[1]减a/2、b/2
  99. // 3. 同理,在求a、b时,也可用touches[0]减touches[1],则求中点坐标也得用touches[0]减a/2、b/2
  100. var x = touches[1].pageX - a / 2;
  101. var y = touches[1].pageY - b / 2;
  102. return { c, x, y };
  103. };
  104. /**
  105. * 修正取值
  106. * @param {Object} a
  107. * @param {Object} b
  108. * @param {Object} c
  109. * @param {Object} reverse 是否反向
  110. */
  111. function correctValue(a, b, c, reverse) {
  112. return num(reverse ? Math.max(Math.min(a, b), c) : Math.min(Math.max(a, b), c));
  113. }
  114. /**
  115. * 旋转90°或270°时检查边界:限制 x、y 拖动范围,禁止滑出边界
  116. * @param {Object} e 点坐标
  117. * @param {Object} xReverse x是否反向
  118. * @param {Object} yReverse y是否反向
  119. */
  120. function checkRotateRange(e, xReverse, yReverse) {
  121. var o = num((img.height - img.width) / 2); // 宽高差值一半
  122. return {
  123. x: correctValue(e.x, -img.height + o + area.width + area.left, area.left + o, xReverse),
  124. y: correctValue(e.y, -img.width - o + area.height + area.top, area.top - o, yReverse)
  125. };
  126. }
  127. /**
  128. * 检查边界:限制 x、y 拖动范围,禁止滑出边界
  129. * @param {Object} e 点坐标
  130. */
  131. function checkRange(e) {
  132. var r = rotate / 90 % 2;
  133. if(r === 1) { // 因图片宽高可能不等,翻转 90° 或 270° 后图片宽高需反着计算,且左右和上下边界要根据差值做偏移
  134. if (area.width === area.height) {
  135. return checkRotateRange(e, img.height < area.height, img.width < area.width);
  136. }
  137. var isInclude = img.height < area.width && img.width < area.height; // 图片是否包含在裁剪区域内
  138. if (img.width < area.height || img.height < area.width) {
  139. if (area.width < area.height && img.width < img.height) {
  140. return isInclude
  141. ? checkRotateRange(e, area.width < area.height, area.width < area.height)
  142. : checkRotateRange(e, false, true);
  143. }
  144. if (area.height < area.width && img.height < img.width) {
  145. return isInclude
  146. ? checkRotateRange(e, area.height < area.width, area.height < area.width)
  147. : checkRotateRange(e, true, false);
  148. }
  149. }
  150. if (img.height >= area.width && img.width >= area.height) {
  151. return checkRotateRange(e, false, false);
  152. }
  153. if (isInclude) {
  154. return area.height < area.width
  155. ? checkRotateRange(e, true, true)
  156. : checkRotateRange(e, area.width < area.height, area.width < area.height);
  157. }
  158. if (img.height < area.width && !img.width < area.height) {
  159. return checkRotateRange(e, true, false);
  160. }
  161. if (!img.height < area.width && img.width < area.height) {
  162. return checkRotateRange(e, false, true);
  163. }
  164. return checkRotateRange(e, img.height < area.height, img.width < area.width);
  165. }
  166. return {
  167. x: correctValue(e.x, -img.width + area.width + area.left, area.left, img.width < area.width),
  168. y: correctValue(e.y, -img.height + area.height + area.top, area.top, img.height < area.height)
  169. };
  170. };
  171. /**
  172. * 变更图片布局信息
  173. * @param {Object} e 布局信息
  174. */
  175. function changeImageRect(e) {
  176. offset.x += e.x || 0;
  177. offset.y += e.y || 0;
  178. var image = e.instance.selectComponent('.crop-image');
  179. if(e.check && area.checkRange) { // 检查边界
  180. var point = checkRange(offset);
  181. if(offset.x !== point.x || offset.y !== point.y) {
  182. offset = point;
  183. }
  184. }
  185. // image.setStyle({
  186. // width: img.width + 'px',
  187. // height: img.height + 'px',
  188. // transform: 'translate(' + offset.x + 'px, ' + offset.y + 'px) rotate(' + rotate +'deg)'
  189. // });
  190. var ox = (img.width - img.oldWidth) / 2;
  191. var oy = (img.height - img.oldHeight) / 2;
  192. image.setStyle({
  193. width: img.oldWidth + 'px',
  194. height: img.oldHeight + 'px',
  195. transform: (img.gpu ? 'translateZ(0) ' : '') + 'translate(' + (offset.x + ox) + 'px, ' + (offset.y + oy) + 'px) rotate(' + rotate +'deg) scale(' + scale + ')'
  196. });
  197. e.instance.callMethod('dataChange', {
  198. width: img.width,
  199. height: img.height,
  200. x: offset.x,
  201. y: offset.y,
  202. rotate: rotate
  203. });
  204. };
  205. /**
  206. * 变更裁剪区域布局信息
  207. * @param {Object} e 布局信息
  208. */
  209. function changeAreaRect(e) {
  210. // 变更蒙版样式
  211. var masks = e.instance.selectAllComponents('.crop-mask-block');
  212. var maskStyles = [
  213. {
  214. left: 0,
  215. width: (area.left + areaOffset.left) + 'px',
  216. top: 0,
  217. bottom: 0,
  218. 'z-index': area.zIndex + 2
  219. },
  220. {
  221. left: (area.right + areaOffset.right) + 'px',
  222. right: 0,
  223. top: 0,
  224. bottom: 0,
  225. 'z-index': area.zIndex + 2
  226. },
  227. {
  228. left: (area.left + areaOffset.left) + 'px',
  229. width: (area.width + areaOffset.right - areaOffset.left) + 'px',
  230. top: 0,
  231. height: (area.top + areaOffset.top) + 'px',
  232. 'z-index': area.zIndex + 2
  233. },
  234. {
  235. left: (area.left + areaOffset.left) + 'px',
  236. width: (area.width + areaOffset.right - areaOffset.left) + 'px',
  237. top: (area.bottom + areaOffset.bottom) + 'px',
  238. // height: (area.top - areaOffset.bottom + sys.offsetBottom) + 'px',
  239. bottom: 0,
  240. 'z-index': area.zIndex + 2
  241. }
  242. ];
  243. var len = masks.length;
  244. for (var i = 0; i < len; i++) {
  245. masks[i].setStyle(maskStyles[i]);
  246. }
  247. // 变更边框样式
  248. if(area.showBorder) {
  249. var border = e.instance.selectComponent('.crop-border');
  250. border.setStyle({
  251. left: (area.left + areaOffset.left) + 'px',
  252. top: (area.top + areaOffset.top) + 'px',
  253. width: (area.width + areaOffset.right - areaOffset.left) + 'px',
  254. height: (area.height + areaOffset.bottom - areaOffset.top) + 'px',
  255. 'z-index': area.zIndex + 3
  256. });
  257. }
  258. // 变更参考线样式
  259. if(area.showGrid) {
  260. var grids = e.instance.selectAllComponents('.crop-grid');
  261. var gridStyles = [
  262. {
  263. 'border-width': '1px 0 0 0',
  264. left: (area.left + areaOffset.left) + 'px',
  265. right: (area.right + areaOffset.right) + 'px',
  266. top: (area.top + areaOffset.top + (area.height + areaOffset.bottom - areaOffset.top) / 3 - 0.5) + 'px',
  267. width: (area.width + areaOffset.right - areaOffset.left) + 'px',
  268. 'z-index': area.zIndex + 3
  269. },
  270. {
  271. 'border-width': '1px 0 0 0',
  272. left: (area.left + areaOffset.left) + 'px',
  273. right: (area.right + areaOffset.right) + 'px',
  274. top: (area.top + areaOffset.top + (area.height + areaOffset.bottom - areaOffset.top) * 2 / 3 - 0.5) + 'px',
  275. width: (area.width + areaOffset.right - areaOffset.left) + 'px',
  276. 'z-index': area.zIndex + 3
  277. },
  278. {
  279. 'border-width': '0 1px 0 0',
  280. top: (area.top + areaOffset.top) + 'px',
  281. bottom: (area.bottom + areaOffset.bottom) + 'px',
  282. left: (area.left + areaOffset.left + (area.width + areaOffset.right - areaOffset.left) / 3 - 0.5) + 'px',
  283. height: (area.height + areaOffset.bottom - areaOffset.top) + 'px',
  284. 'z-index': area.zIndex + 3
  285. },
  286. {
  287. 'border-width': '0 1px 0 0',
  288. top: (area.top + areaOffset.top) + 'px',
  289. bottom: (area.bottom + areaOffset.bottom) + 'px',
  290. left: (area.left + areaOffset.left + (area.width + areaOffset.right - areaOffset.left) * 2 / 3 - 0.5) + 'px',
  291. height: (area.height + areaOffset.bottom - areaOffset.top) + 'px',
  292. 'z-index': area.zIndex + 3
  293. }
  294. ];
  295. var len = grids.length;
  296. for (var i = 0; i < len; i++) {
  297. grids[i].setStyle(gridStyles[i]);
  298. }
  299. }
  300. // 变更四个伸缩角样式
  301. if(area.showAngle) {
  302. var angles = e.instance.selectAllComponents('.crop-angle');
  303. var angleStyles = [
  304. {
  305. 'border-width': area.angleBorderWidth + 'px 0 0 ' + area.angleBorderWidth + 'px',
  306. left: (area.left + areaOffset.left - area.angleBorderWidth) + 'px',
  307. top: (area.top + areaOffset.top - area.angleBorderWidth) + 'px',
  308. 'z-index': area.zIndex + 3
  309. },
  310. {
  311. 'border-width': area.angleBorderWidth + 'px ' + area.angleBorderWidth + 'px 0 0',
  312. left: (area.right + areaOffset.right - area.angleSize) + 'px',
  313. top: (area.top + areaOffset.top - area.angleBorderWidth) + 'px',
  314. 'z-index': area.zIndex + 3
  315. },
  316. {
  317. 'border-width': '0 0 ' + area.angleBorderWidth + 'px ' + area.angleBorderWidth + 'px',
  318. left: (area.left + areaOffset.left - area.angleBorderWidth) + 'px',
  319. top: (area.bottom + areaOffset.bottom - area.angleSize) + 'px',
  320. 'z-index': area.zIndex + 3
  321. },
  322. {
  323. 'border-width': '0 ' + area.angleBorderWidth + 'px ' + area.angleBorderWidth + 'px 0',
  324. left: (area.right + areaOffset.right - area.angleSize) + 'px',
  325. top: (area.bottom + areaOffset.bottom - area.angleSize) + 'px',
  326. 'z-index': area.zIndex + 3
  327. }
  328. ];
  329. var len = angles.length;
  330. for (var i = 0; i < len; i++) {
  331. angles[i].setStyle(angleStyles[i]);
  332. }
  333. }
  334. // 变更圆角样式
  335. if(area.radius > 0) {
  336. var circleBox = e.instance.selectComponent('.crop-circle-box');
  337. var circle = e.instance.selectComponent('.crop-circle');
  338. var radius = area.radius;
  339. if(area.width === area.height && area.radius >= area.width / 2) { // 圆形
  340. radius = (area.width / 2);
  341. } else { // 圆角矩形
  342. if(area.width !== area.height) { // 限制圆角半径不能超过短边的一半
  343. radius = Math.min(area.width / 2, area.height / 2, radius);
  344. }
  345. }
  346. circleBox.setStyle({
  347. left: (area.left + areaOffset.left) + 'px',
  348. top: (area.top + areaOffset.top) + 'px',
  349. width: (area.width + areaOffset.right - areaOffset.left) + 'px',
  350. height: (area.height + areaOffset.bottom - areaOffset.top) + 'px',
  351. 'z-index': area.zIndex + 2
  352. });
  353. circle.setStyle({
  354. 'box-shadow': '0 0 0 ' + Math.max(area.width, area.height) + 'px rgba(51, 51, 51, 0.8)',
  355. 'border-radius': radius + 'px'
  356. });
  357. }
  358. };
  359. /**
  360. * 缩放图片
  361. * @param {Object} e 布局信息
  362. */
  363. function scaleImage(e) {
  364. var last = scale;
  365. scale = Math.min(Math.max(e.scale + scale, minScale), img.maxScale);
  366. if(last !== scale) {
  367. img.width = num(img.oldWidth * scale);
  368. img.height = num(img.oldHeight * scale);
  369. // 参考问题:有一个长4000px、宽4000px的四方形ABCD,A点的坐标固定在(-2000,-2000),
  370. // 该四边形上有一个点E,坐标为(-100,-300),将该四方形复制一份并缩小到90%后,
  371. // 新四边形的A点坐标为多少时可使新四边形的E点与原四边形的E点重合?
  372. // 预期效果:从图中选取某点(参照物)为中心点进行缩放,缩放时无论图像怎么变化,该点位置始终固定不变
  373. // 计算方法:以相同起点先计算缩放前后两点间的距离,再加上原图像偏移量即可
  374. e.x = num((e.x - offset.x) * (1 - scale / last));
  375. e.y = num((e.y - offset.y) * (1 - scale / last));
  376. changeImageRect(e);
  377. return true;
  378. }
  379. return false;
  380. };
  381. /**
  382. * 获取触摸点在哪个角
  383. * @param {number} x 触摸点x轴坐标
  384. * @param {number} y 触摸点y轴坐标
  385. * @return {number} 角的位置:0=无;1=左上;2=右上;3=左下;4=右下;
  386. */
  387. function getToucheAngle(x, y) {
  388. // console.log('getToucheAngle', x, y, JSON.stringify(area))
  389. var o = area.angleBorderWidth; // 需扩大触发范围则把 o 值加大即可
  390. if(y >= area.top - o && y <= area.top + area.angleSize + o) {
  391. if(x >= area.left - o && x <= area.left + area.angleSize + o) {
  392. return 1; // 左上角
  393. } else if(x >= area.right - area.angleSize - o && x <= area.right + o) {
  394. return 2; // 右上角
  395. }
  396. } else if(y >= area.bottom - area.angleSize - o && y <= area.bottom + o) {
  397. if(x >= area.left - o && x <= area.left + area.angleSize + o) {
  398. return 3; // 左下角
  399. } else if(x >= area.right - area.angleSize - o && x <= area.right + o) {
  400. return 4; // 右下角
  401. }
  402. }
  403. return 0; // 无触摸到角
  404. };
  405. /**
  406. * 重置数据
  407. */
  408. function resetData() {
  409. offset = { x: 0, y: 0 };
  410. scale = 1;
  411. minScale = img.minScale;
  412. rotate = 0;
  413. };
  414. /**
  415. * 顺时针翻转图片90°
  416. * @param {Object} e 事件对象
  417. * @param {Object} o 组件实例对象
  418. */
  419. function rotateImage(e, o, r) {
  420. rotate = (rotate + r) % 360;
  421. if(img.minScale >= 1 && area.checkRange) {
  422. // 因图片宽高可能不等,翻转后图片宽高需足够填满裁剪区域
  423. minScale = 1;
  424. if(img.width < area.height) {
  425. minScale = area.height / img.oldWidth;
  426. } else if(img.height < area.width) {
  427. minScale = area.width / img.oldHeight;
  428. }
  429. if(minScale !== 1) {
  430. scaleImage({
  431. instance: o,
  432. scale: minScale - scale,
  433. x: sys.windowWidth / 2,
  434. y: (sys.windowHeight - sys.offsetBottom) / 2
  435. });
  436. }
  437. }
  438. // 由于拖动画布后会导致图片位置偏移,翻转时的旋转中心点需是图片区域+偏移区域的中心点
  439. // 翻转x轴中心点 = (超出裁剪区域右侧的图片宽度 - 超出裁剪区域左侧的图片宽度) / 2
  440. // 翻转y轴中心点 = (超出裁剪区域下方的图片宽度 - 超出裁剪区域上方的图片宽度) / 2
  441. var ox = ((offset.x + img.width - area.right) - (area.left - offset.x)) / 2;
  442. var oy = ((offset.y + img.height - area.bottom) - (area.top - offset.y)) / 2;
  443. changeImageRect({
  444. instance: o,
  445. check: true,
  446. x: -ox - oy,
  447. y: -oy + ox
  448. });
  449. };
  450. module.exports = {
  451. /**
  452. * 初始化:观察数据变更
  453. * @param {Object} newVal 新数据
  454. * @param {Object} oldVal 旧数据
  455. * @param {Object} o 组件实例对象
  456. */
  457. initObserver: function(newVal, oldVal, o, i) {
  458. if(newVal) {
  459. img = newVal.img;
  460. sys = newVal.sys;
  461. area = newVal.area;
  462. minScale = img.minScale;
  463. resetData();
  464. img.src && changeImageRect({
  465. instance: o,
  466. x: (sys.windowWidth - img.width) / 2,
  467. y: (sys.windowHeight - sys.offsetBottom - img.height) / 2
  468. });
  469. changeAreaRect({
  470. instance: o
  471. });
  472. // console.log('initRect', JSON.stringify(newVal))
  473. }
  474. },
  475. /**
  476. * 鼠标滚轮滚动
  477. * @param {Object} e 事件对象
  478. * @param {Object} o 组件实例对象
  479. */
  480. mousewheel: function(e, o) {
  481. if(!img.src) return;
  482. scaleImage({
  483. instance: o,
  484. check: true,
  485. // 鼠标向上滚动时,deltaY 固定 -100,鼠标向下滚动时,deltaY 固定 100
  486. scale: e.detail.deltaY > 0 ? -0.05 : 0.05,
  487. x: e.touches[0].pageX,
  488. y: e.touches[0].pageY
  489. });
  490. },
  491. /**
  492. * 触摸开始
  493. * @param {Object} e 事件对象
  494. * @param {Object} o 组件实例对象
  495. */
  496. touchstart: function(e, o) {
  497. if(!img.src) return;
  498. touches = e.touches;
  499. activeAngle = area.showAngle ? getToucheAngle(touches[0].pageX, touches[0].pageY) : 0;
  500. if(touches.length === 1 && activeAngle !== 0) {
  501. touchType = 'stretch'; // 伸缩裁剪区域
  502. } else {
  503. touchType = '';
  504. }
  505. // console.log('touchstart', JSON.stringify(e), activeAngle)
  506. },
  507. /**
  508. * 触摸移动
  509. * @param {Object} e 事件对象
  510. * @param {Object} o 组件实例对象
  511. */
  512. touchmove: function(e, o) {
  513. if(!img.src) return;
  514. // console.log('touchmove', JSON.stringify(e), JSON.stringify(o))
  515. if(touchType === 'stretch') { // 触摸四个角进行拉伸
  516. var point = e.touches[0];
  517. var start = touches[0];
  518. var x = point.pageX - start.pageX;
  519. var y = point.pageY - start.pageY;
  520. if(x !== 0 || y !== 0) {
  521. var maxX = num(area.width * (1 - area.minScale));
  522. var maxY = num(area.height * (1 - area.minScale));
  523. // console.log(x, y, maxX, maxY, offset, area)
  524. touches[0] = point;
  525. var r = rotate / 90 % 2;
  526. var m = r === 1 ? num((img.height - img.width) / 2) : 0; // 宽高差值一半
  527. var xCompare = r === 1 ? lessThanByFault(img.height, area.width) : lessThanByFault(img.width, area.width);
  528. var yCompare = r === 1 ? lessThanByFault(img.width, area.height) : lessThanByFault(img.height, area.height)
  529. var isInclude = xCompare && yCompare;
  530. var isIntersect = area.checkRange && (xCompare || yCompare); // 图片是否包含在裁剪区域内
  531. var isReverse = !isInclude || num((offset.x - area.left) / area.width) <= num((offset.y - area.top) / area.height) || (area.width > area.height && img.width < img.height && r === 1);
  532. switch(activeAngle) {
  533. case 1: // 左上角
  534. x = num(x + areaOffset.left);
  535. y = num(y + areaOffset.top);
  536. if(x >= 0 && y >= 0) { // 有效滑动
  537. var t = num(offset.y + m - area.top);
  538. var l = num(offset.x - m - area.left);
  539. // && (offset.x + img.width < area.right || offset.y + img.height < area.bottom)
  540. var max = isIntersect && ((l >= 0) || (t >= 0))
  541. ? minimum(t, l)
  542. : false;
  543. if(x > y && isReverse) { // 以x轴滑动距离为缩放基准
  544. maxX = validMax(maxX, max, isInclude, l, t, area.width / area.height);
  545. if(x > maxX) x = maxX;
  546. y = num(x * area.height / area.width);
  547. } else { // 以y轴滑动距离为缩放基准
  548. maxY = validMax(maxY, max, isInclude, t, l, area.height / area.width);
  549. if(y > maxY) y = maxY;
  550. x = num(y * area.width / area.height);
  551. }
  552. areaOffset.left = x;
  553. areaOffset.top = y;
  554. }
  555. break;
  556. case 2: // 右上角
  557. x = num(x + areaOffset.right);
  558. y = num(y + areaOffset.top);
  559. if(x <= 0 && y >= 0) { // 有效滑动
  560. var w = (r === 1 ? img.height : img.width);
  561. var t = num(offset.y + m - area.top);
  562. var l = num(area.right + m - offset.x - w);
  563. var max = isIntersect && ((t >= 0) || (l >= 0))
  564. ? minimum(t, l)
  565. : false;
  566. // var max = isInclude && ((offset.x > 0 && offset.x + img.width <= area.right) || (offset.y > 0 && offset.y >= area.top))
  567. // ? minimum(offset.y - area.top, area.right - offset.x - img.width)
  568. // : false;
  569. // console.log(offset.x, offset.y, img.width, img.height, area.top, area.right, m, max)
  570. // console.log(offset.y + m - area.top, area.right + m - offset.x - w)
  571. if(-x > y && isReverse) { // 以x轴滑动距离为缩放基准
  572. maxX = validMax(maxX, max, isInclude, l, t, area.width / area.height);
  573. if(-x > maxX) x = -maxX;
  574. y = num(-x * area.height / area.width);
  575. } else { // 以y轴滑动距离为缩放基准
  576. maxY = validMax(maxY, max, isInclude, t, l, area.height / area.width);
  577. if(y > maxY) y = maxY;
  578. x = num(-y * area.width / area.height);
  579. }
  580. areaOffset.right = x;
  581. areaOffset.top = y;
  582. }
  583. break;
  584. case 3: // 左下角
  585. x += num(x + areaOffset.left);
  586. y += num(y + areaOffset.bottom);
  587. if(x >= 0 && y <= 0) { // 有效滑动
  588. var w = (r === 1 ? img.width : img.height);
  589. var t = num(area.bottom - m - offset.y - w);
  590. var l = num(offset.x - m - area.left);
  591. var max = isIntersect && ((l >= 0) || (t >= 0))
  592. ? minimum(t, l)
  593. : false;
  594. if(x > -y && isReverse) { // 以x轴滑动距离为缩放基准
  595. maxX = validMax(maxX, max, isInclude, l, t, area.width / area.height);
  596. if(x > maxX) x = maxX;
  597. y = num(-x * area.height / area.width);
  598. } else { // 以y轴滑动距离为缩放基准
  599. maxY = validMax(maxY, max, isInclude, t, l, area.height / area.width);
  600. if(-y > maxY) y = -maxY;
  601. x = num(-y * area.width / area.height);
  602. }
  603. areaOffset.left = x;
  604. areaOffset.bottom = y;
  605. }
  606. break;
  607. case 4: // 右下角
  608. x = num(x + areaOffset.right);
  609. y = num(y + areaOffset.bottom);
  610. if(x <= 0 && y <= 0) { // 有效滑动
  611. var w = (r === 1 ? img.height : img.width);
  612. var h = (r === 1 ? img.width : img.height);
  613. var t = num(area.bottom - offset.y - h - m);
  614. var l = num(area.right + m - offset.x - w);
  615. var max = isIntersect && ((l >= 0) || (t >= 0))
  616. ? minimum(t, l)
  617. : false;
  618. if(-x > -y && isReverse) { // 以x轴滑动距离为缩放基准
  619. maxX = validMax(maxX, max, isInclude, l, t, area.width / area.height);
  620. if(-x > maxX) x = -maxX;
  621. y = num(x * area.height / area.width);
  622. } else { // 以y轴滑动距离为缩放基准
  623. maxY = validMax(maxY, max, isInclude, t, l, area.height / area.width);
  624. if(-y > maxY) y = -maxY;
  625. x = num(y * area.width / area.height);
  626. }
  627. areaOffset.right = x;
  628. areaOffset.bottom = y;
  629. }
  630. break;
  631. }
  632. // console.log(x, y, JSON.stringify(areaOffset))
  633. changeAreaRect({
  634. instance: o,
  635. });
  636. // this.draw();
  637. }
  638. } else if (e.touches.length == 2) { // 双点触摸缩放
  639. var start = getDistanceByTouches(touches);
  640. var end = getDistanceByTouches(e.touches);
  641. scaleImage({
  642. instance: o,
  643. check: !area.bounce,
  644. scale: (end.c - start.c) / 100,
  645. x: end.x,
  646. y: end.y
  647. });
  648. touchType = 'scale';
  649. } else if(touchType === 'scale') {// 从双点触摸变成单点触摸 / 从缩放变成拖动
  650. touchType = 'move';
  651. } else {
  652. changeImageRect({
  653. instance: o,
  654. check: !area.bounce,
  655. x: e.touches[0].pageX - touches[0].pageX,
  656. y: e.touches[0].pageY - touches[0].pageY
  657. });
  658. touchType = 'move';
  659. }
  660. touches = e.touches;
  661. },
  662. /**
  663. * 触摸结束
  664. * @param {Object} e 事件对象
  665. * @param {Object} o 组件实例对象
  666. */
  667. touchend: function(e, o) {
  668. if(!img.src) return;
  669. if(touchType === 'stretch') { // 拉伸裁剪区域的四个角缩放
  670. // 裁剪区域宽度被缩放到多少
  671. var left = areaOffset.left;
  672. var right = areaOffset.right;
  673. var top = areaOffset.top;
  674. var bottom = areaOffset.bottom;
  675. var w = area.width + right - left;
  676. var h = area.height + bottom - top;
  677. // 图像放大倍数
  678. var p = scale * (area.width / w) - scale;
  679. // 复原裁剪区域
  680. areaOffset = { left: 0, right: 0, top: 0, bottom: 0 };
  681. changeAreaRect({
  682. instance: o,
  683. });
  684. scaleImage({
  685. instance: o,
  686. scale: p,
  687. x: area.left + left + (1 === activeAngle || 3 === activeAngle ? w : 0),
  688. y: area.top + top + (1 === activeAngle || 2 === activeAngle ? h : 0)
  689. });
  690. } else if (area.bounce) { // 检查边界并矫正,实现拖动到边界时有回弹效果
  691. changeImageRect({
  692. instance: o,
  693. check: true
  694. });
  695. }
  696. },
  697. /**
  698. * 顺时针翻转图片90°
  699. * @param {Object} e 事件对象
  700. * @param {Object} o 组件实例对象
  701. */
  702. rotateImage: function(e, o) {
  703. rotateImage(e, o, 90);
  704. },
  705. rotateImage90: function(e, o) {
  706. rotateImage(e, o, 90)
  707. },
  708. rotateImage270: function(e, o) {
  709. rotateImage(e, o, 270)
  710. },
  711. // 此处只用于对齐其他平台端的样式参数,防止异常,无作用
  712. imageStyles: '',
  713. maskStylesList: ['', '', '', ''],
  714. borderStyles: '',
  715. gridStylesList: ['', '', '', ''],
  716. angleStylesList: ['', '', '', ''],
  717. circleBoxStyles: '',
  718. circleStyles: '',
  719. }