index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. <template>
  2. <view class="container">
  3. <view class="page-body uni-content-info">
  4. <view class='cropper-content'>
  5. <view v-if="isShowImg" class="uni-corpper" :style="'width:'+cropperInitW+'px;height:'+cropperInitH+'px;background:#000'">
  6. <view class="uni-corpper-content" :style="'width:'+cropperW+'px;height:'+cropperH+'px;left:'+cropperL+'px;top:'+cropperT+'px'">
  7. <image :src="imageSrc" :style="'width:'+cropperW+'px;height:'+cropperH+'px'"></image>
  8. <view class="uni-corpper-crop-box" @touchstart.stop="contentStartMove" @touchmove.stop="contentMoveing" @touchend.stop="contentTouchEnd"
  9. :style="'left:'+cutL+'px;top:'+cutT+'px;right:'+cutR+'px;bottom:'+cutB+'px'">
  10. <view class="uni-cropper-view-box">
  11. <view class="uni-cropper-dashed-h"></view>
  12. <view class="uni-cropper-dashed-v"></view>
  13. <view class="uni-cropper-line-t" data-drag="top" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
  14. <view class="uni-cropper-line-r" data-drag="right" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
  15. <view class="uni-cropper-line-b" data-drag="bottom" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
  16. <view class="uni-cropper-line-l" data-drag="left" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
  17. <view class="uni-cropper-point point-t" data-drag="top" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
  18. <view class="uni-cropper-point point-tr" data-drag="topTight"></view>
  19. <view class="uni-cropper-point point-r" data-drag="right" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
  20. <view class="uni-cropper-point point-rb" data-drag="rightBottom" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
  21. <view class="uni-cropper-point point-b" data-drag="bottom" @touchstart.stop="dragStart" @touchmove.stop="dragMove" @touchend.stop="dragEnd"></view>
  22. <view class="uni-cropper-point point-bl" data-drag="bottomLeft"></view>
  23. <view class="uni-cropper-point point-l" data-drag="left" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
  24. <view class="uni-cropper-point point-lt" data-drag="leftTop"></view>
  25. </view>
  26. </view>
  27. </view>
  28. </view>
  29. </view>
  30. <view class='cropper-config'>
  31. <button type="primary reverse" @click="getImage" style='margin-top: 30rpx;'> 选择头像 </button>
  32. <button type="warn" @click="getImageInfo" style='margin-top: 30rpx;'> 提交 </button>
  33. </view>
  34. <canvas canvas-id="myCanvas" :style="'position:absolute;border: 1px solid red; width:'+imageW+'px;height:'+imageH+'px;top:-9999px;left:-9999px;'"></canvas>
  35. </view>
  36. </view>
  37. </template>
  38. <script>
  39. import config from '@/config'
  40. import store from "@/store"
  41. import { uploadAvatar } from "@/api/system/user"
  42. const baseUrl = config.baseUrl
  43. let sysInfo = uni.getSystemInfoSync()
  44. let SCREEN_WIDTH = sysInfo.screenWidth
  45. let PAGE_X, // 手按下的x位置
  46. PAGE_Y, // 手按下y的位置
  47. PR = sysInfo.pixelRatio, // dpi
  48. T_PAGE_X, // 手移动的时候x的位置
  49. T_PAGE_Y, // 手移动的时候Y的位置
  50. CUT_L, // 初始化拖拽元素的left值
  51. CUT_T, // 初始化拖拽元素的top值
  52. CUT_R, // 初始化拖拽元素的
  53. CUT_B, // 初始化拖拽元素的
  54. CUT_W, // 初始化拖拽元素的宽度
  55. CUT_H, // 初始化拖拽元素的高度
  56. IMG_RATIO, // 图片比例
  57. IMG_REAL_W, // 图片实际的宽度
  58. IMG_REAL_H, // 图片实际的高度
  59. DRAFG_MOVE_RATIO = 1, //移动时候的比例,
  60. INIT_DRAG_POSITION = 100, // 初始化屏幕宽度和裁剪区域的宽度之差,用于设置初始化裁剪的宽度
  61. DRAW_IMAGE_W = sysInfo.screenWidth // 设置生成的图片宽度
  62. export default {
  63. /**
  64. * 页面的初始数据
  65. */
  66. data() {
  67. return {
  68. imageSrc: store.getters.avatar,
  69. isShowImg: false,
  70. // 初始化的宽高
  71. cropperInitW: SCREEN_WIDTH,
  72. cropperInitH: SCREEN_WIDTH,
  73. // 动态的宽高
  74. cropperW: SCREEN_WIDTH,
  75. cropperH: SCREEN_WIDTH,
  76. // 动态的left top值
  77. cropperL: 0,
  78. cropperT: 0,
  79. transL: 0,
  80. transT: 0,
  81. // 图片缩放值
  82. scaleP: 0,
  83. imageW: 0,
  84. imageH: 0,
  85. // 裁剪框 宽高
  86. cutL: 0,
  87. cutT: 0,
  88. cutB: SCREEN_WIDTH,
  89. cutR: '100%',
  90. qualityWidth: DRAW_IMAGE_W,
  91. innerAspectRadio: DRAFG_MOVE_RATIO
  92. }
  93. },
  94. /**
  95. * 生命周期函数--监听页面初次渲染完成
  96. */
  97. onReady: function () {
  98. this.loadImage()
  99. },
  100. methods: {
  101. setData: function (obj) {
  102. let that = this
  103. Object.keys(obj).forEach(function (key) {
  104. that.$set(that.$data, key, obj[key])
  105. })
  106. },
  107. getImage: function () {
  108. var _this = this
  109. uni.chooseImage({
  110. success: function (res) {
  111. _this.setData({
  112. imageSrc: res.tempFilePaths[0],
  113. })
  114. _this.loadImage()
  115. },
  116. })
  117. },
  118. loadImage: function () {
  119. var _this = this
  120. uni.getImageInfo({
  121. src: _this.imageSrc,
  122. success: function success(res) {
  123. IMG_RATIO = 1 / 1
  124. if (IMG_RATIO >= 1) {
  125. IMG_REAL_W = SCREEN_WIDTH
  126. IMG_REAL_H = SCREEN_WIDTH / IMG_RATIO
  127. } else {
  128. IMG_REAL_W = SCREEN_WIDTH * IMG_RATIO
  129. IMG_REAL_H = SCREEN_WIDTH
  130. }
  131. let minRange = IMG_REAL_W > IMG_REAL_H ? IMG_REAL_W : IMG_REAL_H
  132. INIT_DRAG_POSITION = minRange > INIT_DRAG_POSITION ? INIT_DRAG_POSITION : minRange
  133. // 根据图片的宽高显示不同的效果 保证图片可以正常显示
  134. if (IMG_RATIO >= 1) {
  135. let cutT = Math.ceil((SCREEN_WIDTH / IMG_RATIO - (SCREEN_WIDTH / IMG_RATIO - INIT_DRAG_POSITION)) / 2)
  136. let cutB = cutT
  137. let cutL = Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH + INIT_DRAG_POSITION) / 2)
  138. let cutR = cutL
  139. _this.setData({
  140. cropperW: SCREEN_WIDTH,
  141. cropperH: SCREEN_WIDTH / IMG_RATIO,
  142. // 初始化left right
  143. cropperL: Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH) / 2),
  144. cropperT: Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH / IMG_RATIO) / 2),
  145. cutL: cutL,
  146. cutT: cutT,
  147. cutR: cutR,
  148. cutB: cutB,
  149. // 图片缩放值
  150. imageW: IMG_REAL_W,
  151. imageH: IMG_REAL_H,
  152. scaleP: IMG_REAL_W / SCREEN_WIDTH,
  153. qualityWidth: DRAW_IMAGE_W,
  154. innerAspectRadio: IMG_RATIO
  155. })
  156. } else {
  157. let cutL = Math.ceil((SCREEN_WIDTH * IMG_RATIO - (SCREEN_WIDTH * IMG_RATIO)) / 2)
  158. let cutR = cutL
  159. let cutT = Math.ceil((SCREEN_WIDTH - INIT_DRAG_POSITION) / 2)
  160. let cutB = cutT
  161. _this.setData({
  162. cropperW: SCREEN_WIDTH * IMG_RATIO,
  163. cropperH: SCREEN_WIDTH,
  164. // 初始化left right
  165. cropperL: Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH * IMG_RATIO) / 2),
  166. cropperT: Math.ceil((SCREEN_WIDTH - SCREEN_WIDTH) / 2),
  167. cutL: cutL,
  168. cutT: cutT,
  169. cutR: cutR,
  170. cutB: cutB,
  171. // 图片缩放值
  172. imageW: IMG_REAL_W,
  173. imageH: IMG_REAL_H,
  174. scaleP: IMG_REAL_W / SCREEN_WIDTH,
  175. qualityWidth: DRAW_IMAGE_W,
  176. innerAspectRadio: IMG_RATIO
  177. })
  178. }
  179. _this.setData({
  180. isShowImg: true
  181. })
  182. uni.hideLoading()
  183. }
  184. })
  185. },
  186. // 拖动时候触发的touchStart事件
  187. contentStartMove(e) {
  188. PAGE_X = e.touches[0].pageX
  189. PAGE_Y = e.touches[0].pageY
  190. },
  191. // 拖动时候触发的touchMove事件
  192. contentMoveing(e) {
  193. var _this = this
  194. var dragLengthX = (PAGE_X - e.touches[0].pageX) * DRAFG_MOVE_RATIO
  195. var dragLengthY = (PAGE_Y - e.touches[0].pageY) * DRAFG_MOVE_RATIO
  196. // 左移
  197. if (dragLengthX > 0) {
  198. if (this.cutL - dragLengthX < 0) dragLengthX = this.cutL
  199. } else {
  200. if (this.cutR + dragLengthX < 0) dragLengthX = -this.cutR
  201. }
  202. if (dragLengthY > 0) {
  203. if (this.cutT - dragLengthY < 0) dragLengthY = this.cutT
  204. } else {
  205. if (this.cutB + dragLengthY < 0) dragLengthY = -this.cutB
  206. }
  207. this.setData({
  208. cutL: this.cutL - dragLengthX,
  209. cutT: this.cutT - dragLengthY,
  210. cutR: this.cutR + dragLengthX,
  211. cutB: this.cutB + dragLengthY
  212. })
  213. PAGE_X = e.touches[0].pageX
  214. PAGE_Y = e.touches[0].pageY
  215. },
  216. contentTouchEnd() {
  217. },
  218. // 获取图片
  219. getImageInfo() {
  220. var _this = this
  221. uni.showLoading({
  222. title: '图片生成中...',
  223. })
  224. // 将图片写入画布
  225. const ctx = uni.createCanvasContext('myCanvas')
  226. ctx.drawImage(_this.imageSrc, 0, 0, IMG_REAL_W, IMG_REAL_H)
  227. ctx.draw(true, () => {
  228. // 获取画布要裁剪的位置和宽度 均为百分比 * 画布中图片的宽度 保证了在微信小程序中裁剪的图片模糊 位置不对的问题 canvasT = (_this.cutT / _this.cropperH) * (_this.imageH / pixelRatio)
  229. var canvasW = ((_this.cropperW - _this.cutL - _this.cutR) / _this.cropperW) * IMG_REAL_W
  230. var canvasH = ((_this.cropperH - _this.cutT - _this.cutB) / _this.cropperH) * IMG_REAL_H
  231. var canvasL = (_this.cutL / _this.cropperW) * IMG_REAL_W
  232. var canvasT = (_this.cutT / _this.cropperH) * IMG_REAL_H
  233. uni.canvasToTempFilePath({
  234. x: canvasL,
  235. y: canvasT,
  236. width: canvasW,
  237. height: canvasH,
  238. destWidth: canvasW,
  239. destHeight: canvasH,
  240. quality: 0.5,
  241. canvasId: 'myCanvas',
  242. success: function (res) {
  243. uni.hideLoading()
  244. let data = {name: 'avatarfile', filePath: res.tempFilePath}
  245. uploadAvatar(data).then(response => {
  246. store.commit('SET_AVATAR', baseUrl + response.imgUrl)
  247. uni.showToast({ title: "修改成功", icon: 'success' })
  248. uni.navigateBack()
  249. })
  250. }
  251. })
  252. })
  253. },
  254. // 设置大小的时候触发的touchStart事件
  255. dragStart(e) {
  256. T_PAGE_X = e.touches[0].pageX
  257. T_PAGE_Y = e.touches[0].pageY
  258. CUT_L = this.cutL
  259. CUT_R = this.cutR
  260. CUT_B = this.cutB
  261. CUT_T = this.cutT
  262. },
  263. // 设置大小的时候触发的touchMove事件
  264. dragMove(e) {
  265. var _this = this
  266. var dragType = e.target.dataset.drag
  267. switch (dragType) {
  268. case 'right':
  269. var dragLength = (T_PAGE_X - e.touches[0].pageX) * DRAFG_MOVE_RATIO
  270. if (CUT_R + dragLength < 0) dragLength = -CUT_R
  271. this.setData({
  272. cutR: CUT_R + dragLength
  273. })
  274. break
  275. case 'left':
  276. var dragLength = (T_PAGE_X - e.touches[0].pageX) * DRAFG_MOVE_RATIO
  277. if (CUT_L - dragLength < 0) dragLength = CUT_L
  278. if ((CUT_L - dragLength) > (this.cropperW - this.cutR)) dragLength = CUT_L - (this.cropperW - this.cutR)
  279. this.setData({
  280. cutL: CUT_L - dragLength
  281. })
  282. break
  283. case 'top':
  284. var dragLength = (T_PAGE_Y - e.touches[0].pageY) * DRAFG_MOVE_RATIO
  285. if (CUT_T - dragLength < 0) dragLength = CUT_T
  286. if ((CUT_T - dragLength) > (this.cropperH - this.cutB)) dragLength = CUT_T - (this.cropperH - this.cutB)
  287. this.setData({
  288. cutT: CUT_T - dragLength
  289. })
  290. break
  291. case 'bottom':
  292. var dragLength = (T_PAGE_Y - e.touches[0].pageY) * DRAFG_MOVE_RATIO
  293. if (CUT_B + dragLength < 0) dragLength = -CUT_B
  294. this.setData({
  295. cutB: CUT_B + dragLength
  296. })
  297. break
  298. case 'rightBottom':
  299. var dragLengthX = (T_PAGE_X - e.touches[0].pageX) * DRAFG_MOVE_RATIO
  300. var dragLengthY = (T_PAGE_Y - e.touches[0].pageY) * DRAFG_MOVE_RATIO
  301. if (CUT_B + dragLengthY < 0) dragLengthY = -CUT_B
  302. if (CUT_R + dragLengthX < 0) dragLengthX = -CUT_R
  303. let cutB = CUT_B + dragLengthY
  304. let cutR = CUT_R + dragLengthX
  305. this.setData({
  306. cutB: cutB,
  307. cutR: cutR
  308. })
  309. break
  310. default:
  311. break
  312. }
  313. }
  314. }
  315. }
  316. </script>
  317. <style scoped>
  318. .cropper-config {
  319. padding: 20rpx 40rpx;
  320. }
  321. .cropper-content {
  322. min-height: 750rpx;
  323. width: 100%;
  324. }
  325. .uni-corpper {
  326. position: relative;
  327. overflow: hidden;
  328. -webkit-user-select: none;
  329. -moz-user-select: none;
  330. -ms-user-select: none;
  331. user-select: none;
  332. -webkit-tap-highlight-color: transparent;
  333. -webkit-touch-callout: none;
  334. box-sizing: border-box;
  335. }
  336. .uni-corpper-content {
  337. position: relative;
  338. }
  339. .uni-corpper-content image {
  340. display: block;
  341. width: 100%;
  342. min-width: 0 !important;
  343. max-width: none !important;
  344. height: 100%;
  345. min-height: 0 !important;
  346. max-height: none !important;
  347. image-orientation: 0deg !important;
  348. margin: 0 auto;
  349. }
  350. /* 移动图片效果 */
  351. .uni-cropper-drag-box {
  352. position: absolute;
  353. top: 0;
  354. right: 0;
  355. bottom: 0;
  356. left: 0;
  357. cursor: move;
  358. background: rgba(0, 0, 0, 0.6);
  359. z-index: 1;
  360. }
  361. /* 内部的信息 */
  362. .uni-corpper-crop-box {
  363. position: absolute;
  364. background: rgba(255, 255, 255, 0.3);
  365. z-index: 2;
  366. }
  367. .uni-corpper-crop-box .uni-cropper-view-box {
  368. position: relative;
  369. display: block;
  370. width: 100%;
  371. height: 100%;
  372. overflow: visible;
  373. outline: 1rpx solid #69f;
  374. outline-color: rgba(102, 153, 255, .75)
  375. }
  376. /* 横向虚线 */
  377. .uni-cropper-dashed-h {
  378. position: absolute;
  379. top: 33.33333333%;
  380. left: 0;
  381. width: 100%;
  382. height: 33.33333333%;
  383. border-top: 1rpx dashed rgba(255, 255, 255, 0.5);
  384. border-bottom: 1rpx dashed rgba(255, 255, 255, 0.5);
  385. }
  386. /* 纵向虚线 */
  387. .uni-cropper-dashed-v {
  388. position: absolute;
  389. left: 33.33333333%;
  390. top: 0;
  391. width: 33.33333333%;
  392. height: 100%;
  393. border-left: 1rpx dashed rgba(255, 255, 255, 0.5);
  394. border-right: 1rpx dashed rgba(255, 255, 255, 0.5);
  395. }
  396. /* 四个方向的线 为了之后的拖动事件*/
  397. .uni-cropper-line-t {
  398. position: absolute;
  399. display: block;
  400. width: 100%;
  401. background-color: #69f;
  402. top: 0;
  403. left: 0;
  404. height: 1rpx;
  405. opacity: 0.1;
  406. cursor: n-resize;
  407. }
  408. .uni-cropper-line-t::before {
  409. content: '';
  410. position: absolute;
  411. top: 50%;
  412. right: 0rpx;
  413. width: 100%;
  414. -webkit-transform: translate3d(0, -50%, 0);
  415. transform: translate3d(0, -50%, 0);
  416. bottom: 0;
  417. height: 41rpx;
  418. background: transparent;
  419. z-index: 11;
  420. }
  421. .uni-cropper-line-r {
  422. position: absolute;
  423. display: block;
  424. background-color: #69f;
  425. top: 0;
  426. right: 0rpx;
  427. width: 1rpx;
  428. opacity: 0.1;
  429. height: 100%;
  430. cursor: e-resize;
  431. }
  432. .uni-cropper-line-r::before {
  433. content: '';
  434. position: absolute;
  435. top: 0;
  436. left: 50%;
  437. width: 41rpx;
  438. -webkit-transform: translate3d(-50%, 0, 0);
  439. transform: translate3d(-50%, 0, 0);
  440. bottom: 0;
  441. height: 100%;
  442. background: transparent;
  443. z-index: 11;
  444. }
  445. .uni-cropper-line-b {
  446. position: absolute;
  447. display: block;
  448. width: 100%;
  449. background-color: #69f;
  450. bottom: 0;
  451. left: 0;
  452. height: 1rpx;
  453. opacity: 0.1;
  454. cursor: s-resize;
  455. }
  456. .uni-cropper-line-b::before {
  457. content: '';
  458. position: absolute;
  459. top: 50%;
  460. right: 0rpx;
  461. width: 100%;
  462. -webkit-transform: translate3d(0, -50%, 0);
  463. transform: translate3d(0, -50%, 0);
  464. bottom: 0;
  465. height: 41rpx;
  466. background: transparent;
  467. z-index: 11;
  468. }
  469. .uni-cropper-line-l {
  470. position: absolute;
  471. display: block;
  472. background-color: #69f;
  473. top: 0;
  474. left: 0;
  475. width: 1rpx;
  476. opacity: 0.1;
  477. height: 100%;
  478. cursor: w-resize;
  479. }
  480. .uni-cropper-line-l::before {
  481. content: '';
  482. position: absolute;
  483. top: 0;
  484. left: 50%;
  485. width: 41rpx;
  486. -webkit-transform: translate3d(-50%, 0, 0);
  487. transform: translate3d(-50%, 0, 0);
  488. bottom: 0;
  489. height: 100%;
  490. background: transparent;
  491. z-index: 11;
  492. }
  493. .uni-cropper-point {
  494. width: 5rpx;
  495. height: 5rpx;
  496. background-color: #69f;
  497. opacity: .75;
  498. position: absolute;
  499. z-index: 3;
  500. }
  501. .point-t {
  502. top: -3rpx;
  503. left: 50%;
  504. margin-left: -3rpx;
  505. cursor: n-resize;
  506. }
  507. .point-tr {
  508. top: -3rpx;
  509. left: 100%;
  510. margin-left: -3rpx;
  511. cursor: n-resize;
  512. }
  513. .point-r {
  514. top: 50%;
  515. left: 100%;
  516. margin-left: -3rpx;
  517. margin-top: -3rpx;
  518. cursor: n-resize;
  519. }
  520. .point-rb {
  521. left: 100%;
  522. top: 100%;
  523. -webkit-transform: translate3d(-50%, -50%, 0);
  524. transform: translate3d(-50%, -50%, 0);
  525. cursor: n-resize;
  526. width: 36rpx;
  527. height: 36rpx;
  528. background-color: #69f;
  529. position: absolute;
  530. z-index: 1112;
  531. opacity: 1;
  532. }
  533. .point-b {
  534. left: 50%;
  535. top: 100%;
  536. margin-left: -3rpx;
  537. margin-top: -3rpx;
  538. cursor: n-resize;
  539. }
  540. .point-bl {
  541. left: 0%;
  542. top: 100%;
  543. margin-left: -3rpx;
  544. margin-top: -3rpx;
  545. cursor: n-resize;
  546. }
  547. .point-l {
  548. left: 0%;
  549. top: 50%;
  550. margin-left: -3rpx;
  551. margin-top: -3rpx;
  552. cursor: n-resize;
  553. }
  554. .point-lt {
  555. left: 0%;
  556. top: 0%;
  557. margin-left: -3rpx;
  558. margin-top: -3rpx;
  559. cursor: n-resize;
  560. }
  561. /* 裁剪框预览内容 */
  562. .uni-cropper-viewer {
  563. position: relative;
  564. width: 100%;
  565. height: 100%;
  566. overflow: hidden;
  567. }
  568. .uni-cropper-viewer image {
  569. position: absolute;
  570. z-index: 2;
  571. }
  572. </style>