canvas getContext(“2d”) returns null

前端 未结 5 2239
天命终不由人
天命终不由人 2020-12-05 17:53

I\'ve tried this a few different ways, but I keep getting stuck with the same error. I\'ve loaded an image to canvas before, but since I updated Safari a few days ago, I\'m

5条回答
  •  佛祖请我去吃肉
    2020-12-05 18:03

    When you do this:

    window.onload = init();
    

    the function init() will be executed immediately (what causes the error, because getContext() gets called too early, i.e. before the DOM is loaded), and the return value of init() will be stored to window.onload.

    So you want to actually do this:

    window.onload = init;
    

    Note the missing ().

提交回复
热议问题