var x1=0; var x2=0; var y1=0; var y2=0; var selecting = false; var crop_box_div; var xp1,xp2,yp1,yp2,xdif,ydif; var m; function doSelect(e,startSelect,endSelect) { if (!e) var e = window.event; var tg = (e.target) ? e.target : e.srcElement if(selecting) { if(endSelect) { selecting=false; if(Math.abs(x2-x1) <=5 && Math.abs(y2-y1) <=5) { zoomIn(); } crop_box_div.innerHTML=fileName + "^" + xp1 + ";" +yp1 + ";" + xp2+";" +yp2 ; } else { if(e.clientX<=0) { x2=0; } else if(e.clientX>=imgWidth-1) { x2=imgWidth-1; } else { x2=e.clientX; } if(e.clientY<=0) { y2=0; } else if(e.clientY>=imgHeight-1) { y2=imgHeight-1; } else { y2=e.clientY; } } move_select(); } else { if(tg.id=="crop_box") { if(endSelect) { if(e.ctrlKey) { window.clipboardData.setData("Text",fileName + "^" + xp1 + ";" +yp1 + ";" + xp2+";" +yp2 ); } else { do_crop(); } } } else { if(startSelect) { selecting=true; x1=x2=e.clientX; y1=y2=e.clientY; start_select(); } } } window.status=x1 + " " + x2 + " " + y1 + " " + y2; e.cancelBubble=true; } function start_select() { crop_box_div =document.getElementById("crop_box"); if(crop_box_div.style.display=="none")crop_box_div.style.display=""; crop_box_div.style.left=x1 + "px"; crop_box_div.style.top=y1 + "px"; crop_box_div.style.width=2 + "px"; crop_box_div.style.height=2 + "px"; } function move_select() { if((x2-x1)>0) { crop_box_div.style.width=(x2-x1) + "px"; crop_box_div.style.left=x1 + "px"; } else { crop_box_div.style.width=(x1-x2) + "px"; crop_box_div.style.left=x2 + "px"; } if((y2-y1)>0) { crop_box_div.style.height=(y2-y1) + "px"; crop_box_div.style.top=y1 + "px"; } else { crop_box_div.style.height=(y1-y2) + "px"; crop_box_div.style.top=y2 + "px"; } if((xdiff=x2-x1)>0) { xp1=((x1/imgWidth)*(cropRight-cropLeft))+cropLeft; xp2=((x2/imgWidth)*(cropRight-cropLeft))+cropLeft; } else { xp1=((x2/imgWidth)*(cropRight-cropLeft))+cropLeft; xp2=((x1/imgWidth)*(cropRight-cropLeft))+cropLeft; } if((ydiff=y2-y1)>0) { yp1=((y1/imgHeight)*(cropBottom-cropTop))+cropTop; yp2=((y2/imgHeight)*(cropBottom-cropTop))+cropTop; } else { yp1=((y2/imgHeight)*(cropBottom-cropTop))+cropTop; yp2=((y1/imgHeight)*(cropBottom-cropTop))+cropTop; } m=Math.abs(xdiff)/Math.abs(ydiff); } function zoomIn() { //Just a click - create an imaginary area to edges of image from here - 90% - keep aspect ratio - do x 1.8 - as its almost 2 in case cicking in the center var width = Math.min(x1,imgWidth-1-x1) *1.8; var height = Math.min(y1,imgHeight-1-y1) *1.8; x1=x1-(width/2); x2=x1+width; y1=y1-(height/2); y2=y1+height; //crop_box_div.left=() + "px"; //crop_box_div.top=(y1-(height/2)) + "px"; //crop_box_div.width= width + "px"; //crop_box_div.height= height + "px"; } function do_crop() { crop_box_div.style.display="none"; crop_box_div.left="0px"; crop_box_div.top="0px"; crop_box_div.width="0px"; crop_box_div.height="0px"; selecting=false; cropLeft=xp1; cropRight=xp2; cropTop=yp1; cropBottom=yp2; var sw = window.screen.availWidth; var sh = window.screen.availHeight; if(m>1) { //Landscape //width = 80% of screen width //calculate height imgWidth = sw * 0.7; imgHeight = imgWidth / m; } else if(m<1) { //Portrait //height = 80% of screen hieght //calculate width imgHeight = sh * 0.7; imgWidth = imgHeight * m; } else { //square //width = 80% of screen width //height = 80% of screen hieght imgWidth = sw * 0.7; imgHeight = sh * 0.7; } //var imagePath = imagePathBase + "&width=" + imgWidth + "&height=" + imgHeight + "&crop=" + xp1 + ";" +yp1 + ";" + xp2+";" +yp2; //document.body.background = imagePath; fitWindow(); } function fitWindow() { cropLeft=Math.round(cropLeft); cropTop=Math.round(cropTop); cropRight=Math.round(cropRight); cropBottom=Math.round(cropBottom); if (cropLeft!=cropRight && cropTop!=cropBottom) { var imagePath = imagePathBase + "&width=" + Math.round(imgWidth) + "&height=" + Math.round(imgHeight) + "&crop=" +cropLeft + ";" +cropTop + ";" + cropRight+";" + cropBottom; document.body.background = imagePath; //Todo : do mozaic load? window.resizeTo(imgWidth+winExtraX ,imgHeight+winExtraY); } else { alert("Maximum zoom reached"); } }