-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.html
More file actions
35 lines (29 loc) · 848 Bytes
/
input.html
File metadata and controls
35 lines (29 loc) · 848 Bytes
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
<canvas id="meuCanvas" width="800" height="600"></canvas>
<script>
var canvas = document.getElementById("meuCanvas");
var context = canvas.getContext("2d");
// declaração de variáveis ficam aqui
var x = 0;
window.addEventListener( "keydown", onKeyDown, true );
function onKeyDown( e )
{
// Todo esse código entre chaves executa quando apertam uma tecla
if( e.keyCode == 65 ) // Testa se o cara especificamente apertou a tecla 'A'
{
x = x - 10;
}
else if( e.keyCode == 68 ) // Testa se o cara especificamente apertou a tecla 'D'
{
x = x + 10;
}
}
function draw() {
// limpa o canvas
context.clearRect(0,0,800,600);
// desenha um quadrado na posição determinada pela variável x
context.fillRect(x, 50, 100,100);
// prepara pra desenhar novamente
requestAnimationFrame(draw);
}
draw();
</script>