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.

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

            
              canvas.background
canvas.canvas
a.codepen-link(href='https://www.codepen.io/seanfree' target='_blank')
            
          
!
            
              body
	background: black
.canvas
	mix-blend-mode: screen
.canvas, .background, .overlay
	position: absolute
	width: 100vw
	height: 100vh
.codepen-link
	position: absolute
	bottom: 30px
	right: 30px
	height: 40px
	width: 40px
	z-index: 10
	border-radius: 50%
	box-sizing: border-box
	background-image: url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/544318/logo.jpg')
	background-position: center center
	background-size: cover
	opacity: 0.5
	transition: all 0.25s
	&:hover
		opacity: 0.8
		box-shadow: 0 0 6px #efefef
            
          
!
            
              const PI = Math.PI;
const TAU = 2 * PI;
const cos = Math.cos;
const sin = Math.sin;

let canvas, background, creature, mouse;
let tick = 0;
let opts = {
	animation: {
		breathe: true,
		spin: true,
		followMouse: true,
		type: "mobius",
		speed: 4
	},
	motion: {
		rigidity: 0.7,
		responsiveness: 0.3
	},
	color: {
		startHue: 290,
		glow: 0.4
	},
	structure: {
		baseRadius: 40,
		armCount: 26,
		segmentLength: 18,
		segmentMin: 6,
		segmentMax: 28,
		segmentNum: () =>
			Math.round(Math.random() * (opts.structure.segmentMax - opts.structure.segmentMin) + opts.structure.segmentMin)
	},
	noise: {
		enabled: true,
		strength: 0.5,
		type: "rough"
	},
	reset: () => {
		noise.seed(Math.round(2000 * Math.random()));
		creature = new Creature(opts.structure.armCount);
		ctx.clearRect(0, 0, canvas.dimensions.x, canvas.dimensions.y);
	}
};

class Mouse {
	constructor() {
		this.hover = false;
		this.position = new Vector2(
			0.5 * window.innerWidth,
			0.5 * window.innerHeight
		);
		this.animationTarget = new Vector2(
			this.position.x, this.position.y
		);
		this.animate = {
			random: () => {
				if (tick % 180 === 0) {
					this.animationTarget.x = Math.random() * canvas.dimensions.x;
					this.animationTarget.y = Math.random() * canvas.dimensions.y;
				}
				this.position.lerp(this.animationTarget, 0.35);
			},
			mobius: () => {
				this.animationTarget.lerp({
					x: 0.5 * canvas.dimensions.x + (0.35 * canvas.dimensions.x) * cos(tick * 0.0125),
					y: 0.5 * canvas.dimensions.y + (0.35 * canvas.dimensions.y) * sin(tick * 0.025)
				}, 0.85);
				this.position.lerp(this.animationTarget, 0.65);
			},
			idle: () => { 
				this.animationTarget.x = 0.5 * canvas.dimensions.x;
				this.animationTarget.y = 0.5 * canvas.dimensions.y;
				this.position.lerp(this.animationTarget, 1);
			}
		};
	}
}

class Segment {
	constructor(x, y, len, angle, parent, index) {
		this.index = index;
		switch(opts.noise.type) {
			case "rough" :
				this.tick = Math.round(Math.random() * 10 * opts.structure.armCount);
				break;
			case "even" :
				this.tick = 0;
				break;
			case "gradient" :
				this.tick = index * 200;
				break;
			case "fractal" :
				this.tick = noise.simplex3(x * 0.015, y * 0.015, index * 0.015) * 200;
				break;
			default :
				break;
		}
		this.jointSize = Math.random() * 2.5;
		this.parent = parent;
		this.len = len;
		this.angle = angle;
		this.position = new Vector2(x + this.len * cos(this.angle), y + this.len * sin(this.angle));
		this.velocity = new Vector2();
		this.head = new Vector2(
			this.position.x + this.len * cos(this.angle),
			this.position.y + this.len * sin(this.angle)
		);
		this.color = 'hsla(0,0%,0%,0)';
		this.renderProps = {
			line: {
				x1: this.position.x,
				y1: this.position.y,
				x2: this.head.x,
				y2: this.head.y,
				w: 1,
				c: this.color
			},
			arcLine: {
				x: this.head.x,
				y: this.head.y,
				r: this.jointSize,
				s: 0,
				e: TAU,
				c: this.color
			}
		};
	}
	update() {
		this.tick++;
		this.angle = Math.atan2(
			this.parent.position.y - this.position.y,
			this.parent.position.x - this.position.x
		);
		this.head.x = this.parent.position.x;
		this.head.y = this.parent.position.y;
		this.position.lerp(
			{
				x: this.head.x - this.len * cos(this.angle),
				y: this.head.y - this.len * sin(this.angle)
			},
			opts.motion.rigidity
		);
		if (opts.noise.enabled) {	
			let noiseVal = noise.simplex3(this.head.x * 0.0025, this.head.y * 0.0025, this.tick * 0.005) * -TAU;
			this.velocity.x = (cos(noiseVal) * opts.noise.strength);
			this.velocity.y = (sin(noiseVal) * opts.noise.strength);
			this.position.add(this.velocity);
		}
		this.color = `hsla(${-8 * this.index + opts.color.startHue},50%,50%,${1 / (this.index + 0.1)})`;
		this.renderProps = {
			line: {
				x1: this.position.x,
				y1: this.position.y,
				x2: this.head.x,
				y2: this.head.y,
				w: 1,
				c: this.color
			},
			arcLine: {
				x: this.head.x,
				y: this.head.y,
				r: this.jointSize,
				s: 0,
				e: TAU,
				c: this.color
			}
		};
	}
	draw() {
		for (let prop in this.renderProps) {
			canvas[prop](this.renderProps[prop]);
		}
	}
}

class Arm {
	constructor(segmentCount, parent, radius, angle) {
		this.tick = 0;
		this.parent = parent;
		this.segmentCount = segmentCount;
		this.segments = [];
		this.radius = radius;
		this.angle = angle;
		
		let x, y;
		for (let i = 0; i < this.segmentCount; i++) {
			x = this.segments[i - 1]
				? this.segments[i - 1].position.x
				: parent.position.x;
			y = this.segments[i - 1]
				? this.segments[i - 1].position.y
				: parent.position.y;
			this.segments.push(
				new Segment(
					x,
					y,
					opts.structure.segmentLength - i > 2 ? opts.structure.segmentLength - i : 2,
					this.angle,
					this.segments[i - 1] || {
						position: new Vector2(parent.position.x, parent.position.y)
					}, i
				)
			);
		}
	}
	update() {
		this.tick++;
		this.radius = opts.structure.baseRadius;
		if (opts.animation.breathe)
			this.radius += (0.5 * opts.structure.baseRadius * cos(this.tick * 0.05));
		if (opts.noise.enabled) 
			 this.radius += (noise.simplex3(0, 0, this.tick * 0.0075) + 1) * 5;
		for (let i = 0; i < this.segmentCount; i++) {
			this.segments[0].parent.position.x =
				this.parent.position.x +
				(this.radius - i * 50) * cos(this.angle + (opts.animation.spin ? this.tick * 0.0125 : 0));
			this.segments[0].parent.position.y =
				this.parent.position.y +
				(this.radius - i * 50) * sin(this.angle + (opts.animation.spin ? this.tick * 0.0125 : 0));
			this.segments[i].update();
			this.segments[this.segments.length - i - 1].draw();
		}
	}
}

class Creature {
	constructor() {
		this.tick = 0;
		this.arms = [];
		this.armCount = opts.structure.armCount;
		this.velocity = new Vector2(0, 0);
		this.position = new Vector2(
			0.5 * window.innerWidth,
			0.5 * window.innerHeight
		);
		for (var i = 0; i < this.armCount; i++) {
			this.arms.push(
				new Arm(opts.structure.segmentNum(), this, opts.structure.baseRadius, i / this.armCount * TAU)
			);
		}
	}
	animate() {
		this.tick++;
		if (opts.noise.enabled) {
			this.velocity.lerp(
				{
					x:
						cos(
							noise.simplex3(
								this.position.x * 0.0025,
								this.position.y * 0.0025,
								this.tick * 0.0025
							) * PI
						) * opts.noise.strength,
					y:
						sin(
							noise.simplex3(
								this.position.x * 0.0025,
								this.position.y * 0.0025,
								this.tick * 0.0025
							) * PI
						) * opts.noise.strength
				},
				0.0175
			);
		}
		this.position.add(this.velocity);
		this.position.lerp(mouse.position, 0.05 * opts.motion.responsiveness);
		for (let i = 0; i < this.armCount; i++) {
			this.arms[i].update();
		}
	}
}

class Canvas {
	constructor(selector) {
		this.element =
			document.querySelector(selector) ||
			(() => {
				let element = document.createElement("canvas");
				element.style = `position: absolute; width: 100vw; height: 100vh;`;
				document.body.appendChild(element);
				return element;
			})();
		this.ctx = this.element.getContext("2d");
		this.frame = document.createElement("canvas");
		this.buffer = this.frame.getContext("2d");
		this.dimensions = {
			x: 0,
			y: 0
		};
		window.addEventListener("resize", this.resize.bind(this));
		this.resize();
	}
	resize() {
		this.dimensions.x = this.frame.width = this.element.width = window.innerWidth;
		this.dimensions.y = this.frame.height = this.element.height = window.innerHeight;
	}
	fill(c) {
		this.buffer.fillStyle = c;
		this.buffer.fillRect(0, 0, this.dimensions.x, this.dimensions.y);
	}
	clear() {
		this.ctx.clearRect(0, 0, this.dimensions.x, this.dimensions.y);
		this.buffer.clearRect(0, 0, this.dimensions.x, this.dimensions.y);
	}
	clearAt({x1, y1, x2, y2}) {
		this.ctx.clearRect(x1, y1, x2, y2);
		this.buffer.clearRect(x1, y1, x2, y2);
	}
	line({x1, y1, x2, y2, w, c}) {
		this.buffer.beginPath();
		this.buffer.strokeStyle = c;
		this.buffer.lineWidth = w;
		this.buffer.moveTo(x1, y1);
		this.buffer.lineTo(x2, y2);
		this.buffer.stroke();
		this.buffer.closePath();
	}
	arcLine({x, y, r, s, e, c}) {
		this.buffer.beginPath();
		this.buffer.strokeStyle = c;
		this.buffer.arc(x, y, r, s, e);
		this.buffer.stroke();
		this.buffer.closePath();
	}
	render() {
		this.ctx.drawImage(this.frame, 0, 0);
	}
	drawImage({image, x, y}) {
		this.buffer.drawImage(image, x, y);
	}
}

function initGUI() {
	let gui = new dat.GUI();
	
	let f1 = gui.addFolder("animation");
	f1.open();
	f1.add(opts.animation, "type", ["random", "mobius", "idle"]);
	f1.add(opts.animation, "spin");
	f1.add(opts.animation, "breathe");
	f1.add(opts.animation, "followMouse").onFinishChange(() => mouse.hover = false);
	
	let f2 = gui.addFolder("noise");
	f2.open();
	f2.add(opts.noise, "enabled");
	f2.add(opts.noise, "strength").step(0.1).min(0).max(1);
	f2.add(opts.noise, "type", ["rough", "even", "gradient", "fractal"]).onFinishChange(opts.reset);
	
	let f3 = gui.addFolder("motion");
	f3.open();
	f3.add(opts.motion, "rigidity").step(0.1).min(0.1).max(1);
	f3.add(opts.motion, "responsiveness").step(0.1).min(0.1).max(2);
	
	let f4 = gui.addFolder("color");
	f4.open();
	f4.add(opts.color, "startHue").step(1).min(0).max(360);
	f4.add(opts.color, "glow").step(0.1).min(0).max(0.9);
	
	let f5 = gui.addFolder("structure");
	f5.open();
	f5.add(opts.structure, "baseRadius").step(1).min(5).max(80);
	f5.add(opts.structure, "armCount").step(1).min(4).max(48).onFinishChange(opts.reset);
	f5.add(opts.structure, "segmentLength").step(1).min(5).max(40).onFinishChange(opts.reset);
	f5.add(opts.structure, "segmentMin").step(1).min(2).max(12).onFinishChange(opts.reset);
	f5.add(opts.structure, "segmentMax").step(1).min(12).max(36).onFinishChange(opts.reset);
	
	gui.add(opts, "reset");
}

function drawForeground() {
	canvas.clear();
	creature.animate();
	canvas.buffer.save();
	canvas.buffer.globalCompositeOperation = "lighter";
	canvas.buffer.restore();
	canvas.render();
}

function drawBackground() {
	background.fill(`hsla(0,0%,2%,${1 - opts.color.glow})`);
	background.buffer.save();
	background.buffer.filter = 'blur(5px)';
	background.drawImage({
		image: canvas.frame,
		x: 0,
		y: 0
	});
	background.buffer.restore();
	background.render();
}

function loop() {
	tick++;
	if (!mouse.hover) mouse.animate[opts.animation.type]();
	drawForeground();
	drawBackground();
	window.requestAnimationFrame(loop);
}

window.requestAnimationFrame = (() => {
	return (
		window.requestAnimationFrame ||
		window.webkitRequestAnimationFrame ||
		window.mozRequestAnimationFrame ||
		window.oRequestAnimationFrame ||
		window.msRequestAnimationFrame ||
		function(callback) {
			window.setTimeout(callback, 1000 / 60);
		}
	);
})();

window.addEventListener("mousemove", (e) => {
	if (opts.animation.followMouse) {
		mouse.position.x = e.clientX;
		mouse.position.y = e.clientY;
		mouse.hover = true;
	} else {
		mouse.hover = false;
	}
});

window.addEventListener("mouseout", () => {
	mouse.hover = false;
});

window.addEventListener("load", () => {
	noise.seed(Math.round(2000 * Math.random()));
	canvas = new Canvas(".canvas");
	background = new Canvas(".background");
	mouse = new Mouse();
	creature = new Creature(opts.armCount);

	initGUI();
	loop();
});

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

Console