-
Notifications
You must be signed in to change notification settings - Fork 69
/
keyboard-simple.html
61 lines (47 loc) · 1.52 KB
/
keyboard-simple.html
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
<!DOCTYPE html>
<html>
<head>
<title>A-Frame: Simple Keyboard Input</title>
<meta name="description" content="A-Frame Examples">
<script src="js/aframe-master.1.0.4.min.js"></script>
<script src="js/aframe-environment-component.min.js"></script>
<script src="js/keyboard.js"></script>
</head>
<body>
<script type="text/javascript">
AFRAME.registerComponent('keyboard-functions', {
init: function()
{
this.keyboard = new Keyboard();
this.sphere = document.getElementById("sphere");
},
tick: function (time, timeDelta)
{
this.keyboard.update();
if (this.keyboard.isKeyDown("R"))
this.sphere.setAttribute("color", "red");
if (this.keyboard.isKeyDown("Y"))
this.sphere.setAttribute("color", "yellow");
if (this.keyboard.isKeyDown("G"))
this.sphere.setAttribute("color", "green");
if (this.keyboard.isKeyDown("B"))
this.sphere.setAttribute("color", "blue");
let speed = 0.01;
if (this.keyboard.isKeyPressed("I"))
this.sphere.object3D.position.z -= speed;
if (this.keyboard.isKeyPressed("J"))
this.sphere.object3D.position.x -= speed;
if (this.keyboard.isKeyPressed("K"))
this.sphere.object3D.position.z += speed;
if (this.keyboard.isKeyPressed("L"))
this.sphere.object3D.position.x += speed;
}
});
</script>
<a-scene environment="preset: default;">
<a-entity camera look-controls wasd-controls="acceleration:10" position="0 1.6 0"></a-entity>
<a-sphere id="sphere" position="0 2 -5" color="gray"></a-sphere>
<a-entity keyboard-functions></a-entity>
</a-scene>
</body>
</html>