avatarCropper.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <template>
  2. <view class="content">
  3. <view class="cropper-wrapper" :style="{ height: cropperOpt.height + 'px' }">
  4. <canvas class="cropper" :disable-scroll="true" @touchstart="touchStart" @touchmove="touchMove"
  5. @touchend="touchEnd"
  6. :style="{ width: cropperOpt.width, height: cropperOpt.height, backgroundColor: '#999' }"
  7. canvas-id="cropper" id="cropper"></canvas>
  8. <canvas class="cropper" :disable-scroll="true" :style="{
  9. border:'2px solid #000',
  10. position: 'fixed',
  11. top: `-${cropperOpt.width * cropperOpt.pixelRatio}px`,
  12. left: `-${cropperOpt.height * cropperOpt.pixelRatio}px`,
  13. width: `${cropperOpt.width * cropperOpt.pixelRatio}px`,
  14. height: `${cropperOpt.height * cropperOpt.pixelRatio}`
  15. }" canvas-id="targetId" id="targetId"></canvas>
  16. </view>
  17. <view class="cropper-buttons safe-area-padding" :style="{ height: bottomNavHeight + 'px' }">
  18. <!-- #ifdef H5 -->
  19. <view class="upload" @tap="uploadTap">选择图片</view>
  20. <!-- #endif -->
  21. <!-- #ifndef H5 -->
  22. <view class="upload" @tap="uploadTap">重新选择</view>
  23. <!-- #endif -->
  24. <view class="getCropperImage" @tap="getCropperImage(false)">确定</view>
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. import WeCropper from './weCropper.js';
  30. export default {
  31. props: {
  32. // 裁剪矩形框的样式,其中可包含的属性为lineWidth-边框宽度(单位rpx),color: 边框颜色,
  33. // mask-遮罩颜色,一般设置为一个rgba的透明度,如"rgba(0, 0, 0, 0.35)"
  34. boundStyle: {
  35. type: Object,
  36. default () {
  37. return {
  38. lineWidth: 4,
  39. borderColor: 'rgb(245, 245, 245)',
  40. mask: 'rgba(0, 0, 0, 0.35)'
  41. };
  42. }
  43. }
  44. // // 裁剪框宽度,单位rpx
  45. // rectWidth: {
  46. // type: [String, Number],
  47. // default: 400
  48. // },
  49. // // 裁剪框高度,单位rpx
  50. // rectHeight: {
  51. // type: [String, Number],
  52. // default: 400
  53. // },
  54. // // 输出图片宽度,单位rpx
  55. // destWidth: {
  56. // type: [String, Number],
  57. // default: 400
  58. // },
  59. // // 输出图片高度,单位rpx
  60. // destHeight: {
  61. // type: [String, Number],
  62. // default: 400
  63. // },
  64. // // 输出的图片类型,如果发现裁剪的图片很大,可能是因为设置为了"png",改成"jpg"即可
  65. // fileType: {
  66. // type: String,
  67. // default: 'jpg',
  68. // },
  69. // // 生成的图片质量
  70. // // H5上无效,目前不考虑使用此参数
  71. // quality: {
  72. // type: [Number, String],
  73. // default: 1
  74. // }
  75. },
  76. data() {
  77. return {
  78. // 底部导航的高度
  79. bottomNavHeight: 50,
  80. originWidth: 200,
  81. width: 0,
  82. height: 200,
  83. cropperOpt: {
  84. id: 'cropper',
  85. targetId: 'targetCropper',
  86. pixelRatio: 1,
  87. width: 0,
  88. height: 0,
  89. scale: 2.5,
  90. zoom: 8,
  91. cut: {
  92. x: (this.width - this.originWidth) / 2,
  93. y: (this.height - this.originWidth) / 2,
  94. width: this.originWidth,
  95. height: this.originWidth
  96. },
  97. boundStyle: {
  98. lineWidth: uni.upx2px(this.boundStyle.lineWidth),
  99. mask: this.boundStyle.mask,
  100. color: this.boundStyle.borderColor
  101. }
  102. },
  103. // 裁剪框和输出图片的尺寸,高度默认等于宽度
  104. // 输出图片宽度,单位px
  105. destWidth: 300,
  106. // 裁剪框宽度,单位px
  107. rectWidth: 300,
  108. // 输出的图片类型,如果'png'类型发现裁剪的图片太大,改成"jpg"即可
  109. fileType: 'jpg',
  110. src: '', // 选择的图片路径,用于在点击确定时,判断是否选择了图片
  111. };
  112. },
  113. onLoad(option) {
  114. let rectInfo = uni.getSystemInfoSync();
  115. this.width = rectInfo.windowWidth;
  116. this.height = rectInfo.windowHeight - this.bottomNavHeight;
  117. this.cropperOpt.width = this.width;
  118. this.cropperOpt.height = rectInfo.screenHeight;
  119. this.cropperOpt.pixelRatio = rectInfo.pixelRatio;
  120. let rectWidth = Number(280);
  121. this.cropperOpt.cut = {
  122. x: (this.width - rectWidth) / 2,
  123. y: (this.height - rectWidth) / 2,
  124. width: rectWidth,
  125. height: rectWidth
  126. };
  127. // 初始化
  128. this.cropper = new WeCropper(this.cropperOpt)
  129. .on('ready', ctx => {
  130. // wecropper is ready for work!
  131. })
  132. .on('beforeImageLoad', ctx => {
  133. // before picture loaded, i can do something
  134. })
  135. .on('imageLoad', ctx => {
  136. // picture loaded
  137. })
  138. .on('beforeDraw', (ctx, instance) => {
  139. // before canvas draw,i can do something
  140. });
  141. // 设置导航栏样式,以免用户在page.json中没有设置为黑色背景
  142. uni.setNavigationBarColor({
  143. frontColor: '#ffffff',
  144. backgroundColor: '#000000'
  145. });
  146. this.src = option.src; //已选图片
  147. this.cropper.pushOrign(this.src); //图片赋值给裁剪框
  148. },
  149. methods: {
  150. touchStart(e) {
  151. this.cropper.touchStart(e);
  152. },
  153. touchMove(e) {
  154. this.cropper.touchMove(e);
  155. },
  156. touchEnd(e) {
  157. this.cropper.touchEnd(e);
  158. },
  159. getCropperImage(isPre = false) {
  160. if (!this.src) return this.$u.toast('请先选择图片再裁剪');
  161. let cropper_opt = {
  162. destHeight: Number(this.destWidth), // uni.canvasToTempFilePath要求这些参数为数值
  163. destWidth: Number(this.destWidth),
  164. fileType: this.fileType
  165. };
  166. this.cropper.getCropperImage(cropper_opt, (path, err) => {
  167. if (err) {
  168. uni.showModal({
  169. title: '温馨提示',
  170. content: err.message
  171. });
  172. } else {
  173. if (isPre) {
  174. uni.previewImage({
  175. current: '', // 当前显示图片的 http 链接
  176. urls: [path] // 需要预览的图片 http 链接列表
  177. });
  178. } else {
  179. console.log(path);
  180. uni.$emit('uAvatarCropper', path);
  181. this.$u.route({
  182. type: 'back'
  183. });
  184. }
  185. }
  186. });
  187. },
  188. uploadTap() {
  189. const self = this;
  190. uni.chooseImage({
  191. count: 1, // 默认9
  192. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  193. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  194. success: (res) => {
  195. self.src = res.tempFilePaths[0];
  196. // 获取裁剪图片资源后,给data添加src属性及其值
  197. self.cropper.pushOrign(this.src);
  198. }
  199. });
  200. }
  201. }
  202. };
  203. </script>
  204. <style scoped lang="scss">
  205. @mixin vue-flex($direction: row) {
  206. /* #ifndef APP-NVUE */
  207. display: flex;
  208. flex-direction: $direction;
  209. /* #endif */
  210. }
  211. .content {
  212. background: rgba(255, 255, 255, 1);
  213. height: 100vh;
  214. }
  215. .cropper {
  216. position: absolute;
  217. top: 0;
  218. left: 0;
  219. width: 100%;
  220. height: 100vh;
  221. z-index: 11;
  222. }
  223. #targetId {
  224. border: 2px solid #fff !important;
  225. }
  226. .cropper-wrapper {
  227. position: relative;
  228. @include vue-flex;
  229. flex-direction: row;
  230. justify-content: space-between;
  231. align-items: center;
  232. width: 100%;
  233. height: 100vh;
  234. background-color: #000;
  235. }
  236. .cropper-buttons {
  237. padding: 0 46rpx;
  238. box-sizing: border-box;
  239. color: #eee;
  240. width: 100vw;
  241. height: auto;
  242. @include vue-flex;
  243. flex-direction: row;
  244. justify-content: space-between;
  245. align-items: center;
  246. position: fixed;
  247. bottom: 200rpx;
  248. left: 0;
  249. font-size: 28rpx;
  250. z-index: 999;
  251. view {
  252. width: 48%;
  253. text-align: center;
  254. padding: 18rpx 65rpx;
  255. box-sizing: border-box;
  256. background-color: #fff;
  257. border-radius: 4rpx;
  258. font-size: 30rpx;
  259. }
  260. view:first-child {
  261. color: #333;
  262. }
  263. view:last-child {
  264. background: linear-gradient(132deg, #2DD9FF 0%, #2D6DFF 100%);
  265. border-radius: 4rpx 4rpx 4rpx 4rpx;
  266. }
  267. }
  268. </style>