crop.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <template>
  2. <view class="page">
  3. <!-- 顶部导航 -->
  4. <view class="nav-bar">
  5. <view class="status-bar"></view>
  6. <view class="nav-content">
  7. <text class="nav-back" @click="goBack">&#xe6e2;</text>
  8. <text class="nav-title">裁剪头像</text>
  9. <text class="nav-save" @click="saveImage">完成</text>
  10. </view>
  11. </view>
  12. <!-- 裁剪区域 -->
  13. <view class="crop-area"
  14. @touchstart="onTouchStart"
  15. @touchmove.prevent="onTouchMove"
  16. @touchend="onTouchEnd">
  17. <!-- 图片 - 填满显示 -->
  18. <image class="crop-image" :src="imageSrc" mode="scaleToFill"
  19. :style="{
  20. width: imgWidth + 'px',
  21. height: imgHeight + 'px',
  22. left: imgLeft + 'px',
  23. top: imgTop + 'px'
  24. }"></image>
  25. <!-- 圆形遮罩 - 用box-shadow实现圆形镂空 -->
  26. <view class="crop-circle-mask" :style="{
  27. width: cropSize + 'px',
  28. height: cropSize + 'px',
  29. top: cropTop + 'px',
  30. left: cropLeft + 'px'
  31. }"></view>
  32. </view>
  33. <!-- 底部:实时预览 + 提示 -->
  34. <view class="bottom-bar">
  35. <view class="preview-circle">
  36. <image :src="imageSrc" mode="scaleToFill" :style="previewStyle"></image>
  37. </view>
  38. <text class="tip-text">拖动裁剪框 · 双指缩放图片</text>
  39. </view>
  40. <!-- 输出canvas(隐藏) -->
  41. <canvas canvas-id="cropCanvas" class="crop-canvas"
  42. :style="{width: canvasSize + 'px', height: canvasSize + 'px'}"></canvas>
  43. <ay-toast ref="ayToast" />
  44. </view>
  45. </template>
  46. <script>
  47. import ayToast from '@/components/ay-toast/ay-toast.nvue'
  48. export default {
  49. components: {
  50. ayToast
  51. },
  52. data() {
  53. return {
  54. statusBarHeight: 44,
  55. imageSrc: '',
  56. screenWidth: 375,
  57. areaHeight: 500,
  58. // 裁剪框
  59. cropSize: 280,
  60. cropTop: 0,
  61. cropLeft: 0,
  62. // 图片显示
  63. imgWidth: 375,
  64. imgHeight: 500,
  65. imgLeft: 0,
  66. imgTop: 0,
  67. naturalWidth: 0,
  68. naturalHeight: 0,
  69. // 触摸状态
  70. touchMode: 'none',
  71. startX: 0,
  72. startY: 0,
  73. startCropLeft: 0,
  74. startCropTop: 0,
  75. startDistance: 0,
  76. startImgWidth: 0,
  77. startImgHeight: 0,
  78. startImgLeft: 0,
  79. startImgTop: 0,
  80. // 输出
  81. canvasSize: 400,
  82. previewSize: 70
  83. }
  84. },
  85. computed: {
  86. // 实时预览样式 - 将裁剪框内容映射到预览圆中
  87. previewStyle() {
  88. if (!this.imgWidth || !this.cropSize) return {}
  89. const ratio = this.previewSize / this.cropSize
  90. const w = this.imgWidth * ratio
  91. const h = this.imgHeight * ratio
  92. const x = -(this.cropLeft - this.imgLeft) * ratio
  93. const y = -(this.cropTop - this.imgTop) * ratio
  94. return {
  95. width: w + 'px',
  96. height: h + 'px',
  97. marginLeft: x + 'px',
  98. marginTop: y + 'px'
  99. }
  100. }
  101. },
  102. onLoad(options) {
  103. const sysInfo = uni.getSystemInfoSync()
  104. this.statusBarHeight = sysInfo.statusBarHeight || 44
  105. this.screenWidth = sysInfo.windowWidth
  106. const screenHeight = sysInfo.windowHeight
  107. const navHeight = this.statusBarHeight + 44
  108. const bottomHeight = 80
  109. this.areaHeight = screenHeight - navHeight - bottomHeight
  110. // 裁剪框大小为屏幕宽度的 75%
  111. this.cropSize = Math.floor(this.screenWidth * 0.75)
  112. // 裁剪框居中
  113. this.cropLeft = Math.floor((this.screenWidth - this.cropSize) / 2)
  114. this.cropTop = Math.floor((this.areaHeight - this.cropSize) / 2)
  115. if (options.src) {
  116. this.imageSrc = decodeURIComponent(options.src)
  117. this.loadImageInfo()
  118. }
  119. },
  120. methods: {
  121. goBack() {
  122. uni.navigateBack()
  123. },
  124. loadImageInfo() {
  125. uni.getImageInfo({
  126. src: this.imageSrc,
  127. success: (info) => {
  128. this.naturalWidth = info.width
  129. this.naturalHeight = info.height
  130. const ratio = info.width / info.height
  131. // 图片填满整个裁剪区域
  132. const areaRatio = this.screenWidth / this.areaHeight
  133. if (ratio > areaRatio) {
  134. // 图片更宽 - 按高度适配
  135. this.imgHeight = this.areaHeight
  136. this.imgWidth = Math.floor(this.areaHeight * ratio)
  137. } else {
  138. // 图片更高 - 按宽度适配
  139. this.imgWidth = this.screenWidth
  140. this.imgHeight = Math.floor(this.screenWidth / ratio)
  141. }
  142. // 居中
  143. this.imgLeft = Math.floor((this.screenWidth - this.imgWidth) / 2)
  144. this.imgTop = Math.floor((this.areaHeight - this.imgHeight) / 2)
  145. },
  146. fail: () => {
  147. this.imgWidth = this.screenWidth
  148. this.imgHeight = this.areaHeight
  149. this.imgLeft = 0
  150. this.imgTop = 0
  151. }
  152. })
  153. },
  154. // === 触摸事件 ===
  155. onTouchStart(e) {
  156. const touches = e.touches || e.changedTouches
  157. if (touches.length === 1) {
  158. // 单指 → 拖动裁剪框
  159. this.touchMode = 'drag'
  160. this.startX = touches[0].pageX
  161. this.startY = touches[0].pageY
  162. this.startCropLeft = this.cropLeft
  163. this.startCropTop = this.cropTop
  164. } else if (touches.length === 2) {
  165. // 双指 → 缩放图片
  166. this.touchMode = 'zoom'
  167. this.startDistance = this.getDistance(touches[0], touches[1])
  168. this.startImgWidth = this.imgWidth
  169. this.startImgHeight = this.imgHeight
  170. this.startImgLeft = this.imgLeft
  171. this.startImgTop = this.imgTop
  172. }
  173. },
  174. onTouchMove(e) {
  175. const touches = e.touches || e.changedTouches
  176. if (this.touchMode === 'drag' && touches.length === 1) {
  177. const dx = touches[0].pageX - this.startX
  178. const dy = touches[0].pageY - this.startY
  179. // 限制裁剪框在区域内
  180. let newLeft = this.startCropLeft + dx
  181. let newTop = this.startCropTop + dy
  182. newLeft = Math.max(0, Math.min(this.screenWidth - this.cropSize, newLeft))
  183. newTop = Math.max(0, Math.min(this.areaHeight - this.cropSize, newTop))
  184. this.cropLeft = newLeft
  185. this.cropTop = newTop
  186. } else if (this.touchMode === 'zoom' && touches.length === 2) {
  187. const dist = this.getDistance(touches[0], touches[1])
  188. let ratio = dist / this.startDistance
  189. ratio = Math.max(0.5, Math.min(3, ratio))
  190. const newWidth = Math.floor(this.startImgWidth * ratio)
  191. const newHeight = Math.floor(this.startImgHeight * ratio)
  192. // 保持图片中心不变
  193. const centerX = this.startImgLeft + this.startImgWidth / 2
  194. const centerY = this.startImgTop + this.startImgHeight / 2
  195. this.imgWidth = newWidth
  196. this.imgHeight = newHeight
  197. this.imgLeft = Math.floor(centerX - newWidth / 2)
  198. this.imgTop = Math.floor(centerY - newHeight / 2)
  199. }
  200. },
  201. onTouchEnd(e) {
  202. const remaining = e.touches ? e.touches.length : 0
  203. if (this.touchMode === 'zoom' && remaining === 1) {
  204. // 双指结束剩一指 → 切换为拖动
  205. this.touchMode = 'drag'
  206. this.startX = e.touches[0].pageX
  207. this.startY = e.touches[0].pageY
  208. this.startCropLeft = this.cropLeft
  209. this.startCropTop = this.cropTop
  210. } else if (remaining === 0) {
  211. this.touchMode = 'none'
  212. }
  213. },
  214. getDistance(t1, t2) {
  215. const dx = (t1.pageX || t1.screenX) - (t2.pageX || t2.screenX)
  216. const dy = (t1.pageY || t1.screenY) - (t2.pageY || t2.screenY)
  217. return Math.sqrt(dx * dx + dy * dy)
  218. },
  219. // === 保存裁剪 ===
  220. saveImage() {
  221. this.$toastRef.loading('处理中...')
  222. const ctx = uni.createCanvasContext('cropCanvas', this)
  223. const canvasSize = this.canvasSize
  224. // 裁剪框对应canvas的缩放比
  225. const outputScale = canvasSize / this.cropSize
  226. // 图片在canvas上的绘制位置
  227. const drawX = (this.imgLeft - this.cropLeft) * outputScale
  228. const drawY = (this.imgTop - this.cropTop) * outputScale
  229. const drawW = this.imgWidth * outputScale
  230. const drawH = this.imgHeight * outputScale
  231. // 圆形裁剪
  232. ctx.save()
  233. ctx.beginPath()
  234. ctx.arc(canvasSize / 2, canvasSize / 2, canvasSize / 2, 0, 2 * Math.PI)
  235. ctx.clip()
  236. ctx.drawImage(this.imageSrc, drawX, drawY, drawW, drawH)
  237. ctx.restore()
  238. ctx.draw(false, () => {
  239. setTimeout(() => {
  240. uni.canvasToTempFilePath({
  241. canvasId: 'cropCanvas',
  242. x: 0, y: 0,
  243. width: canvasSize,
  244. height: canvasSize,
  245. destWidth: canvasSize,
  246. destHeight: canvasSize,
  247. fileType: 'png',
  248. success: (res) => {
  249. this.$toastRef.hide()
  250. uni.setStorageSync('avatar', res.tempFilePath)
  251. this.$toastRef.success('头像已更新')
  252. setTimeout(() => uni.navigateBack(), 500)
  253. },
  254. fail: (err) => {
  255. console.error('canvasToTempFilePath fail:', err)
  256. this.$toastRef.hide()
  257. uni.setStorageSync('avatar', this.imageSrc)
  258. this.$toastRef.success('头像已更新')
  259. setTimeout(() => uni.navigateBack(), 500)
  260. }
  261. }, this)
  262. }, 500)
  263. })
  264. }
  265. }
  266. }
  267. </script>
  268. <style scoped>
  269. .page {
  270. width: 100%;
  271. height: 100vh;
  272. background-color: #1A1A1A;
  273. display: flex;
  274. flex-direction: column;
  275. overflow: hidden;
  276. }
  277. /* 导航栏 */
  278. .nav-bar {
  279. background-color: #1A1A1A;
  280. }
  281. .status-bar {
  282. height: var(--status-bar-height);
  283. }
  284. .nav-content {
  285. display: flex;
  286. flex-direction: row;
  287. align-items: center;
  288. height: 44px;
  289. padding: 0 15px;
  290. }
  291. .nav-back {
  292. font-family: iconfont;
  293. font-size: 20px;
  294. color: #FFFFFF;
  295. width: 40px;
  296. }
  297. .nav-title {
  298. flex: 1;
  299. text-align: center;
  300. font-size: 17px;
  301. color: #FFFFFF;
  302. font-weight: bold;
  303. }
  304. .nav-save {
  305. font-size: 15px;
  306. color: #389588;
  307. width: 40px;
  308. text-align: right;
  309. }
  310. /* 裁剪区域 */
  311. .crop-area {
  312. flex: 1;
  313. position: relative;
  314. overflow: hidden;
  315. }
  316. .crop-image {
  317. position: absolute;
  318. }
  319. /* 圆形遮罩 - 用box-shadow实现圆形镂空 */
  320. .crop-circle-mask {
  321. position: absolute;
  322. border-radius: 50%;
  323. box-sizing: border-box;
  324. border: 2px solid rgba(255, 255, 255, 0.8);
  325. box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.6);
  326. pointer-events: none;
  327. }
  328. /* 底部栏 */
  329. .bottom-bar {
  330. height: 80px;
  331. display: flex;
  332. flex-direction: row;
  333. align-items: center;
  334. padding: 0 20px;
  335. background-color: #1A1A1A;
  336. }
  337. /* 实时预览圆 */
  338. .preview-circle {
  339. width: 70px;
  340. height: 70px;
  341. border-radius: 50%;
  342. overflow: hidden;
  343. border: 2px solid rgba(255, 255, 255, 0.5);
  344. flex-shrink: 0;
  345. }
  346. .preview-circle image {
  347. display: block;
  348. }
  349. .tip-text {
  350. flex: 1;
  351. text-align: center;
  352. font-size: 13px;
  353. color: #999999;
  354. }
  355. /* canvas隐藏 */
  356. .crop-canvas {
  357. position: fixed;
  358. left: -9999px;
  359. top: -9999px;
  360. }
  361. </style>