-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesfera-animada.js
66 lines (55 loc) · 1.41 KB
/
esfera-animada.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Esfera Animada</title>
<style>
body {
margin: 0;
overflow: hidden;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// Define o tamanho do canvas
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Define a posição inicial da esfera
let x = canvas.width / 2;
let y = canvas.height / 2;
// Define o tamanho e velocidade da esfera
const radius = 50;
const speedX = 2;
const speedY = 2;
function animate() {
requestAnimationFrame(animate);
// Limpa o canvas a cada frame
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Desenha a esfera
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2);
ctx.fillStyle = 'blue';
ctx.fill();
// Verifica as colisões com as bordas do canvas
if (x + radius > canvas.width || x - radius < 0) {
speedX *= -1;
}
if (y + radius > canvas.height || y - radius < 0) {
speedY *= -1;
}
// Atualiza a posição da esfera
x += speedX;
y += speedY;
}
animate();
</script>
</body>
</html>