My animation is supposed to be used in my physics project I am making. I am using 10 millisecond setInterval() and setTimeout() and it glitches in and out. The setTimeout() handles the clear() function in my code, but I have the setTimeout() at 9 milliseconds and it works the best. If someone can help me, here's the code:
var canvas = document.getElementById("canvas") var ctx = canvas.getContext("2d") var x = 10 var y = 0 var xPos = 0 var yPos = 0 function updateVariables() { x = x * 0.9 y = y * -0.9 y = y + 1 xPos += x yPos += y } function clear() { ctx.clearRect(0,0,600,600) } function Draw() { ctx.fillRect(xPos,yPos,20,10) ctx.fillRect(0,131,600,20) setTimeout(clear, 9) } function collisionCheck() { if (yPos > 120) { yPos = 120 } } function keyCheck() { } setInterval(Draw, 10) setInterval(updateVariables, 1) setInterval(collisionCheck, 10) <canvas id="canvas"></canvas>