Try using c.offsetWidth
instead of c.width
. As @BrunoSdoukos has already mentioned width
in the context of how you are trying to use it requires that you set the width as an attribute on the <canvas>
element, like this:
<canvas id="myCanvas" width="480"></canvas>
Because there isn't a width attribute set on the HTML element itself, using c.width
will return the default width, which is 300px.
Because you have specified the element's width using CSS in order to get the elements width in this case you should use:
c.offsetWidth
Here is what MDN says about HTMLElement.offsetWidth:
Typically, offsetWidth is a measurement in pixels of the element's CSS width, including any borders, padding, and vertical scrollbars (if rendered). It does not include the width of pseudo-elements such as ::before or ::after.
You can ready more about this property here:
https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetWidth