gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:27:53 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com