-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
104 lines (86 loc) · 3.16 KB
/
index.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Weird Allergies</title>
<link rel="stylesheet" href="css/main.css">
<script src="js/lib/tracery.js"></script>
<script src="js/grammar.js"></script>
</head>
<body>
<header>
<div class="content">
<h1>Weird Allergies</h1>
</div>
</header>
<div class="content main">
<div id="result">
<p id="text"></p>
<div id="user">
<img src="" alt="" id="userimage">
<div id="username"></div>
</div>
</div>
<button type="button" id="refreshBtn">New</button>
<span class="hint">(spacebar)</span>
</div>
<script type="text/javascript">
var grammar = tracery.createGrammar(grammarData);
/**
* Retrieves a random person from the radomuser.me API and adds it to the UI.
*/
function addRandomPerson() {
var nameNode = document.getElementById('username');
nameNode.innerText = '';
getRandomImage();
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var data = JSON.parse(this.responseText);
var person = data.results[0];
// Name
var name = capitalize(person.name.first) + " " + capitalize(person.name.last);
nameNode.innerText = name;
}
};
xhttp.open("GET", "https://randomuser.me/api/", true);
xhttp.send();
}
function getRandomImage() {
var imageNode = document.getElementById('userimage');
imageNode.src = '';
// imageNode.src = 'https://api.adorable.io/avatars/96/' + Math.random().toString(36);
imageNode.src = 'https://robohash.org/' + Math.random().toString(36) + '.png?set=set1&bgset=bg1&size=48x48';
// Randomly flip and/or rotate the image for more variation
imageNode.style.transform = (Math.random() >= 0.5) ? "scaleX(1)" : "scaleX(-1)";
imageNode.style.transform += " rotate(" + (Math.random() * 2 - 1) * 15 + "deg)";
}
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
/**
* Generate a new text and avatar.
*/
function refresh() {
var resultsDiv = document.getElementById('text');
var sentence = grammar.createFlattened();
resultsDiv.innerText = sentence;
addRandomPerson();
}
// Enable button click
document.getElementById('refreshBtn').addEventListener('click', function(e) {
e.preventDefault();
refresh();
});
// Spacebar
document.body.onkeyup = function(e) {
if (e.keyCode == 32) {
refresh();
}
}
refresh();
</script>
</body>
</html>