关于android:华为快应用在setInterval中绘制canvas动画卡顿怎么破

37次阅读

共计 2414 个字符,预计需要花费 7 分钟才能阅读完成。

景象形容:

快利用中通过 setinterval 周期函数来循环触发 canvas 绘制代码,在华为手机上绘制的动画会很卡顿,不晦涩。

问题代码如下:

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 时的成果会看起来比较慢。

解决办法:

开发者能够先依据设施信息接口获取下设施信息中的引擎的提供商判断是否是华为的快利用引擎,如果是华为快利用引擎则设置间隔时间大于 100ms,不是则能够设置小于 100ms,可解决华为快利用引擎和快利用联盟引擎差别问题。代码如下(红色局部):

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()},

欲了解更多详情,请参阅:

canvas 接口介绍:
https://developer.huawei.com/consumer/cn/doc/development/quickApp-References/quickapp-api-canvas

快利用开发领导文档:https://developer.huawei.com/consumer/cn/doc/development/quickApp-Guides/quickapp-whitepaper


原文链接:https://developer.huawei.com/consumer/cn/forum/topic/0204404988672310224?fid=18

原作者:Mayism

正文完
 0