This is a problem with my last question that I posted, but when I clear the canvas, the objects just glitch out. It disappears and then appears. But why does it glitch? I set the interval to 0 MS, but it's just pop and then appear.
The little glitch is going on here: https://cmt-1.hoogidyboogidy.repl.co/
Probably it's just this device. I use laptop and computer. A windows 7 computer is pretty old and a chromebook, eh just bad.
const c = document.getElementById('c')
const ctx = c.getContext('2d')
c.height = window.innerHeight
c.width = window.innerWidth
let blockInfo = {
h: 15, // Defining height and width so they can be changed. (NOTE: The height and width doesn't have to be the same value)
w: 15
}
let renderedObjects = false // Keep in case you're gonna make colission
let player = {
speed: 0.125,
x: 2,
y: 2,
height: 1,
width: 1
}
function clearObjects() {
// Clear all
ctx.clearRect(0, 0, c.width, c.height)
}
function renderObjects() {
// Render block
ctx.fillStyle = '#00b02f'
ctx.fillRect(5 * blockInfo.w, 5 * blockInfo.h, blockInfo.w, blockInfo.h)
// Render player
ctx.fillStyle = '#000000'
ctx.fillRect(player.x * blockInfo.w, player.y * blockInfo.h, player.width * blockInfo.w, player.height * blockInfo.h)
}
function press(e) {
let w = e.which
if (renderedObjects == true) {
clearObjects()
if (w == 39) {
player.x += player.speed
} else if (w == 37) {
player.x -= player.speed
} else if (w == 38) {
player.y -= player.speed
} else if (w == 40) {
player.y += player.speed
}
}
}
setInterval(function() {
renderedObjects = false
renderObjects()
renderedObjects = true
}, 0)
body {
margin: 0;
overflow: hidden;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body onkeydown="press(event)">
<canvas id="c"></canvas>
<script src="script.js"></script>
</body>
</html>