javascript - Canvas Width and Height from form values -


i have code, canvas height , width take values ncol , nlin values @ first time, doesn't update when values of ncol , nlin values change. should locate code change dynamic? inside if (canvas.getcontext) {} or in function makecnv?

<html> <body> <canvas id="canvas">     <span style="color: red">     <b>canvas</b> not supported</span> </canvas> <br /> col: <input type="text" value="3" id="ncol" /> lin: <input type="text" value="3" id="nlin" /> text: <input type="text" value="text" id="texto" /> <input type="button" value="make canvas" onclick="makecnv()" />  <script> var canvas = document.getelementbyid("canvas"); var line = document.getelementbyid("nlin").value; var cols = document.getelementbyid("ncol").value; var cnv = null;  // resize canvas canvas.width = line * 32; canvas.height = cols * 32; canvas.style="border: blue solid 1px";  if (canvas.getcontext) {     cnv = canvas.getcontext("2d");     makecnv(); } function makecnv(){         cnv.strokestyle = "olive";         // put text in canvas } </script> </body> </html> 

use onchange attribute. add either inputs:

<input onchange="run()"> 

or use in js:

document.getelementbyid("nlin").onchange = run(); 

both require function run defined - has contain variable settings:

var line,     cols;  function run() {     line = document.getelementbyid("nlin").value;     cols = document.getelementbyid("ncol").value; } 

Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -