index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. var sceneReadyBehavior = require('../../behavior-scene/scene-ready');
  2. // 当前触摸点位置
  3. let currentTouchX = 0;
  4. let currentTouchY = 0;
  5. // 触摸点序号
  6. let rotateIdx = -1;
  7. let moveIdx = -1;
  8. //摇杆所处中心
  9. let centerX = 50;
  10. let centerY = 50;
  11. //摇杆宽高
  12. let hWidth = 40;
  13. let hHeight = 40;
  14. //摇杆盘半径
  15. let radius = 50;
  16. Page({
  17. behaviors: [sceneReadyBehavior],
  18. data: {
  19. height: 600,
  20. heightScale: 0.8,
  21. //重置初始位置的标记
  22. reset: 0,
  23. //相对背景偏移,命名 h=handle 摇杆
  24. hTop: 30,
  25. hLeft: 30,
  26. transferData:{
  27. //相对摇杆中心的偏移
  28. biasX: 0,
  29. biasY: 0,
  30. //开始旋转时的基准点
  31. initRotX: 0,
  32. initRotY: 0,
  33. //旋转时的触摸点位置
  34. biasRotX: 0,
  35. biasRotY: 0,
  36. }
  37. },
  38. reset: function () {
  39. this.setData({
  40. reset: this.data.reset + 1
  41. })
  42. },
  43. touchStart(e) {},
  44. touchMove: function (e) {
  45. //无旋转触摸点触屏时,保证移动触摸点为第一触摸点
  46. if (rotateIdx == -1) {
  47. moveIdx = 0;
  48. } else {
  49. //当两点触屏时,保证移动触摸点与旋转触摸点对应的点序列互斥
  50. moveIdx = ~rotateIdx & 0x01;
  51. }
  52. var touchX = e.touches[moveIdx].clientX - 30;
  53. var touchY = e.touches[moveIdx].clientY - (this.data.height - 100);
  54. var posInfo = this.limitPosition(touchX, touchY);
  55. this.setData({
  56. hLeft: centerX - hWidth / 2 + posInfo.posX,
  57. hTop: centerY - hHeight / 2 + posInfo.posY,
  58. transferData:{
  59. biasX: posInfo.posX,
  60. biasY: posInfo.posY,
  61. }
  62. })
  63. },
  64. touchEnd: function (e) {
  65. moveIdx = -1;
  66. //当位移触摸点松开时,保证旋转触摸点取的为第一接触点
  67. if (rotateIdx == 1)
  68. rotateIdx = 0;
  69. this.setData({
  70. hLeft: centerX - hWidth / 2,
  71. hTop: centerY - hHeight / 2,
  72. transferData:{
  73. biasX: 0,
  74. biasY: 0,
  75. }
  76. });
  77. },
  78. touchRotateStart: function (e) {
  79. if (moveIdx == -1) {
  80. rotateIdx = 0;
  81. } else {
  82. rotateIdx = ~moveIdx & 0x01;
  83. }
  84. currentTouchX = e.touches[rotateIdx].clientX;
  85. currentTouchY = e.touches[rotateIdx].clientY;
  86. this.setData({
  87. transferData:{
  88. initRotX: currentTouchX,
  89. initRotY: currentTouchY,
  90. }
  91. })
  92. },
  93. touchRotateMove: function (e) {
  94. if (moveIdx == -1) {
  95. rotateIdx = 0;
  96. } else {
  97. rotateIdx = ~moveIdx & 0x01;
  98. }
  99. currentTouchX = e.touches[rotateIdx].clientX;
  100. currentTouchY = e.touches[rotateIdx].clientY;
  101. this.setData({
  102. transferData:{
  103. biasRotX: currentTouchX,
  104. biasRotY: currentTouchY,
  105. }
  106. })
  107. },
  108. touchRotateEnd: function (e) {
  109. rotateIdx = -1;
  110. //当旋转触摸点松开时,保证位移触摸点取的为第一接触点
  111. if (moveIdx == 1)
  112. moveIdx = 0;
  113. this.setData({
  114. transferData:{
  115. biasRotX: 0,
  116. biasRotY: 0,
  117. }
  118. })
  119. },
  120. // 将位移强度限制在摇杆盘的范围中
  121. limitPosition: function (touchX, touchY) {
  122. var x = touchX - centerX;
  123. var y = touchY - centerY;
  124. var z = Math.sqrt(x * x + y * y);
  125. // 位移未超出摇杆盘范围时
  126. if (z <= radius) {
  127. x = Math.round(x);
  128. y = Math.round(y);
  129. return {
  130. posX: x,
  131. posY: y
  132. };
  133. } else {
  134. // 位移超出摇杆盘范围,需要对应限制位移强度
  135. var ratio = radius / z;
  136. x = x * ratio;
  137. y = y * ratio;
  138. x = Math.round(x);
  139. y = Math.round(y);
  140. return {
  141. posX: x,
  142. posY: y
  143. };
  144. }
  145. }
  146. });