我有这个代码,画布的高度和宽度第一次从 nCol 和 nLin 值中获取值,但是当 nCol 和 nLin 值的值发生变化时不会更新。您应该在哪里找到更改的代码是动态的?在 if (canvas.getContext) {} 或函数 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 the 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 the text in the canvas
}
</script>
</body>
</html>