Skip to content

Instantly share code, notes, and snippets.

@BubuInc
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save BubuInc/c44212d95f6cde2d7733 to your computer and use it in GitHub Desktop.

Select an option

Save BubuInc/c44212d95f6cde2d7733 to your computer and use it in GitHub Desktop.
View big images with mouse movement
<?php
$di = new RecursiveDirectoryIterator('Asdf', RecursiveDirectoryIterator::SKIP_DOTS);
$images = [];
foreach(new RecursiveIteratorIterator($di) as $filename){
if(pathinfo($filename, PATHINFO_EXTENSION) == "jpg") {$images[] = str_replace("\\", "/", (string) $filename);}
}
?>
<!DOCTYPE html>
<html>
<head>
<style>body {overflow: hidden;margin:0;background-color: rgb(32, 32, 32);}</style>
</head>
<body>
<img id="image" src="">
<img id="imagePre" src="" style="display:none">
<script>
// --------VARIABLES----------
var images = <?php echo json_encode($images); ?>;
//x(posicion x), y(posicion y), vw(viewport width), vh(viewport height), iw(image width), ih(image height)
//tx(translate x), ty(translate y), px(padding x), py(padding y), nImg(number of image displayed), z(zoom)
var x, y, vw, vh, iw, ih, tx, ty, px, py, nImg = 0, z = 1;
var imgEl = document.getElementById('image');
var imgPreEl = document.getElementById('imagePre');
// ---------FUNCTIONS---------
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
function getViewportSize(){
vw = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
vh = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
}
function moveImage (event) {
if(typeof event != 'undefined'){x = event.clientX; y = event.clientY;}
if(iw < vw) { tx = Math.floor((vw-iw)/2); }
else { tx = Math.floor((vw-iw-2*px)*(x/vw)+px); }
if(ih < vh) { ty = Math.floor((vh-ih)/2); }
else { ty = Math.floor((vh-ih-2*py)*(y/vh)+py); }
imgEl.style.transform = "translate("+tx+"px,"+ty+"px)";
}
function keys (event) {
switch(event.keyCode){
case 32: next(); break; // space
case 39: next(); break; // right
case 37: prev(); break; // left
case 187: zoomIn(); break; // +
case 189: zoomOut(); break; // -
case 48: resetZoom(); break; // 0
}
}
function next () {
imgEl.style.display = 'none';
imgPreEl.style.display = '';
imgEl.id = 'imagePre';
imgPreEl.id = 'image';
imgEl = document.getElementById('image');
imgPreEl = document.getElementById('imagePre');
iw = Math.floor(imgEl.naturalWidth*z);
ih = Math.floor(imgEl.naturalHeight*z);
imgEl.style.width = Math.floor(iw)+'px';
imgEl.style.height = Math.floor(ih)+'px';
px = 80; py = 80; //TODO
nImg++; if(nImg == images.length) nImg = 0;
imgPreEl.src = images[nImg];
}
function prev () {
nImg--; if(nImg == -1) nImg = images.length-1;
imgEl.src = images[nImg];
}
function zoomIn () {
var max = Math.max(imgEl.naturalWidth*z, imgEl.naturalHeight*z);
if (max < 5000) { z += 0.1;}
iw = Math.floor(imgEl.naturalWidth*z);
ih = Math.floor(imgEl.naturalHeight*z);
imgEl.style.width = Math.floor(imgEl.naturalWidth*z)+'px';
imgEl.style.height = Math.floor(imgEl.naturalHeight*z)+'px';
moveImage();
}
function zoomOut () {
var min = Math.min(imgEl.naturalWidth*z, imgEl.naturalHeight*z);
if (min > 100 && z >= 0.2) { z -= 0.1;}
iw = Math.floor(imgEl.naturalWidth*z);
ih = Math.floor(imgEl.naturalHeight*z);
imgEl.style.width = Math.floor(imgEl.naturalWidth*z)+'px';
imgEl.style.height = Math.floor(imgEl.naturalHeight*z)+'px';
moveImage();
}
function resetZoom () {
z = 1;
iw = Math.floor(imgEl.naturalWidth);
ih = Math.floor(imgEl.naturalHeight);
imgEl.style.width = '';
imgEl.style.height = '';
moveImage();
}
function zoomScroll (event) {
if(event.wheelDelta<0) {zoomOut();}
else{ zoomIn();}
}
//---------REG LISTENERS-------
document.onmousemove = moveImage;
document.onkeydown = keys;
document.onmousewheel = zoomScroll;
document.body.onresize = getViewportSize;
document.body.onclick = next;
imgPreEl.onload = function(){imgPreEl.onload = null, next();}
//----------EXECUTIONS---------
images = shuffleArray(images);
getViewportSize();
imgPreEl.src = images[0];
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment