imageeditor.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <view v-if="!combine">
  3. <view @touchend="touchEnd" @touchmove="touchMove" @touchstart="touchStart" class="cHcontainer" id="container">
  4. <image class="bg" :src="bgPic"></image>
  5. <icon class="cancel" id="cancel" :style="'top:' + (cancelCenterY - 10 + 'px') + ';left:' + (cancelCenterX - 10 + 'px')" type="cancel"></icon>
  6. <icon class="handle" color="green" id="handle" :style="'top:' + (handleCenterY - 10 + 'px') + ';left:' + (handleCenterX - 10 + 'px')" type="waiting"></icon>
  7. <image
  8. class="hat"
  9. id="hat"
  10. :src="currentHatSrc"
  11. :style="
  12. 'top:' +
  13. (hatCenterY - hatSize / 2 - 2 + 'px') +
  14. ';left:' +
  15. (hatCenterX - hatSize / 2 - 2 + 'px') +
  16. ';transform:rotate(' +
  17. (rotate + 'deg') +
  18. ') scale(' +
  19. scale +
  20. ')'
  21. "
  22. ></image>
  23. </view>
  24. <view class="btnContainer">
  25. <button @tap="combinePic" class="cbtn">确定</button>
  26. </view>
  27. <scroll-view class="scrollView" :scrollX="true">
  28. <image
  29. v-for="(src, index) in imgList"
  30. :key="index"
  31. @tap="chooseImg(index)"
  32. class="imgList"
  33. :src="src"
  34. ></image>
  35. </scroll-view>
  36. </view>
  37. </template>
  38. <script>
  39. import hat1 from '../../../images/christmasHat/1.png';
  40. import hat2 from '../../../images/christmasHat/2.png';
  41. import hat3 from '../../../images/christmasHat/3.png';
  42. import hat4 from '../../../images/christmasHat/4.png';
  43. import hat5 from '../../../images/christmasHat/5.png';
  44. import hat6 from '../../../images/christmasHat/6.png';
  45. import hat7 from '../../../images/christmasHat/7.png';
  46. import hat8 from '../../../images/christmasHat/8.png';
  47. import hat9 from '../../../images/christmasHat/9.png';
  48. import hat10 from '../../../images/christmasHat/10.png';
  49. var app = getApp();
  50. export default {
  51. data() {
  52. return {
  53. bgPic: null,
  54. imgList: [hat1, hat2, hat3, hat4, hat5, hat6, hat7, hat8, hat9, hat10],
  55. currentHatId: 1,
  56. currentHatSrc: hat1,
  57. hatCenterX: uni.getSystemInfoSync().windowWidth / 2,
  58. hatCenterY: 150,
  59. cancelCenterX: uni.getSystemInfoSync().windowWidth / 2 - 50 - 2,
  60. cancelCenterY: 100,
  61. handleCenterX: uni.getSystemInfoSync().windowWidth / 2 + 50 - 2,
  62. handleCenterY: 200,
  63. hatSize: 100,
  64. scale: 1,
  65. rotate: 0,
  66. combine: ''
  67. };
  68. },
  69. onLoad: function () {
  70. this.bgPic=app.globalData.bgPic;
  71. },
  72. onReady: function () {
  73. this.hat_center_x = this.hatCenterX;
  74. this.hat_center_y = this.hatCenterY;
  75. this.cancel_center_x = this.cancelCenterX;
  76. this.cancel_center_y = this.cancelCenterY;
  77. this.handle_center_x = this.handleCenterX;
  78. this.handle_center_y = this.handleCenterY;
  79. this.scale = this.scale;
  80. this.rotate = this.rotate;
  81. this.touch_target = '';
  82. this.start_x = 0;
  83. this.start_y = 0;
  84. },
  85. methods: {
  86. touchStart: function (t) {
  87. if ('hat' == t.target.id) {
  88. this.touch_target = 'hat';
  89. } else {
  90. if ('handle' == t.target.id) {
  91. this.touch_target = 'handle';
  92. } else {
  93. this.touch_target = '';
  94. }
  95. }
  96. if ('' != this.touch_target) {
  97. this.start_x = t.touches[0].clientX;
  98. this.start_y = t.touches[0].clientY;
  99. }
  100. },
  101. touchEnd: function (t) {
  102. this.hat_center_x = this.hatCenterX;
  103. this.hat_center_y = this.hatCenterY;
  104. this.cancel_center_x = this.cancelCenterX;
  105. this.cancel_center_y = this.cancelCenterY;
  106. this.handle_center_x = this.handleCenterX;
  107. this.handle_center_y = this.handleCenterY;
  108. this.touch_target = '';
  109. this.scale = this.scale;
  110. this.rotate = this.rotate;
  111. },
  112. touchMove: function (t) {
  113. var e = t.touches[0].clientX;
  114. var a = t.touches[0].clientY;
  115. var h = e - this.start_x;
  116. var n = a - this.start_y;
  117. if ('hat' == this.touch_target) {
  118. this.hatCenterX=this.hatCenterX + h;
  119. this.hatCenterY=this.hatCenterY + n;
  120. this.cancelCenterX=this.cancelCenterX + h;
  121. this.cancelCenterY=this.cancelCenterY + n;
  122. this.handleCenterX=this.handleCenterX + h;
  123. this.handleCenterY=this.handleCenterY + n;
  124. }
  125. if ('handle' == this.touch_target) {
  126. this.handleCenterX=this.handleCenterX + h;
  127. this.handleCenterY=this.handleCenterY + n;
  128. this.cancelCenterX=2 * this.hatCenterX - this.handleCenterX;
  129. this.cancelCenterY=2 * this.hatCenterY - this.handleCenterY;
  130. var s = this.handle_center_x - this.hat_center_x;
  131. var i = this.handle_center_y - this.hat_center_y;
  132. var r = this.handleCenterX - this.hat_center_x;
  133. var c = this.handleCenterY - this.hat_center_y;
  134. var d = Math.sqrt(s * s + i * i);
  135. var l = Math.sqrt(r * r + c * c);
  136. var _ = (Math.atan2(i, s) / Math.PI) * 180;
  137. var o = (Math.atan2(c, r) / Math.PI) * 180;
  138. this.scale=(l / d) * this.scale;
  139. this.rotate=o - _ + this.rotate;
  140. }
  141. this.start_x = e;
  142. this.start_y = a;
  143. },
  144. chooseImg: function (index) {
  145. this.currentHatId = index + 1;
  146. this.currentHatSrc = this.imgList[index];
  147. },
  148. combinePic: function () {
  149. app.globalData.scale = this.scale;
  150. app.globalData.rotate = this.rotate;
  151. app.globalData.hat_center_x = this.hat_center_x;
  152. app.globalData.hat_center_y = this.hat_center_y;
  153. app.globalData.currentHatId = this.currentHatId;
  154. app.globalData.currentHatSrc = this.currentHatSrc;
  155. uni.navigateTo({
  156. url: '../combine/combine'
  157. });
  158. }
  159. }
  160. };
  161. </script>
  162. <style>
  163. @import './imageeditor.css';
  164. </style>