canvas.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="page-body">
  5. <view class="page-body-wrapper">
  6. <!-- #ifdef APP-PLUS || H5 -->
  7. <canvas canvas-id="canvas" class="canvas" :start="startStatus" :change:start="animate.start"
  8. :data-width="canvasWidth" :data-height="canvasWidth"></canvas>
  9. <!-- #endif -->
  10. <!-- #ifndef APP-PLUS || H5 -->
  11. <canvas canvas-id="canvas" id="canvas" class="canvas"></canvas>
  12. <!-- #endif -->
  13. </view>
  14. </view>
  15. </view>
  16. </template>
  17. <script module="animate" lang="renderjs">
  18. function Ball({
  19. x,
  20. y,
  21. vx,
  22. vy,
  23. canvasWidth,
  24. canvasHeight,
  25. ctx
  26. }) {
  27. this.canvasWidth = canvasWidth
  28. this.canvasHeight = canvasHeight
  29. this.ctx = ctx
  30. this.x = x
  31. this.y = y
  32. this.vx = vx
  33. this.vy = vy
  34. this.radius = 5
  35. }
  36. Ball.prototype.draw = function() {
  37. this.ctx.beginPath()
  38. this.ctx.fillStyle = '#007AFF'
  39. this.ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI)
  40. this.ctx.closePath()
  41. this.ctx.fill()
  42. }
  43. Ball.prototype.move = function() {
  44. this.x += this.vx
  45. this.y += this.vy
  46. // 回到中心
  47. // if (getDistance(this.x - this.canvasWidth / 2, this.y - this.canvasHeight / 2) >
  48. // getDistance(this.canvasWidth / 2, this.canvasHeight / 2) + this.radius) {
  49. // this.x = this.canvasWidth / 2
  50. // this.y = this.canvasHeight / 2
  51. // }
  52. // 边框反弹
  53. if (this.x < this.radius) {
  54. this.vx = Math.abs(this.vx)
  55. return
  56. }
  57. if (this.x > this.canvasWidth - this.radius) {
  58. this.vx = -Math.abs(this.vx)
  59. }
  60. if (this.y < this.radius) {
  61. this.vy = Math.abs(this.vy)
  62. return
  63. }
  64. if (this.y > this.canvasWidth - this.radius) {
  65. this.vy = -Math.abs(this.vy)
  66. }
  67. }
  68. function getDistance(x, y) {
  69. return Math.pow(Math.pow(x, 2) + Math.pow(y, 2), 0.5)
  70. }
  71. export default {
  72. methods: {
  73. start(newVal, oldVal, owner, ins) {
  74. let canvasWidth = ins.getDataset().width,
  75. canvasHeight = ins.getDataset().height,
  76. canvasEle = document.querySelectorAll('.canvas>canvas')[0],
  77. ctx = canvasEle.getContext('2d'),
  78. speed = 3,
  79. ballList = [],
  80. layer = 3,
  81. ballInlayer = 20
  82. for (let i = 0; i < layer; i++) {
  83. let radius = getDistance(canvasWidth / 2, canvasHeight / 2) / layer * i
  84. for (let j = 0; j < ballInlayer; j++) {
  85. let deg = j * 2 * Math.PI / ballInlayer,
  86. sin = Math.sin(deg),
  87. cos = Math.cos(deg),
  88. x = radius * cos + canvasWidth / 2,
  89. y = radius * sin + canvasHeight / 2,
  90. vx = speed * cos,
  91. vy = speed * sin
  92. ballList.push(new Ball({
  93. x,
  94. y,
  95. vx,
  96. vy,
  97. canvasWidth,
  98. canvasHeight,
  99. ctx,
  100. radius: 5
  101. }))
  102. }
  103. }
  104. function animate(ballList) {
  105. ctx.clearRect(0, 0, canvasEle.width, canvasEle.height)
  106. ballList.forEach(function(item) {
  107. item.move()
  108. item.draw()
  109. })
  110. requestAnimationFrame(function() {
  111. animate(ballList)
  112. })
  113. }
  114. animate(ballList)
  115. }
  116. }
  117. }
  118. </script>
  119. <script>
  120. // #ifndef APP-PLUS || H5
  121. let ctx = null,
  122. interval = null;
  123. function Ball(x, y, vx, vy, canvasWidth, canvasHeight, ctx) {
  124. this.canvasWidth = canvasWidth
  125. this.canvasHeight = canvasHeight
  126. this.ctx = ctx
  127. this.x = x
  128. this.y = y
  129. this.vx = vx
  130. this.vy = vy
  131. this.radius = 5
  132. }
  133. Ball.prototype.draw = function() {
  134. this.ctx.setFillStyle('#007AFF')
  135. this.ctx.beginPath()
  136. this.ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI)
  137. this.ctx.closePath()
  138. this.ctx.fill()
  139. }
  140. Ball.prototype.move = function() {
  141. this.x += this.vx
  142. this.y += this.vy
  143. // 回到中心
  144. // if (getDistance(this.x - this.canvasWidth / 2, this.y - this.canvasHeight / 2) >
  145. // getDistance(this.canvasWidth / 2, this.canvasHeight / 2) + this.radius) {
  146. // this.x = this.canvasWidth / 2
  147. // this.y = this.canvasHeight / 2
  148. // }
  149. // 边框反弹
  150. if (this.x < this.radius) {
  151. this.vx = Math.abs(this.vx)
  152. return
  153. }
  154. if (this.x > this.canvasWidth - this.radius) {
  155. this.vx = -Math.abs(this.vx)
  156. }
  157. if (this.y < this.radius) {
  158. this.vy = Math.abs(this.vy)
  159. return
  160. }
  161. if (this.y > this.canvasWidth - this.radius) {
  162. this.vy = -Math.abs(this.vy)
  163. }
  164. }
  165. function getDistance(x, y) {
  166. return Math.pow(Math.pow(x, 2) + Math.pow(y, 2), 0.5)
  167. }
  168. // #endif
  169. export default {
  170. data() {
  171. return {
  172. title: 'canvas',
  173. canvasWidth: 0,
  174. startStatus: false,
  175. ballList: []
  176. }
  177. },
  178. onReady: function() {
  179. this.$nextTick(() => {
  180. uni.createSelectorQuery().select(".canvas").boundingClientRect(data => {
  181. this.canvasWidth = data.width
  182. // #ifdef APP-PLUS || H5
  183. this.startStatus = true
  184. // #endif
  185. // #ifndef APP-PLUS || H5
  186. ctx = uni.createCanvasContext('canvas')
  187. this.drawBall()
  188. // #endif
  189. }).exec()
  190. })
  191. },
  192. // #ifndef APP-PLUS || H5
  193. onUnload: function() {
  194. clearInterval(interval);
  195. },
  196. methods: {
  197. drawBall: function() {
  198. let canvasWidth = this.canvasWidth,
  199. canvasHeight = this.canvasWidth,
  200. speed = 3,
  201. ballList = [],
  202. layer = 3,
  203. ballInlayer = 20
  204. for (let i = 0; i < layer; i++) {
  205. let radius = getDistance(canvasWidth / 2, canvasHeight / 2) / layer * i
  206. for (let j = 0; j < ballInlayer; j++) {
  207. let deg = j * 2 * Math.PI / ballInlayer,
  208. sin = Math.sin(deg),
  209. cos = Math.cos(deg),
  210. x = radius * cos + canvasWidth / 2,
  211. y = radius * sin + canvasHeight / 2,
  212. vx = speed * cos,
  213. vy = speed * sin
  214. ballList.push(new Ball(x, y, vx, vy, canvasWidth, canvasHeight, ctx))
  215. }
  216. }
  217. function animate(ballList) {
  218. // ctx.clearRect(0, 0, canvasWidth, canvasHeight)
  219. ballList.forEach(function(item) {
  220. item.move()
  221. item.draw()
  222. })
  223. ctx.draw()
  224. }
  225. interval = setInterval(function() {
  226. animate(ballList)
  227. }, 17)
  228. }
  229. }
  230. // #endif
  231. }
  232. </script>
  233. <style>
  234. .page-body-wrapper {
  235. text-align: center;
  236. }
  237. .canvas {
  238. width: 610rpx;
  239. height: 610rpx;
  240. margin: auto;
  241. background-color: #fff;
  242. }
  243. </style>