tui-image-cropper.vue 28 KB

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