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. 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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <link href='https://fonts.googleapis.com/css?family=Share+Tech+Mono' rel='stylesheet' type='text/css'>
              
            
!

CSS

              
                body {
	margin: 0;
	color: #fff;
	background-color: #000;
	background-image: url(https://i.imgur.com/1LbJT5w.jpg);
	background-position: center center;
	background-size: cover;
	font-family: monospace;
	font-size: 2em;
	position: fixed;
}
p, a {
	margin: 0;
}
body, div, p, a {
	-moz-user-select: none; 
	-khtml-user-select: none; 
	-webkit-user-select: none; 
	-o-user-select: none; 
}
#container {
	width: 100vw;
	height: 100vh;
	margin: 0;
	padding: 0;
}
#menu {
	width: 240px;
	height: 100vh;
	position: fixed;
	top: 0;
	left: 0;
	background-color: #000;
	box-shadow: 0px 4px 4px 4px rgba(0, 0, 0, 0.5);
	z-index: 1;
	overflow-x: hidden;
	overflow-y: scroll;
}
#menu ul {
	margin: 0;
	padding: 0;
}
#menu ul li {
	margin: 0;
	padding: 0;
	width: 100%;
	height: 80px;
	list-style-type: none;
}
#menu ul li a {
	color: #fff;
	text-decoration: none;
	width: 100%;
	height: 100%;
	line-height: 80px;
	display: block;
	margin: 0;
	padding-left: 80px;
	background-color: #111;
	border-top: 1px solid #222;
	border-bottom: 1px solid #000;
	position: relative;
	box-sizing:border-box;
	-moz-box-sizing:border-box;
	-webkit-box-sizing:border-box;
	-webkit-transition: backgroundColor 1s;
	-moz-transition: backgroundColor 1s;
	-o-transition: backgroundColor 1s;
	transition: backgroundColor 1s;
}
#menu ul li a p {
	position: absolute;
	margin: 0;
	padding-left: 8px;
	line-height: 80px;
	background-color: transparent;
}
#menu ul li :hover {
	background-color: #222;
}
#menu ul li :hover > p {
	background-color: transparent;
}
#menu ul li :active {
	top: 1px;
	background-color: #111;
}
.menu-portrait {
	position: absolute;
	left: 0;
	width: 60px;
	height: 60px;
	border-radius: 50%;
	background-position: center;
	background-size: cover;
	margin: 10px;
}
.world {
	margin: 0;
	padding: 0;
	position: absolute;
	border-radius: 50%;
	background-size: cover;
	background-position: center;
}
.orb {
	position: absolute;
	width: 4px;
	height: 4px;
	border-radius: 50%;
	background-color: #fff;
	box-shadow: 0 0 1px 1px #fff;
	display: none;
}
.mercury {
	background-color: #999;
	background-image: url(https://i.imgur.com/xrVC2Fd.png);
}
.venus {
	background-color: #b93;
	background-image: url(https://i.imgur.com/FpNgFiR.jpg);
}
.earth {
	background-color: #249;
	background-image: url(https://i.imgur.com/tCfl1IU.png);
}
.moon {
	background-color: #676;
	background-image: url(https://i.imgur.com/jHcZWjp.png);
}
.mars {
	background-color: #a64;
	background-image: url(https://i.imgur.com/1aCHzBR.png);
}
.jupiter {
	background-image: url(https://i.imgur.com/dQeCY3f.png);
}
.europa {
	background-image: url(https://i.imgur.com/wg5vMVN.png);
}
.io {
	background-image: url(https://i.imgur.com/yxui3Wd.jpg);
}
.callisto {
	background-image: url(https://i.imgur.com/OJDz8Cv.png);
}
.ganymede {
	background-image: url(https://i.imgur.com/TXViNzm.png);
}
.uranus {
	background-image: url(https://i.imgur.com/wW0ZI6r.png);
}
.neptune {
	background-image: url(https://i.imgur.com/Lk8MUq2.jpg);
}
              
            
!

JS

              
                var scale = 1;
// Change planet sizes with the above variable.

var container = document.createElement("div");
container.id = "container";
document.body.appendChild(container);

var menu = document.createElement("nav");
menu.id = "menu";
container.appendChild(menu);

var worldObject = {};
var worlds = [];
var worldList = {
	"mercury":{
			size: 48,
			mass: 33,
	},
	"venus":{
			size: 121,
			mass: 486,
	},
	"earth":{
			size: 127,
			mass: 597,
	},
	"moon":{
			size: 34,
			mass: 7,
	},
	"mars":{
			size: 68,
			mass: 64,
	},
	"jupiter":{
			size: 1398,
			mass: 189860,
	},
	"europa":{
			size: 31,
			mass: 5,
	},
	"io":{
			size: 36,
			mass: 9,
	},
	"callisto":{
			size: 48,
			mass: 11,
	},
	"ganymede":{
			size: 52,
			mass: 15,
	},
	"uranus":{
			size: 507,
			mass: 8681,
	},
	"neptune":{
			size: 492,
			mass: 10243,
	},
};

var menuUL = document.createElement("ul");
menu.appendChild(menuUL);

var menuItems = [];
for(var item in worldList){
	var menuItem = document.createElement("li");
	menuUL.appendChild(menuItem);
	var menuLink = document.createElement("a");
	menuItem.appendChild(menuLink);
	var menuPortrait = document.createElement("div");
	menuPortrait.className = "menu-portrait "+item;
	var menuItemText = document.createElement("p");
	menuItemText.innerHTML = item.toUpperCase();
	menuLink.appendChild(menuItemText);
	menuLink.appendChild(menuPortrait);
	menuItem.type = item;
	menuItems.push(menuItem);
}

function distanceSort(a, b){
	if(a.distance < b.distance)return -1;
	if(a.distance > b.distance)return 1;
	return 0;
}

function Vector(x, y){
	this.x = x;
	this.y = y;
	this.add = function(x, y){
		// Add two vectors
		if(y === undefined)return new Vector(this.x+x.x, this.y+x.y);
		else return new Vector(x.x+y.x, x.y+y.y);
	}
	this.subtract = function(x, y){
		// Subtract two vectors
		if(y === undefined)return new Vector(this.x-x.x, this.y-x.y);
		else return new Vector(x.x-y.x, x.y-y.y);
	}
	this.multiply = function(x, y){
		// Find dot product between this and another vector
		if(y === undefined)return this.x*x.x + this.y*x.y;
		else return x.x*y.x + x.y*y.y;
	}
	this.divide = function(scalar){
		return new Vector(this.x/scalar, this.y/scalar);
	}
	this.scale = function(scalar){
		return new Vector(this.x*scalar, this.y*scalar);
	}
	this.findMagnitude = function(){
		return Math.sqrt(this.x*this.x+this.y*this.y);
	}
	this.copy = function(){
		return new Vector(this.x, this.y);
	}
	this.magnitude = this.findMagnitude();
}
function MouseHandler(){
	var handler = this;
	this.x = 0;
	this.y = 0;
	this.down = false;
	this.clicked = false;
	this.move = function(e){
		handler.x = e.clientX;
		handler.y = e.clientY;
	}
	this.click = function(e){
		handler.down = true;
		handler.clicked = true;
	}
	this.release = function(e){
		handler.down = false;
	}
	this.over = function(element){
		var rect = element.getBoundingClientRect();
		return this.x < rect.right && this.x > rect.left && this.y < rect.bottom && this.y > rect.top;
	}
	document.onmousemove = this.move;
	document.onmousedown = this.click;
	document.onmouseup = this.release;
}
function Orb(x, y){
	this.x = x;
	this.y = y;
	var element = this.element = document.createElement("div");
	element.style.left = this.x+"px";
	element.style.top = this.y+"px";
	element.className = "orb";
	container.appendChild(element);
	this.update = function(){
		element.style.left = this.x+"px";
		element.style.top = this.y+"px";
	}
}
function makeWorld(type){
	var object = worldList[type];
	new World(
		(window.innerWidth-menu.getBoundingClientRect().right) / 2 + menu.getBoundingClientRect().right - object.size*scale / 2, 
		- object.size*scale*2, 
		object.size, 
		object.size, 
		type, 
		object.mass
	);
}
var friction = 0.995;
var gravity = 0.15;
function World(x, y, width, height, type, mass, size){
	if(type === undefined)type = "earth";
	if(mass === undefined)mass = 100;
	if(size === undefined)size = 1;
	this.x = x;
	this.y = y;
	this.width = width*scale;
	this.height = height*scale;
	this.type = type;
	this.mass = mass;
	this.size = size;
	this.center = {x:this.x+this.width/2, y:this.y+this.height/2};
	this.radius = this.height/2;
	this.dragging = false;
	this.dragOffset = {x: 0, y: 0};
	this.velocity = {};
	this.velocity.x = 0;
	this.velocity.y = 0;
	this.last = {};
	this.friction = friction;
	this.gravity = gravity;
	this.rotation = 0;
	var element = this.element = document.createElement("div");
	element.style.left = this.x+"px";
	element.style.top = this.y+"px";
	element.style.width = this.width+"px";
	element.style.height = this.height+"px";
	element.className = "world "+this.type;
	container.appendChild(element);
	worlds.push(this);
	worldObject[type] = this;
	this.remove = function(){
		container.removeChild(element);
		for(var i = 0; i < worlds.length; i ++){
			var w = worlds[i];
			if(this.x === w.x && this.y === w.y && this.type === w.type){
				worlds.splice(i, 1);
			}
		}
		worldObject[type] = undefined;
	}
	this.updateLast = function(){
		this.last.x = this.x;
		this.last.y = this.y;
	}
	this.update = function(){
		if(this.size < 1){
			this.size += (1 - this.size)/ 32;
		}
		this.distX = this.center.x - mouse.x;
		this.distY = this.center.y - mouse.y;
		this.distance = this.distX*this.distX+this.distY*this.distY;
		for(var i = 0; i < worlds.length; i ++){
			var world = worlds[i];
			if(world.x !== this.x && world.y !== this.y){
				var distX = world.center.x - this.center.x;
				var distY = world.center.y - this.center.y;
				var distTotal = distX*distX + distY*distY;
				var distMax = this.radius + world.radius;
				if(distTotal < distMax*distMax){
					// Collision!
					var n = new Vector(world.center.x - this.center.x, world.center.y - this.center.y);
					var un = n.divide(n.magnitude);
					var ut = new Vector(-un.y, un.x);
					var v1 = new Vector(this.velocity.x, this.velocity.y);
					var v2 = new Vector(world.velocity.x, world.velocity.y);
					var v1n = un.multiply(v1);
					var v1t = ut.multiply(v1);
					var v2n = un.multiply(v2);
					var v2t = ut.multiply(v2);
					var nv1t = v1t;
					var nv2t = v2t;
					var nv1n = (v1n*(this.mass-world.mass)+2*world.mass*v2n) / (this.mass+world.mass);
					var nv2n = (v2n*(world.mass-this.mass)+2*this.mass*v1n) / (this.mass+world.mass);
					var nv1nv = un.scale(nv1n);
					var nv1tv = ut.scale(nv1t);
					var nv2nv = un.scale(nv2n);
					var nv2tv = ut.scale(nv2t);
					var nv1 = nv1nv.add(nv1tv);
					var nv2 = nv2nv.add(nv2tv);

					this.velocity.x = nv1.x;
					this.velocity.y = nv1.y;
					world.velocity.x = nv2.x;
					world.velocity.y = nv2.y;

					var radians = Math.atan2(distY, distX);
					world.x = this.center.x + Math.cos(radians)*distMax + (world.x-world.center.x);
					world.y = this.center.y + Math.sin(radians)*distMax + (world.y-world.center.y);
					world.center.x = world.x + world.width/2;
					world.center.y = world.y + world.height/2;
					var radians = Math.atan2(-distY, -distX);
					this.x = world.center.x + Math.cos(radians)*distMax + (this.x-this.center.x);
					this.y = world.center.y + Math.sin(radians)*distMax + (this.y-this.center.y);
				}
			}
		}
		this.velocity.x *= this.friction;
		//this.velocity.y *= this.friction;
		this.velocity.y += this.gravity;
		this.x += this.velocity.x;
		this.y += this.velocity.y;
		if(this.x < menu.getBoundingClientRect().right){
			this.x = menu.getBoundingClientRect().right;
			this.velocity.x *= -1;
		}
		if(this.y < -window.innerHeight/2 && this.velocity.y < 0){
			this.y = -window.innerHeight/2;
			this.velocity.y = 0.5;
		}
		if(this.x > window.innerWidth-this.width){
			this.x = window.innerWidth-this.width;
			this.velocity.x *= -1;
		}
		if(this.y > window.innerHeight-this.height){
			this.y = window.innerHeight-this.height;
			this.velocity.y *= -0.75;
		}
		this.rotation += this.velocity.x;
		while(this.rotation < 0)this.rotation += 360;
		while(this.rotation > 360)this.rotation -= 360;
		this.center.x = this.x+this.width/2;
		this.center.y = this.y+this.height/2;
		this.width = width*scale*this.size;
		this.height = height*scale*this.size;
		this.radius = this.height / 2;
		element.style.left = Math.round(this.x)+"px";
		element.style.top = Math.round(this.y)+"px";
		element.style.width = Math.round(this.width)+"px";
		element.style.height = Math.round(this.height)+"px";
		var rotationString = "rotate("+Math.round(this.rotation)+"deg)";
		element.style.webkitTransform = rotationString;
		element.style.mozTransform = rotationString;
		element.style.oTransform = rotationString;
		element.style.transform = rotationString;
	}
}

var mouse = new MouseHandler();

var fps = 120;
var enterFrameInterval = 1000/fps;

var dragging = false;

var orb = new Orb(512, 512);

makeWorld("earth");
function onEnterFrame(){
	worlds.sort(distanceSort);
	if(mouse.down){
		for(var i = 0; i < worlds.length; i ++){
			var world = worlds[i];
			var distX = world.center.x - mouse.x;
			var distY = world.center.y - mouse.y;
			var distance = distX*distX + distY*distY;
			if(world.dragging || mouse.clicked && distance < world.radius*world.radius){
				dragging = true;
				if(!world.dragging){
					world.dragOffset.x = distX;
					world.dragOffset.y = distY;
				}
				world.dragging = true;
				world.updateLast();
				var targetX = mouse.x - world.width/2 + world.dragOffset.x;
				var targetY = mouse.y - world.height/2 + world.dragOffset.y;
				distX = targetX-world.x;
				distY = targetY-world.y;
				distance = Math.sqrt(distX*distX + distY*distY);
				var radians = Math.atan2(distY, distX);
				world.velocity.x = Math.cos(radians)*distance/32;
				world.velocity.y = Math.sin(radians)*distance/32;
			}
		}
	} else {
		if(dragging){
			for(var i = 0; i < worlds.length; i ++){
				var world = worlds[i];
				if(world.dragging){
					world.velocity.x = world.x - world.last.x;
					world.velocity.y = world.y - world.last.y;
					world.dragging = false;
				}
			}
			dragging = false;
		}
	}
	if(worlds.length > 0){
		var world = worlds[0];
		var radians = Math.atan2(-world.distY, -world.distX);
		orb.x = world.center.x+Math.cos(radians)*world.radius;
		orb.y = world.center.y+Math.sin(radians)*world.radius;
		for(var i = 0; i < worlds.length; i ++){
			var world = worlds[i];
			world.update();
		}
	}
	orb.update();
	if(mouse.clicked){
		for(var i = 0; i < menuItems.length; i ++){
			var item = menuItems[i];
			var object = worldList[item.type];
			if(mouse.over(item)){
				if(worldObject[item.type] === undefined){
					makeWorld(item.type);
				} else {
					worldObject[item.type].remove();
					worldObject[item.type] = undefined;
				}
			}
		}
	}
	mouse.clicked = false;
}

var enterFrame = setInterval(onEnterFrame, enterFrameInterval);
              
            
!
999px

Console