123

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.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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 Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <canvas id="codefloat" width="1170" height="120"><p>Your primitive, sucky browser does not support canvas.</p></canvas>
            
          
!
            
              canvas {
	background-color: #ddd;
	display:block;
	margin:0 auto;
}
            
          
!
            
              
function codefloat() {
	"use strict";

	var theCanvas = document.getElementById("codefloat");
	var context = theCanvas.getContext("2d");
	var codeFloatArray = new Array();
	 
	// Setting up rAF. Thank you Paul Irish.
	window.requestAnimFrame = (function(){
		return  window.requestAnimationFrame	|| 
		window.webkitRequestAnimationFrame		|| 
		window.mozRequestAnimationFrame			|| 
		window.oRequestAnimationFrame			|| 
		window.msRequestAnimationFrame			|| 
		function( callback ){
			window.setTimeout(callback, 1000 / 60);
		};
	})();


	// Initial function calls.
	initCodeFloaters();
	animloop(); // Kicking off the RaF. It will self-call after this.


	// ====================
	// FUNCTIONS
	// ====================


	function animloop(){
		drawCanvas();
		requestAnimFrame(animloop);
	}


	function drawCanvas() {
		// Clear Canvas. Or in this case, add a gradient.
		//context.fillStyle = "#ffffff";
		context.clearRect(0,0,theCanvas.width,theCanvas.height);
		
		//Text
		context.fillStyle = "#000000";
		context.textBaseline = "top";
		context.textAlign="left";
		
		renderCodeFloaters();
	}// end of drawCanvas()
    
    
    function initCodeFloaters() {
		var codeTextSource = {
			JavaScriptName:'JavaScript',
			JavaScriptCode:'if( this > that ){ //do some stuff } else{ //do some other stuff }',
			jQueryName:'jQuery',
			jQueryCode:'$(".myClass").not("li").hide();',
			PHPName:'PHP',
			PHPCode:'<?php include(’myFile.php’); ?>',
			HTMLCode:'<div class=\"myClass\"><a href=\"http://www.someurl.com\">Some Link</a></div>',
			HTML5Name:'HTML5',
			HTML5Code:'<header><nav><section><aside>',
			canvasName:'<canvas></canvas>',
			loremIpsum:'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer eget ante felis.'
		}

		var html5Name1 = new CodeFloater( { x:( theCanvas.width ), y:-15, fontSize:'140px Courier New', speed:0.1, alpha:0.12, text:codeTextSource.HTML5Name } );
		var html5Name2 = new CodeFloater( { x:( theCanvas.width/2 - html5Name1.metrics.width/2 ), y:html5Name1.y, fontSize:html5Name1.fontSize, speed:html5Name1.speed, alpha:html5Name1.alpha, text:html5Name1.text } );    	
		
		var jsName1 = new CodeFloater( { x:theCanvas.width, y:-15, fontSize:'60px Courier New', speed:0.25, alpha:0.27, text:codeTextSource.JavaScriptName } );
		var jsName2 = new CodeFloater( { x:( (theCanvas.width/2) - (jsName1.metrics.width/2) ), y:jsName1.y, fontSize:jsName1.fontSize, speed:jsName1.speed, alpha:jsName1.alpha, text:jsName1.text } );
		
		var phpName1 = new CodeFloater( { x:-30, y:60, fontSize:'62px Courier New', speed:-0.23, alpha:0.20, text:codeTextSource.PHPName } );
		var phpName2 = new CodeFloater( { x:( phpName1.x + phpName1.metrics.width + 190 ), y:phpName1.y, fontSize:phpName1.fontSize, speed:phpName1.speed, alpha:phpName1.alpha, text:phpName1.text } );
		var phpName3 = new CodeFloater( { x:( phpName2.x + phpName2.metrics.width + 200 ), y:phpName1.y, fontSize:phpName1.fontSize, speed:phpName1.speed, alpha:phpName1.alpha, text:phpName1.text } );
		
		var canvasName1 = new CodeFloater( { x:-200, y:30, fontSize:'50px Courier New', speed:0.35, alpha:0.22, text:codeTextSource.canvasName } );
		var canvasName2 = new CodeFloater( { x:( canvasName1.x + canvasName1.metrics.width + 200 ), y:canvasName1.y, fontSize:canvasName1.fontSize, speed:canvasName1.speed, alpha:canvasName1.alpha, text:canvasName1.text } );
		
		var html5Code1 = new CodeFloater( { x:0, y:-5, fontSize:'20px Courier New', speed:0.55, alpha:0.25, text:codeTextSource.HTML5Code } );
		var html5Code2 = new CodeFloater( { x:( html5Code1.x + html5Code1.metrics.width + 27 ), y:html5Code1.y, fontSize:html5Code1.fontSize, speed:html5Code1.speed, alpha:html5Code1.alpha, text:html5Code1.text } );
		var html5Code3 = new CodeFloater( { x:( html5Code2.x + html5Code2.metrics.width + 27 ), y:html5Code1.y, fontSize:html5Code1.fontSize, speed:html5Code1.speed, alpha:html5Code1.alpha, text:html5Code1.text } );
		var html5Code4 = new CodeFloater( { x:( html5Code3.x + html5Code3.metrics.width + 27 ), y:html5Code1.y, fontSize:html5Code1.fontSize, speed:html5Code1.speed, alpha:html5Code1.alpha, text:html5Code1.text } );
		
		var phpCode1 = new CodeFloater( { x:-30, y:12, fontSize:'20px Courier New', speed:-0.4, alpha:0.27, text:codeTextSource.PHPCode } );
		var phpCode2 = new CodeFloater( { x:( phpCode1.x + phpCode1.metrics.width + 10 ), y:phpCode1.y, fontSize:phpCode1.fontSize, speed:phpCode1.speed, alpha:phpCode1.alpha, text:phpCode1.text } );
		var phpCode3 = new CodeFloater( { x:( phpCode2.x + phpCode2.metrics.width + 10 ), y:phpCode1.y, fontSize:phpCode1.fontSize, speed:phpCode1.speed, alpha:phpCode1.alpha, text:phpCode1.text } );
		var phpCode4 = new CodeFloater( { x:( phpCode3.x + phpCode3.metrics.width + 10 ), y:phpCode1.y, fontSize:phpCode1.fontSize, speed:phpCode1.speed, alpha:phpCode1.alpha, text:phpCode1.text } );
		
		var jsCode1 = new CodeFloater( { x:-150, y:29, fontSize:'28px Courier New', speed:0.52, alpha:0.21, text:codeTextSource.JavaScriptCode } );
		var jsCode2 = new CodeFloater( { x:( jsCode1.x + jsCode1.metrics.width + 10 ), y:jsCode1.y, fontSize:jsCode1.fontSize, speed:jsCode1.speed, alpha:jsCode1.alpha, text:jsCode1.text } );
		
		var loremCode1 = new CodeFloater( { x:0, y:53, fontSize:'italic 24px serif', speed:-0.47, alpha:0.26, text:codeTextSource.loremIpsum } );
		var loremCode2 = new CodeFloater( { x:( theCanvas.width ), y:loremCode1.y, fontSize:loremCode1.fontSize, speed:loremCode1.speed, alpha:loremCode1.alpha, text:loremCode1.text } );		
		
		var htmlCode1 = new CodeFloater( { x:( theCanvas.width ), y:75, fontSize:'25px Courier New', speed:0.30, alpha:0.2, text:codeTextSource.HTMLCode } );	    
		var htmlCode2 = new CodeFloater( { x:( (theCanvas.width/2) - (htmlCode1.metrics.width/2) ), y:htmlCode1.y, fontSize:htmlCode1.fontSize, speed:htmlCode1.speed, alpha:htmlCode1.alpha, text:htmlCode1.text } );
		
		var jQCode1 = new CodeFloater( { x:-250, y:102, fontSize:'20px Courier New', speed:0.62, alpha:0.18, text:codeTextSource.jQueryCode } );
		var jQCode2 = new CodeFloater( { x:( jQCode1.x + jQCode1.metrics.width + 10 ), y:jQCode1.y, fontSize:jQCode1.fontSize, speed:jQCode1.speed, alpha:jQCode1.alpha, text:jQCode1.text } );
		var jQCode3 = new CodeFloater( { x:( jQCode2.x + jQCode2.metrics.width + 10 ), y:jQCode1.y, fontSize:jQCode1.fontSize, speed:jQCode1.speed, alpha:jQCode1.alpha, text:jQCode1.text } );
		var jQCode4 = new CodeFloater( { x:( jQCode3.x + jQCode3.metrics.width + 10 ), y:jQCode1.y, fontSize:jQCode1.fontSize, speed:jQCode1.speed, alpha:jQCode1.alpha, text:jQCode1.text } );

		//console.log('distSpace(loremCode1) = ' + distSpace(loremCode1));

		codeFloatArray.push(
			html5Name1, html5Name2,
			jsName1, jsName2, 
			phpName1, phpName2, phpName3, 
			canvasName1, canvasName2, 
			jsCode1, jsCode2,
			jQCode1, jQCode2, jQCode3, jQCode4,
			htmlCode1, htmlCode2,
			html5Code1, html5Code2, html5Code3, html5Code4, 
			phpCode1, phpCode2, phpCode3, phpCode4,
			loremCode1, loremCode2
		);
	    	    
		//function distSpace(floater) {
		//	var howMany = Math.floor(theCanvas.width / floater.metrics.width);
		//	var totalSpace = theCanvas.width - (floater.metrics.width * howMany);
		//	console.log('distSpace(): floater.metrics.width = ' + floater.metrics.width);
		//	return floater.x + floater.metrics.width + (totalSpace / howMany);
		//}
    }// end of initCodeFloaters()
	function renderCodeFloaters() {
		for (var i = 0; i< codeFloatArray.length; i++) {
			codeFloatArray[i].drawSelf();
		}
	}

	// ===========================================================
	function CodeFloater(settings) { // Bouncer Class
		this.x = settings.x;
		this.y = settings.y;
		this.fontSize = settings.fontSize;
		this.speed = settings.speed;
		this.alpha = settings.alpha;
		this.text = settings.text;
		//var textWidth = metrics.width;
		context.save();
		context.font =  this.fontSize;
		this.metrics = context.measureText(this.text);
		context.restore();
		
		CodeFloater.prototype.drawSelf = function () {
			
			this.updatePosition();
			
			context.save();
			context.font =  this.fontSize;
			context.globalAlpha = this.alpha;
			context.fillText(this.text, this.x, this.y);
			context.restore();
		};
		CodeFloater.prototype.updatePosition = function () {
			if (this.speed > 0 && this.x + this.speed >= theCanvas.width) {
				this.x -= theCanvas.width + this.metrics.width + this.speed;
			}
			else if(this.speed < 0 && this.x + this.speed <= -this.metrics.width) {
				this.x += theCanvas.width + this.metrics.width + this.speed;
			}
			else {
				this.x += this.speed;
			}
		};
		CodeFloater.prototype.getWidth = function(){
			return this.metrics.width;
		};
	} //end of CodeFloater class
}// end of codefloat()
codefloat();
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console