-
Notifications
You must be signed in to change notification settings - Fork 1
/
view.html
201 lines (165 loc) · 4.64 KB
/
view.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
<body>
<style>
.flexContainer {
display: flex;
width: 100%;
height: 100%;
}
.imgContainer {
position: relative;
width: fit-content;
margin: auto;
}
#overlay {
position: absolute;
top: 0;
left: 0;
}
.rightDiv {
display: flex;
flex-direction: column;
width: 50%;
}
.buttonDiv {
display: flex;
align-items: center;
justify-content: center;
}
#jisho {
height: 100%;
}
button {
height: 60px;
width: 100%;
}
</style>
<div class="flexContainer">
<div class="imgContainer">
<img id="image" height=100% ></img>
<canvas id="overlay" ></canvas>
</div>
<div class=rightDiv>
<iframe id="jisho" src="about:blank"></iframe>
<div class=buttonDiv>
<button onclick="prev();">prev</button>
<button onclick="next();">next</button>
</div>
</div>
</div>
<script>
class Polygon {
constructor(points) {
this.points = points;
}
drawTo(ctx) {
let scaleX = img.width / img.naturalWidth;
let scaleY = img.height / img.naturalHeight;
ctx.beginPath();
let start = this.points[0];
ctx.moveTo(start.x*scaleX, start.y*scaleY)
for (let point of this.points.slice(1)) {
ctx.lineTo(point.x*scaleX, point.y*scaleY);
}
ctx.closePath();
ctx.stroke();
}
// based on https://github.com/substack/point-in-polygon
pointIsInside(point) {
// ray-casting algorithm based on
// http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
let inside = false;
for (let i = 0, j = this.points.length - 1; i < this.points.length; j = i++) {
let xi = this.points[i].x, yi = this.points[i].y;
let xj = this.points[j].x, yj = this.points[j].y;
let intersect = ((yi > point.y) != (yj > point.y))
&& (point.x < (xj - xi) * (point.y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}
return inside;
}
perimeter() {
let res = 0;
for (let i = 1; i < this.points.length; i++) {
let point = this.points[i];
let prev = this.points[i-1];
res += Math.sqrt((point.x-prev.x)**2 + (point.y-prev.y)**2);
}
return res;
}
}
let annotations = [];
async function annotate() {
canvas.width = img.width;
canvas.height = img.height;
annotations = (await (await fetch(`/annotation?path=${path}`)).json())
.map(o => ({
text: o.text,
boundingBox: new Polygon(o.boundingBox)
}));
}
function drawBoundingBoxes() {
canvas.width = img.width;
canvas.height = img.height;
for (let annotation of annotations) {
annotation.boundingBox.drawTo(ctx);
}
}
function clearBoundingBoxes() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
let down_pos = {x: 0, y: 0};
function findBoundingBoxes(point) {
let res = [];
for (let annotation of annotations) {
if (annotation.boundingBox.pointIsInside(point)) {
res.push(annotation)
}
}
return res;
}
function smallestBoundingBox(annotations) {
let min_index = 0;
let min_value = annotations[0].boundingBox.perimeter();
for (let i = 1; i < annotations.length; i++) {
let annotation = annotations[i];
let value = annotation.boundingBox.perimeter();
if (value < min_value) {
min_index = i;
min_value = value;
}
}
return annotations[min_index];
}
function annotationClick(ev) {
let scaleX = img.naturalWidth / img.width;
let scaleY = img.naturalHeight / img.height;
let annotations = findBoundingBoxes({x: ev.offsetX*scaleX, y: ev.offsetY*scaleY});
if (annotations.length == 0) throw new Error(`no bounding box found`);
jishoFrame.src = `https://jisho.org/search/${encodeURIComponent(smallestBoundingBox(annotations).text)}`;
}
let canvas = document.getElementById('overlay');
let ctx = canvas.getContext('2d');
let img = document.getElementById('image');
let jishoFrame = document.getElementById('jisho');
let params = new URLSearchParams(location.search);
let path = params.get('path');
canvas.onclick = annotationClick;
img.onload = setTimeout(annotate, 50);
img.src = `/img?path=${path}`;
window.onkeydown = ev => {
if (ev.code != "AltLeft") return;
drawBoundingBoxes();
}
window.onkeyup = ev => {
if (ev.code != "AltLeft") return;
clearBoundingBoxes();
}
async function next() {
const nextFile = await (await fetch(`/nextfile?path=${path}`)).text();
location.href = `/view?path=${nextFile}`;
}
async function prev() {
const prevFile = await (await fetch(`/prevfile?path=${path}`)).text();
location.href = `/view?path=${prevFile}`;
}
</script>