canvas-2d.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import {compareVersion} from '../../../../util/util'
  2. Page({
  3. onShareAppMessage() {
  4. return {
  5. title: 'canvas',
  6. path: 'packageComponent/pages/canvas/canvas-2d/canvas-2d'
  7. }
  8. },
  9. data: {
  10. theme: 'light',
  11. canIUse: true,
  12. },
  13. onReady() {
  14. // 解决基础库小于 2.7.0 的兼容问题
  15. const {SDKVersion} = wx.getSystemInfoSync()
  16. console.log(SDKVersion)
  17. if (compareVersion(SDKVersion, '2.7.0') < 0) {
  18. console.log('123')
  19. this.setData({
  20. canIUse: false,
  21. })
  22. } else {
  23. // canvas2D
  24. this.position2D = {
  25. x: 150,
  26. y: 150,
  27. vx: 2,
  28. vy: 2
  29. }
  30. this.x = -100
  31. wx.createSelectorQuery()
  32. .select('#canvas2D')
  33. .fields({
  34. node: true,
  35. size: true,
  36. })
  37. .exec(this.init.bind(this))
  38. }
  39. },
  40. init(res) {
  41. const width = res[0].width
  42. const height = res[0].height
  43. const canvas = res[0].node
  44. // 不支持2d
  45. if (!canvas) {
  46. this.setData({
  47. canIUse: false,
  48. })
  49. return
  50. }
  51. const ctx = canvas.getContext('2d')
  52. const dpr = wx.getSystemInfoSync().pixelRatio
  53. canvas.width = width * dpr
  54. canvas.height = height * dpr
  55. ctx.scale(dpr, dpr)
  56. const renderLoop = () => {
  57. this.render(canvas, ctx)
  58. canvas.requestAnimationFrame(renderLoop)
  59. }
  60. canvas.requestAnimationFrame(renderLoop)
  61. const img = canvas.createImage()
  62. img.onload = () => {
  63. this._img = img
  64. }
  65. img.src = './car.png'
  66. },
  67. render(canvas, ctx) {
  68. ctx.clearRect(0, 0, 305, 305)
  69. this.drawBall2D(ctx)
  70. this.drawCar(ctx)
  71. },
  72. drawBall2D(ctx) {
  73. const p = this.position2D
  74. p.x += p.vx
  75. p.y += p.vy
  76. if (p.x >= 300) {
  77. p.vx = -2
  78. }
  79. if (p.x <= 7) {
  80. p.vx = 2
  81. }
  82. if (p.y >= 300) {
  83. p.vy = -2
  84. }
  85. if (p.y <= 7) {
  86. p.vy = 2
  87. }
  88. function ball(x, y) {
  89. ctx.beginPath()
  90. ctx.arc(x, y, 5, 0, Math.PI * 2)
  91. ctx.fillStyle = '#1aad19'
  92. ctx.strokeStyle = 'rgba(1,1,1,0)'
  93. ctx.fill()
  94. ctx.stroke()
  95. }
  96. ball(p.x, 150)
  97. ball(150, p.y)
  98. ball(300 - p.x, 150)
  99. ball(150, 300 - p.y)
  100. ball(p.x, p.y)
  101. ball(300 - p.x, 300 - p.y)
  102. ball(p.x, 300 - p.y)
  103. ball(300 - p.x, p.y)
  104. },
  105. drawCar(ctx) {
  106. if (!this._img) return
  107. if (this.x > 350) {
  108. this.x = -100
  109. }
  110. ctx.drawImage(this._img, this.x++, 150 - 25, 100, 50)
  111. ctx.restore()
  112. },
  113. onUnload() {
  114. // clearInterval(this.interval)
  115. },
  116. onLoad() {
  117. this.setData({
  118. theme: wx.getSystemInfoSync().theme || 'light'
  119. })
  120. if (wx.onThemeChange) {
  121. wx.onThemeChange(({theme}) => {
  122. this.setData({theme})
  123. })
  124. }
  125. }
  126. })