Go back
7
We only made two small changes here:

1. The ammount the rectangle moves at each frame
2. The interval function is called more frequently now. (a hundred times faster, to be exact!)

The illusion of movement is much more convincing now!

Notice that we had to make the rectangle move in smaller ammounts, or else it would move in the same distances as before except 100 times faster. It would go so quickly that we would barely see it!
<canvas id="canvas" width=300 height=300 style="border: 1px solid black; background: #EEEEEE"></canvas>

<script>

	let canvas = document.getElementById('canvas')
	let ctx = canvas.getContext('2d')

	function drawRect(rect) {
		ctx.fillStyle = rect.color
		ctx.fillRect(rect.x, rect.y, rect.width, rect.height)
	}
	
	let rect = { x: 0, y: 0, width: 32, height: 32, color: "blue" }
	
	function update() {
		ctx.clearRect(0, 0, canvas.width, canvas.height)
		
		//We change our rect movement values to smaller ones, since we're gonna update it faster now
		rect.x += 0.4
		rect.y += 0.1
		
		if (rect.x > canvas.width)
			rect.x = 0
		if (rect.y > canvas.height)
			rect.y = 0
		
		drawRect(rect)
	}
	
	//We call this function more frequently, at 4 milliseconds instead of 400.
	//The small value is to ensure our game will look smooth even at higher frequency screens.
	//We can't go below 4 in many browsers so we leave it at that.
	setInterval(update, 4)


</script>