forked from lovasoa/dezoomify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzoommanager.js
More file actions
242 lines (208 loc) · 6.98 KB
/
zoommanager.js
File metadata and controls
242 lines (208 loc) · 6.98 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
var UI = {};
UI.canvas = document.getElementById("rendering-canvas");
UI.dezoomers = document.getElementById("dezoomers");
/** Adjusts the size of the image, so that is fits page width or page height
**/
UI.changeSize = function () {
var width = UI.canvas.width, height = UI.canvas.height;
console.log("changeSize");
switch (this.fit) {
case "width":
this.fit = "height";
UI.canvas.style.height = window.innerHeight + "px";
UI.canvas.style.width = window.innerHeight / height * width + "px";
break;
case "height":
this.fit = "none";
UI.canvas.style.width = width + "px";
UI.canvas.style.height = height + "px";
break;
default:
this.fit = "width";
UI.canvas.style.width = window.innerWidth + "px";
UI.canvas.style.height = window.innerWidth / width * height + "px";
}
};
/** Sets the width and height of the canvas
**/
UI.setupRendering = function (data) {
document.getElementById("urlform").style.display = "none";
UI.canvas.width = data.width;
UI.canvas.height = data.height;
UI.canvas.onclick = UI.changeSize;
UI.ctx = UI.canvas.getContext("2d");
UI.changeSize();
console.log("Image size: " + data.width + "x" + data.height);
};
UI.drawTile = function(tileImg, x, y) {
UI.ctx.drawImage(tileImg, x, y);
};
UI.error = function(errmsg) {
var elem = document.getElementById("error");
if (errmsg) elem.innerHTML = errmsg;
elem.style.display = "block";
};
UI.reset = function() {
document.getElementById("error").style.display = "none";
document.getElementById("urlform").style.display = "block";
UI.canvas.width = UI.canvas.height = 0;
};
UI.updateProgress = function (percent, text) {
document.getElementById("percent").innerHTML = text + ' (' + parseInt(percent) + ") %";
document.getElementById("progressbar").style.width = percent + "%";
};
UI.loadEnd = function() {
document.getElementById("status").style.display = "none";
};
UI.addDezoomer = function(dezoomer) {
var label = document.createElement("label")
var input = document.createElement("input");
input.type = "radio"
input.name = "dezoomer";
input.id = "dezoomer-" + dezoomer.name;
input.onclick = function() {
ZoomManager.setDezoomer(dezoomer);
}
label.appendChild(input);
label.appendChild(document.createTextNode(dezoomer.name));
UI.dezoomers.appendChild(label);
};
UI.setDezoomer = function(dezoomerName) {
document.getElementById("dezoomer-"+dezoomerName).checked = true;
}
var ZoomManager = {};
ZoomManager.error = function (errmsg) {
console.error(errmsg);
UI.error(errmsg);
};
ZoomManager.updateProgress = UI.updateProgress;
ZoomManager.status = {
"loaded" : 0,
"totalTiles" : 1
};
ZoomManager.startTimer = function () {
var timer = setInterval(function () {
/*Update the User Interface each 500ms, and not in addTile, because it would
slow down the all process to update the UI too often.*/
var loaded = ZoomManager.status.loaded, total = ZoomManager.status.totalTiles;
ZoomManager.updateProgress(100 * loaded / total, "Loading the tiles...");
if (loaded == total) {
clearInterval(timer);
ZoomManager.loadEnd();
}
}, 500);
return timer;
};
ZoomManager.loadEnd = UI.loadEnd;
ZoomManager.readyToRender = function(data) {
data.nbrTilesX = data.nbrTilesX || Math.ceil(data.width / data.tileSize);
data.nbrTilesY = data.nbrTilesY|| Math.ceil(data.height / data.tileSize);
data.totalTiles = data.totalTiles || data.nbrTilesX*data.nbrTilesY;
ZoomManager.status.totalTiles = data.totalTiles;
ZoomManager.data = data;
UI.setupRendering(data);
ZoomManager.updateProgress(0, "Preparing tiles load...");
ZoomManager.startTimer();
var render = ZoomManager.dezoomer.render || ZoomManager.defaultRender;
setTimeout(render, 1, data); //Give time to refresh the UI, in case render would take a long time
};
ZoomManager.defaultRender = function (data) {
var zoom = data.maxZoomLevel || ZoomManager.findMaxZoom(data);
var x=0, y=0;
function nextTile() {
var url = ZoomManager.dezoomer.getTileURL(x,y,zoom,data);
if (data.origin) url = ZoomManager.resolveRelative(url, data.origin);
ZoomManager.addTile(url, x*data.tileSize, y*data.tileSize);
x++;
if (x >= data.nbrTilesX) {x = 0; y++;}
if (y < data.nbrTilesY) requestAnimationFrame(nextTile);
}
nextTile();
};
ZoomManager.addTile = function (url, x, y) {
//Demande une partie de l'image au serveur, et l'affiche lorsqu'elle est reçue
var img = document.createElement("img");
img.onload = function () {
UI.drawTile(img, x, y);
ZoomManager.status.loaded ++;
};
img.src = url;
};
ZoomManager.open = function(url) {
UI.reset();
if (url.indexOf("http") !== 0) {
return ZoomManager.error("You must provide a valid URL.");
}
if (typeof ZoomManager.dezoomer.findFile === "function") {
ZoomManager.dezoomer.findFile(url, function foundFile(filePath) {
ZoomManager.dezoomer.open(ZoomManager.resolveRelative(filePath, url));
});
} else {
ZoomManager.dezoomer.open(url);
}
};
/**
* Call callback with the contents of the page at url
*/
ZoomManager.getFile = function (url, type, callback) {
var PHPSCRIPT = "proxy.php";
var xhr = new XMLHttpRequest();
var codedurl = encodeURIComponent(url);
xhr.open("GET", PHPSCRIPT + "?url=" + codedurl, true);
xhr.onloadstart = function () {
ZoomManager.updateProgress(0, "Sent a request in order to get informations about the image...");
};
xhr.onerror = function (e) {
ZoomManager.error("Unable to connect to the proxy server to get the required informations.");
console.log("XHR error", e);
};
xhr.onloadend = function () {
callback(xhr.response, xhr);
};
if (type === "xml") {
xhr.responseType = "document";
xhr.overrideMimeType("text/xml");
} else {
xhr.responseType = "text";
xhr.overrideMimeType("text/plain");
}
xhr.send(null);
};
/**
* Return the absolute path, given a relative path and a base
*/
ZoomManager.resolveRelative = function resolveRelative(path, base) {
// absolute URL
if (path.match(/\w*:\/\//)) {
return path;
}
// Upper directory
if (path.startsWith("../")) {
return resolveRelative(path.slice(3), base.replace(/\/[^\/]*$/, ''));
}
// Relative to the root
if (path.startsWith('/')) {
var match = base.match(/(\w*:\/\/)?[^\/]*\//) || [base];
return match[0] + path.slice(1);
}
//relative to the current directory
return base.replace(/\/[^\/]*$/, "") + '/' + path;
};
/** Returns the maximum zoom level, knowing the image size, the tile size, and the multiplying factor between two consecutive zoom levels
**/
ZoomManager.findMaxZoom = function (data) {
//For all zoom levels:
//size / zoomFactor^(maxZoomLevel - zoomlevel) = numTilesAtThisZoomLevel * tileSize
//For the baseZoomLevel (0 for zoomify), numTilesAtThisZoomLevel=1
var size = Math.max(data.width, data.height);
return Math.ceil(Math.log(size/data.tileSize) / Math.log(data.zoomFactor)) + (data.baseZoomLevel||0);
};
ZoomManager.dezoomersList = {};
ZoomManager.addDezoomer = function(dezoomer) {
ZoomManager.dezoomersList[dezoomer.name] = dezoomer;
UI.addDezoomer(dezoomer);
}
ZoomManager.setDezoomer = function(dezoomer) {
ZoomManager.dezoomer = dezoomer;
UI.setDezoomer(dezoomer.name);
}