-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheverything.js
193 lines (166 loc) · 4.51 KB
/
everything.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
/*
Manvir Singh
Everything Equation
5/25/15
*/
"use strict";
(function() {
var ROWS = 17;
var COLUMNS = 106;
var pixels;
var rightMouseDown;
var leftMouseDown;
window.onload = function() {
pixels = new Array();
for (var i = 0; i < COLUMNS; i++)
pixels[i] = new Array();
rightMouseDown = 0;
leftMouseDown = 0;
setupPage();
var x = getURLParameter("x");
document.getElementById("textarea").value = x;
updateCanvas();
updateTextArea();
}
function setupPage() {
setupCanvas();
var textarea = document.getElementById("textarea");
textarea.onkeypress = isNumberKey;
textarea.onkeyup = function(e){
e = e || event;
if (e.keyCode === 13) //is enter key
updateCanvas();
return true;
}
document.getElementById("submit").onclick = updateCanvas;
document.getElementById("clear").onclick = clear;
document.getElementById("invert").onclick = function() {
for (var i = 0; i < COLUMNS; i++)
for (var j = 0; j < ROWS; j++)
pixels[i][j] = !pixels[i][j];
drawAll();
updateTextArea();
};
drawAll();
}
function setupCanvas() {
var canvas = document.getElementById("canvas");
canvas.addEventListener("mousemove", draw, false);
canvas.addEventListener('contextmenu', function(evt) { evt.preventDefault(); }, false);
document.onmousedown = function(e) {
if (e.button == 0)
leftMouseDown = 1;
else if (e.button == 2)
rightMouseDown = 1;
draw();
}
document.onmouseup = function(e) {
leftMouseDown = 0;
rightMouseDown = 0;
}
}
function updateTextArea() {
var string = "0b";
for (var i = 0; i < COLUMNS; i++) {
for (var j = 0; j < ROWS; j++) {
if (pixels[i][j])
string += "1";
else
string += "0";
}
}
var number = BigInteger.parse(string);
number = number.multiply(ROWS);
textarea.value = number.toString(10).replace(/(\d{3})/g, '$1 ').replace(/(^\s+|\s+$)/,'');
updateTextField();
}
function updateCanvas(event) {
var number = BigInteger.parse(textarea.value.replace(/\s/g, ''));
number = number.quotient(ROWS);
var string = number.toString(2);
var numZeros = COLUMNS * ROWS - string.length;
var zeros = "";
for (var i = 0; i < numZeros; i++)
zeros += "0";
string = zeros + number.toString(2);
for (var i = 0; i < COLUMNS; i++) {
for (var j = 0; j < ROWS; j++) {
if (string.charAt(ROWS * i + j) == "1")
pixels[i][j] = true;
else
pixels[i][j] = false;
}
}
drawAll();
updateTextArea();
}
function updateTextField() {
var url = window.location.href.split("?")[0];
var x = document.getElementById("textarea").value.replace(/\s/g, '');
if (x != "")
url += "?x=" + x;
document.getElementById("textfield").value = url;
}
function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : event.keyCode;
if ((charCode < 48 || charCode > 57) && charCode != 32)
return false;
return true;
}
function clear() {
textarea.value = "";
for (var i = 0; i < COLUMNS; i++)
for (var j = 0; j < ROWS; j++)
pixels[i][j] = false;
drawAll();
updateCanvas();
updateTextField();
}
function getURLParameter(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null;
}
function draw() {
if (leftMouseDown || rightMouseDown) {
var canvas = document.getElementById("canvas");
var rect = canvas.getBoundingClientRect();
var x = Math.floor((event.x - rect.left) / 14);
var y = Math.floor((event.y - rect.top) / 14);
if (x >= 106) x = 105;
if (leftMouseDown)
pixels[x][ROWS - y - 1] = true;
else if (rightMouseDown)
pixels[x][ROWS - y - 1] = false;
var context = canvas.getContext("2d");
if (pixels[x][ROWS - y - 1])
context.fillStyle = "#000000";
else
context.fillStyle = "#FFFFFF";
context.fillRect(x * 14, y * 14, 14, 14);
drawGrid();
updateTextArea();
}
}
function drawAll() {
var context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
for (var i = 0; i < COLUMNS; i++) {
for (var j = 0; j < ROWS; j++) {
if (pixels[i][j]) context.fillStyle = "#000000";
else context.fillStyle = "#FFFFFF";
context.fillRect(i * 14, (ROWS - j - 1) * 14, 14, 14);
}
}
drawGrid();
}
function drawGrid() {
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
var opts = {
distance : 14,
lineWidth : 1,
gridColor : "#000000",
caption : false
};
new Grid(opts).draw(context);
}
})();