带动画的 canvas 页面内存一直暴涨,10 分钟微信自己退出
发布于 5 年前 作者 bcheng 3717 次浏览 来自 问答

下面是改编的官方的 小程序示例 中的 画布 -》canvas 弹小球的代码:

Page({

  onLoad: function() {

    this.context = wx.createContext();

  },

  onReady: function () {

    this.position = {

      x: 150,

      y: 150,

      vx: 2,

      vy: 2

    }

    this.drawBall()

    this.interval = setInterval(this.drawBall, 17)

  },

  drawBall: function () {

    var p = this.position

    p.x += p.vx

    p.y += p.vy

    if (p.x >= 300) {

      p.vx = -2

    }

    if (p.x <= 7) {

      p.vx = 2

    }

    if (p.y >= 300) {

      p.vy = -2

    }

    if (p.y <= 7) {

      p.vy = 2

    }

    var context = this.context;

    function ball(x, y) {

      context.beginPath(0)

      context.arc(x, y, 5, 0, Math.PI * 2)

      context.setFillStyle(’#1aad19’)

      context.setStrokeStyle(‘rgba(1,1,1,0)’)

      context.fill()

      context.stroke()

    }

    ball(p.x, 150)

    ball(150, p.y)

    ball(300 - p.x, 150)

    ball(150, 300 - p.y)

    ball(p.x, p.y)

    ball(300 - p.x, 300 - p.y)

    ball(p.x, 300 - p.y)

    ball(300 - p.x, p.y)

    wx.drawCanvas({

      canvasId: ‘canvas’,

      actions: context.getActions()

    })

  },

  onUnload: function () {

    clearInterval(this.interval)

  }

})

在小米 MAX 上, android 6.0.1, 4G 内存

用小程序基础库所有版本上均做了测试,大概都是这种现象:

该页面 10 几分钟后, 在调试窗口性能数据中查看到,内存涨到近 700 M后微信直接退出!

麻烦 @胡浩 @梁天智|Albie @smoothieli @帮忙看看。

__
__

我们的程序与这个结构类似,马上要申请上线了,出现这样问题,很是无奈啊。。。

回到顶部