1 | <!DOCTYPE html> |
2 | <html lang="es"> |
3 | |
4 | <head> |
5 | <meta charset="UTF-8"> |
6 | <meta name="viewport" |
7 | content="width=device-width"> |
8 | <title>Animación recta</title> |
9 | <style> |
10 | .sprite { |
11 | position: fixed; |
12 | font-size: 3rem; |
13 | } |
14 | </style> |
15 | </head> |
16 | |
17 | <body> |
18 | <h1>Animación recta</h1> |
19 | <output id="carita" |
20 | class="sprite"> |
21 | 😄 |
22 | </output> |
23 | <script> |
24 | const X_INICIAL = 0 |
25 | const REFRESCO = 5 |
26 | const VELOCIDAD = 0.5 |
27 | let x = X_INICIAL |
28 | setInterval(avanza, REFRESCO) |
29 | |
30 | function avanza() { |
31 | const xFinal = innerWidth |
32 | const yInicial = innerHeight / 2 |
33 | const yFinal = innerHeight |
34 | const y = ((yFinal - yInicial) / |
35 | (xFinal - X_INICIAL)) * |
36 | (x - X_INICIAL) + |
37 | yInicial |
38 | carita.style = |
39 | `left: ${x}px; bottom: ${y}px` |
40 | x = (x + VELOCIDAD) % xFinal |
41 | } |
42 | </script> |
43 | </body> |
44 | |
45 | </html> |