我正在JS中製作一個簡單的利薩如圖生成器。 我無法在html代碼中顯示當前滑塊值。 下面是完整的代碼:在HTML中顯示當前滑塊值(輸入類型=「範圍」)
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
<meta charset="UTF-8">
<title>Lissajous figures</title>
<br>
<script type="application/javascript">
var ctx;
var timer = null;
var aS = new Array();
var rad = 189;
var max=3*360, m1=max-1, m2 = max/2;
var iXo=0, iYo=m2/2;
var parN=2, parM=1;
function init() {
var canvas = document.getElementById("field");
if (canvas.getContext) { ctx = canvas.getContext("2d");
sf=Math.sin(3.14159265/m2); cf=Math.cos(3.14159265/m2);
s=-sf; c=cf;
for (i=0; i<m2; i++) {
s1=s*cf+c*sf; c=c*cf-s*sf; s=s1;
aS[i]=Math.round(rad*(1.+s))+1;
aS[i+m2]=Math.round(rad*(1.-s))+1;
}
startAnimation();
}
}
function draw() {
Xo=aS[iXo],Yo=aS[iYo];
ctx.beginPath();
ctx.moveTo(Xo,Yo);
for (j=max; j>0; j--) {
iX=(iXo+parM) % max; iY=(iYo+parN) % m1;
X=aS[iX]; Y=aS[iY];
ctx.lineTo(X,Y);
iXo=iX; iYo=iY; Xo=X; Yo=Y;
}
ctx.clearRect (0, 0, 500, 500);
ctx.stroke();
\t \t \t ctx.strokeStyle="green";
\t \t \t ctx.lineWidth = 3;
}
function startAnimation() {
setInterval(draw,0);
}
</script>
</head>
<h1>Lissajous Figure Generator</h1>
<br>
<body onload="init();" bgcolor="black">
<center><canvas id="field" width="400" height="400">
</canvas>
<br>
<br>
<br>
<h2>Choose parameters value using sliders below:</h2>
\t <p><h2>A = <input type="range" id="value1" name="parM_choose" min="1" max="9" value="1" step="1" oninput="parM=parseInt(this.value)">
\t <output name="show_parM_val" id="parM">1</output></h2> \t \t \t \t \t \t \t \t \t \t \t
</p>
\t <p><h2>B = <input type="range" id="value2" min="1" max="9" value="2" step="1" oninput="parN=parseInt(this.value)">
<output name="show_parN_val" id="parN">2</output></h2>
</p>
</center>
</body>
</html>
如何使產值的工作?什麼應該是正確的ID? 我說的是下面的代碼片段:
<h2>Choose parameters value using sliders below:</h2>
\t <p><h2>A = <input type="range" id="value1" name="parM_choose" min="1" max="9" value="1" step="1" oninput="parM=parseInt(this.value)">
\t <output name="show_parM_val" id="parM">1</output></h2> \t \t \t \t \t \t \t \t \t \t \t
</p>
\t <p><h2>B = <input type="range" id="value2" min="1" max="9" value="2" step="1" oninput="parN=parseInt(this.value)">
<output name="show_parN_val" id="parN">2</output></h2>
https://fiddle.jshell.net/e2xzk04o/'' – dandavis
parM = <---您正在設置變量...不設置元素的html。 – epascarello