0
所以最近我一直有一個問題,試圖將字符串從文本輸入轉換爲浮點/整數以在各種動畫中使用。有任何想法嗎?目前我正在嘗試使用parsFloat,但這只是停止加載所有的圈子。有沒有其他方法將字符串轉換爲浮點數?謝謝!
試圖將字符串轉換爲浮動在畫布上的JS問題?
function myFunction() {
var degrees = undefined;
var Rad = function degToRad(x) {
degrees = x/(180/Math.PI);
return degrees;
};
var canvas = document.getElementById("canvas"),
c = canvas.getContext("2d");
var posX = 55, posY = 100, gravity = parseFloat(document.getElementById("drag").value) * parseFloat(document.getElementById("gravity").value), vX = parseFloat(document.getElementById("xVel").value), vY = 0;
setInterval(function() {
c.fillStyle = "black";
c.fillRect(0, 0, canvas.width, canvas.height);
c.beginPath();
c.arc(posX, posY, parseFloat(document.getElementById("radius").value), 0, Rad(360), false);
c.fillStyle = "white";
c.fill();
posX+= vX;
posY+= vY;
if (posY > 800-parseFloat(document.getElementById("radius").value)) {
vY *= -document.getElementById("bounce").value;
posY = 800-parseFloat(document.getElementById("radius").value);
vX *= 0.9;
}
vY += gravity;
}, parseFloat(document.getElementById("framerate").value));
}
</script>
</head>
<body>
<input id = "gravity" type="text" placeholder="gravity (m/s)">
<input id = "xVel" type = "text" placeholder="x velocity (m/s)">
<input id = "bounce" type = "text" placeholder="bounce (0-1)">
<input id = "radius" type = "text" placeholder="sphere radius">
<input id = "Mass" type = "text" placeholder="sphere mass">
<input id = "density" type = "text" placeholder="fluid density">
<input id = "framerate" type = "text" placeholder="framerate (ms/frame)">
<input id = "drag" type = "text" placeholder="drage co. (sphere = 0.5)">
<input id = "submit" type = "submit" onclick="myFunction()">
<button type="submit" onclick="location.reload()">done</button>
<canvas id = "canvas" width="5000" height="800">
</canvas>
</body>
</html>
您是否檢查了開發者控制檯的錯誤? – Pointy
我建議只解析一次值並將解析值存儲在一個變量中。 –
看起來像是在這裏工作:https://jsfiddle.net/n1atr86d/ – Michelangelo