-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
247 lines (217 loc) · 6.28 KB
/
app.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
var columns;
var rows;
var gridW;
var color;
var current;
var painting;
var selecting;
var erasing;
var isDown;
var start;
var css;
var fileNameOriginal;
var fileName;
var margins;
var dividor;
var wrapperW;
var wrapperH;
function init() {
start = {};
css = {};
margins = "ON";
dividor = 27;
painting = true;
color = "#000000";
fileNameOriginal = "Best drawing. Ever.";
$(document).on("mouseup", function () {
isDown = false;
});
}
$(document).on("ready", init());
// painting function
function paint() {
if (painting) {
current.css("background-color", color);
current.html("");
}
}
// function that actually erases
function erase() {
current.css("background-color", "#fff");
current.html("x");
}
// grid events
function gridEvents() {
$(".grid_square").on("mousedown", function () {
current = $(this);
if (painting) {
paint();
isDown = true;
}
if (erasing) {
erase();
isDown = true;
}
});
$(".grid_square").on("mouseover", function () {
current = $(this);
current.css("opacity", 0.92);
if (isDown) {
if (painting) { paint(); }
if (erasing) { erase(); }
}
});
$(".grid_square").on("mouseout", function () {
current.css("opacity", 1);
current = undefined;
});
$(".grid_square").on("click", function () {
if (selecting) {
start.pixel = this;
$("#submit_start_pixel").html("CHANGE START PIXEL");
$("#export_alert").css("display", "block");
}
});
}
function drawGrid() {
gridW = 0;
// making sure grid is empy
$("#grid").empty();
// adding rows to grid
for (i = 1; i < rows + 1; i += 1) {
$("#grid").append("<div class='row'>");
}
// adding columns to grid (adding squares to rows)
for (i = 1; i < columns + 1; i += 1) {
$(".row").append("<div class='grid_square'>");
gridW += 27;
}
// setting div of grid so that margin:auto works
$("#grid").width(gridW);
// adding x to squares
$(".grid_square").html("x");
gridEvents();
}
function newFile() {
columns = $("#columns_nr").val();
if (columns === "") {
alert("how many columns will your file have?");
return;
}
columns = parseInt(columns, 10);
rows = $("#rows_nr").val();
if (rows === "") {
alert("how many rows will your file have?");
return;
}
rows = parseInt(rows, 10);
wrapperW = columns * 25;
wrapperH = rows * 25;
fileNameOriginal = $("#file_name").val();
if (fileNameOriginal === "") {
alert("you need to give your file a name!");
return;
}
$("title").html(fileNameOriginal);
$(".alert#new_alert").css("display", "none");
actualAlert = undefined;
drawGrid();
}
function selectColor() {
// setting color to selected by user
color = $("#select_color").val();
$(".alert#color_alert").css("display", "none");
actualAlert = undefined;
painting = true;
erasing = false;
selecting = false;
}
// function to start the eraser
function startEraser() {
painting = false;
erasing = true;
selecting = false;
}
// selecting the "start pixel", the pixel where the shadow will come from
function selectStartPixel() {
selecting = true;
erasing = false;
painting = false;
$("#export_alert").css("display", "none");
}
function downloadFile() {
var download;
start.pixel = $("#grid div:nth-child(1) div:nth-child(1)");
// calculate distances between squares, works like a family where the center of the
// family tree is the start pixel
start.sib = $(start.pixel).siblings();
start.papa = $(start.pixel).parent();
start.papa.sib = $(start.papa).siblings();
for (n = 0; n < start.papa.sib.length; n += 1) {
start.papa.sib[n].sons = $(start.papa.sib[n]).children();
for (i = 0; i < start.papa.sib[n].sons.length; i += 1) {
start.papa.sib[n].sons[i].distance = {
side: Math.round(($(start.pixel).offset().left - $(start.papa.sib[n].sons[i]).offset().left) / dividor),
top: Math.round(($(start.pixel).offset().top - $(start.papa.sib[n].sons[i]).offset().top) / dividor)
};
}
}
for (i = 0; i < start.sib.length; i += 1) {
start.sib[i].distance = {
side: Math.round(($(start.pixel).offset().left - $(start.sib[i]).offset().left) / dividor),
top: 0
};
}
// creating css code
css.code = [];
for (i = 0; i < start.sib.length; i += 1) {
if (!($(start.sib[i]).html() === "x")) {
start.sib[i].code = ((-1 * start.sib[i].distance.side * 25) + "px") + " " + ((-1 * start.sib[i].distance.top * 25) + "px") + " 0 " + $(start.sib[i]).css("background-color") + " ";
css.code.push(start.sib[i].code);
}
}
for (n = 0; n < start.papa.sib.length; n += 1) {
for (i = 0; i < start.papa.sib[n].sons.length; i += 1) {
if (!($(start.papa.sib[n].sons[i]).html() === "x")) {
start.papa.sib[n].sons[i].code = ((-1 * start.papa.sib[n].sons[i].distance.side * 25) + "px") + " " + ((-1 * start.papa.sib[n].sons[i].distance.top * 25) + "px") + " 0 " + $(start.papa.sib[n].sons[i]).css("background-color") + " ";
css.code.push(start.papa.sib[n].sons[i].code);
}
}
}
// creating file to download and downloading it
if ($(start.pixel).html() === "x") {
html = "<div class='wrapper' id=\"wrapper\"><div class='pixel' id=\"startPixel\"></div></div><style>.wrapper#wrapper{width: " + wrapperW + "px; height: " + wrapperH + "px; } .pixel#startPixel{width: 25px; height: 25px; box-shadow: " + css.code + ";} </style>";
} else {
html = "<div class='wrapper' id=\"wrapper\"><div class='pixel' id=\"startPixel\"></div></div><style>.wrapper#wrapper{width: " + wrapperW + "px; height: " + wrapperH + "px; } .pixel#startPixel{background-color: " + $(start.pixel).css("background-color") + "; width: 25px; height: 25px; box-shadow: " + css.code + ";} </style>";
}
download = new File([html], "download.txt", { type: "text/plain;charset=utf-8" });
saveAs(download, fileName);
$("#export_alert").css("display", "none");
actualAlert = undefined;
selecting = false;
}
// to download file as html
function downloadHtml() {
fileName = fileNameOriginal + ".html";
downloadFile();
}
// to download file as txt
function downloadTxt() {
fileName = fileNameOriginal + ".txt";
downloadFile();
}
function toggleMargins() {
if (margins === "ON") {
margins = "OFF";
$("#margins span").html("MARGINS " + margins);
$(".row").css("height", "25px");
$(".grid_square").css("border", "none");
dividor = 25;
} else {
margins = "ON";
$(".row").css("height", "27px");
$(".grid_square").css("border", "1px solid #222");
$("#margins span").html("MARGINS " + margins);
dividor = 27;
}
}