-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
274 lines (218 loc) · 7.18 KB
/
index.js
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
const [
data,
map,
] = await Promise.all([
fetch("/bdsmcompass/data/data.json", {
method: "GET",
headers: {
"Content-Type": "application/json",
},
})
.then((response) => response.json()),
fetch("/bdsmcompass/data/map.json", {
method: "GET",
headers: {
"Content-Type": "application/json",
},
})
.then((response) => response.json()),
]);
const DATA_SOURCE = {
combined: -1,
male: 0,
female: 1,
};
const MALE_XY_MAX = {
x: Math.abs(Object.values(data.male).sort((a, b) => a.x - b.x)[0].x),
y: Math.abs(Object.values(data.male).sort((a, b) => a.y - b.y)[0].y)
};
const FEMALE_XY_MAX = {
x: Math.abs(Object.values(data.female).sort((a, b) => a.x - b.x)[0].x),
y: Math.abs(Object.values(data.female).sort((a, b) => a.y - b.y)[0].y)
};
function normalize(data, max) {
const x = data.x / max.x;
const y = data.y / max.y;
return {
x,
y,
};
}
function getData(key, dataSource) {
if (dataSource == DATA_SOURCE.male) {
const _data = data.male[key];
if (_data == null) {
return null;
}
return normalize(_data, MALE_XY_MAX);
}
else if (dataSource == DATA_SOURCE.female) {
const _data = data.female[key];
if (_data == null) {
return null;
}
return normalize(_data, FEMALE_XY_MAX);
}
else {
const male = data.male[key];
const female = data.female[key];
let combined = {
x: 0,
y: 0,
};
if (male !== undefined && male.x !== undefined && male.y !== undefined) {
const _male = normalize(male, MALE_XY_MAX);
combined.x += _male.x;
combined.y += _male.y;
}
if (female !== undefined && female.x !== undefined && female.y !== undefined) {
const _female = normalize(female, FEMALE_XY_MAX);
combined.x += _female.x;
combined.y += _female.y;
}
combined.x /= 2;
combined.y /= 2;
return combined;
}
}
function extractDataFromLine(line) {
const regex = /(\d+)% (.*)/;
const result = regex.exec(line);
if (result == null) {
return null;
}
const percentage = Number.parseInt(result[1]) * 0.01;
const name = result[2];
return {
percentage,
name,
};
}
function calculatePoliticalOrientation(input, dataSource) {
const mapEntries = Object.entries(map).map(([k, v]) => [k.toLowerCase(), v]);
let pol_or = {
x: 0,
y: 0,
};
let num = 0;
let vanilla = 0;
for (const line of input.split("\n")) {
const bdsmTestData = extractDataFromLine(line);
if (bdsmTestData == null) {
continue;
}
if (bdsmTestData.percentage == 0) {
continue;
}
const key = bdsmTestData.name.toLowerCase();
if (key.includes("vanilla")) {
vanilla = bdsmTestData.percentage;
}
const _mapData = mapEntries.find(([k, v]) => k.includes(key) || key.includes(k));
if (_mapData == null) {
continue;
}
const [_, mapData] = _mapData;
const dataEntries = Object.entries(mapData);
for (const [dataName, dataModifier] of dataEntries) {
const data = getData(dataName, dataSource);
if (data == null) {
continue;
}
pol_or.x += data.x * dataModifier * bdsmTestData.percentage;
pol_or.y += data.y * dataModifier * bdsmTestData.percentage;
num++;
}
}
if (num == 0) {
return null;
}
pol_or.x /= num;
pol_or.y /= num;
pol_or.x *= 1 - vanilla + 1;
pol_or.y *= 1 - vanilla + 1;
pol_or.x = Math.min(1, Math.max(-1, pol_or.x));
pol_or.y = Math.min(1, Math.max(-1, pol_or.y));
return pol_or;
}
function drawCompass(pos) {
// draw political compass
const canvas = document.getElementById('political-compass');
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
// fully white background
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, canvas.width, canvas.height);
const width = canvas.width;
const height = canvas.height;
// draw fields
const drawRect = function (x, y, w, h, color) {
// 25% opacity
this.fillStyle = color + '40';
this.fillRect(x, y, w, h);
}
drawRect.call(ctx, 0, 0, width / 2, height / 2, '#ff0000');
drawRect.call(ctx, width / 2, 0, width / 2, height / 2, '#0000ff');
drawRect.call(ctx, 0, height / 2, width / 2, height / 2, '#00ff00');
drawRect.call(ctx, width / 2, height / 2, width / 2, height / 2, '#ffff00');
// draw lines
const drawLine = function (x1, y1, x2, y2, color) {
this.beginPath();
this.moveTo(x1, y1);
this.lineTo(x2, y2);
this.strokeStyle = color;
this.stroke();
}
drawLine.call(ctx, width / 2, 0, width / 2, height, '#000');
drawLine.call(ctx, 0, height / 2, width, height / 2, '#000');
// draw grid lines
const drawGridLine = function (x1, y1, x2, y2, color) {
this.beginPath();
this.moveTo(x1, y1);
this.lineTo(x2, y2);
this.strokeStyle = color + '2';
this.stroke();
}
const amount = 20;
for (let i = 0; i < amount; i++) {
drawGridLine.call(ctx, 0, i * height / amount, width, i * height / amount, '#000');
drawGridLine.call(ctx, i * width / amount, 0, i * width / amount, height, '#000');
}
// draw text
const drawText = function (text, x, y, color) {
this.fillStyle = color;
this.font = '32px Arial';
this.fillText(text, x, y);
}
drawText.call(ctx, 'Left', 10, height / 2 - 10, '#000');
drawText.call(ctx, 'Right', width - 80, height / 2 - 10, '#000');
drawText.call(ctx, 'Authoritarian', width / 2 - 90, 50, '#000');
drawText.call(ctx, 'Libertarian', width / 2 - 74, height - 10, '#000');
// draw watermark text
const drawWatermark = function (text, x, y, color) {
this.fillStyle = color;
this.font = '20px Arial';
this.fillText(text, x, y);
}
drawWatermark.call(ctx, 'https://angelsflyinhell.github.io/bdsmcompass', 10, height - 10, '#000');
// draw point
const drawPoint = function (x, y, radius, color) {
this.beginPath();
this.arc(x, y, radius, 0, 2 * Math.PI);
this.fillStyle = color;
this.fill();
}
const normalized_x = ((pos.x + 1) / 2) * width;
const normalized_y = ((pos.y * -1 + 1) / 2) * height;
drawPoint.call(ctx, normalized_x, normalized_y, 30, '#92B0FF' + '80');
drawPoint.call(ctx, normalized_x, normalized_y, 12, '#5282FE');
document.getElementById('results').style.display = 'flex'
}
function onSubmit(event) {
event.preventDefault();
const dataSource = DATA_SOURCE[document.getElementById("datapool").value];
const input = document.getElementById("bdsm").value;
const pol_or = calculatePoliticalOrientation(input, dataSource);
drawCompass(pol_or)
}
document.getElementById("form").addEventListener("submit", onSubmit);