tui-image-cropper.vue 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. <template>
  2. <view class="tui-container" @touchmove.stop.prevent="stop">
  3. <view class="tui-image-cropper" @touchend="cutTouchEnd" @touchstart="cutTouchStart" @touchmove="cutTouchMove">
  4. <view class="tui-content">
  5. <view class="tui-content-top tui-bg-transparent" :style="{ height: cutY + 'px', transitionProperty: cutAnimation ? '' : 'background' }"></view>
  6. <view class="tui-content-middle" :style="{ height: canvasHeight + 'px' }">
  7. <view class="tui-bg-transparent" :style="{ width: cutX + 'px', transitionProperty: cutAnimation ? '' : 'background' }"></view>
  8. <view class="tui-cropper-box" :style="{ width: canvasWidth + 'px', height: canvasHeight + 'px', borderColor: borderColor, transitionProperty: cutAnimation ? '' : 'background' }">
  9. <view v-for="(item, index) in 4" :key="index" class="tui-edge" :class="[`tui-${index < 2 ? 'top' : 'bottom'}-${index === 0 || index === 2 ? 'left' : 'right'}`]"
  10. :style="{
  11. width: edgeWidth,
  12. height: edgeWidth,
  13. borderColor: edgeColor,
  14. borderWidth: edgeBorderWidth,
  15. left: index === 0 || index === 2 ? `-${edgeOffsets}` : 'auto',
  16. right: index === 1 || index === 3 ? `-${edgeOffsets}` : 'auto',
  17. top: index < 2 ? `-${edgeOffsets}` : 'auto',
  18. bottom: index > 1 ? `-${edgeOffsets}` : 'auto'
  19. }"></view>
  20. </view>
  21. <view class="tui-flex-auto tui-bg-transparent" :style="{ transitionProperty: cutAnimation ? '' : 'background' }"></view>
  22. </view>
  23. <view class="tui-flex-auto tui-bg-transparent" :style="{ transitionProperty: cutAnimation ? '' : 'background' }"></view>
  24. </view>
  25. <image @load="imageLoad" @error="imageLoad" @touchstart="start" @touchmove="move" @touchend="end" :style="{
  26. width: imgWidth ? imgWidth + 'px' : 'auto',
  27. height: imgHeight ? imgHeight + 'px' : 'auto',
  28. transform: imgTransform,
  29. transitionDuration: (cutAnimation ? 0.35 : 0) + 's'
  30. }"
  31. class="tui-cropper-image" :src="imageUrl" v-if="imageUrl" mode="widthFix"></image>
  32. </view>
  33. <canvas canvas-id="tui-image-cropper" id="tui-image-cropper" :disable-scroll="true" :style="{ width: CROPPER_WIDTH * scaleRatio + 'px', height: CROPPER_HEIGHT * scaleRatio + 'px' }"
  34. class="tui-cropper-canvas"></canvas>
  35. <view class="tui-cropper-tabbar" v-if="!custom">
  36. <view class="tui-op-btn" @tap.stop="back">取消</view>
  37. <image :src="rotateImg" class="tui-rotate-img" @tap="setAngle"></image>
  38. <view class="tui-op-btn" @tap.stop="getImage">完成</view>
  39. </view>
  40. </view>
  41. </template>
  42. <script>
  43. /**
  44. * 注意:组件中使用的图片地址,将文件复制到自己项目中
  45. * 如果图片位置与组件同级,编译成小程序时图片会丢失
  46. * 拷贝static下整个components文件夹
  47. *也可直接转成base64(不建议)
  48. * */
  49. export default {
  50. name: 'tuiImageCropper',
  51. props: {
  52. //图片路径
  53. imageUrl: {
  54. type: String,
  55. default: ''
  56. },
  57. /*
  58. 默认正方形,可修改大小控制比例
  59. 裁剪框高度 px
  60. */
  61. height: {
  62. type: Number,
  63. default: 280
  64. },
  65. //裁剪框宽度 px
  66. width: {
  67. type: Number,
  68. default: 280
  69. },
  70. //裁剪框最小宽度 px
  71. minWidth: {
  72. type: Number,
  73. default: 100
  74. },
  75. //裁剪框最小高度 px
  76. minHeight: {
  77. type: Number,
  78. default: 100
  79. },
  80. //裁剪框最大宽度 px
  81. maxWidth: {
  82. type: Number,
  83. default: 360
  84. },
  85. //裁剪框最大高度 px
  86. maxHeight: {
  87. type: Number,
  88. default: 360
  89. },
  90. //裁剪框border颜色
  91. borderColor: {
  92. type: String,
  93. default: 'rgba(255,255,255,0.1)'
  94. },
  95. //裁剪框边缘线颜色
  96. edgeColor: {
  97. type: String,
  98. default: '#FFFFFF'
  99. },
  100. //裁剪框边缘线宽度 w=h
  101. edgeWidth: {
  102. type: String,
  103. default: '34rpx'
  104. },
  105. //裁剪框边缘线border宽度
  106. edgeBorderWidth: {
  107. type: String,
  108. default: '6rpx'
  109. },
  110. //偏移距离,根据edgeBorderWidth进行调整
  111. edgeOffsets: {
  112. type: String,
  113. default: '6rpx'
  114. },
  115. /**
  116. * 如果宽度和高度都为true则裁剪框禁止拖动
  117. * 裁剪框宽度锁定
  118. */
  119. lockWidth: {
  120. type: Boolean,
  121. default: false
  122. },
  123. //裁剪框高度锁定
  124. lockHeight: {
  125. type: Boolean,
  126. default: false
  127. },
  128. //锁定裁剪框比例(放大或缩小)
  129. lockRatio: {
  130. type: Boolean,
  131. default: false
  132. },
  133. //生成的图片尺寸相对剪裁框的比例
  134. scaleRatio: {
  135. type: Number,
  136. default: 2
  137. },
  138. //图片的质量,取值范围为 (0, 1],不在范围内时当作1.0处理
  139. quality: {
  140. type: Number,
  141. default: 0.8
  142. },
  143. //图片旋转角度
  144. rotateAngle: {
  145. type: Number,
  146. default: 0
  147. },
  148. //图片最小缩放比
  149. minScale: {
  150. type: Number,
  151. default: 0.5
  152. },
  153. //图片最大缩放比
  154. maxScale: {
  155. type: Number,
  156. default: 2
  157. },
  158. //是否禁用触摸旋转(为false则可以触摸转动图片,limitMove为false生效)
  159. disableRotate: {
  160. type: Boolean,
  161. default: true
  162. },
  163. //是否限制移动范围(剪裁框只能在图片内,为true不可触摸转动图片)
  164. limitMove: {
  165. type: Boolean,
  166. default: true
  167. },
  168. //自定义操作栏(为true时隐藏底部操作栏)
  169. custom: {
  170. type: Boolean,
  171. default: false
  172. },
  173. //值发生改变开始裁剪(custom为true时生效)
  174. startCutting: {
  175. type: [Number, Boolean],
  176. default: 0
  177. },
  178. /**
  179. * 是否返回base64(H5端默认base64)
  180. * 支持平台:App,微信小程序,支付宝小程序,H5(默认url就是base64)
  181. **/
  182. isBase64: {
  183. type: Boolean,
  184. default: false
  185. },
  186. //裁剪时是否显示loadding
  187. loadding: {
  188. type: Boolean,
  189. default: true
  190. },
  191. //旋转icon
  192. rotateImg: {
  193. type: String,
  194. default: '/static/components/cropper/img_rotate.png'
  195. }
  196. },
  197. data() {
  198. return {
  199. MOVE_THROTTLE: null, //触摸移动节流setTimeout
  200. MOVE_THROTTLE_FLAG: true, //节流标识
  201. TIME_CUT_CENTER: null,
  202. CROPPER_WIDTH: 200, //裁剪框宽
  203. CROPPER_HEIGHT: 200, //裁剪框高
  204. CUT_START: null,
  205. cutX: 0, //画布x轴起点
  206. cutY: 0, //画布y轴起点0
  207. touchRelative: [{
  208. x: 0,
  209. y: 0
  210. }], //手指或鼠标和图片中心的相对位置
  211. flagCutTouch: false, //是否是拖动裁剪框
  212. hypotenuseLength: 0, //双指触摸时斜边长度
  213. flagEndTouch: false, //是否结束触摸
  214. canvasWidth: 0,
  215. canvasHeight: 0,
  216. imgWidth: 0, //图片宽度
  217. imgHeight: 0, //图片高度
  218. scale: 1, //图片缩放比
  219. angle: 0, //图片旋转角度
  220. cutAnimation: false, //是否开启图片和裁剪框过渡
  221. cutAnimationTime: null,
  222. imgTop: 0, //图片上边距
  223. imgLeft: 0, //图片左边距
  224. ctx: null,
  225. sysInfo: null
  226. };
  227. },
  228. computed: {
  229. imgTransform: function() {
  230. return `translate3d(${this.imgLeft - this.imgWidth / 2}px,${this.imgTop - this.imgHeight / 2}px,0) scale(${this.scale}) rotate(${this.angle}deg)`;
  231. }
  232. },
  233. watch: {
  234. imageUrl(val, oldVal) {
  235. this.imageReset();
  236. this.showLoading();
  237. uni.getImageInfo({
  238. src: val,
  239. success: res => {
  240. //计算图片尺寸
  241. this.imgComputeSize(res.width, res.height);
  242. if (this.limitMove) {
  243. //限制移动,不留空白处理
  244. this.imgMarginDetectionScale();
  245. }
  246. },
  247. fail: err => {
  248. this.imgComputeSize();
  249. if (this.limitMove) {
  250. this.imgMarginDetectionScale();
  251. }
  252. }
  253. });
  254. },
  255. //监听截取框宽高变化
  256. canvasWidth(val) {
  257. if (val < this.minWidth) {
  258. this.canvasWidth = this.minWidth;
  259. }
  260. this.computeCutSize();
  261. },
  262. canvasHeight(val) {
  263. if (val < this.minHeight) {
  264. this.canvasHeight = this.minHeight;
  265. }
  266. this.computeCutSize();
  267. },
  268. rotateAngle(val) {
  269. this.cutAnimation = true;
  270. this.angle = val;
  271. },
  272. angle(val) {
  273. this.moveStop();
  274. if (this.limitMove && val % 90) {
  275. this.angle = Math.round(val / 90) * 90;
  276. }
  277. this.imgMarginDetectionScale();
  278. },
  279. cutAnimation(val) {
  280. //开启过渡260毫秒之后自动关闭
  281. clearTimeout(this.cutAnimationTime);
  282. if (val) {
  283. this.cutAnimationTime = setTimeout(() => {
  284. this.cutAnimation = false;
  285. }, 260);
  286. }
  287. },
  288. limitMove(val) {
  289. if (val) {
  290. if (this.angle % 90) {
  291. this.angle = Math.round(this.angle / 90) * 90;
  292. }
  293. this.imgMarginDetectionScale();
  294. }
  295. },
  296. cutY(value) {
  297. this.cutDetectionPosition();
  298. },
  299. cutX(value) {
  300. this.cutDetectionPosition();
  301. },
  302. startCutting(val) {
  303. if (this.custom && val) {
  304. this.getImage();
  305. }
  306. }
  307. },
  308. mounted() {
  309. this.sysInfo = uni.getSystemInfoSync();
  310. this.imgTop = this.sysInfo.windowHeight / 2;
  311. this.imgLeft = this.sysInfo.windowWidth / 2;
  312. this.CROPPER_WIDTH = this.width;
  313. this.CROPPER_HEIGHT = this.height;
  314. this.canvasHeight = this.height;
  315. this.canvasWidth = this.width;
  316. this.ctx = uni.createCanvasContext('tui-image-cropper', this);
  317. this.setCutCenter();
  318. //设置裁剪框大小>设置图片尺寸>绘制canvas
  319. this.computeCutSize();
  320. //检查裁剪框是否在范围内
  321. this.cutDetectionPosition();
  322. setTimeout(() => {
  323. this.$emit('ready', {});
  324. }, 200);
  325. },
  326. methods: {
  327. //网络图片转成本地文件[同步执行]
  328. async getLocalImage(url) {
  329. return await new Promise((resolve, reject) => {
  330. uni.downloadFile({
  331. url: url,
  332. success: res => {
  333. resolve(res.tempFilePath);
  334. },
  335. fail: res => {
  336. reject(false)
  337. }
  338. })
  339. })
  340. },
  341. //返回裁剪后图片信息
  342. getImage() {
  343. if (!this.imageUrl) {
  344. uni.showToast({
  345. title: '请选择图片',
  346. icon: 'none'
  347. });
  348. return;
  349. }
  350. this.loadding && this.showLoading();
  351. let draw = async () => {
  352. //图片实际大小
  353. let imgWidth = this.imgWidth * this.scale * this.scaleRatio;
  354. let imgHeight = this.imgHeight * this.scale * this.scaleRatio;
  355. //canvas和图片的相对距离
  356. let xpos = this.imgLeft - this.cutX;
  357. let ypos = this.imgTop - this.cutY;
  358. //旋转画布
  359. this.ctx.translate(xpos * this.scaleRatio, ypos * this.scaleRatio);
  360. this.ctx.rotate((this.angle * Math.PI) / 180);
  361. let imgUrl = this.imageUrl;
  362. // #ifdef APP-PLUS || MP-WEIXIN
  363. if (~this.imageUrl.indexOf('https:')) {
  364. imgUrl = await this.getLocalImage(this.imageUrl)
  365. }
  366. // #endif
  367. this.ctx.drawImage(imgUrl, -imgWidth / 2, -imgHeight / 2, imgWidth, imgHeight);
  368. this.ctx.draw(false, () => {
  369. let params = {
  370. width: this.canvasWidth * this.scaleRatio,
  371. height: Math.round(this.canvasHeight * this.scaleRatio),
  372. destWidth: this.canvasWidth * this.scaleRatio,
  373. destHeight: Math.round(this.canvasHeight) * this.scaleRatio,
  374. fileType: 'png',
  375. quality: this.quality
  376. };
  377. let data = {
  378. url: '',
  379. base64: '',
  380. width: this.canvasWidth * this.scaleRatio,
  381. height: this.canvasHeight * this.scaleRatio
  382. };
  383. // #ifdef MP-ALIPAY
  384. if (this.isBase64) {
  385. this.ctx.toDataURL(params).then(dataURL => {
  386. data.base64 = dataURL;
  387. this.loadding && uni.hideLoading();
  388. this.$emit('cropper', data);
  389. });
  390. } else {
  391. this.ctx.toTempFilePath({
  392. ...params,
  393. success: res => {
  394. data.url = res.tempFilePath;
  395. this.loadding && uni.hideLoading();
  396. this.$emit('cropper', data);
  397. }
  398. });
  399. }
  400. // #endif
  401. // #ifndef MP-ALIPAY
  402. // #ifdef MP-BAIDU || MP-TOUTIAO || H5
  403. this.isBase64 = false;
  404. // #endif
  405. if (this.isBase64) {
  406. uni.canvasGetImageData({
  407. canvasId: 'tui-image-cropper',
  408. x: 0,
  409. y: 0,
  410. width: this.canvasWidth * this.scaleRatio,
  411. height: Math.round(this.canvasHeight * this.scaleRatio),
  412. success: res => {
  413. const arrayBuffer = new Uint8Array(res.data);
  414. const base64 = uni.arrayBufferToBase64(arrayBuffer);
  415. data.base64 = base64;
  416. this.loadding && uni.hideLoading();
  417. this.$emit('cropper', data);
  418. }
  419. },this);
  420. } else {
  421. uni.canvasToTempFilePath({
  422. ...params,
  423. canvasId: 'tui-image-cropper',
  424. success: res => {
  425. data.url = res.tempFilePath;
  426. // #ifdef H5
  427. data.base64 = res.tempFilePath;
  428. // #endif
  429. this.loadding && uni.hideLoading();
  430. this.$emit('cropper', data);
  431. },
  432. fail(res) {
  433. console.log(res);
  434. }
  435. },
  436. this
  437. );
  438. }
  439. // #endif
  440. });
  441. };
  442. if (this.CROPPER_WIDTH != this.canvasWidth || this.CROPPER_HEIGHT != this.canvasHeight) {
  443. this.CROPPER_WIDTH = this.canvasWidth;
  444. this.CROPPER_HEIGHT = this.canvasHeight;
  445. this.ctx.draw();
  446. this.$nextTick(() => {
  447. setTimeout(() => {
  448. draw();
  449. }, 100);
  450. });
  451. } else {
  452. draw();
  453. }
  454. },
  455. /**
  456. * 设置剪裁框和图片居中
  457. */
  458. setCutCenter() {
  459. let sys = this.sysInfo || uni.getSystemInfoSync();
  460. let cutY = (sys.windowHeight - this.canvasHeight) * 0.5;
  461. let cutX = (sys.windowWidth - this.canvasWidth) * 0.5;
  462. //顺序不能变
  463. this.imgTop = this.imgTop - this.cutY + cutY;
  464. this.cutY = cutY; //截取的框上边距
  465. this.imgLeft = this.imgLeft - this.cutX + cutX;
  466. this.cutX = cutX; //截取的框左边距
  467. },
  468. imageReset() {
  469. // this.cutAnimation = true;
  470. this.scale = 1;
  471. this.angle = 0;
  472. let sys = this.sysInfo || uni.getSystemInfoSync();
  473. this.imgTop = sys.windowHeight / 2;
  474. this.imgLeft = sys.windowWidth / 2;
  475. },
  476. imageLoad(e) {
  477. this.imageReset();
  478. uni.hideLoading();
  479. this.$emit('imageLoad', {});
  480. },
  481. //检测剪裁框位置是否在允许的范围内(屏幕内)
  482. cutDetectionPosition() {
  483. let cutDetectionPositionTop = () => {
  484. //检测上边距是否在范围内
  485. if (this.cutY < 0) {
  486. this.cutY = 0;
  487. }
  488. if (this.cutY > this.sysInfo.windowHeight - this.canvasHeight) {
  489. this.cutY = this.sysInfo.windowHeight - this.canvasHeight;
  490. }
  491. },
  492. cutDetectionPositionLeft = () => {
  493. //检测左边距是否在范围内
  494. if (this.cutX < 0) {
  495. this.cutX = 0;
  496. }
  497. if (this.cutX > this.sysInfo.windowWidth - this.canvasWidth) {
  498. this.cutX = this.sysInfo.windowWidth - this.canvasWidth;
  499. }
  500. };
  501. //裁剪框坐标处理(如果只写一个参数则另一个默认为0,都不写默认居中)
  502. if (this.cutY == null && this.cutX == null) {
  503. let cutY = (this.sysInfo.windowHeight - this.canvasHeight) * 0.5;
  504. let cutX = (this.sysInfo.windowWidth - this.canvasWidth) * 0.5;
  505. this.cutY = cutY; //截取的框上边距
  506. this.cutX = cutX; //截取的框左边距
  507. } else if (this.cutY != null && this.cutX != null) {
  508. cutDetectionPositionTop();
  509. cutDetectionPositionLeft();
  510. } else if (this.cutY != null && this.cutX == null) {
  511. cutDetectionPositionTop();
  512. this.cutX = (this.sysInfo.windowWidth - this.canvasWidth) / 2;
  513. } else if (this.cutY == null && this.cutX != null) {
  514. cutDetectionPositionLeft();
  515. this.cutY = (this.sysInfo.windowHeight - this.canvasHeight) / 2;
  516. }
  517. },
  518. /**
  519. * 图片边缘检测-位置
  520. */
  521. imgMarginDetectionPosition(scale) {
  522. if (!this.limitMove) return;
  523. let left = this.imgLeft;
  524. let top = this.imgTop;
  525. scale = scale || this.scale;
  526. let imgWidth = this.imgWidth;
  527. let imgHeight = this.imgHeight;
  528. if ((this.angle / 90) % 2) {
  529. imgWidth = this.imgHeight;
  530. imgHeight = this.imgWidth;
  531. }
  532. left = this.cutX + (imgWidth * scale) / 2 >= left ? left : this.cutX + (imgWidth * scale) / 2;
  533. left = this.cutX + this.canvasWidth - (imgWidth * scale) / 2 <= left ? left : this.cutX + this.canvasWidth - (
  534. imgWidth * scale) / 2;
  535. top = this.cutY + (imgHeight * scale) / 2 >= top ? top : this.cutY + (imgHeight * scale) / 2;
  536. top = this.cutY + this.canvasHeight - (imgHeight * scale) / 2 <= top ? top : this.cutY + this.canvasHeight - (
  537. imgHeight * scale) / 2;
  538. this.imgLeft = left;
  539. this.imgTop = top;
  540. this.scale = scale;
  541. },
  542. /**
  543. * 图片边缘检测-缩放
  544. */
  545. imgMarginDetectionScale(scale) {
  546. if (!this.limitMove) return;
  547. scale = scale || this.scale;
  548. let imgWidth = this.imgWidth;
  549. let imgHeight = this.imgHeight;
  550. if ((this.angle / 90) % 2) {
  551. imgWidth = this.imgHeight;
  552. imgHeight = this.imgWidth;
  553. }
  554. if (imgWidth * scale < this.canvasWidth) {
  555. scale = this.canvasWidth / imgWidth;
  556. }
  557. if (imgHeight * scale < this.canvasHeight) {
  558. scale = Math.max(scale, this.canvasHeight / imgHeight);
  559. }
  560. this.imgMarginDetectionPosition(scale);
  561. },
  562. /**
  563. * 计算图片尺寸
  564. */
  565. imgComputeSize(width, height) {
  566. //默认按图片最小边 = 对应裁剪框尺寸
  567. let imgWidth = width,
  568. imgHeight = height;
  569. if (imgWidth && imgHeight) {
  570. if (imgWidth / imgHeight > (this.canvasWidth || this.width) / (this.canvasHeight || this.height)) {
  571. imgHeight = this.canvasHeight || this.height;
  572. imgWidth = (width / height) * imgHeight;
  573. } else {
  574. imgWidth = this.canvasWidth || this.width;
  575. imgHeight = (height / width) * imgWidth;
  576. }
  577. } else {
  578. let sys = this.sysInfo || uni.getSystemInfoSync();
  579. imgWidth = sys.windowWidth;
  580. imgHeight = 0;
  581. }
  582. this.imgWidth = imgWidth;
  583. this.imgHeight = imgHeight;
  584. },
  585. //改变截取框大小
  586. computeCutSize() {
  587. if (this.canvasWidth > this.sysInfo.windowWidth) {
  588. this.canvasWidth = this.sysInfo.windowWidth;
  589. } else if (this.canvasWidth + this.cutX > this.sysInfo.windowWidth) {
  590. this.cutX = this.sysInfo.windowWidth - this.cutX;
  591. }
  592. if (this.canvasHeight > this.sysInfo.windowHeight) {
  593. this.canvasHeight = this.sysInfo.windowHeight;
  594. } else if (this.canvasHeight + this.cutY > this.sysInfo.windowHeight) {
  595. this.cutY = this.sysInfo.windowHeight - this.cutY;
  596. }
  597. },
  598. //开始触摸
  599. start(e) {
  600. this.flagEndTouch = false;
  601. if (e.touches.length == 1) {
  602. //单指拖动
  603. this.touchRelative[0] = {
  604. x: e.touches[0].clientX - this.imgLeft,
  605. y: e.touches[0].clientY - this.imgTop
  606. };
  607. } else {
  608. //双指放大
  609. let width = Math.abs(e.touches[0].clientX - e.touches[1].clientX);
  610. let height = Math.abs(e.touches[0].clientY - e.touches[1].clientY);
  611. this.touchRelative = [{
  612. x: e.touches[0].clientX - this.imgLeft,
  613. y: e.touches[0].clientY - this.imgTop
  614. },
  615. {
  616. x: e.touches[1].clientX - this.imgLeft,
  617. y: e.touches[1].clientY - this.imgTop
  618. }
  619. ];
  620. this.hypotenuseLength = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
  621. }
  622. },
  623. moveThrottle() {
  624. if (this.sysInfo.platform == 'android') {
  625. clearTimeout(this.MOVE_THROTTLE);
  626. this.MOVE_THROTTLE = setTimeout(() => {
  627. this.MOVE_THROTTLE_FLAG = true;
  628. }, 800 / 40);
  629. return this.MOVE_THROTTLE_FLAG;
  630. } else {
  631. this.MOVE_THROTTLE_FLAG = true;
  632. }
  633. },
  634. move(e) {
  635. if (this.flagEndTouch || !this.MOVE_THROTTLE_FLAG) return;
  636. this.MOVE_THROTTLE_FLAG = false;
  637. this.moveThrottle();
  638. this.moveDuring();
  639. if (e.touches.length == 1) {
  640. //单指拖动
  641. let left = e.touches[0].clientX - this.touchRelative[0].x,
  642. top = e.touches[0].clientY - this.touchRelative[0].y;
  643. //图像边缘检测,防止截取到空白
  644. this.imgLeft = left;
  645. this.imgTop = top;
  646. this.imgMarginDetectionPosition();
  647. } else {
  648. //双指放大
  649. let width = Math.abs(e.touches[0].clientX - e.touches[1].clientX),
  650. height = Math.abs(e.touches[0].clientY - e.touches[1].clientY),
  651. hypotenuse = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2)),
  652. scale = this.scale * (hypotenuse / this.hypotenuseLength),
  653. current_deg = 0;
  654. scale = scale <= this.minScale ? this.minScale : scale;
  655. scale = scale >= this.maxScale ? this.maxScale : scale;
  656. //图像边缘检测,防止截取到空白
  657. // this.scale = scale;
  658. this.imgMarginDetectionScale(scale);
  659. //双指旋转(如果没禁用旋转)
  660. let touchRelative = [{
  661. x: e.touches[0].clientX - this.imgLeft,
  662. y: e.touches[0].clientY - this.imgTop
  663. },
  664. {
  665. x: e.touches[1].clientX - this.imgLeft,
  666. y: e.touches[1].clientY - this.imgTop
  667. }
  668. ];
  669. if (!this.disableRotate) {
  670. let first_atan = (180 / Math.PI) * Math.atan2(touchRelative[0].y, touchRelative[0].x);
  671. let first_atan_old = (180 / Math.PI) * Math.atan2(this.touchRelative[0].y, this.touchRelative[0].x);
  672. let second_atan = (180 / Math.PI) * Math.atan2(touchRelative[1].y, touchRelative[1].x);
  673. let second_atan_old = (180 / Math.PI) * Math.atan2(this.touchRelative[1].y, this.touchRelative[1].x);
  674. //当前旋转的角度
  675. let first_deg = first_atan - first_atan_old,
  676. second_deg = second_atan - second_atan_old;
  677. if (first_deg != 0) {
  678. current_deg = first_deg;
  679. } else if (second_deg != 0) {
  680. current_deg = second_deg;
  681. }
  682. }
  683. this.touchRelative = touchRelative;
  684. this.hypotenuseLength = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
  685. //更新视图
  686. this.angle = this.angle + current_deg;
  687. this.scale = this.scale;
  688. }
  689. },
  690. //结束操作
  691. end(e) {
  692. this.flagEndTouch = true;
  693. this.moveStop();
  694. },
  695. //裁剪框处理
  696. cutTouchMove(e) {
  697. if (this.flagCutTouch && this.MOVE_THROTTLE_FLAG) {
  698. if (this.lockRatio && (this.lockWidth || this.lockHeight)) return;
  699. //节流
  700. this.MOVE_THROTTLE_FLAG = false;
  701. this.moveThrottle();
  702. let width = this.canvasWidth,
  703. height = this.canvasHeight,
  704. cutY = this.cutY,
  705. cutX = this.cutX,
  706. size_correct = () => {
  707. width = width <= this.maxWidth ? (width >= this.minWidth ? width : this.minWidth) : this.maxWidth;
  708. height = height <= this.maxHeight ? (height >= this.minHeight ? height : this.minHeight) : this.maxHeight;
  709. },
  710. size_inspect = () => {
  711. if ((width > this.maxWidth || width < this.minWidth || height > this.maxHeight || height < this.minHeight) &&
  712. this.lockRatio) {
  713. size_correct();
  714. return false;
  715. } else {
  716. size_correct();
  717. return true;
  718. }
  719. };
  720. height = this.CUT_START.height + (this.CUT_START.corner > 1 && this.CUT_START.corner < 4 ? 1 : -1) * (this.CUT_START
  721. .y - e.touches[0].clientY);
  722. switch (this.CUT_START.corner) {
  723. case 1:
  724. width = this.CUT_START.width - this.CUT_START.x + e.touches[0].clientX;
  725. if (this.lockRatio) {
  726. height = width / (this.canvasWidth / this.canvasHeight);
  727. }
  728. if (!size_inspect()) return;
  729. break;
  730. case 2:
  731. width = this.CUT_START.width - this.CUT_START.x + e.touches[0].clientX;
  732. if (this.lockRatio) {
  733. height = width / (this.canvasWidth / this.canvasHeight);
  734. }
  735. if (!size_inspect()) return;
  736. cutY = this.CUT_START.cutY - (height - this.CUT_START.height);
  737. break;
  738. case 3:
  739. width = this.CUT_START.width + this.CUT_START.x - e.touches[0].clientX;
  740. if (this.lockRatio) {
  741. height = width / (this.canvasWidth / this.canvasHeight);
  742. }
  743. if (!size_inspect()) return;
  744. cutY = this.CUT_START.cutY - (height - this.CUT_START.height);
  745. cutX = this.CUT_START.cutX - (width - this.CUT_START.width);
  746. break;
  747. case 4:
  748. width = this.CUT_START.width + this.CUT_START.x - e.touches[0].clientX;
  749. if (this.lockRatio) {
  750. height = width / (this.canvasWidth / this.canvasHeight);
  751. }
  752. if (!size_inspect()) return;
  753. cutX = this.CUT_START.cutX - (width - this.CUT_START.width);
  754. break;
  755. default:
  756. break;
  757. }
  758. if (!this.lockWidth && !this.lockHeight) {
  759. this.canvasWidth = width;
  760. this.cutX = cutX;
  761. this.canvasHeight = height;
  762. this.cutY = cutY;
  763. } else if (!this.lockWidth) {
  764. this.canvasWidth = width;
  765. this.cutX = cutX;
  766. } else if (!this.lockHeight) {
  767. this.canvasHeight = height;
  768. this.cutY = cutY;
  769. }
  770. this.imgMarginDetectionScale();
  771. }
  772. },
  773. cutTouchStart(e) {
  774. let currentX = e.touches[0].clientX;
  775. let currentY = e.touches[0].clientY;
  776. /*
  777. * (右下-1 右上-2 左上-3 左下-4)
  778. * left_x [3,4]
  779. * top_y [2,3]
  780. * right_x [1,2]
  781. * bottom_y [1,4]
  782. */
  783. let left_x1 = this.cutX - 24;
  784. let left_x2 = this.cutX + 24;
  785. let top_y1 = this.cutY - 24;
  786. let top_y2 = this.cutY + 24;
  787. let right_x1 = this.cutX + this.canvasWidth - 24;
  788. let right_x2 = this.cutX + this.canvasWidth + 24;
  789. let bottom_y1 = this.cutY + this.canvasHeight - 24;
  790. let bottom_y2 = this.cutY + this.canvasHeight + 24;
  791. if (currentX > right_x1 && currentX < right_x2 && currentY > bottom_y1 && currentY < bottom_y2) {
  792. this.moveDuring();
  793. this.flagCutTouch = true;
  794. this.flagEndTouch = true;
  795. this.CUT_START = {
  796. width: this.canvasWidth,
  797. height: this.canvasHeight,
  798. x: currentX,
  799. y: currentY,
  800. corner: 1
  801. };
  802. } else if (currentX > right_x1 && currentX < right_x2 && currentY > top_y1 && currentY < top_y2) {
  803. this.moveDuring();
  804. this.flagCutTouch = true;
  805. this.flagEndTouch = true;
  806. this.CUT_START = {
  807. width: this.canvasWidth,
  808. height: this.canvasHeight,
  809. x: currentX,
  810. y: currentY,
  811. cutY: this.cutY,
  812. cutX: this.cutX,
  813. corner: 2
  814. };
  815. } else if (currentX > left_x1 && currentX < left_x2 && currentY > top_y1 && currentY < top_y2) {
  816. this.moveDuring();
  817. this.flagCutTouch = true;
  818. this.flagEndTouch = true;
  819. this.CUT_START = {
  820. width: this.canvasWidth,
  821. height: this.canvasHeight,
  822. cutY: this.cutY,
  823. cutX: this.cutX,
  824. x: currentX,
  825. y: currentY,
  826. corner: 3
  827. };
  828. } else if (currentX > left_x1 && currentX < left_x2 && currentY > bottom_y1 && currentY < bottom_y2) {
  829. this.moveDuring();
  830. this.flagCutTouch = true;
  831. this.flagEndTouch = true;
  832. this.CUT_START = {
  833. width: this.canvasWidth,
  834. height: this.canvasHeight,
  835. cutY: this.cutY,
  836. cutX: this.cutX,
  837. x: currentX,
  838. y: currentY,
  839. corner: 4
  840. };
  841. }
  842. },
  843. cutTouchEnd(e) {
  844. this.moveStop();
  845. this.flagCutTouch = false;
  846. },
  847. //停止移动时需要做的操作
  848. moveStop() {
  849. //清空之前的自动居中延迟函数并添加最新的
  850. clearTimeout(this.TIME_CUT_CENTER);
  851. this.TIME_CUT_CENTER = setTimeout(() => {
  852. //动画启动
  853. if (!this.cutAnimation) {
  854. this.cutAnimation = true;
  855. }
  856. this.setCutCenter();
  857. }, 800);
  858. },
  859. //移动中
  860. moveDuring() {
  861. //清空之前的自动居中延迟函数
  862. clearTimeout(this.TIME_CUT_CENTER);
  863. },
  864. showLoading() {
  865. uni.showLoading({
  866. title: '请稍候...',
  867. mask: true
  868. });
  869. },
  870. stop() {},
  871. back() {
  872. uni.navigateBack();
  873. },
  874. setAngle() {
  875. this.cutAnimation = true;
  876. this.angle = this.angle + 90;
  877. }
  878. }
  879. };
  880. </script>
  881. <style scoped>
  882. .tui-container {
  883. width: 100vw;
  884. height: 100vh;
  885. background-color: rgba(0, 0, 0, 0.6);
  886. position: fixed;
  887. top: 0;
  888. left: 0;
  889. z-index: 1;
  890. }
  891. .tui-image-cropper {
  892. width: 100vw;
  893. height: 100vh;
  894. position: absolute;
  895. }
  896. .tui-content {
  897. width: 100vw;
  898. height: 100vh;
  899. position: absolute;
  900. z-index: 9;
  901. display: flex;
  902. flex-direction: column;
  903. pointer-events: none;
  904. }
  905. .tui-bg-transparent {
  906. background-color: rgba(0, 0, 0, 0.6);
  907. transition-duration: 0.35s;
  908. }
  909. .tui-content-top {
  910. pointer-events: none;
  911. }
  912. .tui-content-middle {
  913. width: 100%;
  914. height: 200px;
  915. display: flex;
  916. box-sizing: border-box;
  917. }
  918. .tui-cropper-box {
  919. position: relative;
  920. /* transition-duration: 0.3s; */
  921. border-style: solid;
  922. border-width: 1rpx;
  923. box-sizing: border-box;
  924. }
  925. .tui-flex-auto {
  926. flex: auto;
  927. }
  928. .tui-cropper-image {
  929. width: 100%;
  930. border-style: none;
  931. position: absolute;
  932. top: 0;
  933. left: 0;
  934. z-index: 2;
  935. -webkit-backface-visibility: hidden;
  936. backface-visibility: hidden;
  937. transform-origin: center;
  938. }
  939. .tui-cropper-canvas {
  940. position: fixed;
  941. z-index: 10;
  942. left: -2000px;
  943. top: -2000px;
  944. pointer-events: none;
  945. }
  946. .tui-edge {
  947. border-style: solid;
  948. pointer-events: auto;
  949. position: absolute;
  950. box-sizing: border-box;
  951. }
  952. .tui-top-left {
  953. border-bottom-width: 0 !important;
  954. border-right-width: 0 !important;
  955. }
  956. .tui-top-right {
  957. border-bottom-width: 0 !important;
  958. border-left-width: 0 !important;
  959. }
  960. .tui-bottom-left {
  961. border-top-width: 0 !important;
  962. border-right-width: 0 !important;
  963. }
  964. .tui-bottom-right {
  965. border-top-width: 0 !important;
  966. border-left-width: 0 !important;
  967. }
  968. .tui-cropper-tabbar {
  969. width: 100%;
  970. height: 120rpx;
  971. padding: 0 40rpx;
  972. box-sizing: border-box;
  973. position: fixed;
  974. left: 0;
  975. bottom: 0;
  976. z-index: 99;
  977. display: flex;
  978. align-items: center;
  979. justify-content: space-between;
  980. color: #ffffff;
  981. font-size: 32rpx;
  982. }
  983. .tui-cropper-tabbar::after {
  984. content: ' ';
  985. position: absolute;
  986. top: 0;
  987. right: 0;
  988. left: 0;
  989. border-top: 1rpx solid rgba(255, 255, 255, 0.2);
  990. -webkit-transform: scaleY(0.5) translateZ(0);
  991. transform: scaleY(0.5) translateZ(0);
  992. transform-origin: 0 100%;
  993. }
  994. .tui-op-btn {
  995. height: 80rpx;
  996. display: flex;
  997. align-items: center;
  998. }
  999. .tui-rotate-img {
  1000. width: 44rpx;
  1001. height: 44rpx;
  1002. }
  1003. </style>