cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <html>
  
  <body>
  	
		<canvas id="canvas"></canvas>
		
		<div id="controls">
			
				<h1>Circle warp</h1>
				
				<p>Click and drag around to warp, move with the sliders to adjust effects (no sliders in FF, just type values). Images are pretty large so please be patient! Also, because of the sizes of the images, anything that isn't Chrome seems to choke, sorry about that! See a post about this experiment <a href="http://neilcarpenter.com/labs/circle-warp/">here.</a></p>
				
				<p>
					<label>Circles</label>
					<input class="controller" name="circles" type="range" min="1" max="200" step="1" value="10">
				</p>
				
				<p>
					<label>Circle radii</label>
					<input class="controller" name="radius" type="range" min="3" max="150" step="1" value="50">
				</p>				
								
				<p>
					<label>Rotation speed</label>
					<input class="controller" name="rotationIncrement" type="range" min="0.1" max="50" value="20">
				</p>
				
				<p>
					<label>Circle offset</label>
					<input class="controller" name="offsetCircles" type="range" min="0" max="5" value="1">
				</p>
				
				<button id="change-photo">Different photo please</button>
				
				<p id="attribution"></p>
				
				<button id="reset-canvas">Reset</button>
			
			<a href="#" class="toggle-controls" id="open">Open controls</a>
			<a href="#" class="toggle-controls" id="close">Close controls</a>
		</div>		
		
	</body>
  
  <a href="https://github.com/neilcarpenter/Circle-warp" id="ribbon" target="_blank"><img style="position: absolute; top: 0; right: 0; border: 0; z-index: 50;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png" alt="Fork me on GitHub"></a>
	
</html>
            
          
!
            
              @import url("https://fonts.googleapis.com/css?family=Carrois+Gothic|Sigmar+One");

html, body {
    -webkit-font-smoothing: antialiased;
    font: normal 12px/14px "Carrois Gothic", sans-serif;
    width: 100%;
    height: 100%;
    margin: 0;
    overflow: hidden;
    color: #fff;

    -webkit-user-select: none;
       -moz-user-select: none;
        -ms-user-select: none;
            user-select: none; 
}

body {
    background: url(http://neilcarpenter.com/demos/canvas/warp/spinner.gif) no-repeat center center;
}

#canvas {
    z-index: 10;
    position: absolute;
    top: 50%;
    left: 50%;
    background: white url(http://neilcarpenter.com/demos/canvas/warp/spinner.gif) no-repeat center center;
}

h1 {
    font: normal 24px/24px "Sigmar One", Helvetica, Arial;
    margin: 0;
    text-align: center;
    color: rgba(255, 255, 255, 0.7);
    text-shadow: 0 0 5px rgba(255, 255, 255, 0.5);    
}

#controls {
    background: rgba(0, 0, 0, 0.7);
    position: fixed;
    top: 0;
    left: 20px;
    width: 250px;
    padding: 20px;
    z-index: 15;
    -webkit-transform-origin: top center;
       -moz-transform-origin: top center;
         -o-transform-origin: top center;
            transform-origin: top center;
    -webkit-transform: rotate(0deg);
       -moz-transform: rotate(0deg);
         -o-transform: rotate(0deg);
            transform: rotate(0deg);
    -webkit-transition: -webkit-transform .5s ease-in-out;
       -moz-transition:    -moz-transform .5s ease-in-out;
         -o-transition:      -o-transform .5s ease-in-out;
            transition:         transform .5s ease-in-out;
}

#controls.closed {
    -webkit-transform: rotate(-180deg);
       -moz-transform: rotate(-180deg);
         -o-transform: rotate(-180deg);
            transform: rotate(-180deg);
}

.controller {
    float: right;
    width: 150px;
}
input[type='range'] {  
    -webkit-appearance: none;
    color: #fff;
    background: rgba(255, 255, 255, 0.2);
    background: -webkit-linear-gradient(top, rgba(255,255,255,0) 0%,rgba(255,255,255,0) 40%,rgba(255,255,255,0.4) 50%,rgba(255,255,255,0) 60%,rgba(255,255,255,0) 100%); /* Chrome10+,Safari5.1+ */
}

.toggle-controls {
    position: absolute;
    display: block;
    height: 10px;
    background: rgba(0, 0, 0, 0.8);
    width: 290px;
    left: 0;
    text-align: center;
    padding: 3px 0 7px;
    text-decoration: none;
    color: white;
    text-shadow: none;
}
.toggle-controls:hover {
        background: rgb(0, 0, 0);
    }


#close {
    bottom: -20px;
}

#open {
    top: -20px;
    -webkit-transform: rotate(180deg);
       -moz-transform: rotate(180deg);
         -o-transform: rotate(180deg);
            transform: rotate(180deg);
}

button {
    background: rgba(255, 255, 255, 0.2);
    color: #fff;
    border: 0;
    border-radius: 2px;
    padding: 7px 10px;
    box-shadow: 0 0 3px 0px rgba(255,255,255, 0.3);
    cursor: pointer;
}
button:hover {
        background: rgba(255, 255, 255, 0.1);
    }

p a {
    color: #fff;
}
p a:hover {
        color: #FF0084;
    }
            
          
!
            
              /*
 *
 * Recreating a photo manipulation effect I saw once,
 * For explanation, see neilcarpenter.com/labs/circle-warp
 *
 */



// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
 
// requestAnimationFrame polyfill by Erik Möller
// fixes from Paul Irish and Tino Zijdel
 
(function() {
    var lastTime = 0;
    var vendors = ['ms', 'moz', 'webkit', 'o'];
    for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
        window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
        window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
                                   || window[vendors[x]+'CancelRequestAnimationFrame'];
    }
 
    if (!window.requestAnimationFrame)
        window.requestAnimationFrame = function(callback, element) {
            var currTime = new Date().getTime();
            var timeToCall = Math.max(0, 16 - (currTime - lastTime));
            var id = window.setTimeout(function() { callback(currTime + timeToCall); },
              timeToCall);
            lastTime = currTime + timeToCall;
            return id;
        };
 
    if (!window.cancelAnimationFrame)
        window.cancelAnimationFrame = function(id) {
            clearTimeout(id);
        };
}());

var WARP = {

  options: {
		circles: 10,
		radius: 50,
		rotationIncrement: 20,
		offsetCircles: 1
	},

	animation: null,

	c: null,
	ctx: null,

	WIDTH: window.innerWidth,
	HEIGHT: window.innerHeight,

	mouse: { x: null, y: null, down: false },

	img: null,
	imageCounter: 0,
	images: [
		{url: 'http://neilcarpenter.com/demos/canvas/warp/1.jpg', flickr: 'http://www.flickr.com/photos/ianlucero/7076979089/'},
		{url: 'http://neilcarpenter.com/demos/canvas/warp/2.jpg', flickr: 'http://www.flickr.com/photos/asterix611/5284089469/'},
		{url: 'http://neilcarpenter.com/demos/canvas/warp/3.jpg', flickr: 'http://www.flickr.com/photos/felstone/7058947189/'},
		{url: 'http://neilcarpenter.com/demos/canvas/warp/4.jpg', flickr: 'http://www.flickr.com/photos/chrisschoenbohm/6288866486/'},
		{url: 'http://neilcarpenter.com/demos/canvas/warp/5.jpg', flickr: 'http://www.flickr.com/photos/clio1789/6932622015/'},
		{url: 'http://neilcarpenter.com/demos/canvas/warp/6.jpg', flickr: 'http://www.flickr.com/photos/davic/2848707301/'},
		{url: 'http://neilcarpenter.com/demos/canvas/warp/7.jpg', flickr: 'http://www.flickr.com/photos/luciosantos/7960223222/'},
		{url: 'http://neilcarpenter.com/demos/canvas/warp/8.jpg', flickr: 'http://www.flickr.com/photos/yannickgar/5486890633/'},
		{url: 'http://neilcarpenter.com/demos/canvas/warp/9.jpg', flickr: 'http://www.flickr.com/photos/mattharvey1/7486855830/'},
		{url: 'http://neilcarpenter.com/demos/canvas/warp/10.jpg', flickr: 'http://www.flickr.com/photos/mattharvey1/7486825158/'},
		{url: 'http://neilcarpenter.com/demos/canvas/warp/11.jpg', flickr: 'http://www.flickr.com/photos/sabeth718/6802988236/'},
		{url: 'http://neilcarpenter.com/demos/canvas/warp/12.jpg', flickr: 'http://www.flickr.com/photos/lynt/5784300047/'}
	],

	imgURL: function() {
		return this.images[this.imageCounter].url;
	},

	imgWidth: null,
	imgHeight: null,
	imgOffsetX: null,
	imgOffsetY: null,

	rotationCounter: 1,
	activeCircles: 0,

	init: function () {
		this.c = document.getElementById( 'canvas' );
		this.ctx = this.c.getContext( '2d' );
		this.c.width = this.WIDTH;
		this.c.height = this.HEIGHT;

		this.c.addEventListener('mousemove', this.mouseMove, false);
		this.c.addEventListener('mousedown', this.mouseDown, false);
		this.c.addEventListener('mouseup', this.mouseUp, false);
		this.c.addEventListener('mouseout', this.mouseOut, false);

		this.loadImage();

		window.onresize = function () {
			WARP.WIDTH = window.innerWidth;
			WARP.HEIGHT = window.innerHeight;
			WARP.c.width = WARP.WIDTH;
			WARP.c.height = WARP.HEIGHT;

			WARP.reset();
		};
	},

	loadImage: function(url) {
		this.img = new Image();
		this.img.src = this.imgURL();
		this.img.onload = function() {
			
			WARP.drawImage();
		};

		var attributionHolder = document.getElementById('attribution');
		attributionHolder.innerHTML = 'This photo is from <a href="' + this.images[this.imageCounter].flickr + '" target="_blank">Flickr</a>';
	},

	drawImage: function() {
		var c = this.c,
			i = this.img,
			iw = i.width,
			ih = i.height;
		

		this.imgWidth = iw <= this.WIDTH ? iw : this.WIDTH;
		this.imgHeight = ih * (this.imgWidth / iw);

		this.imgOffsetX = this.imgWidth >= this.WIDTH ? 0 : ((this.WIDTH - this.imgWidth) / 2);
		this.imgOffsetY = this.imgHeight >= this.HEIGHT ? 0 : ((this.HEIGHT - this.imgHeight) / 2);

		c.width = this.imgWidth;
		c.height = this.imgHeight;
		c.style.marginLeft = -(this.imgWidth / 2) + 'px';
		c.style.marginTop = -(this.imgHeight / 2) + 'px';

		this.ctx.drawImage(i, 0, 0, this.imgWidth, this.imgHeight);

		window.cancelAnimationFrame(this.animation);
		this.animation = null;

		WARP.draw();
	},

	draw: function () {
		this.animation = requestAnimationFrame( function(){ WARP.draw(); } );

		this.warp();
	},

	warp: function() {
		if (!this.mouse.down) {
			return;
		}
		var ctx = this.ctx,
			o = this.options,
			maxRadius = o.circles * o.radius,
			rotation = (o.rotationIncrement / 100) * this.rotationCounter,
			HALF_WIDTH = this.imgWidth / 2,
			HALF_HEIGHT = this.imgHeight / 2,
			localRadius,
			xOffset = 0,
			yOffset = 0;

		this.ctx.drawImage(this.img, 0, 0, this.imgWidth, this.imgHeight);

		for (var i = 0; i < this.activeCircles; i++) {
			localRadius = maxRadius - (o.radius * i);

			// if (o.offsetCircles === 1) {
				xOffset = ((this.mouse.x - HALF_WIDTH) / HALF_WIDTH) * o.offsetCircles;
				xOffset = xOffset * localRadius;
				yOffset = ((this.mouse.y - HALF_HEIGHT) / HALF_HEIGHT) * o.offsetCircles;
				yOffset = yOffset * localRadius;
			// }

			ctx.save();
			ctx.beginPath();
			ctx.arc((this.mouse.x - xOffset), (this.mouse.y - yOffset), localRadius, 0, (Math.PI * 2), true);
			ctx.clip();

			// this.clear();
			ctx.translate(this.mouse.x, this.mouse.y);
			ctx.rotate((rotation * i) * Math.PI / 180);

			ctx.drawImage(this.img, -(this.mouse.x), -(this.mouse.y), this.imgWidth, this.imgHeight);

			ctx.restore();
		}

		this.rotationCounter += 1;

		if (this.activeCircles !== this.options.circles) {
			this.activeCircles += 1;
		}
	},

	clear: function() {
		this.ctx.clearRect(0, 0, this.c.width, this.c.height);
	},

	reset: function() {
		WARP.ctx.clearRect(0, 0, WARP.c.width, WARP.c.height);
		WARP.drawImage();
	},

	mouseMove: function (e) {
		WARP.mouse.x = e.offsetX || (e.layerX - WARP.c.offsetLeft);
		WARP.mouse.y = e.offsetY || (e.layerY - WARP.c.offsetTop);
	},

	mouseDown: function () {
		WARP.clear();
		WARP.mouse.down = true;
	},

	mouseUp: function () {
		WARP.mouse.down = false;
		WARP.rotationCounter = 1;
		WARP.activeCircles = 1;
	},

	mouseOut: function () {
		WARP.mouse.down = false;
	},

};

function eventListenerz() {
	var inputs = document.getElementsByClassName('controller'),
		resetBtn = document.getElementById('reset-canvas'),
		changePhotoBtn = document.getElementById('change-photo');

	function onChange() {
		var name = this.name,
			value = this.value,
			max = this.getAttribute('max');

		value = +value;

		if (value > max) {
			value = max;
			this.value = max;
		}

		WARP.options[name] = value;

	}

	for (var i = 0; i < inputs.length; i++) {
		inputs[i].addEventListener('change', onChange, false);
	}

	var controlToggles = document.getElementsByClassName('toggle-controls'),
		controls = document.getElementById('controls');

	function toggleControls(e) {
		e.preventDefault();
		controls.className = controls.className === 'closed' ? '' : 'closed';
	}

	for (var j = 0; j < 2; j++) {
		controlToggles[j].addEventListener('click', toggleControls, false);
	}

	resetBtn.addEventListener('click', WARP.reset, false);

	changePhotoBtn.addEventListener('click', function() {
		WARP.clear();

		if (WARP.imageCounter === (WARP.images.length - 1)) {
			WARP.imageCounter = 0;
		}
		else {
			WARP.imageCounter++;
		}

		WARP.loadImage();
	}, false);

}

window.onload = function() {

	WARP.init();

	eventListenerz();

};
            
          
!
999px
Loading ..................

Console