收藏
回答

TypeError: ctx.draw is not a function 是什么原因?

  wx.createSelectorQuery()

            .select('#myCanvas'// 在 WXML 中填入的 id

            .fields({

                node: true,

                size: true

            })

            .exec((res) => {

                // Canvas 对象

                const canvas = res[0].node

                // 渲染上下文

                const ctx = canvas.getContext('2d')


                // Canvas 画布的实际绘制宽高

                const width = res[0].width

                const height = res[0].height


                // 初始化画布大小

                const dpr = wx.getWindowInfo().pixelRatio

                canvas.width = width * dpr

                canvas.height = height * dpr

                ctx.scale(dpr, dpr)



                ctx.moveTo(10, 50)

                ctx.lineTo(100, 50)

                ctx.stroke()

                ctx.draw()


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

1 个回答

  • optimistic
    optimistic
    2023-09-14

    确定canvas对象是这么拿的吗?ctx 这个对象不存在

    2023-09-14
    有用 1
    回复 5
    • WH
      WH
      2023-09-14
      官网https://developers.weixin.qq.com/miniprogram/dev/framework/ability/canvas.html  的 ”第四步:进行绘制
      在开发者工具中预览效果“  跳转到 开发者工具之后的 index.js里面的示例程序 就是这样的 方法获得的ctx
      2023-09-14
      回复
    • WH
      WH
      2023-09-14
      另外:按照官网  https://developers.weixin.qq.com/miniprogram/dev/api/canvas/CanvasContext.moveTo.html 示例的程序片段,搬到我的开发者工具后:const app = getApp()
      Page({
          data: {
          },
          onLoad() {
              const ctx = wx.createCanvasContext('myCanvas')
              ctx.moveTo(10, 10)
              ctx.lineTo(100, 10)
              ctx.moveTo(10, 50)
              ctx.lineTo(100, 50)
              ctx.stroke()
              ctx.draw()
          },
      })  
      这个程序根本不能运行,
      2023-09-14
      回复
    • optimistic
      optimistic
      2023-09-14回复WH
      如果在组件内,需要加上.in(this)
      2023-09-14
      回复
    • optimistic
      optimistic
      2023-09-14回复WH
      在 ready 阶段做节点获取,onload还没渲染完成
      2023-09-14
      回复
    • optimistic
      optimistic
      2023-09-14回复WH
      onReady
      2023-09-14
      回复
登录 后发表内容