洛阳学员端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

284 lines
7.0 KiB

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