prompt-box.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <template>
  2. <view>
  3. <canvas style="width: 375px; height: 270px;" canvas-id="promptCanvas"></canvas>
  4. </view>
  5. </template>
  6. <script>
  7. import utilData from "../../util/util-js/util-data.js"
  8. export default {
  9. props: {
  10. calorie: {
  11. type: Number,
  12. default: 0
  13. },
  14. },
  15. data() {
  16. return {
  17. tipCanvasInterval: null,
  18. context: null,
  19. bShow: false,
  20. promptInterval:null,
  21. }
  22. },
  23. watch: {
  24. calorie(val) {
  25. // this.onPlay();
  26. }
  27. },
  28. created() {
  29. var context = uni.createCanvasContext('promptCanvas')
  30. this.context = context;
  31. setTimeout(()=>{
  32. //循环一个提示
  33. this.promptInterval = setInterval(() => {
  34. if (this.bShow) {
  35. this.hideDraw();
  36. } else {
  37. this.Draw();
  38. }
  39. this.bShow = !this.bShow;
  40. }, 60000)
  41. },500)
  42. },
  43. beforeDestroy() {
  44. clearInterval(this.promptInterval);
  45. this.promptInterval = null;
  46. },
  47. methods: {
  48. hideDraw() {
  49. // console.log("hide draw");
  50. clearInterval(this.tipCanvasInterval);
  51. this.tipCanvasInterval = null;
  52. this.context.beginPath();
  53. this.context.clearRect(0, 0, 1000, 1000);
  54. this.context.draw();
  55. },
  56. Draw() {
  57. // console.log("Draw");
  58. var context = this.context;
  59. let pos = {
  60. x: 200,
  61. y: 90
  62. };
  63. let tempCount = 0;
  64. let index = 0;
  65. this.tipCanvasInterval = setInterval(() => {
  66. if (30 <= tempCount) {
  67. if (index == 0) {
  68. index++;
  69. tempCount = 0;
  70. } else if (index == 1) {
  71. //最后绘制背景板和文字
  72. index++;
  73. tempCount = 0;
  74. } else {
  75. clearInterval(this.tipCanvasInterval);
  76. this.tipCanvasInterval = null;
  77. }
  78. return;
  79. }
  80. tempCount++;
  81. context.setStrokeStyle("#FFFFFF")
  82. context.setLineWidth(0.33)
  83. //线段最后一个点位置
  84. let endPos = {
  85. x: pos.x,
  86. y: pos.y
  87. };
  88. //起始点
  89. context.beginPath();
  90. context.setStrokeStyle("rgba(255,255,255,0)")
  91. context.setLineWidth(0.33)
  92. context.moveTo(endPos.x + 5, endPos.y)
  93. context.arc(endPos.x, endPos.y, 5, 0, 2 * Math.PI, true)
  94. context.fillStyle = "rgba(255,255,255,0.3)"
  95. context.fill()
  96. context.stroke()
  97. context.beginPath();
  98. context.setStrokeStyle("#FFFFFF")
  99. context.moveTo(endPos.x + 2, endPos.y)
  100. context.arc(endPos.x, endPos.y, 2, 0, 2 * Math.PI, true)
  101. context.fillStyle = "#FFFFFF"
  102. context.fill()
  103. context.stroke()
  104. if (index == 0) {
  105. endPos.x = pos.x + tempCount;
  106. endPos.y = pos.y - tempCount * 2;
  107. context.beginPath();
  108. context.moveTo(pos.x, pos.y)
  109. context.lineTo(endPos.x, pos.y - tempCount * 2)
  110. context.stroke()
  111. } else if (index == 1) {
  112. let x = pos.x + 30;
  113. let y = pos.y - 60;
  114. endPos.x = x + tempCount;
  115. endPos.y = y;
  116. context.beginPath();
  117. context.moveTo(pos.x, pos.y)
  118. context.lineTo(x, y)
  119. context.stroke()
  120. context.beginPath();
  121. context.moveTo(x, y)
  122. context.lineTo(x + tempCount, y)
  123. context.stroke()
  124. } else if (index == 2) {
  125. let x = pos.x + 30;
  126. let y = pos.y - 60;
  127. //白点的位置
  128. endPos.x = x + 30;
  129. endPos.y = y;
  130. context.beginPath();
  131. context.moveTo(pos.x, pos.y)
  132. context.lineTo(x, y)
  133. context.stroke()
  134. context.beginPath();
  135. context.moveTo(x, y)
  136. context.lineTo(x + 30, y)
  137. context.stroke()
  138. //偏移量调整
  139. let yPos = 0;
  140. let xPos = 15;
  141. context.setStrokeStyle("#FFFFFF")
  142. context.setLineWidth(1)
  143. context.beginPath();
  144. context.moveTo(endPos.x + xPos, endPos.y - 10 + yPos)
  145. context.lineTo(endPos.x + 7 + xPos, endPos.y - 20 + yPos)
  146. context.lineTo(endPos.x + 85 + xPos, endPos.y - 20 + yPos)
  147. context.lineTo(endPos.x + 85 + xPos, endPos.y + yPos)
  148. context.lineTo(endPos.x + 73 + xPos, endPos.y + 10 + yPos)
  149. context.lineTo(endPos.x + xPos, endPos.y + 10 + yPos)
  150. context.lineTo(endPos.x + xPos, endPos.y - 10 + yPos)
  151. // context.fillStyle = "#FFFFFF"
  152. // context.fill()
  153. context.stroke()
  154. context.fillStyle = "#FFFFFF"
  155. context.font = "12px Arial";
  156. let kg = utilData.calorieConversionKg(this.calorie);
  157. context.fillText("约 "+kg.toFixed(2)+" 公斤", endPos.x + 10 + xPos, endPos.y + yPos);
  158. }
  159. //绘制点移动
  160. context.beginPath();
  161. context.setStrokeStyle("rgba(255,255,255,0)")
  162. context.setLineWidth(0.33)
  163. context.moveTo(endPos.x + 5, endPos.y)
  164. context.arc(endPos.x, endPos.y, 5, 0, 2 * Math.PI, true)
  165. context.fillStyle = "rgba(255,255,255,0.3)"
  166. context.fill()
  167. context.stroke()
  168. context.beginPath();
  169. context.setStrokeStyle("#FFFFFF")
  170. context.moveTo(endPos.x + 2, endPos.y)
  171. context.arc(endPos.x, endPos.y, 2, 0, 2 * Math.PI, true)
  172. context.fillStyle = "#FFFFFF"
  173. context.fill()
  174. context.stroke()
  175. context.draw()
  176. }, 10)
  177. }
  178. }
  179. }
  180. </script>
  181. <style>
  182. </style>