I have the following code in html:
<canvas id="myCanvas" width =800 height=800>
I want, instead of specifying the width
as 800
, to call the JavaScript function getWidth()
to get the width e.g.
<canvas id="myCanvas" width =getWidth() height=800>
What is the correct syntax to do it? Because what I'm doing doesn't work.
Resizing the canvas on the fly is quite easy. To do it, simply set the width and height properties of the Canvas object, and then redraw the canvas contents: Canvas . width = 600 ; Canvas .
<canvas> is an HTML element which can be used to draw graphics via scripting (usually JavaScript).
You can get the width and height of a canvas element simply by accessing those properties of the element. For example: var canvas = document. getElementById('mycanvas'); var width = canvas.
You can set the width like this :
function draw() { var ctx = (a canvas context); ctx.canvas.width = window.innerWidth; ctx.canvas.height = window.innerHeight; //...drawing code... }
function setWidth(width) { var canvas = document.getElementById("myCanvas"); canvas.width = width; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With