qf-image-cropper.render.js 24 KB

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