ParabolaBall.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <template>
  2. <view>
  3. <view v-for="(item, index) in dots" :key="index" :style="{
  4. position: 'fixed',
  5. borderRadius: '50%',
  6. left: item.left + 'px',
  7. top: item.top + 'px',
  8. display: item.show ? '' : 'none',
  9. width: size + 'px',
  10. height: size + 'px',
  11. background: color,
  12. zIndex: zIndex
  13. }">
  14. <image :src="item.src" mode="aspectFill" style="width: 100%;height: 100%;border-radius:inherit"
  15. v-if="item.src"></image>
  16. </view>
  17. </view>
  18. </template>
  19. <script>
  20. export default {
  21. props: {
  22. size: { // 尺寸:单位px
  23. type: Number,
  24. default: 20
  25. },
  26. color: {
  27. type: String,
  28. default: '#f5222d'
  29. },
  30. zIndex: {
  31. type: Number,
  32. default: 999
  33. },
  34. duration: {
  35. type: Number,
  36. default: 500
  37. }
  38. },
  39. data() {
  40. return {
  41. dots: [
  42. /* { src: '', left: 0, top: 0, show: false } */
  43. ]
  44. };
  45. },
  46. methods: {
  47. showBall({
  48. start,
  49. end,
  50. src
  51. }) {
  52. return new Promise(resolve => {
  53. let ball = this.dots.find(v => !v.show)
  54. if (!ball) {
  55. ball = {
  56. src: '',
  57. left: 0,
  58. top: 0,
  59. show: false
  60. }
  61. this.dots.push(ball)
  62. }
  63. let t = this.duration,
  64. starX = start.x - this.size / 2,
  65. starY = start.y - this.size / 2,
  66. endX = 50 - this.size / 2,
  67. endY = 640 - this.size / 2,
  68. starT = Date.now()
  69. let Sx = endX - starX,
  70. Sy = endY - starY,
  71. Ax = -(2 * Sx / (t * t)) / 5, // 加速度
  72. Ay = Math.abs(Ax),
  73. Vox = Sx / t - (Ax * t) / 2, // 初速度
  74. Voy = Sy / t - (Ay * t) / 2
  75. const run = () => {
  76. const To = Date.now() - starT
  77. const x = starX + (Vox * To + Ax * To * To / 2),
  78. y = starY + (Voy * To + Ay * To * To / 2)
  79. ball.left = x
  80. ball.top = y
  81. if (To < t) {
  82. setTimeout(run)
  83. } else {
  84. ball.show = false
  85. resolve()
  86. }
  87. }
  88. ball.src = src
  89. ball.show = true
  90. run()
  91. })
  92. }
  93. }
  94. }
  95. </script>
  96. <style>
  97. </style>