I don't know why this canvas is null

后端 未结 1 1728
刺人心
刺人心 2021-01-23 10:38

So I\'ve been looking at a couple of examples of how to fill a canvas with some other image, and as soon as I rearrange the code slightly they stop working. I\'m noticing some

1条回答
  •  爱一瞬间的悲伤
    2021-01-23 10:48

    At the time before 'onload' runs, when the HTML is first being parsed and it runs your script, there are no elements in the document. It hasn't reached the > tag yet, so the "document" is empty. This is still an opportune time to initialize Javascript-only variables and perhaps some other things, but really a large part of your starting code should occur inside of 'onLoad'.

    In short: Your code's success very much depends on when you call getElementById, not where the var aCanvas statement is. Hope that helps your understanding.

    0 讨论(0)
提交回复
热议问题