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) {}, });