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.

223 lines
4.8 KiB

3 years ago
  1. <template>
  2. <view class="content">
  3. <view class="title-view" :style="{height: navigationBarHeight + 'px'}">
  4. <navigator open-type="navigateBack" class="back-btn mix-icon icon-xiangzuo"></navigator>
  5. <text class="title">裁剪</text>
  6. <text class="empty"></text>
  7. </view>
  8. <view class="cropper-wrapper">
  9. <canvas
  10. class="cropper"
  11. disable-scroll="true"
  12. @touchstart="touchStart"
  13. @touchmove="touchMove"
  14. @touchend="touchEnd"
  15. :style="{ width: cropperOpt.width, height: cropperOpt.height }"
  16. canvas-id="cropper"
  17. ></canvas>
  18. </view>
  19. <view class="cropper-buttons">
  20. <view class="btn upload" @tap="uploadTap">重选</view>
  21. <view class="btn getCropperImage" @tap="getCropperImage">确定</view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. import weCropper from './cut.js';
  27. const device = uni.getSystemInfoSync();
  28. const width = device.windowWidth;
  29. const height = device.windowHeight;
  30. export default {
  31. data() {
  32. return {
  33. cropperOpt: {
  34. id: 'cropper',
  35. width: width,
  36. height: height,
  37. scale: 2.5,
  38. zoom: 8,
  39. cut: {
  40. x: (width - 200) / 2,
  41. y: (height - this.systemInfo.navigationBarHeight - this.systemInfo.statusBarHeight - 200) / 2,
  42. width: 200,
  43. height: 200
  44. }
  45. },
  46. weCropper: ''
  47. };
  48. },
  49. computed: {
  50. navigationBarHeight(){
  51. console.log(this.systemInfo.navigationBarHeight);
  52. return this.systemInfo.navigationBarHeight;
  53. }
  54. },
  55. onLoad(option) {
  56. // do something
  57. const cropperOpt = this.cropperOpt;
  58. const src = option.src;
  59. console.log(src);
  60. if (src) {
  61. Object.assign(cropperOpt, {
  62. src
  63. });
  64. this.weCropper = new weCropper(cropperOpt)
  65. .on('ready', function(ctx) {})
  66. .on('beforeImageLoad', ctx => {
  67. /* uni.showToast({
  68. title: '上传中',
  69. icon: 'loading',
  70. duration: 3000
  71. }); */
  72. })
  73. .on('imageLoad', ctx => {
  74. uni.hideToast();
  75. });
  76. }
  77. },
  78. methods: {
  79. touchStart(e) {
  80. this.weCropper.touchStart(e);
  81. },
  82. touchMove(e) {
  83. this.weCropper.touchMove(e);
  84. },
  85. touchEnd(e) {
  86. this.weCropper.touchEnd(e);
  87. },
  88. convertBase64UrlToBlob(dataURI, type) {
  89. var binary = atob(dataURI.split(',')[1]);
  90. var array = [];
  91. for (var i = 0; i < binary.length; i++) {
  92. array.push(binary.charCodeAt(i));
  93. }
  94. return new Blob([new Uint8Array(array)], {
  95. type: type
  96. }, {
  97. filename: '1111.jpg'
  98. });
  99. },
  100. blobToDataURL(blob) {
  101. var a = new FileReader();
  102. a.readAsDataURL(blob); //读取文件保存在result中
  103. a.onload = function(e) {
  104. var getRes = e.target.result; //读取的结果在result中
  105. console.log(getRes);
  106. };
  107. },
  108. getCropperImage() {
  109. let _this = this;
  110. this.weCropper.getCropperImage(avatar => {
  111. if (avatar) {
  112. this.$util.prePage().setAvatar(avatar);
  113. uni.navigateBack();
  114. } else {
  115. console.log('获取图片失败,请稍后重试');
  116. }
  117. });
  118. },
  119. uploadTap() {
  120. const self = this;
  121. uni.chooseImage({
  122. count: 1, // 默认9
  123. sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
  124. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  125. success(res) {
  126. let src = res.tempFilePaths[0];
  127. // 获取裁剪图片资源后,给data添加src属性及其值
  128. self.weCropper.pushOrign(src);
  129. }
  130. });
  131. }
  132. },
  133. };
  134. </script>
  135. <style lang="scss">
  136. page, .content{
  137. width: 100%;
  138. height: 100%;
  139. overflow: hidden;
  140. }
  141. .content {
  142. display: flex;
  143. flex-direction: column;
  144. background-color: #000;
  145. padding-top: var(--status-bar-height);
  146. }
  147. .title-view{
  148. flex-shrink: 0;
  149. display: flex;
  150. align-items: center;
  151. justify-content: space-between;
  152. width: 100%;
  153. background: transparent;
  154. .back-btn{
  155. display: flex;
  156. justify-content: center;
  157. align-items: center;
  158. width: 42px;
  159. height: 40px;
  160. font-size: 18px;
  161. color: #fff;
  162. }
  163. .title{
  164. font-size: 17px;
  165. color: #fff;
  166. }
  167. .empty{
  168. width: 42px;
  169. }
  170. }
  171. .cropper {
  172. width: 100%;
  173. flex: 1;
  174. }
  175. .cropper-wrapper {
  176. flex: 1;
  177. position: relative;
  178. display: flex;
  179. flex-direction: column;
  180. justify-content: space-between;
  181. align-items: center;
  182. width: 100%;
  183. background-color: #000;
  184. }
  185. .cropper-buttons {
  186. flex-shrink: 0;
  187. display: flex;
  188. justify-content: space-between;
  189. align-items: center;
  190. width: 100%;
  191. height: 50px;
  192. background-color: rgba(0, 0, 0, 0.4);
  193. .btn{
  194. width: 100px;
  195. height: 50px;
  196. line-height: 50px;
  197. font-size: 15px;
  198. color: #fff;
  199. &.upload{
  200. padding-left: 20px;
  201. }
  202. &.getCropperImage{
  203. padding-right: 20px;
  204. text-align: right;
  205. }
  206. }
  207. }
  208. </style>