收藏
回答

canvas在小游戏开放域中调用drawImage会报错

框架类型 问题类型 API/组件名称 终端类型 操作系统 微信版本 基础库版本
小游戏 Bug wx.getSharedCanvas 微信iOS客户端 Android 6.6.7 2.1.1

let sharedCanvas = wx.getSharedCanvas();

let sharedContext = sharedCanvas.getContext('2d')


let canvas = wx.createCanvas();

canvas.width = sharedCanvas.width;

canvas.height = sharedCanvas.height;


wx.onMessage(data => {

 sharedContext.drawImage(canvas, 0, 0);

});


以上代码会报错,同样会报错的还有wx.createImage返回的对象一样会报错,这个刚才看到论坛里有一样的问题,到现在仍然没解决。

如果不在wx.onMessage理调用,比如直接在代码段里调用是没问题的,只要是在wx.xxxx的回调里执行都会报错,希望尽快解决这个问题。

------------------------------------------------------------------------------------------------------------------------------------------

gameSubContextThirdScriptError
Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLImageElement or SVGImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap or OffscreenCanvas)';at onMessage callback function
TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLImageElement or SVGImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap or OffscreenCanvas)'
    at CanvasRenderingContext2D.t.(anonymous function) (http://127.0.0.1:57565/game/__dev__/WAGame.js:12:17613)
    at CanvasRenderingContext2D.e.(anonymous function) [as drawImage] (http://127.0.0.1:57565/game/__dev__/WAGame.js:12:18953)
    at Function.<anonymous> (http://127.0.0.1:57565/game/js/open/index.js:11:17)
    at f.<anonymous> (http://127.0.0.1:57565/game/__subdev__/WAGameSubContext.js:4:26185)
    at f.emit (http://127.0.0.1:57565/game/__subdev__/WAGameSubContext.js:4:453)
    at http://127.0.0.1:57565/game/__subdev__/WAGameSubContext.js:4:8953
    at Object.p [as subscribeHandler] (http://127.0.0.1:57565/game/__subdev__/WAGameSubContext.js:3:13572)
    at http://127.0.0.1:57565/game/__dev__/WAGame.js:3:16001
    at Array.forEach (<anonymous>)
    at Object.S [as publish] (http://127.0.0.1:57565/game/__dev__/WAGame.js:3:15965)



回答关注问题邀请回答
收藏

1 个回答

  • 晨
    2018-06-27

    代码片段的链接好像有问题,麻烦再提供一下

    2018-06-27
    有用
    回复 6
    • 夕伽
      夕伽
      2018-06-27

      什么链接?截图吗?截图内容就是图片上面那一段报错代码

      2018-06-27
      回复
    • 晨
      2018-06-27回复夕伽

      代码片段的链接

      2018-06-27
      回复
    • 夕伽
      夕伽
      2018-06-27回复

      代码片段我只是写了其中一个接口,整个有关联的就是我贴的那段代码。由于帖子好像没法编辑,点击编辑直接变成空白了,所以我也不知道怎么给你链接。。如果要看具体的就是下面这段代码,只要放入到微信小游戏开放域的index.js中,然后主域投递任何消息就能重新这个bug了。

      let sharedCanvas = wx.getSharedCanvas();
       
      let sharedContext = sharedCanvas.getContext('2d')
       
       
      let canvas = wx.createCanvas();
       
      canvas.width = sharedCanvas.width;
       
      canvas.height = sharedCanvas.height;
       
       
      wx.onMessage(data => {
       
       sharedContext.drawImage(canvas, 0, 0);
       
      });


      2018-06-27
      回复
    • 夕伽
      夕伽
      2018-06-27回复

      或者你的意思是要那个报错的示例项目?

      2018-06-27
      回复
    • 晨
      2018-06-27回复夕伽

      麻烦给个相关的代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html),我们这边可以直接运行。要跟的问题比较多,没时间一个个的去写case复现,还麻烦配合一下

      2018-06-27
      回复
    查看更多(1)
登录 后发表内容