image-cropper.js 20 KB

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