javascript - 如何正确添加到使用 requestAnimationFrame 不断更新的变量

标签 javascript math html5-canvas requestanimationframe

我有一个 fillRect,每次更新输入值时我都会尝试垂直移动它。我的问题是,由于某种奇怪的原因,一旦输入值达到 10,y 边距就会跳跃。这是为什么?

document.addEventListener('DOMContentLoaded', domloaded, false);

function domloaded() {

  //variables for canvas API
  var c = document.getElementById('canvas');
  var ctx = c.getContext('2d');

  function draw() {
    //GET current Yposition
    var yPos = document.getElementById('yPos').value;
    var y = 1 + yPos;
    //remove previous canvas, necessary to reiterate
    ctx.clearRect(0, 0, 700, 700);
    //drawing
    ctx.fillStyle = '#000';
    ctx.fillRect(200, y, 200, 55);

    requestAnimationFrame(draw);
  }
  draw();
}
body {
  background-color: blue;
  width: 100%;
  height: 100%;
}
input,
h1 {
  text-align: center;
  font-size: 20px;
  color: yellow;
  box-sizing: border-box;
}
#container {
  margin: auto;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  min-height: 500px;
}
.floatLeft {
  float: left;
  width: 50%;
  padding: 5% 0;
  background: red;
}
#control {
  float: left;
  width: 30%;
  height: 500px;
  background-color: red;
  color: white;
}
#view {
  float: left;
  width: 70%;
  height: 700px;
  background-color: white;
  color: white;
}
<body>
  <div id="container">
    <h1>Book Canvas Demo</h1>
    <div id="control">
      <h1 class="floatLeft">Y Position:</h1>
      <input id="yPos" type="number" step="1" min="0" max="500" value="0" class="floatLeft">
    </div>
    <div id="view">
      <canvas id="canvas" width="700" height="700"></canvas>
    </div>
  </div>
</body>

换句话说,如何正确添加一个使用 requestAnimationFrame 不断更新的值。谢谢。

最佳答案

您试图将yPos用作数字,但实际上它是一个字符串。在我看来,将任何内容转换为数字的最简单方法是在前面使用一元加号:

    var yPos = +document.getElementById('yPos').value;

document.addEventListener('DOMContentLoaded', domloaded, false);

function domloaded() {

  //variables for canvas API
  var c = document.getElementById('canvas');
  var ctx = c.getContext('2d');

  function draw() {
    //GET current Yposition
    var yPos = +document.getElementById('yPos').value;
    var y = 1 + yPos;
    //remove previous canvas, necessary to reiterate
    ctx.clearRect(0, 0, 700, 700);
    //drawing
    ctx.fillStyle = '#000';
    ctx.fillRect(200, y, 200, 55);

    requestAnimationFrame(draw);
  }
  draw();
}
body {
  background-color: blue;
  width: 100%;
  height: 100%;
}
input,
h1 {
  text-align: center;
  font-size: 20px;
  color: yellow;
  box-sizing: border-box;
}
#container {
  margin: auto;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  min-height: 500px;
}
.floatLeft {
  float: left;
  width: 50%;
  padding: 5% 0;
  background: red;
}
#control {
  float: left;
  width: 30%;
  height: 500px;
  background-color: red;
  color: white;
}
#view {
  float: left;
  width: 70%;
  height: 700px;
  background-color: white;
  color: white;
}
<body>
  <div id="container">
    <h1>Book Canvas Demo</h1>
    <div id="control">
      <h1 class="floatLeft">Y Position:</h1>
      <input id="yPos" type="number" step="1" min="0" max="500" value="0" class="floatLeft">
    </div>
    <div id="view">
      <canvas id="canvas" width="700" height="700"></canvas>
    </div>
  </div>
</body>

关于javascript - 如何正确添加到使用 requestAnimationFrame 不断更新的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28545140/

相关文章:

c++ - 运行时错误 : applying non-zero offset 18446744073709551615 to null pointer (basic_string. h)

python - Python 中的单位转换

javascript - 使用 img 标签自动调整大小或 Canvas 重绘来调整缩略图图像预览大小?

javascript - Fabric.js setHeight() 和 scaleToHeight() 给出不同的高度值

javascript - 如何仅在用户打印时显示某些内容?

javascript - 无法生成体素球体

javascript - Socket.io 和 Webscocket 在同一个服务器上监听

javascript - 如何查看Socket IO发送的数据?

javascript - 在 jQuery 中创建动态表单数据

javascript - 两个对象数组: merge with same key