当前位置:   article > 正文

头像裁剪【微信小程序、uni-app】_uni-cropper

uni-cropper

 一、子组件,通过插件更改的头像裁剪 头像上传裁切/剪切组件 - DCloud 插件市场

  1. <template>
  2. <view class="container">
  3. <view class="page-body uni-content-info">
  4. <view class='cropper-content'>
  5. <view v-if="isShowImg" class="uni-corpper" :style="'width:'+cropperInitW+'px;height:'+cropperInitH+'px;'">
  6. <view class="cropper-modal" :style="'width:'+cropperInitW+'px;height:'+cropperInitH+'px;'"></view>
  7. <view class="uni-corpper-content" :style="'width:'+cropperW+'px;height:'+cropperH+'px;left:'+cropperL+'px;top:'+cropperT+'px'">
  8. <image :src="imageSrc" :style="'width:'+cropperW+'px;height:'+cropperH+'px'"></image>
  9. <view class="cropper-modal-box" :style="'left:'+cutL+'px;top:'+cutT+'px;right:'+cutR+'px;bottom:'+cutB+'px'">
  10. <img :src="imageSrc" :style="'width:'+cropperW+'px;height:'+cropperH+'px;transform:translate3d('+cutL*-1+'px, '+cutT*-1+'px, 0px) '" />
  11. </view>
  12. <view class="uni-corpper-crop-box" @touchstart.stop="contentStartMove" @touchmove.stop="contentMoveing" @touchend.stop="contentTouchEnd"
  13. :style="'left:'+cutL+'px;top:'+cutT+'px;right:'+cutR+'px;bottom:'+cutB+'px'">
  14. <view class="uni-cropper-view-box">
  15. <view class="uni-cropper-dashed-h"></view>
  16. <view class="uni-cropper-dashed-v"></view>
  17. <view class="uni-cropper-line-t" data-drag="top" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
  18. <view class="uni-cropper-line-r" data-drag="right" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
  19. <view class="uni-cropper-line-b" data-drag="bottom" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
  20. <view class="uni-cropper-line-l" data-drag="left" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
  21. <view class="uni-cropper-point point-t" data-drag="top" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
  22. <view class="uni-cropper-point point-tr" data-drag="topTight"></view>
  23. <view class="uni-cropper-point point-r" data-drag="right" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
  24. <view class="uni-cropper-point point-rb" data-drag="rightBottom" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
  25. <view class="uni-cropper-point point-b" data-drag="bottom" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
  26. <view class="uni-cropper-point point-bl" data-drag="bottomLeft"></view>
  27. <view class="uni-cropper-point point-l" data-drag="left" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
  28. <view class="uni-cropper-point point-lt" data-drag="leftTop"></view>
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. </view>
  34. <view class='cropper-config'>
  35. <!-- :style="'background-color: '+selectButtonBackgroundColor+';color: '+selectButtonColor+';'" -->
  36. <view class="cropper-choose" @click="getImage">{
  37. { selectButtonText }}</view>
  38. <!-- :style="'background-color: '+saveButtonBackgroundColor+';color: '+saveButtonColor+';'" -->
  39. <view class="cropper-confirm" @click="getImageInfo">{
  40. { isShowImg ? '上传' : '取消' }}</view>
  41. </view>
  42. <canvas canvas-id="myCanvas" v-if="originalW>0 && originalH>0" :style="'position:absolute;border: 1px solid red; width:'+(original?originalW:imageW)+'px;height:'+(original?originalH:imageH)+'px;top:-9999px;left:-9999px;'"></canvas>
  43. </view>
  44. </view>
  45. </template>
  46. <script>
  47. let sysInfo = uni.getSystemInfoSync();
  48. let SCREEN_WIDTH = sysInfo.screenWidth
  49. let PAGE_X, // 手按下的x位置
  50. PAGE_Y, // 手按下y的位置
  51. PR = sysInfo.pixelRatio, // dpi
  52. T_PAGE_X, // 手移动的时候x的位置
  53. T_PAGE_Y, // 手移动的时候Y的位置
  54. CUT_L, // 初始化拖拽元素的left值
  55. CUT_T, // 初始化拖拽元素的top值
  56. CUT_R, // 初始化拖拽元素的
  57. CUT_B, // 初始化拖拽元素的
  58. CUT_W, // 初始化拖拽元素的宽度
  59. CUT_H, // 初始化拖拽元素的高度
  60. IMG_RATIO, // 图片比例
  61. IMG_REAL_W, // 图片实际的宽度
  62. IMG_REAL_H, // 图片实际的高度
  63. DRAFG_MOVE_RATIO = 1, //移动时候的比例,
  64. INIT_DRAG_POSITION = 200, // 初始化屏幕宽度和裁剪区域的宽度之差,用于设置初始化裁剪的宽度
  65. ORIGINAL_W, //原始图片宽度
  66. ORIGINAL_H, //原始图片高度
  67. DRAW_IMAGE_W = sysInfo.screenWidth // 设置生成的图片宽度
  68. export default {
  69. name:'OkingtzCropper',
  70. props:{
  71. //是否显示头像裁剪
  72. is_avatar:{
  73. type:Boolean,
  74. default:false
  75. },
  76. original:{
  77. // 是否按照原始大小返回
  78. type:Boolean,
  79. default:true
  80. },
  81. fixed:{
  82. // 优先级比较
  83. type:Boolean,
  84. default:true
  85. },
  86. fixedNumber:{
  87. type:Array,
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/287097
推荐阅读
相关标签