-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.js
More file actions
375 lines (317 loc) · 9.57 KB
/
function.js
File metadata and controls
375 lines (317 loc) · 9.57 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
var text_left = [];
var text_top = [];
var text_width = [];
var text_height = [];
var formula_left = [];
var formula_top = [];
var formula_width = [];
var formula_height = [];
var image_left = [];
var image_top = [];
var image_width = [];
var image_height = [];
var image_data;
var cur_mode = 0;
var stage;
var text_layer;
var formula_layer;
var image_layer;
var cur_graph;
// calculate where the canvas is on the window
// (used to help calculate mouseX/mouseY)
var offsetX = 0;
var offsetY = 0;
var scrollX = 0;
var scrollY = 0;
// this flage is true when the user is dragging the mouse
var isDown = false;
// these vars will hold the starting mouse position
var startX;
var startY;
var width;
var height;
function handleMouseDown(e) {
// save the starting x/y of the rectangle
startX = e.evt.offsetX;
startY = e.evt.offsetY;
//console.log(" startX = " + startX);
//console.log(" startY = " + startY);
// set a flag indicating the drag has begun
isDown = true;
if (cur_mode == 1) {
text_left.push(startX);
text_top.push(startY);
var rect = new Konva.Rect({
x: startX,
y: startY,
width: 0,
height: 0,
stroke:'rgb(76, 175, 80)',
opacity:0.8,
strokeWidth: 4
});
cur_graph = rect;
text_layer.add(rect);
text_layer.draw();
}
if (cur_mode == 2) {
formula_left.push(startX);
formula_top.push(startY);
var rect = new Konva.Rect({
x: startX,
y: startY,
width: 0,
height: 0,
stroke:'#4c9baf',
opacity:0.8,
strokeWidth: 4
});
cur_graph = rect;
formula_layer.add(rect);
formula_layer.draw();
}
if (cur_mode == 3) {
image_left.push(startX);
image_top.push(startY);
var rect = new Konva.Rect({
x: startX,
y: startY,
width: 0,
height: 0,
stroke:'#7a60d8',
opacity:0.8,
strokeWidth: 4
});
cur_graph = rect;
image_layer.add(rect);
image_layer.draw();
}
}
function handleMouseUp(e) {
// the drag is over, clear the dragging flag
isDown = false;
if (cur_mode == 1) {
text_width.push(width);
text_height.push(height);
}
if (cur_mode == 2) {
formula_width.push(width);
formula_height.push(height);
}
if (cur_mode == 3) {
image_width.push(width);
image_height.push(height);
}
}
function handleMouseOut(e) {
// the drag is over, clear the dragging flag
isDown = false;
}
function handleMouseMove(e) {
// if we're not dragging, just return
if (!isDown) {
return;
}
width = e.evt.offsetX - startX;
height = e.evt.offsetY - startY;
// console.log(" width = " + width);
// console.log(" height = " + height);
// Put your mousemove stuff here
// if (cur_mode == 1) {
// text_width = width;
// text_height = height;
// }
// if (cur_mode == 2) {
// formula_width = width;
// formula_height = height;
// }
// if (cur_mode == 3) {
// image_width = width;
// image_height = height;
// }
cur_graph.setAttrs({
width: width,
height: height
});
}
function onDrawTextRect() {
cur_mode = 1; // draw text
console.log(" cur_mode = " + cur_mode);
if (stage) {
text_layer = new Konva.Layer();
stage.add(text_layer);
}
}
function onDrawFormulaRect() {
cur_mode = 2; // draw formula
console.log(" cur_mode = " + cur_mode);
if (stage) {
formula_layer = new Konva.Layer();
stage.add(formula_layer);
}
}
function onDrawImageRect() {
cur_mode = 3; // draw image
console.log(" cur_mode = " + cur_mode);
if (stage) {
image_layer = new Konva.Layer();
stage.add(image_layer);
}
}
function initKonvaStages()
{
stage.on("mousedown", handleMouseDown);
stage.on("mouseup", handleMouseUp);
//stage.on("mouseout", handleMouseOut);
stage.on("mousemove", handleMouseMove);
}
function imageLoaded() {
afiles = document.getElementById('imgupload').files;
cur_file = afiles[0]; // get the first file
console.log("cur_file : ");
console.log(cur_file.name);
var reads = new FileReader();
reads.onload = function(e) {
var img = document.createElement("img");
img.onload = function(event) {
var canvas = document.getElementById('image_canvas');
var ctx = canvas.getContext("2d");
var tw = canvas.getBoundingClientRect().width;
var th = canvas.getBoundingClientRect().height;
console.log("tw width", tw);
console.log("th height", th);
var MAX_WIDTH = tw;
var MAX_HEIGHT = th;
ctx.drawImage(img, 0, 0);
var width = img.width;
var height = img.height;
console.log("origin width", width);
console.log("origin height", height);
if (width > height) {
if (width > MAX_WIDTH) {
height *= MAX_WIDTH / width;
width = MAX_WIDTH;
}
} else {
if (height > MAX_HEIGHT) {
width *= MAX_HEIGHT / height;
height = MAX_HEIGHT;
}
}
console.log("dst width", width);
console.log("dst height", height);
canvas.width = width;
canvas.height = height;
var ctx2 = canvas.getContext("2d");
ctx2.drawImage(img, 0, 0, width, height);
var dataurl = canvas.toDataURL("image/png");
image_data = dataurl;
markdown = "[img1]:" + dataurl;
document.getElementById('markdown_area').value = markdown;
stage = new Konva.Stage({
container: 'canvas_div',
width: width,
height: height,
});
var layer = new Konva.Layer();
stage.add(layer);
var konva_image = new Konva.Image({
x: 0,
y: 0,
image: canvas,
width: width,
height: height
});
layer.add(konva_image);
layer.batchDraw();
offsetX = canvas.offsetLeft;
offsetY = canvas.offsetTop;
scrollX = canvas.scrollLeft;
scrollY = canvas.scrollTop;
console.log(" offsetX = " + offsetX);
console.log(" offsetY = " + offsetY);
console.log(" scrollX = " + scrollX);
console.log(" scrollY = " + scrollY);
initKonvaStages();
};
img.src = this.result;
};
reads.readAsDataURL(cur_file);
}
function uploadImage(){
document.getElementById("imgupload").click();
}
function onConvert() {
var obj = new Object();
obj.formula = new Object();
obj.formula.rects = [];
var len = formula_left.length;
for (let i = 0; i < len; i++) {
var rect = [formula_left[i],formula_top[i],formula_width[i],formula_height[i]];
obj.formula.rects.push(rect);
}
obj.text = new Object();
obj.text.rects = [];
var lent = text_left.length;
for (let i = 0; i < lent; i++) {
var rect = [text_left[i],text_top[i],text_width[i],text_height[i]];
obj.text.rects.push(rect);
}
obj.image = new Object();
obj.image.rects = [];
var leni = image_left.length;
for (let i = 0; i < leni; i++) {
var rect = [image_left[i],image_top[i],image_width[i],image_height[i]];
obj.image.rects.push(rect);
}
var jsonString= JSON.stringify(obj);
console.log("jsonString: ", jsonString);
url = 'http://127.0.0.1:5000/convert';
console.log("request : " + url);
let headers = new Headers();
// headers.append('Content-Type', 'application/json');
// headers.append('Accept', 'application/json');
headers.append('Origin','http://127.0.0.1:5000');
// headers.append('Access-Control-Allow-Origin','*');
headers.append('Access-Control-Request-Headers','access-control-allow-origin,content-type');
let bytes = atob(image_data.split(',')[1])
let ab = new ArrayBuffer(bytes.length)
let ia = new Uint8Array(ab)
for (let i = 0; i < bytes.length; i++) {
ia[i] = bytes.charCodeAt(i)
}
let blob = new Blob([ab], {type: 'image/png'})
let formData = new FormData();
formData.append("rect",jsonString);
formData.append("image",blob, new Date().getTime() + '.png');
fetch(url, {
mode: 'cors',
credentials: 'omit',
method: 'POST',
headers: headers,
body: formData
})
.then( async (response) => {
// get json response here
let data = await response.json();
reco = data["recognize"];
if(response.status === 200){
console.log("response ok : " + reco);
document.getElementById('markdown_area').value = reco;
}else{
console.log("response failed : " + response.status)
}
})
.catch((err) => {
console.log(err);
});
}
function onCopy() {
var input = document.createElement('textarea');
input.innerHTML = document.getElementById('markdown_area').value;
document.body.appendChild(input);
input.select();
var result = document.execCommand('copy');
document.body.removeChild(input);
return result;
}