javascript - Generating Canvas width and height attributes to be the same size as a rectangle of user inputted size -


the code below, on start up, generates rectangle same size canvas correct. problem when user clicks button new canvas generated rectangle not appear. please help.

<!doctype html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"> </script> <script> $(document).ready(function(){ <!-- function change <canvas> attributes of "width" , "height" same size rectangle -->    $("#btn").click(function(){    $("#mycanvas").attr("height", $("#height").val());    $("#mycanvas").attr("width", $("#width").val());   }); }); </script>  </head>  <body>   &nbsp;rectangle width:   <input type="number" id="width" value="400">   &nbsp;rectangle height:   <input type="number" id="height" value="400">   &nbsp;       <br>    <canvas id="mycanvas" width=400 height=400 style="border:0px solid #d3d3d3;">    browser not support html5 canvas tag.</canvas>  <script> rect(); function rect(){   width = document.getelementbyid("width").value;   height = document.getelementbyid("height").value;   var c = document.getelementbyid("mycanvas");   var ctx = c.getcontext("2d");   ctx.strokestyle = "#ff0000";   ctx.strokerect(0,0, width, height); } </script> <button id="btn" onclick = "rect()">submit draw rectangle same size canvas</button>      </body> </html> 

remove $(document).ready(function(){ function , change function rect() below:-

 function rect(){     width = document.getelementbyid("width").value;    height = document.getelementbyid("height").value;    var c = document.getelementbyid("mycanvas");    var ctx = c.getcontext("2d");    ctx.canvas.height = height;//add     ctx.canvas.width = width;// ,    ctx.strokestyle = "#ff0000";    ctx.strokerect(0,0, width, height);     

}

demo


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -