cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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.

            
              *
	margin: 0
	filter: brightness(150%)

canvas
	width: 100%
	height: 100vh

body 
	background-color: #403
	overflow: hidden
	
.gradient
	width: 100%
	height: 100vh
	background-color: navy
	z-index: 2
	position: fixed
	bottom: 0
	opacity: 0.3

.ground
	z-index: -1
	content: ''
	position: fixed
	height: 50%
	left: -100%
	bottom: -8%
	width: 300% 
	border-radius: 100%
	background-color: #030

.sky-box
	filter: invert(100%)
	z-index: -1
	height: 100%
	width: 100%
	top: 0
	left: 0
	background: url(https://www.joshmorony.com/wp-content/uploads/2016/04/mountains-back.png) 0% 0% fixed repeat-x
	background-size: 200% 40%
	position: fixed
            
          
!
            
              window.onload = () => {
	const CANVAS = document.getElementsByTagName("canvas")[0];
	const SKY_BOX = document.getElementsByClassName("sky-box")[0];
	const CTX = CANVAS.getContext("2d");
	const SENSITIVITY = 0.04;
	const SPRITES = [];
	const CAMERA_INIT = {
		x: 0,
		y: -1.3,
		z: -3
	};
	const ELEMENTS = {
		trees: {
			number: 50,
			range: {
				x: 10,
				y: 0,
				z: 10
			},
			origin: {
				x: 0,
				y: 0,
				z: 0
			},
			urls: [
				"https://goo.gl/gCzXR4",
				"https://goo.gl/DFsMwX",
				"https://goo.gl/JqoxHz",
				"https://goo.gl/9Su5pW",
				"https://goo.gl/YTQwiJ",
				"https://goo.gl/rUQVhu"
			]
		},
		clouds: {
			number: 30,
			range: {
				x: 10,
				y: 0.5,
				z: 10
			},
			origin: {
				x: 0,
				y: 2,
				z: 0
			},
			urls: ["https://goo.gl/HgEjL9"]
		},
		groundItems: {
			number: 20,
			range: {
				x: 10,
				y: 0,
				z: 10
			},
			origin: {
				x: 0,
				y: 0,
				z: 0
			},
			urls: [
				"https://goo.gl/yJGDoR",
				"https://goo.gl/g4N9Lp",
				"https://goo.gl/Agqp41"
			]
		}
	};

	let width, height, camera, imagesToLoad;
	let worldRot = 0;
	let dWorldRot = 0;
	let time = 0;
	let loadedImages = 0;

	class Vector {
		constructor(x, y, z) {
			this.x = x;
			this.y = y;
			this.z = z;
		}
		project() {
			const DZ = height / (camera.z - this.z);
			const PX = (camera.x + this.x) * DZ;
			const PY = (camera.y + this.y) * DZ;
			return {
				x: PX + width / 2,
				y: PY + height / 2,
				dz: DZ,
				z: this.z
			};
		}
		rotate(ang) {
			const COS = Math.cos(ang);
			const SIN = Math.sin(ang);
			const X = this.x;
			const Y = this.y;
			const Z = this.z;
			this.x = X * COS - Z * SIN;
			this.z = X * SIN + Z * COS;
		}
	}

	class Sprite {
		constructor(v, images) {
			this.sz = 2;
			this.v = v;
			this.image = images[(Math.random() * images.length) | 0];
			this.xl = this.image.naturalWidth * this.sz;
			this.yl = this.image.naturalHeight * this.sz;
		}
		rotate(ang) {
			this.v.rotate(ang);
		}
		render() {
			const P = this.v.project();
			const SZ = P.dz / height;
			const X_OFFS = this.xl / 6;
			if (outOfBounds(P.z, P.x + X_OFFS, P.y, this.xl * SZ, this.yl * SZ)) return;
			CTX.beginPath();
			CTX.drawImage(this.image, P.x + X_OFFS, P.y, this.xl * SZ, this.yl * SZ);
			CTX.closePath();
		}
	}

	function outOfBounds(z, x, y, xl, yl) {
		return z <= camera.z || x < 0 || y < 0 || x >= width - xl || y >= height - yl;
	}

	function signedRandom(n) {
		return n * (Math.random() - Math.random());
	}

	const renderSprites = () => {
		SPRITES.sort((a, b) => b.v.z - a.v.z);
		for (let s of SPRITES) {
			s.rotate(dWorldRot);
			s.render();
		}
	};

	const updateSkyBox = () => {
		SKY_BOX.style.backgroundPosition = -(worldRot / Math.PI * 2) * 100 + "%";
	};

	const loop = () => {
		worldRot += dWorldRot;
		CTX.clearRect(0, 0, width, height);
		updateSkyBox();
		renderSprites();
		++time;
		requestAnimationFrame(loop);
	};

	const create = e => {
		for (let i = 0; i < e.number; ++i) {
			const X = e.origin.x + signedRandom(e.range.x);
			const Y = e.origin.y + signedRandom(e.range.y);
			const Z = e.origin.z + signedRandom(e.range.z);
			SPRITES.push(new Sprite(new Vector(X, Y, Z), e.images));
		}
	};

	const go = () => {
		camera = new Vector(CAMERA_INIT.x, CAMERA_INIT.y, CAMERA_INIT.z);
		for (let i in ELEMENTS) create(ELEMENTS[i]);
		loop();
	};

	const setDim = () => {
		width = window.innerWidth;
		height = window.innerHeight;
		CANVAS.width = width;
		CANVAS.height = height;
	};

	const handleLoadImage = () => {
		++loadedImages;
		if (loadedImages === imagesToLoad) go();
	};

	const preload = function(o) {
		o.images = [];
		for (let i = 0; i < o.urls.length; ++i) {
			o.images.push(new Image());
			o.images[o.images.length - 1].src = o.urls[i];
			o.images[o.images.length - 1].addEventListener("load", handleLoadImage);
		}
	};

	const preloadImages = () => {
		imagesToLoad = (() => {
			let total = 0;
			for (let i in ELEMENTS) {
				total += ELEMENTS[i].urls.length;
			}
			return total;
		})();
		for (let i in ELEMENTS) {
			preload(ELEMENTS[i]);
		}
	};

	const handleMouseMove = e => {
		const MOUSE_X = event.clientX - width / 2;
		dWorldRot = -MOUSE_X / width * SENSITIVITY;
	};

	const listen = () => {
		document.addEventListener("mousemove", handleMouseMove, false);
		window.onresize = setDim;
	};

	(() => {
		setDim();
		updateSkyBox();
		preloadImages();
		listen();
	})();
};

            
          
!
999px
Loading ..................

Console