line.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. line: null,
  5. index: 0,
  6. circle: cc.Graphics,
  7. temp: cc.Graphics,
  8. oriBeta: cc.Graphics,
  9. oriGamma: cc.Graphics,
  10. oriAlpha: cc.Graphics,
  11. oriX: cc.Graphics,
  12. lineArray: [],
  13. tempArray: [],
  14. //限制条件的数组
  15. limitArray: [],
  16. bPause: false,
  17. /**
  18. * 示波器
  19. */
  20. },
  21. // LIFE-CYCLE CALLBACKS:
  22. onLoad() {
  23. this.line = this.getComponent(cc.Graphics);
  24. // this.line.strokeColor = cc.Color.GREEN;
  25. this.line.moveTo(0, 0);
  26. this.line.stroke();
  27. if (this.circle) {
  28. this.circle.moveTo(0, 0);
  29. this.circle.stroke();
  30. }
  31. //陀螺仪
  32. if (this.oriBeta) {
  33. this.oriBeta.moveTo(0, 0);
  34. this.oriBeta.stroke();
  35. }
  36. if (this.oriGamma) {
  37. this.oriGamma.moveTo(0, 0);
  38. this.oriGamma.stroke();
  39. }
  40. if (this.oriAlpha) {
  41. this.oriAlpha.moveTo(0, 0);
  42. this.oriAlpha.stroke();
  43. }
  44. if (this.oriX) {
  45. this.oriX.moveTo(0, 0);
  46. this.oriX.stroke();
  47. }
  48. },
  49. start() {
  50. // setInterval(() => {
  51. // this.onButton();
  52. // }, 200);
  53. },
  54. onButton() {
  55. // let random = Math.ceil(Math.random() * 100);
  56. // this.onUpdateDraw(random);
  57. this.tempArray = [];
  58. this.limitArray = [];
  59. let max = 0, min = 0, maxIndex = 0, minIndex = 0, allMax = 0, allMin = 0;
  60. let _count = 0;
  61. //滤波去掉非峰值
  62. for (let i = 0; i < this.lineArray.length - 1; i++) {
  63. if (this.lineArray[i].value > 0) {
  64. if (min != 0) {
  65. let temp = { index: minIndex, value: min };
  66. this.tempArray.push(temp);
  67. if (min < allMin) {
  68. allMin = min;
  69. }
  70. min = 0;
  71. minIndex = 0;
  72. }
  73. if (this.lineArray[i].value > this.lineArray[i + 1].value) {
  74. max = this.lineArray[i].value;
  75. maxIndex = this.lineArray[i].index;
  76. }
  77. else {
  78. let temp = { index: this.lineArray[i].index, value: 0 };
  79. this.tempArray.push(temp);
  80. }
  81. } else if (this.lineArray[i].value < 0) {
  82. if (max != 0) {
  83. let temp = { index: maxIndex, value: max };
  84. this.tempArray.push(temp);
  85. if (max > allMax) {
  86. allMax = max;
  87. }
  88. max = 0;
  89. maxIndex = 0;
  90. }
  91. if (this.lineArray[i].value < this.lineArray[i + 1].value) {
  92. min = this.lineArray[i].value;
  93. minIndex = this.lineArray[i].index;
  94. }
  95. else {
  96. let temp = { index: this.lineArray[i].index, value: 0 };
  97. this.tempArray.push(temp);
  98. }
  99. }
  100. //假如数据波动连续小于一个阀值,并且到达一定极限,判断是波动完成一个周期
  101. if (Math.abs(this.lineArray[i].value) < 5) {
  102. if (5 === _count) {
  103. //记录最大最小值
  104. let _limit = { index: this.lineArray[i].index, max: allMax, min: allMin };
  105. this.limitArray.push(_limit);
  106. allMax = 0;
  107. allMin = 0;
  108. }
  109. _count++;
  110. } else {
  111. _count = 0;
  112. }
  113. }
  114. if (this.temp) {
  115. this.temp.clear();
  116. this.temp.moveTo(0, 0);
  117. this.temp.stroke();
  118. }
  119. let _currentIndex = 0;
  120. // console.log("this.limitArray:", this.limitArray);
  121. if (this.tempArray.length == 0) return;
  122. for (let i = 0; i < this.tempArray.length; i++) {
  123. //处理判断极值
  124. let temp = this.tempArray[i].value;
  125. let _index = this.tempArray[i].index;
  126. // console.log("allMin:", temp, allMin, allMax);
  127. // for(let j = 0; j< this.limitArray.length;j++){
  128. // if(_index > this.limitArray[j].index){
  129. // _currentIndex = j;
  130. // }
  131. // }
  132. if (_index > this.limitArray[_currentIndex].index) {
  133. _currentIndex++;
  134. if (_currentIndex >= this.limitArray.length) {
  135. _currentIndex = this.limitArray.length - 1;
  136. }
  137. }
  138. // console.log( _index +" ~~ "+_currentIndex+"== _currentIndex:", this.limitArray[_currentIndex]);
  139. // if (temp > this.limitArray[_currentIndex].min && temp < this.limitArray[_currentIndex].max) {
  140. // this.temp.lineTo(_index, 0);
  141. // } else {
  142. // this.temp.lineTo(_index, temp * 3);
  143. // console.warn(_currentIndex,temp);
  144. // }
  145. this.temp.lineTo(_index, temp * 3);
  146. this.temp.stroke();
  147. }
  148. },
  149. clear() {
  150. if (this.circle) {
  151. this.circle.clear();
  152. }
  153. this.line.clear();
  154. this.index = 0;
  155. this.line.moveTo(0, 0);
  156. this.line.stroke();
  157. this.lineArray = [];
  158. if (this.oriBeta) {
  159. this.oriBeta.clear();
  160. this.oriBeta.moveTo(0, 0);
  161. this.oriBeta.stroke();
  162. }
  163. if (this.oriGamma) {
  164. this.oriGamma.clear();
  165. this.oriGamma.moveTo(0, 0);
  166. this.oriGamma.stroke();
  167. }
  168. if (this.oriAlpha) {
  169. this.oriAlpha.clear();
  170. this.oriAlpha.moveTo(0, 0);
  171. this.oriAlpha.stroke();
  172. }
  173. if (this.oriX) {
  174. this.oriX.clear();
  175. this.oriX.moveTo(0, 0);
  176. this.oriX.stroke();
  177. }
  178. },
  179. onPause() {
  180. this.bPause = !this.bPause;
  181. if (this.bPause) {
  182. webView.onUnBind();
  183. } else {
  184. webView.onBind();
  185. }
  186. },
  187. onUpdateIndex() {
  188. if (this.bPause) return;
  189. this.index += 5;
  190. },
  191. onUpdateDraw(x) {
  192. if (this.bPause) return;
  193. this.line.lineTo(this.index, x * 2);
  194. this.line.stroke();
  195. if (this.circle) {
  196. this.circle.fillColor = cc.Color.RED;
  197. this.circle.circle(this.index, x * 2, 2);
  198. this.circle.fill();
  199. this.circle.stroke();
  200. }
  201. let temp = { index: this.index, value: x };
  202. this.lineArray.push(temp);
  203. },
  204. onUpdateDrawFromColor(x, color, size) {
  205. if (this.bPause) return;
  206. this.line.lineTo(this.index, x * 2);
  207. this.line.stroke();
  208. if (this.circle) {
  209. this.circle.fillColor = color;
  210. this.circle.circle(this.index, x * 2, size);
  211. this.circle.fill();
  212. this.circle.stroke();
  213. }
  214. let temp = { index: this.index, value: x };
  215. this.lineArray.push(temp);
  216. },
  217. onUpdateDrawLastValueFromColor(x, color, size, index) {
  218. if (this.bPause) return;
  219. if (this.circle) {
  220. this.circle.fillColor = color;
  221. this.circle.circle(index * 5, x * 2, size);
  222. this.circle.fill();
  223. this.circle.stroke();
  224. }
  225. },
  226. onUpdateDrawOriBeta(beta) {
  227. this.oriBeta.lineTo(this.index, beta);
  228. this.oriBeta.stroke();
  229. },
  230. onUpdateDrawOriGamma(gamma) {
  231. if (this.bPause) return;
  232. this.oriGamma.lineTo(this.index, gamma);
  233. this.oriGamma.stroke();
  234. // this.oriX.lineTo(this.index, 0);
  235. // this.oriX.stroke();
  236. },
  237. onUpdateDrawOriAlpha(alpha) {
  238. this.oriAlpha.lineTo(this.index, alpha);
  239. this.oriAlpha.stroke();
  240. },
  241. //重新绘制
  242. onResetDraw() {
  243. if (this.circle) {
  244. this.circle.clear();
  245. }
  246. this.line.clear();
  247. this.index = 0;
  248. this.line.moveTo(0, 0);
  249. this.line.stroke();
  250. if (this.oriBeta) {
  251. this.oriBeta.clear();
  252. this.oriBeta.moveTo(0, 0);
  253. this.oriBeta.stroke();
  254. }
  255. if (this.oriGamma) {
  256. this.oriGamma.clear();
  257. this.oriGamma.moveTo(0, 0);
  258. this.oriGamma.stroke();
  259. }
  260. if (this.oriAlpha) {
  261. this.oriAlpha.clear();
  262. this.oriAlpha.moveTo(0, 0);
  263. this.oriAlpha.stroke();
  264. }
  265. if (this.oriX) {
  266. this.oriX.clear();
  267. this.oriX.moveTo(0, 0);
  268. this.oriX.stroke();
  269. }
  270. for (let i = 0; i < this.lineArray.length; i++) {
  271. this.line.lineTo(this.index, x * 5);
  272. this.line.stroke();
  273. if (this.circle) {
  274. this.circle.circle(this.index, x * 5, 2);
  275. this.circle.fillColor = cc.Color.RED;
  276. this.circle.fill();
  277. this.circle.stroke();
  278. }
  279. this.index += 5;
  280. }
  281. },
  282. drawCicleFromIndex(index, x, z, g) {
  283. // console.log(index);
  284. if (this.circle) {
  285. this.circle.clear();
  286. this.circle.fillColor = cc.Color.YELLOW;
  287. this.circle.circle(index * 5 + 2, 1, 5);
  288. this.circle.fill();
  289. this.circle.stroke();
  290. }
  291. this.line.clear();
  292. this.line.moveTo(index * 5 + 2, 0);
  293. this.line.lineTo(index * 5 + 2, -600);
  294. this.line.stroke();
  295. },
  296. // update (dt) {},
  297. });