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

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

    I had a problem because my canvas was inside of a container without ID so I used this jquery code below

    $('.cropArea canvas').width()
    
    0 讨论(0)
  • 2020-12-13 17:31

    now starting 2015 all (major?) browsers seem to alow c.width and c.height to get the canvas internal size, but:

    the question as the answers are missleading, because the a canvas has in principle 2 different/independent sizes.

    The "html" lets say CSS width/height and its own (attribute-) width/height

    look at this short example of different sizing, where I put a 200/200 canvas into a 300/100 html-element

    With most examples (all I saw) there is no css-size set, so theese get implizit the width and height of the (drawing-) canvas size. But that is not a must, and can produce funy results, if you take the wrong size - ie. css widht/height for inner positioning.

    0 讨论(0)
提交回复
热议问题