-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
115 lines (96 loc) · 2.87 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
105
106
107
108
109
110
111
112
113
114
115
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Randicons</title>
<style media="screen">
html,
body {
height: 100%;
}
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
#container {
margin: 24px;
padding: 24px;
background-color: #eee;
}
.randicon {
width: 240px;
height: 240px;
display: flex;
flex-wrap: wrap;
flex-direction: column;
}
</style>
</head>
<body>
<div id="container"></div>
<form onsubmit="onFormSubmit(event)">
<input type="number" name="size" value="5" min="2" max="10">
<button type="submit">Generate</button>
</form>
<script type="text/javascript">
var size = 5;
var container = document.getElementById('container');
function generateMatrix(size) {
var matrix = [];
for (var i = 0; i < Math.ceil(size * 0.5); i++) {
var column = [];
for (var j = 0; j < size; j++) {
column.push(Math.random() < 0.5);
}
matrix.push(column);
}
for (var i = Math.ceil(size * 0.5); i < size; i++) {
var column = matrix[size - i - 1];
matrix.push(column);
}
return matrix;
}
function generateColorHex() {
var color = Math.floor(Math.random() * 16777216).toString(16);
return `000000${color}`.substr(-6);
}
function generatePixel(relativeSize, colorHex) {
var pixel = document.createElement('div');
pixel.style.width = `${relativeSize}%`;
pixel.style.height = `${relativeSize}%`;
pixel.style.backgroundColor = `#${colorHex}`;
return pixel;
}
function generateIdenticon(size) {
var randicon = document.createElement('div');
randicon.className = 'randicon';
var matrix = generateMatrix(size);
var pixelColorHex = generateColorHex();
var pixelRelativeSize = 100 / size;
for (var column of matrix) {
for (var pixelValue of column) {
randicon.appendChild(generatePixel(pixelRelativeSize, pixelValue && pixelColorHex));
}
}
return randicon;
}
function removeChildElements(parent) {
for (var firstChild = parent.firstChild; firstChild; firstChild = parent.firstChild) {
parent.removeChild(firstChild);
}
}
function showNewIdenticon(size) {
removeChildElements(container);
container.appendChild(generateIdenticon(size));
}
function onFormSubmit(event) {
event.preventDefault();
var size = parseInt(document.getElementsByName('size')[0].value);
showNewIdenticon(size);
}
showNewIdenticon(size);
</script>
</body>
</html>