How do I get the width and height of a HTML5 canvas?

后端 未结 8 1246
陌清茗
陌清茗 2020-12-13 17:04

How can i get the width and height of the canvas element in JavaScript?

Also, what is the \"context\" of the canvas I keep reading about?

8条回答
  •  伪装坚强ぢ
    2020-12-13 17:18

    Well, all the answers before aren't entirely correct. 2 of major browsers don't support those 2 properties (IE is one of them) or use them differently.

    Better solution (supported by most browsers, but I didn't check Safari):

    var canvas = document.getElementById('mycanvas');
    var width = canvas.scrollWidth;
    var height = canvas.scrollHeight;
    

    At least I get correct values with scrollWidth and -Height and MUST set canvas.width and height when it is resized.

提交回复
热议问题