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

              
                


	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<style type="text/css">

	</style>




	<div id="container">
		<img id="image" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/175711/crayon.jpg"></div>
    <canvas id="canvas"></canvas>
	</div>
	<script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/175711/delaunay.js"></script>
	<script src="//cdnjs.cloudflare.com/ajax/libs/gsap/1.13.2/TweenMax.min.js"></script>






              
            
!

CSS

              
                		body {
			background-color: #000;
			margin: 0;
			overflow: hidden;
		}

		canvas {
			position: absolute;
			backface-visibility: hidden;
			-webkit-backface-visibility: hidden;
			-moz-backface-visibility: hidden;
			-ms-backface-visibility: hidden;
		}

		img {
			position: absolute;
			cursor: pointer;
      
      display: none;
		}

		#container {
			position: absolute;
			width: 768px;
			height: 485px;
			left: 0;
			right: 0;
			top: 0;
			bottom: 0;
			margin: auto;
		}
              
            
!

JS

              
                console.clear();

var image = document.querySelector("#image");
var canvas = document.querySelector("#canvas");
var ctx = canvas.getContext("2d");

var size = 10;
var width = 0;
var height = 0;
var rows = 0;
var cols = 0;
var total = 0;

var rects = [];

var tl = new TimelineMax({
  repeat: -1,
  yoyo: true,
  onUpdate: update
});

window.addEventListener("load", init);

function init() {
    
  canvas.width  = width  = Math.round(image.naturalWidth  / size) * size;
  canvas.height = height = Math.round(image.naturalHeight / size) * size;
  
  cols = width  / size;
  rows = height / size;
  
  total = rows * cols;
  
  for (var row = 0; row < rows; row++) {
    for (var col = 0; col < cols; col++) {
      
      var rect = {
        x: col * size,
        y: row * size,
        alpha: 0
      };
      
      tl.to(rect, 1, { alpha: 1 }, random(1));      
      rects.push(rect);
    }
  }  
}

function update() {
  
  ctx.clearRect(0, 0, width, height);
  
  for (var i = 0; i < total; i++) {
    
    var rect = rects[i];
    
    if (!rect.alpha) continue;
    
    ctx.globalAlpha = rect.alpha;
    ctx.drawImage(image, rect.x, rect.y, size, size, rect.x, rect.y, size, size);    
  }  
}

function random(min, max) {
  if (max == null) { max = min; min = 0; }
  return Math.random() * (max - min) + min;
}

// 	var container = document.getElementById('container');

// 	window.onload = function() {
// 		createCanvases();
// 	};


// 	var image = document.getElementById('image');


// 	function createCanvases() {
// 		var imageWidth = image.clientWidth;
// 		var imageHeight = image.clientHeight;


// 		var tl0 = new TimelineMax({onComplete:function() {
// 			//console.log('main end');
// 		}});

// 		container.removeChild(image);

// 		var options = {
// 			imageWidth: imageWidth,
// 			imageHeight:imageHeight,
// 			grainDensity: 50
// 		}
// 		for (var w = 0; w < options.imageWidth; w += options.grainDensity) {
// 			for (var h = 0; h < options.imageHeight; h += options.grainDensity) {
// 				var canvas = createCanvas({left:w, top:h}, {width:options.grainDensity, height:options.grainDensity});
// 				var tl1 = new TimelineMax();
// 				tl1.fromTo(canvas, 2,{alpha:0}, {alpha:1});
// 				container.appendChild(canvas);
// 				tl0.insert(tl1, randomRange(0,5));
// 			}
// 		}

// 	}

// 	function createCanvas(position, size) {
// 		var canvas = document.createElement('canvas');
// 		canvas.width = size.width;
// 		canvas.height = size.height;
// 		canvas.style.width = size.width + 'px';
// 		canvas.style.height = size.height + 'px';

// 		canvas.style.left = position.left + 'px';
// 		canvas.style.top = position.top + 'px';
// 		var ctx = canvas.getContext('2d');

// 		ctx.drawImage(image, position.left, position.top, size.width, size.height, 0,0, size.width, size.height);

// 		return canvas;
// 	}

// 	function randomRange(min, max) {
// 		return min + (max - min) * Math.random();
// 	}

              
            
!
999px

Console