u-avatar-cropper.vue 7.7 KB

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