-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathcreateBoxes.html
222 lines (192 loc) · 6.13 KB
/
createBoxes.html
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
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="jquery-ui/jquery-ui.structure.min.css">
<link rel="stylesheet" type="text/css" href="jquery-ui/jquery-ui.theme.min.css">
<link rel="stylesheet" type="text/css" href="jquery-ui/jquery-ui.min.css">
<style>
body {
font-family: Helvetica, Arial, Sans-Serif;
font-size:14px;
}
#canvas {
border:1px solid black;
background-repeat:no-repeat;
width:150px;
height:150px;
/*Makes the image larger, to make boxes easier to position*/
background-size:100%;
}
.resizeDiv {
border:1px solid red;
width:40px;
height:40px;
position:relative;
}
#output {
font-family:"Lucida Console", Monaco, monospace;
}
#download {
cursor:pointer;
text-decoration:underline;
color:#00f;
}
</style>
</head>
<body>
<table>
<tr>
<td>
<div id="title"></div>
<p>Center each box over its corresponding character. The box file on the right is downloadable</p>
<div id="canvas">
</div>
</td>
<td>
<div id="output"></div>
<span id="download" style="">Download .box</span>
</td>
</tr>
<tr>
<td>
<input type="file" id="fileElem" accept="image/*" style="display:none" onchange="handleFiles(this.files)" multiple>
<p>
<a href="#" id="fileSelect">Add files</a>
<div id="fileList">
<p>No files selected!</p>
</div>
</td>
<td></td>
</tr>
</table>
<script>
window.URL = window.URL || window.webkitURL;
const img_multiplier = 3;
//Global file list
var fileList;
var fileIndex = 0;
var fileSelect = document.getElementById("fileSelect"),
fileElem = document.getElementById("fileElem"),
fileList = document.getElementById("fileList");
fileSelect.addEventListener("click", function (e) {
if (fileElem) {
fileElem.click();
}
e.preventDefault(); // prevent navigation to "#"
}, false);
function addNewFile() {
if(window.fileList.length <= window.fileIndex) {
$('#canvas').css('background-image','');
return;
}
var img = document.createElement("img");
img.src = window.URL.createObjectURL(window.fileList[window.fileIndex]);
$('#canvas').css('background-image', 'url(' + img.src + ')');
img.onload = function() {
window.URL.revokeObjectURL(this.src);
$('#canvas').css('height', img.height*img_multiplier+'px');
$('#canvas').css('width', img.width*img_multiplier+'px');
}
var info = document.createElement("span");
//info.innerHTML = files[i].name + ": " + files[i].size + " bytes";
addSquares(window.fileList[window.fileIndex].name);
window.fileIndex++;
console.log('WINDOW FILE LIST:')
console.log(window.fileList.length)
const filenames = [];
for(var i = window.fileIndex; i < window.fileList.length; i++) {
filenames.push(window.fileList[i].name);
}
console.log('SETTING INNER HTML:');
console.log(filenames.join('<br/>'))
document.getElementById("fileList").innerHTML = filenames.join('<br/>');
}
function handleFiles(files) {
if (!files.length) {
fileList.innerHTML = "<p>No files selected!</p>";
} else {
window.fileList = files;
window.fileIndex = 0;
addNewFile();
}
}
$('body').on('mouseup', 'div.resizeDiv', function() {
var outputText = "";
$( ".resizeDiv" ).each(function() {
var canvasLeft = $('#canvas').position().left;
var canvasTop = $('#canvas').position().top;
var position = $(this).position();
var top = Math.round((position.top-canvasTop)/img_multiplier);
var left = Math.round((position.left-canvasLeft)/img_multiplier);
var right = Math.round((position.left + $(this).width()-canvasLeft)/img_multiplier);
var bottom = Math.round((position.top + $(this).height()-canvasTop)/img_multiplier);
outputText += $(this).attr("value")+" "+left+" "+top+" "+right+" "+bottom+"<br>";
});
console.log(outputText);
$("#output").html(outputText);
});
function addSquares(filename) {
//Get rid of the old squares
$('.resizeDiv').remove();
var title = "";
for(var i=0; i < filename.length; i++) {
if(filename.charAt(i) == ".") {
$("#title").html(title);
//Clear any box file already generated
$('#content').html();
//Trigger a click on the first square to make sure the box file/content area gets populated
$('#0').trigger('click');
return;
}
//If we haven't hit a "." yet, add characters
title = title+filename.charAt(i);
$("<div/>", {
"class": "resizeDiv",
"id": i,
}).resizable().draggable().css('left', i*50+"px").css('display','inline-block').css('opacity','.7')
.attr('value',filename.charAt(i)).prepend(filename.charAt(i))
.addClass("resizeDiv").attr('id', i)
.appendTo("#canvas");
}
}
$('.resizeDiv')
.resizable({
start: function(e, ui) {
console.log('resizing started');
},
resize: function(e, ui) {
},
stop: function(e, ui) {
console.log('resizing stopped');
}
});
$("#download").click(function(){
console.log("Clicked on download link");
var boxText = $("#output").html();
boxText = boxText.replace(/<br>/g, "\n");
//<a download='FileName' href='data:application/octet-stream;charset=utf-16le;base64,//5mAG8AbwAgAGIAYQByAAoA'>
//<a href="">text file</a>
//var link = "data:text/plain;charset=utf-8;base64,//"+boxText;
var link = "data:application/octet-stream;filename="+$("#title").html()+".box," + encodeURIComponent(boxText);
console.log($("#title").html());
console.log(link);
saveAs(link, $("#title").html()+".box");
});
function saveAs(uri, filename) {
var link = document.createElement('a');
if (typeof link.download === 'string') {
document.body.appendChild(link); // Firefox requires the link to be in the body
link.download = filename;
link.href = uri;
link.click();
document.body.removeChild(link); // remove the link when done
} else {
location.replace(uri);
}
//Add new file to the window
addNewFile();
}
</script>
</body>
</html>