I don’t know why but I can’t set the canvas width and height, and am not sure what I am doing wrong.
I create the page like this:
//create div
var d = document.createElement('div');
d.id = 'G';
d.className = 'GameOuput';
document.getElementById('E').appendChild(d);
var i = 1;
var c = document.createElement('canvas');
c.id = 'layer' + i;
c.className = 'c';
c.style['z-index'] = i;
document.getElementById('G').appendChild(c);
console.log(document.getElementById('G').style.width);
c.width = document.getElementById('G').style.width;
c.height = document.getElementById('G').style.height;
The the canvas shows:
<canvas id="layer1" class="c" style="z-index: 1;" width="0" height="0"></canvas>
console.log(document.getElementById('G').style.width); = blank
What am i doing wrong here??
Try:
Typically, an element’s
offsetWidthis a measurement which includes the element borders, the element horizontal padding, the element vertical scrollbar (if present, if rendered) and the element CSS width.