快应用中经过setinterval周期函数来循环触发canvas绘制代码,在华为手机上绘制的动画会很卡顿,不流畅。java
click0() { this.speed = 0.3 let ctx = this.$element('canvas').getContext('2d') setInterval(() => { this.num0 += 2 this.noise = Math.min(0.5, 1) * this.MAX this._draw(ctx) this.MAX <= 200 && (this.MAX += 4) }, 20) }, _draw(ctx) { this.phase = (this.phase + this.speed) % (Math.PI * 64) ctx.clearRect(0, 0, this.width, this.height) this._drawLine(ctx, -2, 'rgba(0, 194, 255, 0.2)') this._drawLine(ctx, -6, 'rgba(0, 194, 255, 0.4)') this._drawLine(ctx, 4, 'rgba(0, 194, 255, 0.6)') this._drawLine(ctx, 2, 'rgba(0, 194, 255, 0.8)') this._drawLine(ctx, 1, 'rgba(0, 194, 255, 1)', 4) },
this._draw()方法中的canvas绘制操做时间长,最低须要100ms的绘制时间,而代码中周期时间只有20ms,华为快应用引擎会执行这个周期内的操做,而后才能执行下一个周期。因此设置为20ms时的效果会看起来比较慢。canvas
开发者能够先根据设备信息接口获取下设备信息中的引擎的提供商判断是不是华为的快应用引擎,若是是华为快应用引擎则设置间隔时间大于100ms,不是则能够设置小于100ms,可解决华为快应用引擎和快应用联盟引擎差别问题。代码以下(红色部分):api
onShow: function () { var that = this device.getInfo({ success: function (ret) { console.log("handling success:", JSON.stringify(ret)); that.engineProvider = ret.engineProvider; }, fail: function (erromsg, errocode) { console.log("message:", erromsg, errocode); } }) }, click0() { var that = this this.speed = 0.3 console.log(that.engineProvider) let ctx = this.$element('canvas').getContext('2d') if (that.engineProvider === "huawei") { setInterval(() => { this.num0 += 2 this.noise = Math.min(0.5, 1) * this.MAX this._draw(ctx) this.MAX <= 200 && (this.MAX += 4) }, 120) } else { setInterval(() => { this.num0 += 2 this.noise = Math.min(0.5, 1) * this.MAX this._draw(ctx) this.MAX <= 200 && (this.MAX += 4) }, 20) } }, _draw(ctx) { this.phase = (this.phase + this.speed) % (Math.PI * 64) ctx.clearRect(0, 0, this.width, this.height) this._drawLine(ctx, -2, 'rgba(0, 194, 255, 0.2)') this._drawLine(ctx, -6, 'rgba(0, 194, 255, 0.4)') this._drawLine(ctx, 4, 'rgba(0, 194, 255, 0.6)') this._drawLine(ctx, 2, 'rgba(0, 194, 255, 0.8)') this._drawLine(ctx, 1, 'rgba(0, 194, 255, 1)', 4) }, _drawLine(ctx, attenuation, color, width) { ctx.save() ctx.moveTo(0, 0); ctx.beginPath(); ctx.strokeStyle = color; ctx.lineWidth = width || 1; var x, y; for (var i = -this.K; i <= this.K; i += 0.01) { x = this.width * ((i + this.K) / (this.K * 2)) y = this.height / 2 + this.noise * this._globalAttenuationFn(i) * (1 / attenuation) * Math.sin(this.F * i - this.phase) ctx.lineTo(x, y) } ctx.stroke() ctx.restore() },
欲了解更多详情,请参阅:app
canvas接口介绍:
https://developer.huawei.com/consumer/cn/doc/development/quickApp-References/quickapp-api-canvaside
快应用开发指导文档:https://developer.huawei.com/consumer/cn/doc/development/quickApp-Guides/quickapp-whitepaper函数
原文连接:https://developer.huawei.com/consumer/cn/forum/topic/0204404988672310224?fid=18动画
原做者:Mayismui