在HTML

时间:2017-01-10 22:47:11

标签: javascript html css input

我在JS中制作了一个简单的Lissajous图形生成器。 我无法在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();
			ctx.strokeStyle="green";
			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>
	<p><h2>A = <input type="range" id="value1" name="parM_choose" min="1" max="9" value="1" step="1" oninput="parM=parseInt(this.value)">
	<output name="show_parM_val" id="parM">1</output></h2>											
    </p>
	<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>
    	<p><h2>A = <input type="range" id="value1" name="parM_choose" min="1" max="9" value="1" step="1" oninput="parM=parseInt(this.value)">
    	<output name="show_parM_val" id="parM">1</output></h2>											
        </p>
    	<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>

2 个答案:

答案 0 :(得分:2)

您的问题是您在幻灯片中设置全局范围变量,但在代码中使用局部变量,请查看此更新的工作示例:

<!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;
    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();
      ctx.strokeStyle = "green";
      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>
    <div style="background: #999;">
    <h2>Choose parameters value using sliders below:</h2>
    <p>
      <h2>A = <input type="range" id="value1" name="parM_choose" min="1" max="9" value="1" step="1" oninput="document.getElementById('parM').innerText = parseInt(this.value);parM=parseInt(this.value)">
	<output name="show_parM_val" id="parM">1</output></h2>	
    </p>
    <p>
      <h2>B = <input type="range" id="value2" min="1" max="9" value="2" step="1" oninput="document.getElementById('parN').innerText = parseInt(this.value);parN=parseInt(this.value)">
   <output name="show_parN_val" id="parN">2</output></h2>
    </p>
    </div>
  </center>
</body>

</html>

答案 1 :(得分:1)

这很有效。 使用函数updateTextInput(val)函数定位元素以更新字段中的值。 在这里回答:HTML5 input type range show range value

  <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();
            ctx.strokeStyle="green";
            ctx.lineWidth = 3;
        }
        function startAnimation() {
            setInterval(draw,0);
        }
        function updateTextInput(val) {
          document.getElementById('textInput').value=val;
        }
        function updateTextInput2(val) {
          document.getElementById('textInput2').value=val;
        }
    </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>
    <p><h2>A = <input type="range" id="value1" name="parM_choose" min="1" max="9" value="1" step="1" oninput="parM=parseInt(this.value)" onchange="updateTextInput(this.value);">
    <input type="text" id="textInput" value="">
    </p>
    <p><h2>B = <input type="range" id="value2" min="1" max="9" value="2" step="1" oninput="parN=parseInt(this.value)" onchange="updateTextInput2(this.value);">
  <input type="text" id="textInput2" value="">



    </p>
</center>
</body>
</html>