HTML5 Dynamically create Canvas

前端 未结 5 937
北海茫月
北海茫月 2020-12-05 03:43

Hi there I have a question about dynamically creating a canvas using javascript.

I create a canvas like this:

var canvas = document.createElement(\'c         


        
5条回答
  •  既然无缘
    2020-12-05 04:35

    It happens because you call it before DOM has loaded. Firstly, create the element and add atrributes to it, then after DOM has loaded call it. In your case it should look like that:

    var canvas = document.createElement('canvas');
    canvas.id     = "CursorLayer";
    canvas.width  = 1224;
    canvas.height = 768;
    canvas.style.zIndex   = 8;
    canvas.style.position = "absolute";
    canvas.style.border   = "1px solid";
    window.onload = function() {
        document.getElementById("CursorLayer");
    }
    

提交回复
热议问题