View Full Version : Resolved Canvas in function

07-25-2011, 02:56 PM
<input id="a" size=4 name="neg" placeholder="1st Screen size in inch">
<input id="b" size=4 name="hoyr" placeholder="2nd Screen size in inch">
<input type="submit" value="GO!" onclick="getValue()">

<canvas id="MyCanvas" width="1000" height="1000"></canvas>

<script type="text/javascript">
function getValue()
var x = document.getElementById("a").value;
var y = document.getElementById("b").value;

var aWidth;
var aHeight;
var bWidth;
var bHeight;

var c= document.getElementById("MyCanvas");
var ctx= c.getContext("2d");


Here, it draws canvas then disappears immediately.

Philip M
07-25-2011, 03:37 PM
You have not closed your </script> tag.

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

07-25-2011, 09:25 PM
Of course it disappears immediately because you are submitting a form which always results in a page refresh

So you'll have to either
1. Move the onclick from a button type "submit" to a button type "button" OR
2. "return false" at the very end of getValue()

07-26-2011, 12:01 PM
Thanks it works!