Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

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.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="container">
	<svg id="svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
	<defs>
<!-- 	FF throws an error if I try to get the bbox of an element inside of a symbol -->
<!-- 		<symbol id="Source"> -->
<!-- 			<image id="image" xlink:href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/259155/cat_landscape.jpg" x="0" y="0" height="2448" width="3264"/> -->
<!-- 		</symbol> -->
		
		<mask id="mask">
			<g id="grid"></g>
		</mask>
	</defs>
		

		<g mask="url(#mask)">
			<image id="image" xlink:href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/259155/cat_landscape.jpg" x="0" y="0" height="2448" width="3264"/>
		</g>
<!-- 			<use x="0" y="0" xlink:href="#Source" /> -->

		
	</svg>
</div>
              
            
!

CSS

              
                html, body {
	background: hsla(240, 0%, 80%, 1);
	min-height: 100vh;
}

body {
	display: flex;
	align-items: center;
	justify-content: center;
}

#container {
	width: 80%;
	background: hsla(240, 0%, 70%, 1);
	box-shadow: 0px 0px 3px 3px hsla(240, 0%, 60%, 0.8);
/* 	visibility: hidden; */
}

svg {
	width: 100%;
	height: 100%;
	fill: white;
}


              
            
!

JS

              
                // wait until DOM is ready
document.addEventListener("DOMContentLoaded", function(event) {

// wait until window, stylesheets, images, links, and other media assets are loaded
window.onload = function() {

 // All ready, start!
 init();

 };

});






// We manipulate a few of the DOM elements via JavaScript in order to minimise the amount of editing that would otherwise have to be done to the DOM elements every time the ratio/size of the image is altered


// TODO add a resize event



function init() {

	// Get the image size
	var img = document.querySelector("#image").getBBox();
	// Update the <symbol> tag's viewbox based on the image size
	// document.getElementById("Source").setAttribute("viewBox", "0 0 " + img.width + " " + img.height);

	// Calculate the ratio of the image
	var ratio = calculateRatio(img.width, img.height);


	// Get the main SVG tag
	var svg = document.getElementById("svg");
	
	svg.setAttribute("viewBox", "0 0 " + img.width + " " + img.height);
	

	var container = document.getElementById("container");

	// Update its height to match the image ratio
	container.style.height = updateHeight(container.offsetWidth, ratio) +"px";


	// Calculate the aspect ratio of the image.
	function calculateRatio(w,h) {
		return Math.max(w,h) / Math.min(w,h);
	}


	// Return an updated height based on  width/ratio
	function updateHeight(width, ratio) {
		return Math.round(width / ratio);
	}




	// Create X squares of a size along a grid the size of the image
	var squares = {
		ttlHorz: 10,
		ttlVert: 10,
	}
	var squareSize = calculateSquareSize(img.width, squares.ttlHorz);
	// Originally, I had it up so that the user would only had to set the size of the square but, because we are now setting the viewBox on the whole SVG, this no longer works as designed and has been replaced with the above
	// var squareSize = 20;
	// var squares = {
	// 	ttlHorz: squaresPerLength(svg.getBBox().width, squareSize),
	// 	ttlVert: squaresPerLength(svg.getBBox().height, squareSize),
	// }
	
	// function squaresPerLength(width, size) {
	// 	return width / size;
	// }

	function calculateSquareSize(totalWidth, numOfSquares) {
		return totalWidth / numOfSquares;
	}

	var g = document.getElementById("grid");
	var arr = [];

	// Loop thru the total squares and create the grid
	for(var i = 0; i < squares.ttlHorz; i++) {
		for(var j = 0; j < squares.ttlVert; j++) {
		var square = document.createElementNS("http://www.w3.org/2000/svg", "rect");
		square.setAttribute("width", squareSize);
		square.setAttribute("height", squareSize);
		square.setAttribute("x", i * squareSize);
		square.setAttribute("y", j * squareSize);

			arr.push(square);
			g.appendChild(square);
		}
	}




	function shuffle(array) {
			var currentIndex = array.length, temporaryValue, randomIndex;

			// While there remain elements to shuffle...
			while (0 !== currentIndex) {

					// Pick a remaining element...
					randomIndex = Math.floor(Math.random() * currentIndex);
					currentIndex -= 1;

					// And swap it with the current element.
					temporaryValue = array[currentIndex];
					array[currentIndex] = array[randomIndex];
					array[randomIndex] = temporaryValue;
			}

			return array;
	}



	function randomIntFromInterval(min,max)
	{
					return Math.floor(Math.random()*(max-min+1)+min);
	}


	var newArr = shuffle(arr)

	// Animate the squares
	TweenMax.staggerTo(newArr, 1.5, {
		autoAlpha:0,
		repeat:-1,
		repeatDelay:1,
		yoyo:true,
		ease:Power3.easeInOut,
		cycle:{
			delay:[0,0.15,0.3,0.45,0.6,0.75,0.9],
			x:function() {
				// Firefox does not seem to animate elements inside of a mask
				return randomIntFromInterval(-100,100);
			},
			y:function() {
				// Firefox does not seem to animate elements inside of a mask
				return randomIntFromInterval(-100,100);
			}
		}
	});
	
	TweenMax.to(container, 0.3, {autoAlpha:1});
};
              
            
!
999px

Console