当前位置:   article > 正文

uniapp实现图片手指自由旋转剪裁功能(自定义剪裁区域)_sysinfo.platform

sysinfo.platform

1.新建公共组件components / cropping / cropping.vue 将代码复制进去

 复制代码放入cropping.vue

  1. <template name="yq-avatar">
  2. <view class="croppage">
  3. <canvas canvas-id="avatar-canvas" id="avatar-canvas" class="my-canvas" :style="{top: sT, height: csH}"
  4. :disable-scroll="false"></canvas>
  5. <canvas canvas-id="oper-canvas" id="oper-canvas" class="oper-canvas" :style="{top: sT1, height: csH1}"
  6. :disable-scroll="false" @touchstart="fStart" @touchmove="fMove" @touchend="fEnd">
  7. </canvas>
  8. <canvas canvas-id="prv-canvas" id="prv-canvas" class="prv-canvas" :disable-scroll="false" @touchstart="fHideImg"
  9. :style="{ height: csH, top: pT }">
  10. </canvas>
  11. <view class="oper-wrapper" :style="{top:tp}">
  12. <view class="oper">
  13. <view class="btn-wrapper">
  14. <view @click="rechoose" hover-class="hover" :style="{width: bW}"><text>重选</text></view>
  15. <view @click="fUpload" hover-class="hover" :style="{width: bW}"><text>确定</text></view>
  16. </view>
  17. </view>
  18. </view>
  19. </view>
  20. </template>
  21. <script>
  22. const tH = 50;
  23. export default {
  24. data() {
  25. return {
  26. sT1num: 0,
  27. csH1: '0px',
  28. sT1: 0,
  29. csH: '0px',
  30. sD: 'none',
  31. sT: '-10000px',
  32. pT: '-10000px',
  33. iS: {},
  34. sS: {},
  35. bW: '19%',
  36. bD: 'flex',
  37. tp: 0,
  38. };
  39. },
  40. props: {
  41. avatarSrc: '',
  42. avatarStyle: '',
  43. selWidth: '',
  44. selHeight: '',
  45. expWidth: '',
  46. expHeight: '',
  47. minScale: '',
  48. maxScale: '',
  49. canScale: '',
  50. canRotate: '',
  51. lockWidth: '',
  52. lockHeight: '',
  53. stretch: '',
  54. lock: '',
  55. fileType: '',
  56. noTab: '',
  57. inner: '',
  58. quality: '',
  59. index: '',
  60. bgImage: '',
  61. },
  62. created() {
  63. this.cc = uni.createCanvasContext('avatar-canvas', this);
  64. this.cco = uni.createCanvasContext('oper-canvas', this);
  65. this.ccp = uni.createCanvasContext('prv-canvas', this);
  66. this.qlty = parseFloat(this.quality) || 1; //画质
  67. this.letRotate = (this.canRotate === false || this.inner === true || this.inner === 'true' || this
  68. .canRotate === 'false') ? 0 : 1; //是否旋转
  69. this.letScale = (this.canScale === false || this.canScale === 'false') ? 0 : 1; //是否放大
  70. this.isin = (this.inner === true || this.inner === 'true') ? 1 : 0;
  71. this.indx = this.index || undefined; //禁止旋转并在图片范围内移动
  72. this.mnScale = parseFloat(this.minScale) || 0.3; //缩小比例
  73. this.mxScale = parseFloat(this.maxScale) || 4; //放大比列
  74. this.noBar = (this.noTab === true || this.noTab === 'true') ? 1 : 0; //是否存在nobar
  75. this.stc = this.stretch; //自动铺满
  76. this.lck = this.lock; //锁定图方向 x y long short longSel shortSel
  77. this.fType = this.fileType === 'jpg' ? 'jpg' : 'png';
  78. if (this.isin || !this.letRotate) {
  79. this.bW = '24%';
  80. this.bD = 'none';
  81. } else {
  82. this.bW = '19%';
  83. this.bD = 'flex';
  84. }
  85. if (this.noBar) {
  86. this.fWindowResize();
  87. } else {
  88. uni.showTabBar({
  89. fail: () => {
  90. this.noBar = 1;
  91. },
  92. success: () => {
  93. this.noBar = 0;
  94. },
  95. complete: (res) => {
  96. this.fWindowResize();
  97. }
  98. });
  99. }
  100. },
  101. methods: {
  102. //重选
  103. rechoose() {
  104. const that = this
  105. uni.chooseImage({
  106. count: 1,
  107. sizeType: ['original', 'compressed'],
  108. sourceType: ["album"],
  109. success: (res) => {
  110. that.sT1 = that.sT
  111. that.csH1 = that.csH
  112. that.fSelect(res.tempFilePaths[0])
  113. }
  114. });
  115. },
  116. //初始化各种数据
  117. fWindowResize() {
  118. let sysInfo = uni.getSystemInfoSync();
  119. this.platform = sysInfo.platform;
  120. this.wW = sysInfo.windowWidth;
  121. // #ifndef H5
  122. let phone = uni.getSystemInfoSync().platform
  123. if (phone == 'ios') {
  124. let h = uni.upx2px(60); //将rpx单位值转换成px 裁剪框自己
  125. let b = uni.upx2px(100); //底部按钮
  126. this.sT1 = (sysInfo.windowHeight - h - b) / 2 + 'px' //裁剪框距离顶部+px
  127. this.sT1num = (sysInfo.windowHeight - h - b) / 2 //裁剪框距离顶部不+px
  128. } else {
  129. this.drawTop = 0;
  130. }
  131. // #endif
  132. // #ifndef MP-ALIPAY
  133. this.wH = sysInfo.windowHeight; //设备高
  134. if (!this.noBar) this.wH += tH; //th=50
  135. this.csH = this.wH - tH + 'px'; //高=设备高-50(导航)
  136. if (phone == 'ios') { //适配ios
  137. this.csH1 = (this.wH - 50) / 2 + 'px'
  138. } else {
  139. this.csH1 = this.wH - tH + 'px';
  140. }
  141. // #endif
  142. this.tp = this.csH;
  143. this.pxRatio = this.wW / 750; //设备宽/750 比列
  144. this.expWidth && (this.eW = this.expWidth.toString().indexOf('upx') >= 0 ? parseInt(this.expWidth) * this
  145. .pxRatio : parseInt(this.expWidth));
  146. this.expHeight && (this.eH = this.expHeight.toString().indexOf('upx') >= 0 ? parseInt(this.expHeight) *
  147. this.pxRatio : parseInt(this.expHeight));
  148. this.fHideImg();
  149. },
  150. fSelect(r) {
  151. if (this.fSelecting) return;
  152. this.fSelecting = true;
  153. setTimeout(() => {
  154. this.fSelecting = false;
  155. }, 500); //防抖
  156. let path = this.imgPath = r; //需要剪裁的图片路径
  157. // 获取图片信息
  158. uni.getImageInfo({
  159. src: path,
  160. success: r => {
  161. this.imgWidth = r.width;
  162. this.imgHeight = r.height;
  163. this.path = path;
  164. if (!this.hasSel) {
  165. let style = this.sS || {};
  166. if (this.selWidth && this.selHeight) { //设置的剪裁区域宽高
  167. let sW = this.selWidth.toString().indexOf('upx') >= 0 ?
  168. parseInt(this.selWidth) * this.pxRatio : parseInt(
  169. this.selWidth),
  170. sH = this.selHeight.toString().indexOf('upx') >= 0 ?
  171. parseInt(this.selHeight) * this.pxRatio : parseInt(
  172. this.selHeight);
  173. style.width = sW + 'px';
  174. style.height = sH + 'px';
  175. style.top = ((this.wH - sH - tH) | 0) / 2 + 'px';
  176. style.left = ((this.wW - sW) | 0) / 2 + 'px';
  177. // }
  178. } else {
  179. uni.showModal({
  180. title: '裁剪框的宽或高没有设置',
  181. showCancel: false
  182. })
  183. return;
  184. }
  185. this.sS = style;
  186. }
  187. if (this.noBar) {
  188. setTimeout(() => {
  189. this.fDrawInit(true);
  190. }, 200)
  191. } else {
  192. uni.hideTabBar({
  193. complete: () => {
  194. setTimeout(() => {
  195. this.fDrawInit(true);
  196. }, 200)
  197. }
  198. });
  199. }
  200. },
  201. fail: () => {
  202. uni.showToast({
  203. title: "请选择正确图片",
  204. duration: 2000,
  205. })
  206. },
  207. complete() {
  208. uni.hideLoading();
  209. }
  210. });
  211. },
  212. //剪裁确定
  213. fUpload() {
  214. uni.showLoading({
  215. title: '图片上传中...',
  216. mask: true
  217. });
  218. if (this.fUploading) return;
  219. this.fUploading = true;
  220. setTimeout(() => {
  221. this.fUploading = false;
  222. }, 1000)
  223. let style = this.sS,
  224. x = parseInt(style.left),
  225. y = parseInt(style.top),
  226. width = parseInt(style.width),
  227. height = parseInt(style.height),
  228. expWidth = this.eW || (width * this.pixelRatio),
  229. expHeight = this.eH || (height * this.pixelRatio);
  230. this.fHideImg();
  231. // #ifndef MP-ALIPAY
  232. let phone = uni.getSystemInfoSync().platform
  233. if (phone == 'ios') {
  234. y = this.sT1num
  235. }
  236. uni.canvasToTempFilePath({
  237. x: x,
  238. y: y,
  239. width: width,
  240. height: height,
  241. destWidth: expWidth,
  242. destHeight: expHeight,
  243. canvasId: 'avatar-canvas',
  244. fileType: this.fType,
  245. quality: this.qlty,
  246. success: (r) => {
  247. r = r.tempFilePath;
  248. // #ifndef H5
  249. this.$emit("upload", {
  250. avatar: this.imgSrc,
  251. path: r,
  252. index: this.indx,
  253. data: this.rtn,
  254. base64: this.base64 || null
  255. });
  256. // #endif
  257. },
  258. fail: (res) => {
  259. uni.showToast({
  260. title: "error1",
  261. duration: 2000,
  262. })
  263. },
  264. complete: () => {
  265. uni.hideLoading();
  266. this.noBar || uni.showTabBar();
  267. this.$emit("end");
  268. }
  269. }, this);
  270. // #endif
  271. },
  272. fDrawInit(ini = false) {
  273. let allWidth = this.wW, //设备宽
  274. allHeight = this.wH, //设备高
  275. imgWidth = this.imgWidth, //图宽
  276. imgHeight = this.imgHeight, //图高
  277. imgRadio = imgWidth / imgHeight, //图比
  278. useWidth = allWidth - 40, //设备宽-40
  279. useHeight = allHeight - tH - 80, //设备高-80
  280. useRadio = useWidth / useHeight,
  281. sW = parseInt(this.sS.width), //图片信息
  282. sH = parseInt(this.sS.height);
  283. this.fixWidth = 0;
  284. this.fixHeight = 0;
  285. this.lckWidth = 0;
  286. this.lckHeight = 0;
  287. switch (this.stc) { //以下是自动铺满的算法
  288. case 'x':
  289. this.fixWidth = 1;
  290. break;
  291. case 'y':
  292. this.fixHeight = 1;
  293. break;
  294. case 'long':
  295. if (imgRadio > 1) this.fixWidth = 1;
  296. else this.fixHeight = 1;
  297. break;
  298. case 'short':
  299. if (imgRadio > 1) this.fixHeight = 1;
  300. else this.fixWidth = 1;
  301. break;
  302. case 'longSel':
  303. if (sW > sH) this.fixWidth = 1;
  304. else this.fixHeight = 1;
  305. break;
  306. case 'shortSel':
  307. if (sW > sH) this.fixHeight = 1;
  308. else this.fixWidth = 1;
  309. break;
  310. }
  311. switch (this.lck) { //以下锁定屏幕的移动方向
  312. case 'x':
  313. this.lckWidth = 1;
  314. break;
  315. case 'y':
  316. this.lckHeight = 1;
  317. break;
  318. case 'long':
  319. if (imgRadio > 1) this.lckWidth = 1;
  320. else this.lckHeight = 1;
  321. break;
  322. case 'short':
  323. if (imgRadio > 1) this.lckHeight = 1;
  324. else this.lckWidth = 1;
  325. break;
  326. case 'longSel':
  327. if (sW > sH) this.lckWidth = 1;
  328. else this.lckHeight = 1;
  329. break;
  330. case 'shortSel':
  331. if (sW > sH) this.lckHeight = 1;
  332. else this.lckWidth = 1;
  333. break;
  334. }
  335. if (this.fixWidth) {
  336. useWidth = sW;
  337. useHeight = useWidth / imgRadio;
  338. } else if (this.fixHeight) {
  339. useHeight = sH;
  340. useWidth = useHeight * imgRadio;
  341. } else if (imgRadio < useRadio) {
  342. if (imgHeight < useHeight) {
  343. useWidth = imgWidth;
  344. useHeight = imgHeight;
  345. } else {
  346. useWidth = useHeight * imgRadio;
  347. }
  348. } else {
  349. if (imgWidth < useWidth) {
  350. useWidth = imgWidth;
  351. useHeight = imgHeight;
  352. } else {
  353. useHeight = useWidth / imgRadio;
  354. }
  355. }
  356. if (this.isin) {
  357. if (useWidth < sW) {
  358. useWidth = sW;
  359. useHeight = useWidth / imgRadio;
  360. this.lckHeight = 0;
  361. }
  362. if (useHeight < sH) {
  363. useHeight = sH;
  364. useWidth = useHeight * imgRadio;
  365. this.lckWidth = 0;
  366. }
  367. }
  368. this.scaleSize = 1;
  369. this.rotateDeg = 0;
  370. this.posWidth = (allWidth - useWidth) / 2 | 0;
  371. this.posHeight = (allHeight - useHeight - tH) / 2 | 0;
  372. this.useWidth = useWidth | 0;
  373. this.useHeight = useHeight | 0;
  374. this.centerX = this.posWidth + useWidth / 2;
  375. this.centerY = this.posHeight + useHeight / 2;
  376. this.focusX = 0;
  377. this.focusY = 0;
  378. let style = this.sS,
  379. left = parseInt(style.left),
  380. top = parseInt(style.top),
  381. width = parseInt(style.width),
  382. height = parseInt(style.height),
  383. canvas = this.canvas,
  384. canvasOper = this.canvasOper,
  385. cc = this.cc, //avatar-canvas
  386. cco = this.cco; //oper-canvas 裁剪
  387. cco.beginPath(); //开始创建一个路径
  388. cco.setLineWidth(3); //设置线条的宽度 px
  389. cco.setGlobalAlpha(1); //设置全局画笔透明度。
  390. cco.setStrokeStyle('white'); //设置边框颜色。如果没有设置 fillStyle,默认颜色为 black。
  391. cco.strokeRect(left, top, width, height); //画一个矩形(非填充)。用 setFillStroke() 设置边框颜色,如果没设置默认是黑色。
  392. cco.setFillStyle('black');
  393. cco.setGlobalAlpha(0.5);
  394. cco.fillRect(0, 0, this.wW, top); //填充一个矩形
  395. cco.fillRect(0, top, left, height);
  396. cco.fillRect(0, top + height, this.wW, this.wH - height - top - tH);
  397. cco.fillRect(left + width, top, this.wW - width - left, height);
  398. cco.setGlobalAlpha(1);
  399. cco.setStrokeStyle('red');
  400. cco.moveTo(left + 15, top); //把路径移动到画布中的指定点,不创建线条。用 stroke() 方法来画线条。
  401. cco.lineTo(left, top); //增加一个新点,然后创建一条从上次指定点到目标点的线。
  402. cco.lineTo(left, top + 15);
  403. cco.moveTo(left + width - 15, top);
  404. cco.lineTo(left + width, top);
  405. cco.lineTo(left + width, top + 15);
  406. cco.moveTo(left + 15, top + height);
  407. cco.lineTo(left, top + height);
  408. cco.lineTo(left, top + height - 15);
  409. cco.moveTo(left + width - 15, top + height);
  410. cco.lineTo(left + width, top + height);
  411. cco.lineTo(left + width, top + height - 15);
  412. cco.stroke(); //画线条
  413. cco.draw(false, () => { //将之前在绘图上下文中的描述(路径、变形、样式)画到 canvas 中。
  414. if (ini) {
  415. this.sT = this.drawTop + 'px';
  416. this.fDrawImage(true); //绘制背景色
  417. }
  418. });
  419. this.$emit("init");
  420. },
  421. //绘制背景色
  422. fDrawImage(ini = false) {
  423. let tm_now = Date.now(); //当前时间
  424. if (tm_now - this.drawTm < 20) return;
  425. this.drawTm = tm_now;
  426. let cc = this.cc,
  427. imgWidth = this.useWidth * this.scaleSize,
  428. imgHeight = this.useHeight * this.scaleSize;
  429. if (this.bgImage) { //如果背景图
  430. // #ifndef MP-ALIPAY
  431. cc.drawImage(this.bgImage, 0, 0, this.wW, this.wH - tH); //绘制图像到画布。
  432. // #endif
  433. } else {
  434. cc.fillRect(0, 0, this.wW, this.wH - tH); //填充一个矩形
  435. }
  436. if (this.isin) { //禁止旋转并在图片范围内移动
  437. let cx = this.focusX * (this.scaleSize - 1),
  438. cy = this.focusY * (this.scaleSize - 1);
  439. cc.translate(this.centerX, this.centerY);
  440. cc.rotate(this.rotateDeg * Math.PI / 180);
  441. cc.drawImage(this.imgPath, this.posWidth - this.centerX - cx, this.posHeight - this.centerY - cy,
  442. imgWidth, imgHeight);
  443. } else {
  444. cc.translate(this.posWidth + imgWidth / 2, this.posHeight + imgHeight / 2);
  445. cc.rotate(this.rotateDeg * Math.PI / 180);
  446. cc.drawImage(this.imgPath, -imgWidth / 2, -imgHeight / 2, imgWidth, imgHeight);
  447. }
  448. cc.draw(false);
  449. },
  450. //旋转
  451. fRotate() {
  452. this.rotateDeg += 90 - this.rotateDeg % 90;
  453. this.fDrawImage();
  454. },
  455. //手指触摸开始
  456. fStart(e) {
  457. let touches = e.touches,
  458. touch0 = touches[0],
  459. touch1 = touches[1];
  460. this.touch0 = touch0;
  461. this.touch1 = touch1;
  462. if (touch1) {
  463. let x = touch1.x - touch0.x,
  464. y = touch1.y - touch0.y;
  465. this.fgDistance = Math.sqrt(x * x + y * y);
  466. }
  467. },
  468. //手指触摸后移动
  469. fMove(e) {
  470. let touches = e.touches,
  471. touch0 = touches[0],
  472. touch1 = touches[1];
  473. if (touch1) {
  474. let x = touch1.x - touch0.x,
  475. y = touch1.y - touch0.y,
  476. fgDistance = Math.sqrt(x * x + y * y),
  477. scaleSize = 0.005 * (fgDistance - this.fgDistance),
  478. beScaleSize = this.scaleSize + scaleSize;
  479. do {
  480. if (!this.letScale) break;
  481. if (beScaleSize < this.mnScale) break;
  482. if (beScaleSize > this.mxScale) break;
  483. let growX = this.useWidth * scaleSize / 2,
  484. growY = this.useHeight * scaleSize / 2;
  485. if (this.isin) {
  486. let imgWidth = this.useWidth * beScaleSize,
  487. imgHeight = this.useHeight * beScaleSize,
  488. l = this.posWidth - growX,
  489. t = this.posHeight - growY,
  490. r = l + imgWidth,
  491. b = t + imgHeight,
  492. left = parseInt(this.sS.left),
  493. top = parseInt(this.sS.top),
  494. width = parseInt(this.sS.width),
  495. height = parseInt(this.sS.height),
  496. right = left + width,
  497. bottom = top + height,
  498. cx, cy;
  499. if (imgWidth <= width || imgHeight <= height) break;
  500. this.cx = cx = this.focusX * beScaleSize - this.focusX,
  501. this.cy = cy = this.focusY * beScaleSize - this.focusY;
  502. this.posWidth -= growX;
  503. this.posHeight -= growY;
  504. if (this.posWidth - cx > left) {
  505. this.posWidth = left + cx;
  506. }
  507. if (this.posWidth + imgWidth - cx < right) {
  508. this.posWidth = right - imgWidth + cx;
  509. }
  510. if (this.posHeight - cy > top) {
  511. this.posHeight = top + cy;
  512. }
  513. if (this.posHeight + imgHeight - cy < bottom) {
  514. this.posHeight = bottom - imgHeight + cy;
  515. }
  516. } else {
  517. this.posWidth -= growX;
  518. this.posHeight -= growY;
  519. }
  520. this.scaleSize = beScaleSize;
  521. } while (0);
  522. this.fgDistance = fgDistance;
  523. if (touch1.x !== touch0.x && this.letRotate) {
  524. x = (this.touch1.y - this.touch0.y) / (this.touch1.x - this.touch0.x);
  525. y = (touch1.y - touch0.y) / (touch1.x - touch0.x);
  526. this.rotateDeg += Math.atan((y - x) / (1 + x * y)) * 180 / Math.PI;
  527. this.touch0 = touch0;
  528. this.touch1 = touch1;
  529. }
  530. this.fDrawImage();
  531. } else if (this.touch0) {
  532. let x = touch0.x - this.touch0.x,
  533. y = touch0.y - this.touch0.y,
  534. beX = this.posWidth + x,
  535. beY = this.posHeight + y;
  536. if (this.isin) {
  537. let imgWidth = this.useWidth * this.scaleSize,
  538. imgHeight = this.useHeight * this.scaleSize,
  539. l = beX,
  540. t = beY,
  541. r = l + imgWidth,
  542. b = t + imgHeight,
  543. left = parseInt(this.sS.left),
  544. top = parseInt(this.sS.top),
  545. right = left + parseInt(this.sS.width),
  546. bottom = top + parseInt(this.sS.height),
  547. cx, cy;
  548. this.cx = cx = this.focusX * this.scaleSize - this.focusX;
  549. this.cy = cy = this.focusY * this.scaleSize - this.focusY;
  550. if (!this.lckWidth && Math.abs(x) < 100) {
  551. if (left < l - cx) {
  552. this.posWidth = left + cx;
  553. } else if (right > r - cx) {
  554. this.posWidth = right - imgWidth + cx;
  555. } else {
  556. this.posWidth = beX;
  557. this.focusX -= x;
  558. }
  559. }
  560. if (!this.lckHeight && Math.abs(y) < 100) {
  561. if (top < t - cy) {
  562. this.focusY -= (top + cy - this.posHeight);
  563. this.posHeight = top + cy;
  564. } else if (bottom > b - cy) {
  565. this.focusY -= (bottom + cy - (this.posHeight + imgHeight));
  566. this.posHeight = bottom - imgHeight + cy;
  567. } else {
  568. this.posHeight = beY;
  569. this.focusY -= y;
  570. }
  571. }
  572. } else {
  573. if (Math.abs(x) < 100 && !this.lckWidth) this.posWidth = beX;
  574. if (Math.abs(y) < 100 && !this.lckHeight) this.posHeight = beY;
  575. this.focusX -= x;
  576. this.focusY -= y;
  577. }
  578. this.touch0 = touch0;
  579. this.fDrawImage();
  580. }
  581. },
  582. //手指触摸动作结束
  583. fEnd(e) {
  584. let touches = e.touches,
  585. touch0 = touches && touches[0],
  586. touch1 = touches && touches[1];
  587. if (touch0) {
  588. this.touch0 = touch0;
  589. } else {
  590. this.touch0 = null;
  591. this.touch1 = null;
  592. }
  593. },
  594. fHideImg() {
  595. this.prvImg = '';
  596. this.pT = '-10000px';
  597. this.prvImgData = null;
  598. this.target = null;
  599. },
  600. }
  601. }
  602. </script>
  603. <style>
  604. .croppage {
  605. width: 100%;
  606. height: 100%;
  607. box-sizing: border-box;
  608. background-color: #000000;
  609. position: relative;
  610. }
  611. .my-canvas {
  612. /* display: flex; */
  613. position: absolute !important;
  614. left: 0;
  615. z-index: 100000;
  616. width: 100%;
  617. }
  618. .my-avatar {
  619. width: 150upx;
  620. height: 150upx;
  621. border-radius: 100%;
  622. }
  623. .oper-canvas {
  624. /* display: flex; */
  625. position: absolute !important;
  626. left: 0;
  627. z-index: 100000;
  628. width: 100%;
  629. }
  630. .prv-canvas {
  631. display: flex;
  632. position: fixed !important;
  633. background: #000000;
  634. left: 0;
  635. z-index: 200000;
  636. width: 100%;
  637. }
  638. .oper-wrapper {
  639. height: 100rpx;
  640. position: fixed !important;
  641. box-sizing: border-box;
  642. border: 1px solid #F1F1F1;
  643. background: #ffffff;
  644. width: 100%;
  645. left: 0;
  646. bottom: 0;
  647. z-index: 99999999;
  648. flex-direction: row;
  649. }
  650. .oper {
  651. display: flex;
  652. flex-direction: column;
  653. justify-content: center;
  654. padding: 10upx 20upx;
  655. width: 100%;
  656. height: 100%;
  657. box-sizing: border-box;
  658. align-self: center;
  659. }
  660. .btn-wrapper {
  661. display: flex;
  662. flex-direction: row;
  663. flex-grow: 1;
  664. height: 50px;
  665. justify-content: space-between;
  666. }
  667. .btn-wrapper view {
  668. display: flex;
  669. align-items: center;
  670. justify-content: center;
  671. font-size: 16px;
  672. color: #3b7ffb;
  673. border-radius: 6%;
  674. }
  675. .hover {
  676. background: #f1f1f1;
  677. border-radius: 6%;
  678. }
  679. .clr-wrapper {
  680. display: flex;
  681. flex-direction: row;
  682. flex-grow: 1;
  683. }
  684. .clr-wrapper view {
  685. display: flex;
  686. align-items: center;
  687. justify-content: center;
  688. font-size: 16px;
  689. color: #333;
  690. border: 1px solid #f1f1f1;
  691. border-radius: 6%;
  692. }
  693. .my-slider {
  694. flex-grow: 1;
  695. }
  696. </style>

2.在需要使用的页面引入组件

  1. <template>
  2. <view>
  3. <Cropping @upload="doUpload" ref="cropping" :selWidth="screenWidth" selHeight="60rpx" />
  4. </view>
  5. </template>
  6. <script>
  7. import Cropping from "@/components/cropping/cropping.vue";
  8. export default {
  9. components: {
  10. Cropping
  11. },
  12. data() {
  13. return {
  14. result:'',
  15. screenWidth: ''
  16. }
  17. },
  18. onLoad(option) {
  19. let getWindowInfo = uni.getWindowInfo()
  20. this.screenWidth = getWindowInfo.screenWidth
  21. //screenWidth定义需要裁剪的宽度 selHeight为高度 自行定义传入
  22. this.$nextTick(()=>{
  23. //调用组件的fSelect方法传入需要裁剪的图片路径
  24. //option.img为需要裁剪的图片路径 自行定义传入
  25. this.$refs.cropping.fSelect(option.img)
  26. })
  27. },
  28. methods: {
  29. //剪裁确认
  30. doUpload(rsp) {
  31. console.log(rsp)
  32. //rsp.path为截取的图片路径
  33. },
  34. }
  35. }
  36. </script>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/287118?site
推荐阅读