<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<!DOCTYPE html><html lang='en' class=''>
<head><script src='//production-assets.codepen.io/assets/editor/live/console_runner-079c09a0e3b9ff743e39ee2d5637b9216b3545af0de366d4b9aad9dc87e26bfd.js'></script><script src='//production-assets.codepen.io/assets/editor/live/events_runner-73716630c22bbc8cff4bd0f07b135f00a0bdc5d14629260c3ec49e5606f98fdd.js'></script><script src='//production-assets.codepen.io/assets/editor/live/css_live_reload_init-2c0dc5167d60a5af3ee189d570b1835129687ea2a61bee3513dee3a50c115a77.js'></script><meta charset='UTF-8'><meta name="robots" content="noindex"><link rel="shortcut icon" type="image/x-icon" href="//production-assets.codepen.io/assets/favicon/favicon-8ea04875e70c4b0bb41da869e81236e54394d63638a1ef12fa558a4a835f1164.ico" /><link rel="mask-icon" type="" href="//production-assets.codepen.io/assets/favicon/logo-pin-f2d2b6d2c61838f7e76325261b7195c27224080bc099486ddd6dccb469b8e8e6.svg" color="#111" /><link rel="canonical" href="https://codepen.io/_massimo/pen/QOaXyy" />
<style class="cp-pen-styles">@import url("https://fonts.googleapis.com/css?family=Open+Sans");
body {
margin: 0;
overflow: hidden;
background: #000;
}
body canvas {
position: absolute;
left: 50%;
top: 50%;
}
body #c {
border-radius: 50%;
pointer-events: none;
}
body li, body .close-button {
font-family: 'Open Sans', sans-serif;
font-size: 14px;
text-transform: capitalize;
}
</style></head><body>
<canvas id="bg"></canvas>
<canvas id="c"></canvas>
<script src='//production-assets.codepen.io/assets/common/stopExecutionOnTimeout-b2a7b3fe212eaa732349046d8416e00a9dec26eb7fd347590fbced3ab38af52e.js'></script><script src='https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.6.2/dat.gui.min.js'></script>
<script >var bg = document.getElementById('bg'),
c = document.getElementById('c'),
ctx = c.getContext('2d'),
src = "https://i.kinja-img.com/gawker-media/image/upload/t_original/fwf4rfhsob5wnkwlrwzl.jpg",
s = {
size: 200,
zoom: 1,
brighten: 0,
invert: false,
grayscale: false,
threshold : false
};
var gui = new dat.GUI();
gui.add(s, "size", 50, 400, 1);
gui.add(s, "zoom", 1, 5, 1);
gui.add(s, "brighten", 0, 255, 1);
gui.add(s, "invert");
gui.add(s, "grayscale");
gui.add(s, "threshold");
var img = new Image();
img.crossOrigin = "Anonymous";
img.onload = function() {
var w = this.width,
h = this.height;
bg.width = w;
bg.height = h;
bg.style.marginLeft = -w/2 + 'px';
bg.style.marginTop = -h/2 + 'px';
bg.getContext('2d').drawImage(img, 0, 0, w, h);
window.addEventListener("mousemove", distortion);
window.addEventListener("touchmove", distortion);
}
img.src = src;
function distortion(e) {
var cx = (e.touches ? e.touches[0].clientX : e.clientX),
cy = (e.touches ? e.touches[0].clientY : e.clientY),
size = s.size,
zoom = s.zoom;
c.width = size;
c.height = size;
c.style.left = cx - size / 2 + 'px';
c.style.top = cy - size / 2 + 'px';
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, size, size);
ctx.drawImage(
bg,
cx - bg.offsetLeft - .5 * size / zoom,
cy - bg.offsetTop - .5 * size / zoom,
size / zoom,
size / zoom,
0,
0,
size,
size
);
var imgData = ctx.getImageData(0, 0, size, size);
pixels = imgData.data,
pixelsCopy = pixels.slice();
for(var y=0; y<size; y++) {if (window.CP.shouldStopExecution(2)){break;}if (window.CP.shouldStopExecution(2)){break;}
for(var x=0; x<size; x++) {if (window.CP.shouldStopExecution(1)){break;}if (window.CP.shouldStopExecution(1)){break;}
var index = 4*(x + y*size);
//fishEye
var dx = x - .5*size,
dy = y - .5*size,
angle = Math.atan2(dy, dx),
rad = Math.hypot(dy, dx),
rs = rad * rad / Math.hypot(.5*size, .5*size),
px = Math.round(.5*size + rs * Math.cos(angle)),
py = Math.round(.5*size + rs * Math.sin(angle));
var newIndex = 4*(px + py*size);
pixelsCopy[index + 0] = pixels[newIndex + 0];
pixelsCopy[index + 1] = pixels[newIndex + 1];
pixelsCopy[index + 2] = pixels[newIndex + 2];
//end fishEye
if (s.invert) {
pixelsCopy[index + 0] = 255 - pixelsCopy[index + 0];
pixelsCopy[index + 1] = 255 - pixelsCopy[index + 1];
pixelsCopy[index + 2] = 255 - pixelsCopy[index + 2];
}
if (s.grayscale) {
var gs = 0.2126 * pixelsCopy[index + 0] +
0.7152 * pixelsCopy[index + 1] +
0.0722 * pixelsCopy[index + 2];
pixelsCopy[index + 0] =
pixelsCopy[index + 1] =
pixelsCopy[index + 2] = Math.round(gs);
}
if (s.threshold) {
var th = (0.2126 * pixelsCopy[index + 0] +
0.7152 * pixelsCopy[index + 1] +
0.0722 * pixelsCopy[index + 2] >= 127) ? 255 : 0;
pixelsCopy[index + 0] =
pixelsCopy[index + 1] =
pixelsCopy[index + 2] = Math.round(th);
}
}
window.CP.exitedLoop(1);
window.CP.exitedLoop(1);
}
window.CP.exitedLoop(2);
window.CP.exitedLoop(2);
for(var y=0; y<size; y++) {if (window.CP.shouldStopExecution(4)){break;}if (window.CP.shouldStopExecution(4)){break;}
for(var x=0; x<size; x++) {if (window.CP.shouldStopExecution(3)){break;}if (window.CP.shouldStopExecution(3)){break;}
var index = 4*(x + y*size);
pixels[index + 0] = pixelsCopy[index + 0] + s.brighten;
pixels[index + 1] = pixelsCopy[index + 1] + s.brighten;
pixels[index + 2] = pixelsCopy[index + 2] + s.brighten;
}
window.CP.exitedLoop(3);
window.CP.exitedLoop(3);
}
window.CP.exitedLoop(4);
window.CP.exitedLoop(4);
ctx.putImageData(imgData, 0, 0);
}
//# sourceURL=pen.js
</script>
</body></html>