fruitMachine.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <template>
  2. <view >
  3. <view class="fruit-container">
  4. <view class="fruitBg-container">
  5. <view class="fruitBg"></view>
  6. <view class="fruitBg"></view>
  7. <view class="fruitBg"></view>
  8. </view>
  9. <view class="fruit" :class="runing1?'fruitPlay':'fruitPaused'" :style="{backgroundImage:`url(${imageSrc})`,'--i':fruitIndex[0]}"></view>
  10. <view class="fruit" :class="runing2?'fruitPlay':'fruitPaused'" :style="{backgroundImage:`url(${imageSrc})`,'--i':fruitIndex[1]}"></view>
  11. <view class="fruit" :class="runing3?'fruitPlay':'fruitPaused'" :style="{backgroundImage:`url(${imageSrc})`,'--i':fruitIndex[2]}"></view>
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. import imageSrc from "@/static/fruitMachine/fruitMachine.png"
  17. import fruit from "./fruit.js"
  18. export default {
  19. props: {
  20. calorie: {
  21. type: Number,
  22. default: 10
  23. },
  24. },
  25. data() {
  26. return {
  27. runing1: false,
  28. runing2: false,
  29. runing3: false,
  30. imageSrc: imageSrc,
  31. fruitIndex: [0, 0, 0],
  32. // 水果卡路里数组,对应图片
  33. // 问号,花生,虾饺,煎蛋,火腿,冰淇淋,中薯,炸鸡,可乐,奶油,炒饭,汉堡,东陂肉
  34. // fruitArray: [{
  35. // name: '问号',
  36. // value: 0
  37. // }, {
  38. // name: '花生',
  39. // value: 10
  40. // }, {
  41. // name: '虾饺',
  42. // value: 50
  43. // }, {
  44. // name: '煎蛋',
  45. // value: 100
  46. // }, {
  47. // name: '火腿',
  48. // value: 150
  49. // }, {
  50. // name: '冰淇淋',
  51. // value: 200
  52. // }, {
  53. // name: '中薯',
  54. // value: 250
  55. // }, {
  56. // name: '炸鸡',
  57. // value: 300
  58. // }, {
  59. // name: '可乐',
  60. // value: 350
  61. // }, {
  62. // name: '奶油',
  63. // value: 400
  64. // }, {
  65. // name: '炒饭',
  66. // value: 450
  67. // }, {
  68. // name: '汉堡',
  69. // value: 500
  70. // }, {
  71. // name: '东陂肉',
  72. // value: 550
  73. // }],
  74. // currentCalorie: this.calorie
  75. //击打状态音效
  76. fruitRuningUrl: "/static/elect/hit-ten.mp3",
  77. fruitAudioContext:null,
  78. bCanPlay:false,
  79. };
  80. },
  81. watch: {
  82. calorie(newVal,oldVal) {
  83. // console.log("fruitMachine",newVal,oldVal);
  84. // let _temp = Number(newVal).sub(oldVal);
  85. // if(Number(_temp)>=10)
  86. // {
  87. this.onPlayAudio();
  88. // }
  89. this.onPlay();
  90. }
  91. },
  92. created() {
  93. this.onPlay();
  94. // console.log("=======",imageSrc);
  95. this.fruitAudioContext = uni.createInnerAudioContext();
  96. this.fruitAudioContext.autoplay = false;
  97. this.fruitAudioContext.src = this.fruitRuningUrl;
  98. this.fruitAudioContext.volume = 0.5;
  99. },
  100. methods: {
  101. onPersonalHide(){
  102. },
  103. onPersonalShow(){
  104. },
  105. onCanPlay(){
  106. this.bCanPlay = true;
  107. },
  108. onPlayAudio(){
  109. if(this.fruitAudioContext && this.bCanPlay){
  110. this.bCanPlay = false;
  111. this.fruitAudioContext.stop();
  112. this.fruitAudioContext.play();
  113. }
  114. },
  115. onUpdateCalorie() {
  116. this.fruitIndex = [0, 0, 0];
  117. this.fruitIndex=fruit.getFruitIndex(this.calorie);
  118. // console.log("this.fruitIndex:",this.fruitIndex);
  119. },
  120. onPlay() {
  121. if (!this.runing1 && !this.runing2 && !this.runing3) {
  122. if(this.calorie < 10 )
  123. return;
  124. // this.onPlayAudio();
  125. this.runing1 = true
  126. setTimeout(() => {
  127. this.runing2 = true;
  128. }, 500)
  129. setTimeout(() => {
  130. this.runing3 = true;
  131. }, 1000)
  132. setTimeout(() => {
  133. this.onUpdateCalorie();
  134. this.onPaused();
  135. }, 2000)
  136. }
  137. },
  138. onPaused() {
  139. if (this.runing1 && this.runing2 && this.runing3) {
  140. this.runing1 = false
  141. setTimeout(() => {
  142. this.runing2 = false;
  143. }, 1000)
  144. setTimeout(() => {
  145. this.runing3 = false;
  146. }, 2000)
  147. }
  148. }
  149. }
  150. }
  151. </script>
  152. <style lang="scss">
  153. .fruit-container {
  154. width: 400px;
  155. height: 100px;
  156. margin-top: 60px;
  157. // border: 1rpx solid #000000;
  158. position: relative;
  159. overflow: hidden;
  160. display: flex;
  161. transform: scale(0.3);
  162. justify-content: center;
  163. flex-direction: row;
  164. }
  165. .fruitBg-container {
  166. position: absolute;
  167. left: 0;
  168. top: 0;
  169. width: 100%;
  170. height: 100%;
  171. display: flex;
  172. justify-content: center;
  173. flex-direction: row;
  174. }
  175. .fruitBg {
  176. width: 100px;
  177. height: 100px;
  178. position: relative;
  179. // background-color: #007AFF;
  180. background-image: linear-gradient(to bottom, rgba(0, 0, 0, 0), rgba($color: #FFFFFF, $alpha: 0.3), rgba(0, 0, 0, 0));
  181. margin: 5px;
  182. }
  183. .fruit {
  184. background-repeat: no-repeat;
  185. width: 100px;
  186. height: 100px;
  187. position: relative;
  188. margin: 5px;
  189. // animation: fruit-run 10s infinite;
  190. // background-color: #007AFF;
  191. }
  192. .fruitPlay {
  193. animation-name: fruit-run;
  194. animation-duration: 1s;
  195. animation-iteration-count: infinite;
  196. animation-timing-function: linear;
  197. animation-play-state: running;
  198. }
  199. .fruitPaused {
  200. background-position: 0 calc(-100px * var(--i));
  201. transition: background-position 2s;
  202. }
  203. @keyframes fruit-run {
  204. 0% {
  205. background-position: 0 0;
  206. }
  207. @for $i from 1 through 11 {
  208. #{$i*9.09}% {
  209. background-position: 0 -100px*$i;
  210. }
  211. }
  212. 100% {
  213. background-position: -0px -1200px;
  214. }
  215. }
  216. </style>