|
function dezoomify() { |
|
//Fetches some informations about the image, and then draw it |
|
var url = encodeURIComponent(document.getElementById("url").value); |
|
var xhr = new XMLHttpRequest(); |
|
|
|
xhr.onreadystatechange = function () { |
|
if (xhr.readyState == 4) { |
|
if (xhr.status == 200) { |
|
var xml = xhr.responseXML; |
|
path = xml.firstChild.getAttribute("path"); |
|
|
|
var infos = xml.getElementsByTagName("IMAGE_PROPERTIES")[0]; |
|
if (!infos) { |
|
document.getElementById("error").style.display = "block"; |
|
console.log(xhr.responseText); |
|
} |
|
width = parseInt(infos.getAttribute("WIDTH")); |
|
height = parseInt(infos.getAttribute("HEIGHT")); |
|
tileSize = parseInt(infos.getAttribute("TILESIZE")); |
|
numTiles = parseInt(infos.getAttribute("NUMTILES")); |
|
|
|
remove(document.forms[0]); |
|
drawImage(); |
|
} else { |
|
error = document.getElementById("error"); |
|
error.innerHTML = "Error : Unable to join the server. Error code " + xhr.status; |
|
error.style.display = "block"; |
|
} |
|
} |
|
document.getElementById("progressbar").style.width = 25 * xhr.readyState + "%"; |
|
}; |
|
|
|
xhr.open("GET", "dezoomify.php?url=" + url, true); |
|
xhr.send(null); |
|
} |
|
|
|
function remove(el) { |
|
el.parentNode.removeChild(el); |
|
} |
|
|
|
function addTile(url, x, y) { |
|
//Demande une partie de l'image au serveur, et l'affiche lorsqu'elle est reçue |
|
var i = document.createElement("img"); |
|
i.onload = function () { |
|
ctx.drawImage(i, x, y); |
|
loaded++; |
|
} |
|
i.src = url; |
|
} |
|
|
|
|
|
function loadEnd() { |
|
//Fonction appelée lorsque l'image est entièrement chargée |
|
clearInterval(timer); |
|
var status = document.getElementById("status"); |
|
status.parentNode.removeChild(status); |
|
} |
|
|
|
function changeSize() { |
|
//Ajuste la taille de l'image, pour qu'elle soit en taille de base, ou ajustée à la largeur ou la hauteur de la page |
|
if (typeof(fit) == "undefined") { |
|
//Fit page width |
|
fit = "width"; |
|
canvas.style.width = window.innerWidth + "px"; |
|
canvas.style.height = window.innerWidth / width * height + "px"; |
|
} else if (fit == "height") { |
|
//Max zoom |
|
fit = undefined; |
|
canvas.style.width = width + "px"; |
|
canvas.style.height = height + "px"; |
|
} else if (fit == "width") { |
|
//Fit page height |
|
fit = "height"; |
|
canvas.style.height = window.innerHeight + "px"; |
|
canvas.style.width = window.innerHeight / height * width + "px"; |
|
} |
|
} |
|
|
|
function findZoom(size) { |
|
//Fonction de BG |
|
return Math.floor(Math.log(size) / Math.log(2) - 7); |
|
} |
|
|
|
function drawImage() { |
|
//Tout le code. On devrait peut-être le couper en plusieurs fonctions |
|
canvas = document.createElement("canvas"); |
|
ctx = canvas.getContext("2d"); |
|
|
|
canvas.width = width; |
|
canvas.height = height; |
|
|
|
canvas.onclick = changeSize; |
|
document.getElementById("dezoomifiedImage").appendChild(canvas); |
|
|
|
changeSize("width"); |
|
console.log(width, height) |
|
console.log((width > height) ? findZoom(width) : findZoom(height)) |
|
zoom = (width > height) ? findZoom(width) : findZoom(height); |
|
|
|
var nbrTilesX = Math.ceil(width / tileSize); |
|
var nbrTilesY = Math.ceil(height / tileSize); |
|
|
|
loaded = 0; |
|
totalTiles = nbrTilesX * nbrTilesY; //Total number of tiles to load |
|
|
|
var skippedTiles = numTiles - totalTiles; |
|
|
|
var tileGroup = Math.floor(skippedTiles / 256); |
|
var tileGroupCounter = skippedTiles % 256; |
|
|
|
var x, y; |
|
for (y = 0; y < nbrTilesY; y++) { |
|
for (x = 0; x < nbrTilesX; x++) { |
|
|
|
if (tileGroupCounter >= 256) { |
|
tileGroup++; |
|
tileGroupCounter = 0; |
|
} |
|
|
|
tileGroupCounter++; |
|
|
|
var url = path + "/TileGroup" + tileGroup + "/" + zoom + "-" + x + "-" + y + ".jpg"; |
|
addTile(url, x * tileSize, y * tileSize); |
|
} |
|
} |
|
|
|
timer = setInterval(function () { |
|
document.getElementById("percent").innerHTML = Math.floor(100 * loaded / totalTiles) + " %"; |
|
document.getElementById("progressbar").style.width = 100 * loaded / totalTiles + "%"; |
|
|
|
if (loaded == totalTiles) { |
|
loadEnd(); |
|
} |
|
}, 200); |
|
} |