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

              
                <div class="hero"></div>

<div class="canvas-wrapper">
    <div class="fader-top"></div>
    <canvas id="starfield"></canvas>
    <div class="fader-bot"></div>
</div>

<div class="footer"></div>
              
            
!

CSS

              
                $dark: #19212B;

* {
	margin: 0;
	padding: 0;
	box-sizing: border-box;
}

body {
  	background: $dark;
}

.hero, .footer {
	width: 100%;
	height: 50vh;
	position: relative;
}

.canvas-wrapper {
	position: relative;
	display: flex;
	align-items: center;
	justify-content: center;
	text-align: center;
}

#starfield {
	width: 100%;
	height: 100vh;
	position: relative;
}

.fader-top, .fader-bot {
	width: 100%;
	height: 10vh;
	position: absolute;
	z-index: 1;
}

.fader-top {
	top: 0vh;
	background: linear-gradient(to top, transparent, $dark);
}

.fader-bot {
	bottom: 0vh;
	background: linear-gradient(to bottom, transparent, $dark);
}
              
            
!

JS

              
                // TO DO:
// 1. Weight randomness of smaller stars so there are more of them and fewer large ones
// 2. Make distant stars twinkle

console.clear();

const debounce = (callback, wait) => {
	let timeoutId = null;
	return (...args) => {
		window.clearTimeout(timeoutId);
		timeoutId = window.setTimeout(() => {
			callback.apply(null, args);
		}, wait);
	};
}

function starfieldInit() {
	
    const TAO = Math.PI * 2;
    const numStars = 150;
    const canvas = document.querySelector("#starfield");
    const ctx = canvas.getContext("2d");
    const stars = [];
	const starTweens = [];
	
    let vw = canvas.width = innerWidth;
    let vh = canvas.height = innerHeight;
	
    class Star {
        constructor({ x, y, radius }) {
            this.x = x;
            this.y = y; 
            this.radius = radius;
        }
		
        draw() {
            ctx.beginPath();
            ctx.arc(this.x, this.y, this.radius, 0, TAO);
            ctx.shadowBlur = 10;
            ctx.shadowColor = '#00aaff';
           	ctx.fillStyle = 'rgba(255, 255, 255, ' + this.radius * 0.125 + ')';
            ctx.fill();
        }
    }
	
    function createStars() {
        for (let i = 0; i < numStars; i++) {
			// const newRadius = gsap.utils.random(1, 5, 1);
			
			// const newRadius = gsap.utils.random(4, 20, 1) / 4; // quadruple the variety

			// const rnd = gsap.utils.random(4, 20, 1) / 4;
			// const newRadius = rnd < 2.5 ? rnd : gsap.utils.random(4, 20, 1) / 4; // second roll for a lower value
			
			const rndArray = [1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 4, 5]
			const newRadius = gsap.utils.random(rndArray);
            const newX = gsap.utils.random(0, vw);
            const newY = vh + (newRadius * 2);
            const newStar = new Star({ x: newX, y: newY, radius: newRadius });
            
            stars.push(newStar);
        }   
    }
	
	function animateStars() {
		stars.forEach(moveStar);
		function moveStar(star, i) {

			const minDuration = 30,
				maxDuration = 90,
				minRadius = 1,
				maxRadius = 5,
				normalizedRadius = (star.radius - minRadius) / (maxRadius - minRadius),
				duration = (1 - normalizedRadius) * (maxDuration - minDuration) + minDuration;

			// all stars are single tweens, starting from the bottom and going to the top (of the viewport)
			const starAnim = gsap.to(star, {
				duration: duration,
				y: () => 0 - (star.radius * 2), // star begins at bottom, tweens to the top
				ease: "none",
				repeat: -1
			});
			
			starAnim.progress(gsap.utils.random(0, 0.99)); // each star begins at a random progress point of its tween
			starAnim.eventCallback("onReverseComplete", function() { // on reverse (when a star exits the bottom of the screen)...
				starAnim.progress(1); // ...its progress moves to the end (i.e. to the top of the viewport)
			});
			starTweens.push(starAnim);
		}
	}
	
	gsap.ticker.add(updateCanvas);
	function updateCanvas() {
		ctx.clearRect(0, 0, vw, vh);
		for (let i = 0; i < numStars; i++) {
			stars[i].draw();
		}
	}
	
	function startStars() {
		createStars();
		animateStars();
	}
	startStars();

	function restartStars() {
		stars.length = 0;
		starTweens.length = 0;
		
		vw = canvas.width = innerWidth;
		vh = canvas.height = innerHeight;
		ctx.clearRect(0, 0, vw, vh);
		
		startStars();
	}

	window.addEventListener('resize', debounce(e => {
		restartStars();
	}, 100));

	ScrollTrigger.create({
		trigger: ".canvas-wrapper",
		pin: true,
		start: "0%",
		end: "+600%",
		scrub: 2,
		onUpdate: self => {

			let velocity = self.getVelocity();
			let speed = velocity/250;
			
			starTweens.forEach(function(starTween) {
				starTween.timeScale(speed);
				gsap.to(starTween, {
					overwrite: true,
					timeScale: 1
				});
			});

		}
	});

}

starfieldInit();
              
            
!
999px

Console