Is it possible to fix the width and height of an HTML5 canvas element?
The usual way is the following :
<canvas id="canvas" width="300" height="300"></canvas>
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
canvasDOM element has.heightand.widthproperties that correspond to theheight="…"andwidth="…"attributes. Set them to numeric values in JavaScript code to resize your canvas. For example:Note that this clears the canvas, though you should follow with
ctx.clearRect( 0, 0, ctx.canvas.width, ctx.canvas.height);to handle those browsers that don’t fully clear the canvas. You’ll need to redraw of any content you wanted displayed after the size change.Note further that the height and width are the logical canvas dimensions used for drawing and are different from the
style.heightandstyle.widthCSS attributes. If you don’t set the CSS attributes, the intrinsic size of the canvas will be used as its display size; if you do set the CSS attributes, and they differ from the canvas dimensions, your content will be scaled in the browser. For example:See this live example of a canvas that is zoomed in by 4x.