复制官方文档的小游戏起步中的代码,为什么无法渲染图片?
发布于 6 年前 作者 mingfan 7107 次浏览 来自 问答

大佬门,帮忙看看,谢谢.

const canvas = wx.createCanvas()

const ctx = canvas.getContext(‘2d’)

export default class Main {

  constructor() {

    let {

      windowWidth,

      windowHeight

    } = wx.getSystemInfoSync()

    this.windowWidth = windowWidth

    this.windowHeight = windowHeight

    this.render()

  }

  render() {

    const _this = this

    let cw = canvas.width

    let ch = canvas.height

    let x = cw / 2 - 50,

      y = 0

    ctx.fillStyle = ‘#1aad19’

    //_this.drawRect(x,0 )

    const image = wx.createImage()

    let imgLoad = false

    let imgX = cw / 2 - 50

    let imgY = ch - 150

    image.scr = ‘images/hero.png’

    image.onload = function () {

      imgLoad = true

    }

    setInterval(function () {

      if (y == _this.windowHeight) y = 0;

      _this.drawRect(x, y++)

      // console.log(y);

      if (imgLoad) {

        ctx.drawImage(image, imgX, imgY)

      }

    }, 16)

  }

  drawRect(x, y, w = 100, h = 100) {

    ctx.clearRect(x, y - 1, w, h)

    ctx.fillRect(x, y, w, h)

  }

}

回到顶部