Page({ onReady: function() { this.position = { x: 150, y: 150, vx: 2, vy: 2 }, this.obj = { offset: 0, baseLine: 40, direction: 1, waveDirection: 1 }, // var offset = 5 this.drawQuadraticCurve3() this.interval = setInterval(this.drawQuadraticCurve3, 1) console.log(">>>>>>>>>" + this.obj.offset) }, /** * 画大波浪 */ drawQuadraticCurve3: function() { var obj = this.obj var startX = 20, itemWidth = 100, offset = obj.offset, baseLine = obj.baseLine, waveHeight = 10, direction = obj.direction, waveDirection = obj.waveDirection const ctx = wx.createCanvasContext('myCanvas3') function getWaveHeigh(i) { if (i % 2 == 0) { // return baseLine + waveHeight } return baseLine - waveHeight } ctx.beginPath() ctx.moveTo(-itemWidth * 6, baseLine) ctx.setFillStyle('#4BF6EE') for (var i = -6; i < 5; i++) { startX = i * itemWidth; var currentX = startX + (itemWidth / 2) + offset var currentY = getWaveHeigh(i) var currentEndX = startX + itemWidth + offset ctx.quadraticCurveTo(currentEndX - 10, currentY, currentEndX, baseLine) ctx.stroke() } //填充海水 ctx.lineTo(0, 2000) ctx.setFillStyle('#4BF6EE') ctx.fill() ctx.draw() if (obj.waveDirection == 1) { obj.offset = obj.offset + 1 } else if (obj.waveDirection == -1) { obj.offset = obj.offset - 1 } if (obj.offset == 400) { obj.offset = 0 } if (obj.offset == 50 || obj.offset == 1) { // obj.waveDirection = obj.waveDirection * -1 } // if (direction == 1) { // obj.baseLine = obj.baseLine + 1 // } else if (direction == -1) { // obj.baseLine = obj.baseLine - 1 // } if (obj.baseLine >= 50 || obj.baseLine <= 40) { console.log("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") //obj.direction = (obj.direction * -1) } }, onUnload: function() { clearInterval(this.interval) }})