下面的那些代码放在onload里面
离屏canvas上drawImage的问题新手一枚,在学习canvas时碰到以下疑问,根据开发文档自己写demo的时候,在离屏canvas上画了一个红色的矩形,然后在上屏canvas上画一个黄色的矩形,然后把离屏canvas绘制到上屏canvas上,代码如下 [代码]var[代码] [代码]screenCanvas = wx.createCanvas()[代码][代码]var[代码] [代码]offScreenCanvas = wx.createCanvas()[代码][代码]var[代码] [代码]offContext = offScreenCanvas.getContext([代码][代码]'2d'[代码][代码])[代码][代码]offContext.fillStyle = [代码][代码]'red'[代码][代码]offContext.fillRect(0, 0, 100, 100)[代码][代码]var[代码] [代码]screenContext = screenCanvas.getContext([代码][代码]'2d'[代码][代码])[代码][代码] [代码][代码]screenContext.fillStyle = [代码][代码]'yellow'[代码][代码] [代码][代码]screenContext.fillRect(50,50,100,100)[代码][代码]screenContext.drawImage(offScreenCanvas, 0, 0)[代码]保存以后,模拟器确实得到了预期的结果 [图片] 然后我想把矩形换成图片,代码修改如下: [代码]var[代码] [代码]screenCanvas = wx.createCanvas()[代码][代码]var[代码] [代码]offScreenCanvas = wx.createCanvas()[代码][代码]var[代码] [代码]offContext = offScreenCanvas.getContext([代码][代码]'2d'[代码][代码])[代码][代码]var[代码] [代码]offImg = wx.createImage()[代码][代码]offImg.onload = [代码][代码]function[代码][代码](){[代码][代码] [代码][代码]offContext.drawImage(offImg,0, 0)[代码][代码]}[代码][代码]offImg.src = [代码][代码]'images/enemy.png'[代码][代码]var[代码] [代码]screenContext = screenCanvas.getContext([代码][代码]'2d'[代码][代码])[代码][代码] [代码][代码]var[代码] [代码]screenImg = wx.createImage()[代码][代码] [代码][代码]screenImg.onload = [代码][代码]function[代码][代码](){[代码][代码] [代码][代码]screenContext.drawImage(screenImg,50, 50)[代码][代码] [代码][代码]}[代码][代码] [代码][代码]screenImg.src = [代码][代码]'images/hero.png'[代码][代码]screenContext.drawImage(offScreenCanvas, 0, 0)[代码]可是结果只画了上屏的图片,即hero.png,而没有画出离屏的图片,即enemy.png,请教下是我哪里写得不对吗? [图片]
2019-06-15