-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.htm
188 lines (163 loc) · 7.15 KB
/
index.htm
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Moji demo</title>
<style>
input {
font-family: 'Courier New', Courier, monospace;
}
.frm {
margin-bottom: 1em;
}
canvas {
border: 1px solid black;
margin: 1em;
}
</style>
</head>
<body>
<h1>Moji demo</h1>
<p>
This is a sample implementation of an identicon algorithm described in <cite>LUGGEN, Michael. Moji: The advent of large identifiers and how to conquer them as human. MSc. thesis. eXascale Infolab, University of Fribourg. 2012.</cite> [<a href="https://exascale.info/assets/pdf/students/MSc_Thesis_-_Michael_Luggen__14_09_2012.pdf">available online</a>]
</p>
<div class="frm">
<label for="hash">Hash:</label>
<input id="hash" size="40" />
<button id="btnRandom">Random</button><br />
<label for="selAlgorithm">Algorithm: </label>
<select id="selAlgorithm">
<option value="mojiPoints">Moji: Show points</option>
<option value="mojiFigBlobLines">Moji: Figure blob lines</option>
<option value="mojiFigBlobQuadCurves" selected="selected">Moji: Figure blob quadratic curves</option>
</select><br />
<label for="inputSaturation">Saturation:</label>
<input id="inputSaturation" type="range" min="0" max="1" step="0.01" value="0.5" />
</div>
<div class="canvas">
<canvas id="canvas" width="200" height="200"></canvas>
</div>
<script>
(function () {
const SIZE = 32;
const CENTER = SIZE / 2;
// const PALETTE = ['#332288', '#117733', '#44AA99', '#88CCEE', '#DDCC77', '#CC6677', '#AA4499', '#882255'];
const PALETTE = ["6b9ac4","97d8c4","eff2f1","f4b942",
"ffe156","ffe9ce","ffb5c2","3777ff"];
function mainInit() {
const btnRandom = document.getElementById('btnRandom');
const inputHash = document.getElementById('hash');
const inputSaturation = document.getElementById('inputSaturation');
const selAlgorithm = document.getElementById('selAlgorithm');
let dirty = true;
function rerender() {
render(inputHash.value.replaceAll(/[^a-f0-9]/ig, ''), selAlgorithm.value, inputSaturation.value);
dirty = false;
return false;
}
selAlgorithm.onchange = rerender;
inputHash.onchange = rerender;
inputHash.onkeydown = () => {
if (!dirty) {
setTimeout(rerender, 100);
dirty = true;
}
return true;
}
inputSaturation.onchange = rerender;
btnRandom.onclick = function () {
inputHash.value = [...Array(40)].map(() => Math.floor(Math.random() * 16).toString(16)).join('');
rerender();
return false;
}
btnRandom.onclick();
}
function getJump(data, len) {
return data * SIZE * SIZE / ((len + 1) * 16 / 2);
}
function getPoints(hash, saturation) {
let jump = 0;
let pos = 0;
const len = hash.length;
let m = [];
for (let u = 0; u < SIZE; ++u) {
for (let v = 0; v < SIZE; ++v) {
if (jump <= 0 && pos < len) {
const data = parseInt(hash[pos++], 16);
if (data / 15 > 1 - saturation) {
m.push([u, v]);
}
jump = getJump(data, len);
} else {
--jump;
}
}
}
m.sort(function (a, b) {
return Math.atan2(a[0] - CENTER, a[1] - CENTER) - Math.atan2(b[0] - CENTER, b[1] - CENTER);
});
return m;
}
function renderPoints(ctx, pts, PIXELSIZE) {
ctx.fillStyle = 'black';
for (let pt of pts) {
ctx.fillRect(pt[0] * PIXELSIZE, pt[1] * PIXELSIZE, PIXELSIZE, PIXELSIZE);
}
}
function renderFigureBlobLines(ctx, pts, PIXELSIZE) {
ctx.lineWidth = 1;
ctx.beginPath();
let start = pts[0];
ctx.moveTo(start[0] * PIXELSIZE, start[1] * PIXELSIZE);
for (let i = 1; i < pts.length; ++i) {
let a = pts[i - 1];
let b = pts[i];
ctx.lineTo(b[0] * PIXELSIZE, b[1] * PIXELSIZE);
}
ctx.lineTo(start[0] * PIXELSIZE, start[1] * PIXELSIZE);
ctx.fillStyle = 'black';
ctx.fill();
ctx.closePath();
}
function renderFigureBlobQuadCurves(ctx, pts, PIXELSIZE) {
ctx.lineWidth = 1;
ctx.beginPath();
let start = pts[0];
ctx.moveTo(start[0] * PIXELSIZE, start[1] * PIXELSIZE);
for (let i = 1; i < pts.length; i += 2) {
let a = pts[i - 1];
let b = pts[i];
ctx.quadraticCurveTo(a[0] * PIXELSIZE, a[1] * PIXELSIZE, b[0] * PIXELSIZE, b[1] * PIXELSIZE);
}
ctx.lineTo(start[0] * PIXELSIZE, start[1] * PIXELSIZE);
ctx.fillStyle = 'black';
ctx.fill();
ctx.closePath();
}
function render(hash, alg, saturation) {
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const CANVAS_SIZE = Math.min(canvas.width, canvas.height);
const PIXELSIZE = Math.floor(CANVAS_SIZE / SIZE);
ctx.fillStyle = '#' + PALETTE[parseInt(hash[hash.length - 1], 16) % 8];
ctx.fillRect(0, 0, canvas.width, canvas.height);
switch (alg) {
case 'mojiPoints':
renderPoints(ctx, getPoints(hash, saturation), PIXELSIZE);
break;
case 'mojiFigBlobLines':
renderFigureBlobLines(ctx, getPoints(hash, saturation), PIXELSIZE);
break;
case 'mojiFigBlobQuadCurves':
renderFigureBlobQuadCurves(ctx, getPoints(hash, saturation), PIXELSIZE);
break;
default:
alert('Unknown algorithm!');
break;
}
}
window.onload = mainInit;
})();
</script>
</body>
</html>