u-avatar-cropper.vue 7.1 KB

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