-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
187 lines (163 loc) · 5.49 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<style>
body {
background-color: lightgrey;
}
div.container {
display: flex;
justify-content: center;
flex-direction: column;
}
canvas#my-canvas {
width: 100%;
}
#div-canvas {
width: 90vw;
height: 400px;
margin: auto;
overflow: scroll;
border: 1px solid black;
overflow-x: hidden;
}
div.button-container {
align-self: center;
}
button{
width: 250px;
margin: 20px;
font-size: 24px;
background-color: steelblue;
color: white;
}
div.analysis-container {
background-color: white;
margin: 40px;
padding: 20px;
}
</style>
<body>
<div class="container">
<div class="button-container">
<button id="step-btn">Step</button>
<button id="auto-btn">Auto Step</button>
<button id="clear-btn">Clear</button>
</div>
<div id="div-canvas">
<canvas id="my-canvas"></canvas>
</div>
</div>
<div id="charts">
</div>
<div class="analysis-container">
<h2>Analysis</h2>
<div id="analysis">
</div>
</div>
<script>
const myCanvas = document.getElementById('my-canvas');
const context = myCanvas.getContext('2d',{willReadFrequently: true});
function analysisLog(text) {
let theDiv = document.getElementById("analysis");
let pNode = document.createElement('p');
let result = text.replace(/\n/g, "<br/>");
pNode.innerHTML = result;
theDiv.appendChild(pNode);
}
</script>
<script src="./js.js?v=0.34"></script>
<script>
function makeChart() {
}
function getCountDictionary(array) {
var countDictionary = {};
for (var i = 0; i < array.length; i++) {
var element = array[i];
if (countDictionary[element]) {
countDictionary[element]++;
} else {
countDictionary[element] = 1;
}
}
return countDictionary;
}
const img = new Image();
img.src = './sample-page.png';
img.onload = () => {
// Put the image on to a virtual canvas matching the original size.
// This is to work with the canvas data at maximum resolution while still
// being able to zoom out the canvas for larger image widths.
const canvas2 = document.createElement('canvas');
const context2 = canvas2.getContext('2d');
canvas2.width = img.width;
canvas2.height = img.height;
context2.drawImage(img, 0, 0, img.width, img.height );
const myData = context2.getImageData(0, 0, img.width, img.height);
const div_canvas = document.getElementById("div-canvas");
const canvas_scale = img.width / div_canvas.clientWidth;
const canvas_height = img.height / canvas_scale;
const canvas_width = img.width / canvas_scale;
myCanvas.height = canvas_height; //img.height;
myCanvas.width = canvas_width; //img.width;
context.drawImage(img, 0, 0, canvas_width, canvas_height );
// Read the pixel data of the canvas
//const imageData1 = context.getImageData(0, 0, img.width, img.height);
const data = myData.data;
let raster = data_to_raster(data, img.width, img.height);
let boxes = find_boxes(raster);
let row_sorted = get_row_sorted(boxes);
const stepbtn = document.getElementById("step-btn");
const autobtn = document.getElementById("auto-btn");
const clearbtn = document.getElementById("clear-btn");
let timer = null;
let stepboxes = new StepBoxes(context, img, row_sorted, canvas_scale);
clearbtn.addEventListener("click", function() {
context.drawImage(img, 0, 0, canvas_width, canvas_height );
stepboxes = new StepBoxes(context, img, row_sorted, canvas_scale);
if (timer) {
clearInterval(timer);
autobtn.textContent = "Auto Step";
timer = null;
}
});
stepbtn.addEventListener("click", function() {
stepboxes.step();
});
autobtn.addEventListener("click", function() {
if (timer) {
clearInterval(timer);
autobtn.textContent = "Auto Step";
timer = null;
} else {
timer = setInterval(()=>{
let result = stepboxes.step()
if (stepboxes.done) {
clearInterval(timer);
autobtn.textContent = "Auto Step";
timer = null;
console.log("Cancelling timer");
} else if (Array.isArray(result) && result.length > 0) {
let sortedElements = result.flat().filter( element => (element.spacingRight != null && element.spacingRight > 0) ).map( element => element.spacingRight ).toSorted((a,b) => a - b);
console.log(sortedElements);
let middleElementIdx = Math.trunc(sortedElements.length / 2);
let UpperQuartileIdx = Math.trunc(3*sortedElements.length / 4);
let medianElementSpacing = sortedElements[middleElementIdx];
let upperQuartileElementSpacing = sortedElements[UpperQuartileIdx];
let counts = getCountDictionary(sortedElements);
console.log(counts);
console.log(`Median spacing = ${medianElementSpacing}`);
console.log(`Upper quartile spacing = ${upperQuartileElementSpacing}`);
let text = `Upper quartile \nspacing = ${upperQuartileElementSpacing}`;
}
}, 50);
autobtn.textContent = "Stop Auto Step";
}
});
};
</script>
</body>
</html>