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

后端 未结 8 1251
陌清茗
陌清茗 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:22

    The context object allows you to manipulate the canvas; you can draw rectangles for example and a lot more.

    If you want to get the width and height, you can just use the standard HTML attributes width and height:

    var canvas = document.getElementById( 'yourCanvasID' );
    var ctx = canvas.getContext( '2d' );
    
    alert( canvas.width );
    alert( canvas.height ); 
    

提交回复
热议问题