forked from KeelyHill/tuppers-formula
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
206 lines (163 loc) · 5.32 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
var src = $('#grid');
var wrap = $('<div id="grid-overlay"></div>');
var gsize = 7;
var grid = 1802;
// these are 1 less due to counting from 0
var cols = 105; // does not really matter
var rows = 16; // matters alot.
var gridArea = (cols + 1) * (rows + 1);
var tmpRow;
var tmpCol;
/* Overlay */
var tbl = $('<table onmousedown="return false;"></table>');
for (var y = rows; y >= 0; y--) {
var tr = $("<tr id='tr" + y + "'></tr>");
for (var x = 0; x <= cols; x++) {
var td = $("<td id='td" + x + "' rel-row='" + y + "' rel-col='" + x + "'></td>");
td.css('width', gsize + 'px').css('height', gsize + 'px');
td.addClass('unselected');
tr.append(td);
}
tbl.append(tr);
}
// attach overlay
wrap.append(tbl);
src.after(wrap);
/* Formula functions */
var bitString = "";
var decimal = BigNumber;
var setBitString = function(string) {
if (string.length > gridArea) bitError.text("Thats too big!"); else bitError.text("");
bitString = string.replace(/0+$/g, "");
$("#bitArea").val(bitString);
}
var setDecString = function(bigNum) {
decimal = bigNum;
var decString = decimal.toFixed();
convertToWords(decString);
if ($("#showCommas")[0].checked) {
decString = decimal.toFormat(3).slice(0, -4)
}
$("#decArea").val(decString);
}
var convertToWords = function(decString) {
$('#wordsArea').text(toWordsConverted(decString));
}
var setBitMap = function() {
var i = 0;
for (var x = 0; x <= cols; x++) {
for (var y = 0; y <= rows; y++) {
var tr = $("#tr" + y);
var td = tr.find("#td" + x);
var bit = bitString[i];
if (bit == "0" || !bit) {
td.addClass('unselected');
td.removeClass('selected');
}
if (bit == "1") {
td.addClass('selected');
td.removeClass('unselected');
}
i++;
}
}
}
var getDecimalFromMap = function () {
var bitString = "";
for (var y = 0; y <= cols; y++) {
for (var x = 0; x <= rows; x++) {
var boolBit = $("table #tr" + x + " #td" + y).hasClass('selected') ? 1 : 0;
bitString += boolBit;
}
}
var decimal = new BigNumber(bitString, 2)
setBitString(bitString);
setDecString(decimal.times(17));
}
/* Events */
$('#grid-overlay td').hover(function () {
$(this).toggleClass('hover');
});
$('#grid-overlay td').mouseup(function () {
let localRow = $(this).attr('rel-row');
let localCol = $(this).attr('rel-col');
for (let i = Math.min(tmpRow, localRow); i <= Math.max(tmpRow, localRow); i++) {
for (let j = Math.min(tmpCol, localCol); j <= Math.max(tmpCol, localCol); j++) {
$('#tr' + i + ' #td' + j).toggleClass('selected').toggleClass('unselected');
}
}
getDecimalFromMap();
});
$('#grid-overlay td').mousedown(function () {
tmpRow = $(this).attr('rel-row');
tmpCol = $(this).attr('rel-col');
});
var bitError = $('#bitError');
var decError = $('#decError');
$('#bitArea').keyup(function() {
input = $(this).val().replace(/ /g, '').replace(',', '');
if(!/^[0-1]*$/.test(input) && input != "") bitError.text("Not a binary number.");
else if (input != "") {
bitError.text("");
setBitString(input);
setDecString(new BigNumber(input, 2).times(17));
setBitMap();
}
});
$('#decArea').keyup(function() {
input = $(this).val().replace(/ /g, '').replace(',', '');
if(!/^\d+$/.test(input) && input != "") decError.text("Not a positive number.");
else if (input != "") {
bigInput = new BigNumber(input);
decError.text("");
setBitString(bigInput.dividedBy(17).floor().toString(2).padStart(1802, 0));
setDecString(bigInput);
setBitMap();
}
});
$("#showCommas").click(function() {
var decString = decimal.toFixed();
if (this.checked) {
decString = decimal.toFormat(3).slice(0, -4);
}
$("#decArea").val(decString);
});
String.prototype.repeat = function(num) {
return new Array(num + 1).join(this);
}
$("#presets button").click(function() {
var b = $(this);
decError.text("");
big = new BigNumber(b.attr('dec'));
setDecString(big);
// decimal = big;
setBitString("0".repeat(parseInt(b.attr('p'))) + big.dividedBy(17).floor().toString(2));
setBitMap();
});
$("#presets #netpbm").change(function(evt) {
var input = evt.target.files[0];
var reader = new FileReader();
reader.onload = function(e){
try {
var image = NetPBM.load(e.target.result);
var canvas = image.getCanvas().getContext("2d");
var data = canvas.getImageData(0, 0, cols + 1, rows + 1).data;
var bitString = "";
for (var x = 0; x <= cols ; x++) {
var offsetX = 4*x;
for (var y = rows; y >= 0; y--) {
var offsetY = (cols + 1) * (y) * 4;
bitString += data[offsetY + offsetX] != 0 ? "0" : "1";
}
};
bitError.text("");
setBitString(bitString);
decError.text("");
setDecString(new BigNumber(bitString, 2).times(17));
setBitMap();
} catch (ex) {
alert(ex.message);
}
}
reader.readAsBinaryString(input);
});