-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavaScript.js
329 lines (319 loc) · 11.6 KB
/
JavaScript.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
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
var upLeftX = 0;
var upLeftY = 0;
var totalWidth = 0;
var totalHeight = 0;
var realWidth = 0;
var realHeight = 0;
var zoom = 0;
var image = null;
var fileName = "";
var annotations = [];
var brightness = 50;
var contrast = 50;
var red = 50;
var green = 50;
var blue = 50;
var inputBox = null;
//Set up the page after the whole DOM is loaded.
window.onload = function() {
//Necessary so that the image doesn't stretch. Maybe add a waiting period here so that it doesn't fire too often.
window.addEventListener("resize", function() {
renderImage();
});
//Handle annotation viewing.
$("#mainCanvas").bind("click", function (e) {
if (e.shiftKey == 1) {
renderTextBox(e);
return;
}
//Refresh the image.
renderImage();
//Click coordinates, relative to upper-left corner of the canvas.
var clickX = e.layerX;
var clickY = e.layerY;
//Do a hit-test for each annotation.
for (var i = 0; i < annotations.length; i++) {
var annotationX = annotations[i].rlX;
var annotationY = annotations[i].rlY;
//If hotspot is currently on canvas (otherwise rlX == rlY == null) and the click is inside the circle:
if (annotations[i].rlX && annotations[i].rlY && (clickX - annotationX) * (clickX - annotationX) + (clickY - annotationY) * (clickY - annotationY) <= 8 * 8) {
var ctx = $("#mainCanvas")[0].getContext('2d');
ctx.font = "14px Arial";
var textWidth = ctx.measureText(annotations[i].note);
//Set upper-left corner of annotation rectangle to the center of the circle.
var rectangleBaseX = annotationX;
var rectangleBaseY = annotationY;
//If the rectangle gets outside the screen, move it until it doesn't.
while (rectangleBaseX + textWidth.width + 20 >= $("#mainCanvas").attr("clientWidth")) {
rectangleBaseX -= 15;
}
while (rectangleBaseY + 40 >= $("#mainCanvas").attr("clientHeight")) {
rectangleBaseY -= 15;
}
//Draw the rectangle in white.
ctx.beginPath();
ctx.rect(rectangleBaseX, rectangleBaseY, textWidth.width + 20, 40);
ctx.fillStyle = "white";
ctx.strokeStyle = "black";
ctx.fill();
ctx.lineWidth = 2;
ctx.stroke();
//Draw the text.
ctx.fillStyle = "black";
ctx.fillText(annotations[i].note, rectangleBaseX + 10, rectangleBaseY + 25);
}
}
});
//Bind the events to sliders.
$("#brightnessrange").bind("change", function (e) {
brightness = parseInt($(this).val());
setImageParameters(brightness, contrast, red, green, blue);
});
$("#contrastrange").bind("change", function (e) {
contrast = parseInt($(this).val());
setImageParameters(brightness, contrast, red, green, blue);
});
$("#redrange").bind("change", function (e) {
red = parseInt($(this).val());
setImageParameters(brightness, contrast, red, green, blue);
});
$("#greenrange").bind("change", function (e) {
green = parseInt($(this).val());
setImageParameters(brightness, contrast, red, green, blue);
});
$("#bluerange").bind("change", function (e) {
blue = parseInt($(this).val());
setImageParameters(brightness, contrast, red, green, blue);
});
$("#zoomrange").bind("change", function(e) {
zoom = parseInt($(this).val());
if (fileName != "") loadPicture();
});
showList();
};
function renderTextBox(e) {
//Refresh the image.
renderImage();
//Click coordinates, relative to upper-left corner of the canvas.
var clickX = e.layerX;
var clickY = e.layerY;
while (clickX + 355 >= $("#mainCanvas").attr("clientWidth")) {
clickX -= 15;
}
while (clickY + 20 >= $("#mainCanvas").attr("clientHeight")) {
clickY -= 15;
}
$("#centerPanel").append('<input type="text" id="commentBox" style="position:absolute;left:' + clickX +
'px;top:' + clickY +
'px;width:350px"/>');
$("#commentBox").bind("keydown", function(event) {
if (event.keyCode == 13) {
//alert((e.layerX + upLeftX) * (realWidth / totalWidth) + ";" + (e.layerY + upLeftY) * (realHeight / totalHeight));
$.ajax({
type: "POST",
url: "Default6.aspx",
data: {
"x": Math.round((e.layerX + upLeftX) * (realWidth / totalWidth)),
"y": Math.round((e.layerY + upLeftY) * (realHeight / totalHeight)),
"note": $("#commentBox").val(),
"fname": fileName
},
success: function() {
$.ajax({
url: "Default5.aspx?fileName=" + fileName,
dataType: "json",
success: function (d) {
annotations = d.annotations;
renderImage();
}
});
}
});
$(this).remove();
return false;
}
});
$("#commentBox").bind("blur", function() {
$(this).remove();
return false;
});
$("#commentBox").focus();
}
function setImageParameters(br, co, r, g, b) {
Caman("#hiddenImage", function() {
this.revert();
this.brightness(br - 50);
this.contrast(co - 50);
this.channels({
red: (r - 50),
green: (g - 50),
blue: (b - 50)
});
this.render(function() {
renderImage();
});
});
}
function setBrightness(b) {
Caman("#hiddenImage", function () {
//Undo the previous changes.
this.revert();
//Set relative brightness (b is <0; 100>, we need <-50; 50>).
this.brightness(b - 50);
//When done, refresh the image.
this.render(function () {
renderImage();
});
});
}
function setContrast(c) {
//See setBrightness.
Caman("#hiddenImage", function () {
this.revert();
this.contrast(c - 50);
this.render(function () {
renderImage();
});
});
}
function showList() {
//Grab the list of files from the server.
$.ajax({
url: "Default3.aspx",
dataType: "json",
success: function(data) {
var fileArray = data.files;
//Wipe out the previous contents of the list (TODO necessary?)
$("#linkList").empty();
//For each file, add a link to show it.
for (var index = 0; index < fileArray.length; index++) {
$("#linkList").append("<a href=\"#\" onclick=\"fileName = \'" + fileArray[index].name + "\'; upLeftX = upLeftY = zoom = 0; loadPicture(); return false;\">" + fileArray[index].name + "</a>");
$("#linkList").append("<br \>");
}
}
});
}
function renderImage() {
$("#mainCanvas")[0].getContext('2d').restore();
//Set the width/height attributes of the canvas to actual width/height of the rendered element.
$("#mainCanvas").attr('width', $("#mainCanvas").attr("clientWidth"));
$("#mainCanvas").attr('height', $("#mainCanvas").attr("clientHeight"));
//Get the full image's dimensions.
totalWidth = $("#hiddenImage").attr("width");
totalHeight = $("#hiddenImage").attr("height");
var ctx = $("#mainCanvas")[0].getContext('2d');
//Clip the image to canvas dimensions and draw it on canvas.
ctx.drawImage($("#hiddenImage")[0], upLeftX, upLeftY, $("#mainCanvas").attr("clientWidth"), $("#mainCanvas").attr("clientHeight"), 0, 0, $("#mainCanvas").attr("clientWidth"), $("#mainCanvas").attr("clientHeight"));
//Draw the annotation hotspots. For each annotation:
for (var i = 0; i < annotations.length; i++) {
//Calculate where it goes on the resized picture.
var x = (totalWidth * annotations[i].x) / realWidth;
var y = (totalHeight * annotations[i].y) / realHeight;
//If it's visible, then:
if (x >= upLeftX && y >= upLeftY && x < upLeftX + $("#mainCanvas").attr("clientWidth") && y < upLeftY + $("#mainCanvas").attr("clientHeight")) {
//Get the coordinates relative to the canvas' upper-left corner.
annotations[i].rlX = x - upLeftX;
annotations[i].rlY = y - upLeftY;
//Draw the circle.
ctx.beginPath();
ctx.arc(annotations[i].rlX, annotations[i].rlY, 8, 0, 2 * Math.PI, false);
ctx.fillStyle = "red";
ctx.fill();
//If it's not visible, then mark it as such.
} else {
annotations[i].rlX = null;
annotations[i].rlY = null;
}
}
}
function loadPicture() {
//Because fuck CamanJS, we need to remove the Caman-created canvas and add the actual img element.
$('#hiddenImage').remove();
$('#hiddenPictures').append("<img id='hiddenImage' />");
$("#hiddenImage").error(function () { alert("Error!"); });
//Set the picture-fully-loaded handler.
$('#hiddenImage').load(function () {
//Get real image width/height for annotations.
$.ajax({
url: "Default4.aspx?fileName=" + fileName,
dataType: "json",
//async: false,
success: function (data) {
realWidth = data.x;
realHeight = data.y;
//Get the annotations.
$.ajax({
url: "Default5.aspx?fileName=" + fileName,
dataType: "json",
success: function(d) {
annotations = d.annotations;
renderImage();
}
});
}
});
//Re-set image parameters.
setImageParameters(brightness, contrast, red, green, blue);
});
//Load the picture.
$('#hiddenImage').attr("src", "Default2.aspx?viewPortX=" + parseInt($("#mainCanvas")[0].clientWidth) +
"&viewPortY=" + parseInt($("#mainCanvas")[0].clientHeight) +
"&zoomLevel=" + zoom +
"&fileName=" + fileName);
}
function moveVPRight() {
if (upLeftX + $("#mainCanvas")[0].clientWidth + 40 <= totalWidth) {
upLeftX += 40;
renderImage();
}
else if (upLeftX + $("#mainCanvas")[0].clientWidth <= totalWidth) {
upLeftX = totalWidth - $("#mainCanvas")[0].width;
renderImage();
}
}
function moveVPLeft() {
//Move by 40 pixels if possible,
if (upLeftX - 40 >= 0) {
upLeftX -= 40;
renderImage();
}
//otherwise move as far as you can.
else if (upLeftX > 0) {
upLeftX = 0;
renderImage();
}
}
function moveVPDown() {
if (upLeftY + $("#mainCanvas")[0].clientHeight + 40 <= totalHeight) {
upLeftY += 40;
renderImage();
}
else if (upLeftY + $("#mainCanvas")[0].clientHeight <= totalHeight) {
upLeftY = totalHeight - $("#mainCanvas")[0].height;
renderImage();
}
}
function moveVPUp() {
if (upLeftY - 40 >= 0) {
upLeftY -= 40;
renderImage();
}
else if (upLeftY > 0) {
upLeftY = 0;
renderImage();
}
}
function zoomIn() {
if (zoom < 10) {
zoom++;
$("#zoomrange").val(zoom);
if (fileName != "") loadPicture();
}
}
function zoomOut() {
if (zoom > 0) {
zoom--;
$("#zoomrange").val(zoom);
if (fileName != "") loadPicture();
}
}