Codice di Partenza:
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="500" height="500" style="border:1px solid #d3d3d3;">Your browser does not support the HTML5 canvas tag.</canvas>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var step=3;
var dot = {
x:30,
y:0,
velX:6,
velY:7
};
function clearDot() {
ctx.clearRect(0,0,500,500);
}
function paintDot() {
ctx.strokeStyle = "red";
ctx.fillStyle="red";
ctx.beginPath();
ctx.arc(dot.x,dot.y,10,0, 2*Math.PI);
ctx.fill();
ctx.stroke();
}
function move() {
// update position
if (dot.y>=100){
step=-1
}
if (dot.y<1)
step=1;
clearDot();
/*if (dot.y=0){
step=1;
}*/
;
dot.x = dot.x ;//+ 10;//dot.velX;
dot.y = dot.y + step//dot.velY;
paintDot();
}
paintDot(); // disegna il pallino all'inizio
setInterval(move, 10); // lancia la funzione move ogni 100ms
</script>
</body>
</html>