I'm a beginner to coding and more specifically JavaScript and html stuff, and right now I'm just trying to draw a square and make it move to the right. However, when I try to create my server then it just loads the page forever, making it so that I am unable to test my code. Is there any way that I can make it so that my page will actually load? here is my code so far:
html:
<html>
<canvas id = 'canvas' height = '500' width = '500'></canvas>
<script src = 'sus.js'></script>
</html>
JavaScript:
const canvas = document.getElementById('canvas')
const ctx = canvas.getContext('2d')
ctx.beginPath()
ctx.strokeRect(0, 0, 500, 500)
let x = 50
while(true){
ctx.beginPath()
ctx.fillRect(x, 50, 50, 50)
x
}
Thank You!
CodePudding user response:
I commented and I will answer as well.
while(true)
means - run infinitely, because there is no stopping point for the loop.
Try to set the while loop to run until x will be bigger to 200.
while(x <= 200){
ctx.beginPath()
ctx.fillRect(x, 50, 50, 50)
x
}
CodePudding user response:
I figured it out, I had an infinite loop running in my JS file, so my html file would simply sit for eternity waiting for my JS file to return something. Therefore, loading infinitely.