tui-image-cropper.vue 29 KB

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