on-accelerometer-change.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. Page({
  2. onShareAppMessage() {
  3. return {
  4. title: '监听重力感应数据',
  5. path: 'packageAPI/pages/device/on-accelerometer-change/on-accelerometer-change'
  6. }
  7. },
  8. data: {
  9. theme: 'light',
  10. x: 0,
  11. y: 0,
  12. z: 0,
  13. enabled: true
  14. },
  15. onReady() {
  16. this.drawBigBall()
  17. const that = this
  18. this.position = {
  19. x: 151,
  20. y: 151,
  21. vx: 0,
  22. vy: 0,
  23. ax: 0,
  24. ay: 0
  25. }
  26. wx.onAccelerometerChange(function (res) {
  27. that.setData({
  28. x: res.x.toFixed(2),
  29. y: res.y.toFixed(2),
  30. z: res.z.toFixed(2)
  31. })
  32. that.position.ax = Math.sin((res.x * Math.PI) / 2)
  33. that.position.ay = -Math.sin((res.y * Math.PI) / 2)
  34. // that.drawSmallBall()
  35. })
  36. this.interval = setInterval(function () {
  37. that.drawSmallBall()
  38. }, 17)
  39. },
  40. drawBigBall() {
  41. const context = wx.createContext()
  42. context.beginPath(0)
  43. context.arc(151, 151, 140, 0, Math.PI * 2)
  44. context.setFillStyle('#ffffff')
  45. context.setStrokeStyle('#aaaaaa')
  46. context.fill()
  47. // context.stroke()
  48. wx.drawCanvas({
  49. canvasId: 'big-ball',
  50. actions: context.getActions()
  51. })
  52. },
  53. drawSmallBall() {
  54. const p = this.position
  55. let strokeStyle = 'rgba(1,1,1,0)'
  56. p.x += p.vx
  57. p.y += p.vy
  58. p.vx += p.ax
  59. p.vy += p.ay
  60. // eslint-disable-next-line
  61. if (Math.sqrt(Math.pow(Math.abs(p.x) - 151, 2) + Math.pow(Math.abs(p.y) - 151, 2)) >= 115) {
  62. if (p.x > 151 && p.vx > 0) {
  63. p.vx = 0
  64. }
  65. if (p.x < 151 && p.vx < 0) {
  66. p.vx = 0
  67. }
  68. if (p.y > 151 && p.vy > 0) {
  69. p.vy = 0
  70. }
  71. if (p.y < 151 && p.vy < 0) {
  72. p.vy = 0
  73. }
  74. strokeStyle = '#ff0000'
  75. }
  76. const context = wx.createContext()
  77. context.beginPath(0)
  78. context.arc(p.x, p.y, 15, 0, Math.PI * 2)
  79. context.setFillStyle('#1aad19')
  80. context.setStrokeStyle(strokeStyle)
  81. context.fill()
  82. // context.stroke()
  83. wx.drawCanvas({
  84. canvasId: 'small-ball',
  85. actions: context.getActions()
  86. })
  87. },
  88. startAccelerometer() {
  89. if (this.data.enabled) {
  90. return
  91. }
  92. const that = this
  93. wx.startAccelerometer({
  94. success() {
  95. that.setData({
  96. enabled: true
  97. })
  98. }
  99. })
  100. },
  101. stopAccelerometer() {
  102. if (!this.data.enabled) {
  103. return
  104. }
  105. const that = this
  106. wx.stopAccelerometer({
  107. success() {
  108. that.setData({
  109. enabled: false
  110. })
  111. }
  112. })
  113. },
  114. onUnload() {
  115. clearInterval(this.interval)
  116. },
  117. onLoad() {
  118. this.setData({
  119. theme: wx.getSystemInfoSync().theme || 'light'
  120. })
  121. if (wx.onThemeChange) {
  122. wx.onThemeChange(({theme}) => {
  123. this.setData({theme})
  124. })
  125. }
  126. }
  127. })