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 Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

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.

            
              <canvas id="canvas" data-image="http://unsplash.it/g/450/200/?random=1"></canvas>

<div class="intro">
	<h1>Interactive mosaic background</h1>
	<p>Had to do this effect in a recent project and wanted to share it with you :). To change the background, edit the data-image on the canvas tag. You can also change the magnet effect intensity by changing the magnet variable</p>
</div>
            
          
!
            
              body{
	overflow:hidden;
	margin:0;
	&:before{
		content:'';
		background:#c4252a url(http://subtlepatterns2015.subtlepatterns.netdna-cdn.com/patterns/cheap_diagonal_fabric.png);
		background-blend-mode: multiply;
		mix-blend-mode: multiply;
		position:absolute;
		left:0;
		top:0;
		width:100%;
		height:100%;
		z-index: 10;
	}
}
canvas{
	opacity:0;
	transition:1s opacity cubic-bezier(.55,0,.1,1);
	&.ready{
		opacity:0.4;
	}
	
}

.intro{
	position: absolute;
	padding:20px;
	transform:translate(-50%, -50%);
	left:50%;
	top:50%;
	text-align:center;
	color:#fafafa;
	z-index: 10;
	width:100%;
	max-width:700px;
	font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif; 
	text-shadow:0px 5px 20px black;
	h1{
		font-size:40px;
		font-weight:300;
		letter-spacing:2px;
	}
	p{
		letter-spacing:1px;
		line-height:24px;
	}
}
            
          
!
            
              # Variables
canvas = document.getElementById('canvas');
ctx = canvas.getContext('2d');
width = canvas.width = window.innerWidth;
height = canvas.height = window.innerHeight;
imgSrc = canvas.dataset.image;
img = new Image();
useGrid = true
imgInfo = {};
imgs = [];
grids = [];
magnet = 2000;
mouse = {
	x:1
	y:0
}

init = ()->
	addListeners();
	
	img.onload = (e)->
		# Check for firefox. 
		imgInfo.width = if e.path then e.path[0].width else e.target.width
		imgInfo.height = if e.path then e.path[0].height else e.target.height 
			
		numberToShow = (Math.ceil(window.innerWidth / imgInfo.width)) * (Math.ceil(window.innerHeight / imgInfo.height))
		
		createGrid() if useGrid;
		populateCanvas(numberToShow * 4);
		
		# Image is ready and we're ready to go
		canvas.classList.add('ready');
		render();
		
	img.src = imgSrc;
	
addListeners = ()->
	window.addEventListener('resize',resizeCanvas);
	window.addEventListener('mousemove', updateMouse);
	window.addEventListener('touchmove', updateMouse);
	
updateMouse = (e)->

	mouse.x = e.clientX
	mouse.y = e.clientY
	
resizeCanvas = ()->
	width = canvas.width = window.innerWidth;
	height = canvas.height = window.innerHeight;
	
# Magic
populateCanvas = (nb)->
	i = 0;
	while i <= nb
		p = new Photo();
		imgs.push p
		i++;
	
createGrid = ()->
	imgScale = 0.5
	grid = {
		row: Math.ceil(window.innerWidth / (imgInfo.width * imgScale))
		cols: Math.ceil(window.innerHeight / (imgInfo.height * imgScale))
		rowWidth: imgInfo.width * imgScale
		colHeight: imgInfo.height * imgScale
	}
	
	for r in [0...grid.row]
		x = r * grid.rowWidth
		for c in [0...grid.cols]
			y = c * grid.colHeight
			
			item = new gridItem(x,y,grid.rowWidth,grid.colHeight)
			grids.push item;
			
	for i in [0...grids.length]
		grids[i].draw();
	
gridItem = (x = 0, y = 0, w, h)->
	this.draw = ()->
		ctx.drawImage(img, x, y, w, h);
		return
	return

Photo = ()->
	seed = Math.random() * (2.5 - 0.7) + 0.7;
	w = imgInfo.width / seed
	h = imgInfo.height / seed
	x = window.innerWidth * Math.random()
	finalX = x
	y = window.innerHeight * Math.random()
	finalY = y	
	console.log("INIT Y :: #{finalY} || INIT X :: #{finalX}")
	r = Math.random() * (180 - (-180)) + (-180)
	
	forceX = 0
	forceY = 0
	
	TO_RADIANS = Math.PI/180
	
	this.update = ()->
		x0 = x
		y0 = y
		x1 = mouse.x
		y1 = mouse.y
		
		dx = x1 - x0
		dy = y1 - y0
		
		distance = Math.sqrt((dx * dx) + (dy * dy))
		powerX = x0 - (dx / distance) * magnet / distance;
		powerY = y0 - (dy / distance) * magnet / distance
		
		forceX = (forceX + (finalX - x0) / 2) / 2.1
		forceY = (forceY + (finalY - y0) / 2) / 2.2

		

		x = powerX + forceX
		y = powerY + forceY
	
		return
	this.draw = ()->
		rotateAndPaintImage(ctx, img, r * TO_RADIANS, x, y, w / 2, h / 2, w, h)
	return

rotateAndPaintImage = ( context, image, angle , positionX, positionY, axisX, axisY, widthX, widthY)->
	context.translate( positionX, positionY );
	context.rotate( angle );
	context.drawImage( image, -axisX, -axisY, widthX, widthY );
	context.rotate( -angle );
	context.translate( -positionX, -positionY );

render = ()->
	x = 0
	y = 0
	ctx.clearRect(0,0,width,height)
	while y < grids.length
		grids[y].draw()
		y++
	while x < imgs.length
		imgs[x].update()
		imgs[x].draw()
		x++
		
	requestAnimationFrame render
	
	
		
init();
            
          
!
999px
Loading ..................

Console