| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- cc.Class({
- extends: cc.Component,
- properties: {
- line: null,
- index: 0,
- circle: cc.Graphics,
- temp: cc.Graphics,
- oriBeta: cc.Graphics,
- oriGamma: cc.Graphics,
- oriAlpha: cc.Graphics,
- oriX: cc.Graphics,
- lineArray: [],
- tempArray: [],
- //限制条件的数组
- limitArray: [],
- bPause: false,
- /**
- * 示波器
- */
- },
- // LIFE-CYCLE CALLBACKS:
- onLoad() {
- this.line = this.getComponent(cc.Graphics);
- // this.line.strokeColor = cc.Color.GREEN;
- this.line.moveTo(0, 0);
- this.line.stroke();
- if (this.circle) {
- this.circle.moveTo(0, 0);
- this.circle.stroke();
- }
- //陀螺仪
- if (this.oriBeta) {
- this.oriBeta.moveTo(0, 0);
- this.oriBeta.stroke();
- }
- if (this.oriGamma) {
- this.oriGamma.moveTo(0, 0);
- this.oriGamma.stroke();
- }
- if (this.oriAlpha) {
- this.oriAlpha.moveTo(0, 0);
- this.oriAlpha.stroke();
- }
- if (this.oriX) {
- this.oriX.moveTo(0, 0);
- this.oriX.stroke();
- }
- },
- start() {
- // setInterval(() => {
- // this.onButton();
- // }, 200);
- },
- onButton() {
- // let random = Math.ceil(Math.random() * 100);
- // this.onUpdateDraw(random);
- this.tempArray = [];
- this.limitArray = [];
- let max = 0, min = 0, maxIndex = 0, minIndex = 0, allMax = 0, allMin = 0;
- let _count = 0;
- //滤波去掉非峰值
- for (let i = 0; i < this.lineArray.length - 1; i++) {
- if (this.lineArray[i].value > 0) {
- if (min != 0) {
- let temp = { index: minIndex, value: min };
- this.tempArray.push(temp);
- if (min < allMin) {
- allMin = min;
- }
- min = 0;
- minIndex = 0;
- }
- if (this.lineArray[i].value > this.lineArray[i + 1].value) {
- max = this.lineArray[i].value;
- maxIndex = this.lineArray[i].index;
- }
- else {
- let temp = { index: this.lineArray[i].index, value: 0 };
- this.tempArray.push(temp);
- }
- } else if (this.lineArray[i].value < 0) {
- if (max != 0) {
- let temp = { index: maxIndex, value: max };
- this.tempArray.push(temp);
- if (max > allMax) {
- allMax = max;
- }
- max = 0;
- maxIndex = 0;
- }
- if (this.lineArray[i].value < this.lineArray[i + 1].value) {
- min = this.lineArray[i].value;
- minIndex = this.lineArray[i].index;
- }
- else {
- let temp = { index: this.lineArray[i].index, value: 0 };
- this.tempArray.push(temp);
- }
- }
- //假如数据波动连续小于一个阀值,并且到达一定极限,判断是波动完成一个周期
- if (Math.abs(this.lineArray[i].value) < 5) {
- if (5 === _count) {
- //记录最大最小值
- let _limit = { index: this.lineArray[i].index, max: allMax, min: allMin };
- this.limitArray.push(_limit);
- allMax = 0;
- allMin = 0;
- }
- _count++;
- } else {
- _count = 0;
- }
- }
- if (this.temp) {
- this.temp.clear();
- this.temp.moveTo(0, 0);
- this.temp.stroke();
- }
- let _currentIndex = 0;
- // console.log("this.limitArray:", this.limitArray);
- if (this.tempArray.length == 0) return;
- for (let i = 0; i < this.tempArray.length; i++) {
- //处理判断极值
- let temp = this.tempArray[i].value;
- let _index = this.tempArray[i].index;
- // console.log("allMin:", temp, allMin, allMax);
- // for(let j = 0; j< this.limitArray.length;j++){
- // if(_index > this.limitArray[j].index){
- // _currentIndex = j;
- // }
- // }
- if (_index > this.limitArray[_currentIndex].index) {
- _currentIndex++;
- if (_currentIndex >= this.limitArray.length) {
- _currentIndex = this.limitArray.length - 1;
- }
- }
- // console.log( _index +" ~~ "+_currentIndex+"== _currentIndex:", this.limitArray[_currentIndex]);
- // if (temp > this.limitArray[_currentIndex].min && temp < this.limitArray[_currentIndex].max) {
- // this.temp.lineTo(_index, 0);
- // } else {
- // this.temp.lineTo(_index, temp * 3);
- // console.warn(_currentIndex,temp);
- // }
- this.temp.lineTo(_index, temp * 3);
- this.temp.stroke();
- }
- },
- clear() {
- if (this.circle) {
- this.circle.clear();
- }
- this.line.clear();
- this.index = 0;
- this.line.moveTo(0, 0);
- this.line.stroke();
- this.lineArray = [];
- if (this.oriBeta) {
- this.oriBeta.clear();
- this.oriBeta.moveTo(0, 0);
- this.oriBeta.stroke();
- }
- if (this.oriGamma) {
- this.oriGamma.clear();
- this.oriGamma.moveTo(0, 0);
- this.oriGamma.stroke();
- }
- if (this.oriAlpha) {
- this.oriAlpha.clear();
- this.oriAlpha.moveTo(0, 0);
- this.oriAlpha.stroke();
- }
- if (this.oriX) {
- this.oriX.clear();
- this.oriX.moveTo(0, 0);
- this.oriX.stroke();
- }
- },
- onPause() {
- this.bPause = !this.bPause;
- if (this.bPause) {
- webView.onUnBind();
- } else {
- webView.onBind();
- }
- },
- onUpdateIndex() {
- if (this.bPause) return;
- this.index += 5;
- },
- onUpdateDraw(x) {
- if (this.bPause) return;
- this.line.lineTo(this.index, x * 2);
- this.line.stroke();
- if (this.circle) {
- this.circle.fillColor = cc.Color.RED;
- this.circle.circle(this.index, x * 2, 2);
- this.circle.fill();
- this.circle.stroke();
- }
- let temp = { index: this.index, value: x };
- this.lineArray.push(temp);
- },
- onUpdateDrawFromColor(x, color, size) {
- if (this.bPause) return;
- this.line.lineTo(this.index, x * 2);
- this.line.stroke();
- if (this.circle) {
- this.circle.fillColor = color;
- this.circle.circle(this.index, x * 2, size);
- this.circle.fill();
- this.circle.stroke();
- }
- let temp = { index: this.index, value: x };
- this.lineArray.push(temp);
- },
- onUpdateDrawLastValueFromColor(x, color, size, index) {
- if (this.bPause) return;
- if (this.circle) {
- this.circle.fillColor = color;
- this.circle.circle(index * 5, x * 2, size);
- this.circle.fill();
- this.circle.stroke();
- }
- },
- onUpdateDrawOriBeta(beta) {
- this.oriBeta.lineTo(this.index, beta);
- this.oriBeta.stroke();
- },
- onUpdateDrawOriGamma(gamma) {
- if (this.bPause) return;
- this.oriGamma.lineTo(this.index, gamma);
- this.oriGamma.stroke();
- // this.oriX.lineTo(this.index, 0);
- // this.oriX.stroke();
- },
- onUpdateDrawOriAlpha(alpha) {
- this.oriAlpha.lineTo(this.index, alpha);
- this.oriAlpha.stroke();
- },
- //重新绘制
- onResetDraw() {
- if (this.circle) {
- this.circle.clear();
- }
- this.line.clear();
- this.index = 0;
- this.line.moveTo(0, 0);
- this.line.stroke();
- if (this.oriBeta) {
- this.oriBeta.clear();
- this.oriBeta.moveTo(0, 0);
- this.oriBeta.stroke();
- }
- if (this.oriGamma) {
- this.oriGamma.clear();
- this.oriGamma.moveTo(0, 0);
- this.oriGamma.stroke();
- }
- if (this.oriAlpha) {
- this.oriAlpha.clear();
- this.oriAlpha.moveTo(0, 0);
- this.oriAlpha.stroke();
- }
- if (this.oriX) {
- this.oriX.clear();
- this.oriX.moveTo(0, 0);
- this.oriX.stroke();
- }
- for (let i = 0; i < this.lineArray.length; i++) {
- this.line.lineTo(this.index, x * 5);
- this.line.stroke();
- if (this.circle) {
- this.circle.circle(this.index, x * 5, 2);
- this.circle.fillColor = cc.Color.RED;
- this.circle.fill();
- this.circle.stroke();
- }
- this.index += 5;
- }
- },
- drawCicleFromIndex(index, x, z, g) {
- // console.log(index);
- if (this.circle) {
- this.circle.clear();
- this.circle.fillColor = cc.Color.YELLOW;
- this.circle.circle(index * 5 + 2, 1, 5);
- this.circle.fill();
- this.circle.stroke();
- }
- this.line.clear();
- this.line.moveTo(index * 5 + 2, 0);
- this.line.lineTo(index * 5 + 2, -600);
- this.line.stroke();
- },
- // update (dt) {},
- });
|