Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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 class="title">
	<h1>Sugar, Sugar</h1>
</div> -->
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Corben:700');

html, body
{
	margin: 0;
	overflow: hidden;
	height: 100%;
	width: 100%;
	background-color: #032C3F;
}

.title
{
	position: fixed;
	z-index: 10;
	top: 0;
	right: 0;
	bottom: 0;
	left: 0;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	
	h1
	{
		font-size: 5vw;
		display: inline-block;
		color: #032C3F;
		width: auto;
		font-family: 'Corben', cursive;
	}
}
              
            
!

JS

              
                console.clear();

declare var THREE:any;
declare var TweenMax:any;

TweenMax.lagSmoothing(0);

let colors = 
{
	background: '#032C3F'
}

let getRandomFromArray = function(arr)
{
	return arr[Math.floor(Math.random() * arr.length)]
}

class Stage
{
	floor: any;
	
	private container: any;
	private camera: any;
	private scene: any;
	private renderer: any;
	private light: any;
	private softLight: any;
	private group: any;
	
	constructor()
	{
		// container
		
		this.container = document.createElement( 'div' );
		document.body.appendChild( this.container );
		
		// renderer
		
		this.renderer = new THREE.WebGLRenderer({
			antialias: true,
			alpha: false
		});
		
		this.renderer.setSize(window.innerWidth, window.innerHeight);
		this.renderer.setClearColor(colors.background, 1);
		this.renderer.shadowMap.enabled = true;
		this.renderer.shadowMap.type = THREE.PCFSoftShadowMap;
		this.container.appendChild( this.renderer.domElement );
		
		// scene

		this.scene = new THREE.Scene();

		// camera

		let aspect = window.innerWidth / window.innerHeight;
		
		this.camera = new THREE.PerspectiveCamera( 30, aspect, 1, 1500 );
		this.camera.position.z = 100; 
		this.camera.lookAt(new THREE.Vector3(0, 0, 0));

		//light

		this.light = new THREE.DirectionalLight(0xffffff, 0.8);
		//this.light.castShadow = true;
		this.light.position.set(8, 10, 10);
		this.scene.add(this.light);

		this.softLight = new THREE.AmbientLight( 0xffffff, 0.4 );
		this.scene.add(this.softLight)

		// group

		this.group = new THREE.Group();
		this.scene.add(this.group);
		
		window.addEventListener( 'resize', () => {this.onResize()}, false );
	}
	
	onResize = function()
	{
		this.camera.aspect = window.innerWidth / window.innerHeight;
		this.camera.updateProjectionMatrix();
		this.renderer.setSize( window.innerWidth, window.innerHeight );
	}
	
	render = function()
	{
		this.renderer.render(this.scene, this.camera);
	}

	add = function(elem)
	{
		this.scene.add(elem);
	}

	remove = function(elem)
	{
		this.scene.remove(elem);
	}
	
}

class Sprinkle
{
	group: any;
	colors: string[] = ['#E54B4B', '#712F79', '#FF9770'];
	
	constructor()
	{
		this.group = new THREE.Group();
		let scale = 0.8;
		this.group.scale.set(scale, scale, scale);

		var geometry = new THREE.CylinderGeometry(0.2, 0.2, 2, 5);
		var material = new THREE.MeshToonMaterial({
			color: getRandomFromArray(this.colors)
		});

		var mesh = new THREE.Mesh(geometry, material);
		mesh.position.y = -0.1;
		mesh.position.x = -0.1;
		mesh.position.z = -1;
		// mesh.castShadow = true;
		// mesh.receiveShadow = true;

		this.group.add(mesh);
	}
}

class Gumball
{
	group: any;
	colors: string[] = ['#DDDDDD', '#FF70A6', '#519872'];
	
	constructor()
	{
		this.group = new THREE.Group();
		let scale = 1;
		this.group.scale.set(scale, scale, scale);
		
		var geometry = new THREE.TetrahedronGeometry(0.5, 1);
		var material = new THREE.MeshToonMaterial({
			color: getRandomFromArray(this.colors)
		});

		var mesh = new THREE.Mesh(geometry, material);
		mesh.position.y = Math.random() - 1;
		mesh.position.x = Math.random() - 1;
		mesh.position.z = Math.random() - 1;
		// this.mesh.castShadow = true;
		// this.mesh.receiveShadow = true;

		this.group.add(mesh);
	}
}

class ChocolateButton
{
	group: any;
	colors: string[] = ['#E9EB87', '#FF686B', '#C98BB9'];
	
	constructor()
	{
		this.group = new THREE.Group();
		let scale = 0.5;
		this.group.scale.set(scale, scale, scale);
		
		var geometry = new THREE.CylinderGeometry(2, 1.5, 0.5, 10);
		var material = new THREE.MeshToonMaterial({
			color: '#594935'
		});
		var chocolate = new THREE.Mesh(geometry, material);

		this.group.add(chocolate);
	}
}

class Marshmallow
{
	group: any;
	
	constructor()
	{
		this.group = new THREE.Group();
		let scale = 0.5;
		this.group.scale.set(scale, scale, scale);
		
		var geometry = new THREE.CylinderGeometry(1.5, 1.5, 3, 15);
		var material = new THREE.MeshToonMaterial({
			color: '#dddddd'
		});
		let mallow = new THREE.Mesh(geometry, material);

		this.group.add(mallow);
	}
}

class Lollipop
{
	group: any;
	colors: string[] = ['#E9EB87', '#FF686B', '#C98BB9'];
	
	constructor()
	{
		this.group = new THREE.Group();
		let scale = 0.5;
		this.group.scale.set(scale, scale, scale);
		
		var geometry = new THREE.CylinderGeometry(0.3, 0.3, 4, 5);
		var material = new THREE.MeshToonMaterial({
			color: '#CCCCCC'
		});
		var stick = new THREE.Mesh(geometry, material);
		stick.position.y = -2.5;
		// this.stick.castShadow = true;
		// this.stick.receiveShadow = true;
		
		var geometry = new THREE.CylinderGeometry(2, 2, 0.7, 20);
		var material = new THREE.MeshToonMaterial({
			color: getRandomFromArray(this.colors)
		});
		var lolly = new THREE.Mesh(geometry, material);
		lolly.rotation.z = 1.5708;
		// this.lolly.castShadow = true;
		// this.lolly.receiveShadow = true;

		this.group.add(stick);
		this.group.add(lolly);
	}
}

class App
{
	private candies: any[] = [
		ChocolateButton, 
		Marshmallow, 
		Sprinkle, Sprinkle, Sprinkle, Sprinkle, Sprinkle, Sprinkle, 
		Gumball, Gumball, Gumball, Gumball, Gumball, Gumball, 
		Lollipop
	];
	textImage:any;
	count:number = 0;
	
	constructor()
	{
		this.stage = new Stage();
		var loader = new THREE.ImageLoader();
		loader.load( 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/557388/sugar.png', image =>
		{
			this.textImage = getImageData(image);
			this.tick();
			this.addCandy();
		} );
		
	}
	
	addCandy = function()
	{
		this.count ++;
		var candy = new this.candies[Math.floor(Math.random() * this.candies.length)]();
		
		candy.group.position.z = (Math.random() * 80) - 40;
		this.stage.add(candy.group);
		this.animate(candy);
		
		if(this.count < 800) setTimeout(() => {this.addCandy()}, 10 + (Math.random() * 10))
	}
	
	
	getTextPosition = function() 
	{
		//get a position based on img pixels
		let position;
		let gotIt:boolean = false;
		let Z_SPREAD = 0;
		let IMAGE_SCALE = window.innerWidth / 9000;
		console.log(IMAGE_SCALE)
		if(IMAGE_SCALE > 0.1) IMAGE_SCALE = 0.1
		
		while(gotIt == false) 
		{
			//randomly select a pixel in image data
			var imgx = Math.round(this.textImage.width * Math.random());
			var imgy = Math.round(this.textImage.height * Math.random());
			var col = getPixel(this.textImage, imgx, imgy);
			//read color from image
			if(col.r > 0) {
				//if not black - set it
				position = new THREE.Vector3((imgx - this.textImage.width / 2) * IMAGE_SCALE, (imgy - this.textImage.height / 2) * IMAGE_SCALE, Math.random() * Z_SPREAD * 2 - Z_SPREAD);
				gotIt = true;
			} else {
				//if black - loop
				gotIt = false;
			}
		}

		return position;
	}
	
	getRandomRotation = function():number
	{
		return Math.random() * 20;
	}
	
	getRandomY = function():number
	{
		var range = window.innerHeight / 20;
		return Math.random() * range - (range / 2);
	}
	
	animate = function(candy:any)
	{
		let speed:number = 1 + (Math.random() * 2);

		let vec3 = this.getTextPosition();
		TweenMax.fromTo(candy.group.position, speed, 
			{ y: 40, x: Math.random() * vec3.x, z: -50}, 
			{ y: vec3.y - (vec3.y * 2), x: vec3.x, z: vec3.z, ease: Power4.easeOut}
		)
		
		TweenMax.fromTo(candy.group.rotation, speed, 
						{x: this.getRandomRotation(), y: this.getRandomRotation(), z: this.getRandomRotation()}, 
						{x: this.getRandomRotation(), y: this.getRandomRotation(), z: this.getRandomRotation(), ease: Power4.easeOut})
	}

	tick = function()
	{
		this.stage.render();
		requestAnimationFrame(() => {this.tick()});
	}
}

let app = new App();

function getImageData( image ) {

    var canvas = document.createElement( 'canvas' );
    canvas.width = image.width;
    canvas.height = image.height;

    var context = canvas.getContext( '2d' );
    context.drawImage( image, 0, 0 );

    return context.getImageData( 0, 0, image.width, image.height );

}

function getPixel(imgData, x, y) {
	var r, g, b, a, offset = x * 4 + y * 4 * imgData.width;
	r = imgData.data[offset];
	g = imgData.data[offset + 1];
	b = imgData.data[offset + 2];
	a = imgData.data[offset + 3];
	//console( "rgba(" + r + "," + g + "," + b + "," + a + ")");
	var col = new THREE.Color(0xffffff);
	col.setRGB(r / 256, g / 256, b / 256);
	return col;
}
              
            
!
999px

Console