css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

            
              script.
	window.canvasOptions = {
		autoClear: true,
		autoPushPop: true,
		centered: true
	};
            
          
!
            
              let canvas, ctx;

let scl = 10;
let speedLimit = 0.9 * scl;
let friction = 0.75;
let roboto;
let particles = [];
let oldDate = '';
let textStringArgs = [ 0, 0, 2 * scl ];
let timeStringParts = [ 'Hour', 'Minute', 'Second' ].map(n => `get${n}s`);
let textString = '';
let textBounds = {};
let points = {};
let textPoints = [];
let order = [];

function preload() {
	roboto = loadFont('https://alca.tv/static/codepen/pens/common/RobotoMono-Bold.ttf');
}

function setup() {
	canvas = createCanvas(windowWidth, windowHeight);
	ctx = canvas.drawingContext;
	
	for(let i = 0; i < 180 * scl; i++) {
		let p = new Particle();
		p.applyForce(p5.Vector.random2D().mult(random(1 * scl, 2 * scl)));
		particles.push(p);
	}
	
	textBounds = roboto.textBounds('0', ...textStringArgs);
	
	[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ':' ].forEach(renderCharacter);
}

function draw() {
	ctx.clearRect(0, 0, width, height);
	fill(255);
	translate(width / 2, height / 2);
	
	let changed = getDate();
	
	ctx.beginPath();
	
	particles.forEach((p, i) => {
		let index = floor(map(i, 0, particles.length, 0, textPoints.length));
		let textPoint = textPoints[index % textPoints.length];
		
		p	.draw()
			.attractTo(textPoint)
			.update();
	});
	
	ctx.fillStyle = 'hsl(210, 100%, 50%)';
	ctx.fill();
	
	if(changed) { // Rotate the particles
		for(let i = 0; i < particles.length * 0.333; i++) {
			let p = particles.pop();
			p.applyForce(-18 * scl, -10 * scl);
			particles.unshift(p);
		}
	}
}

function renderCharacter(c) {
	let pts = roboto.textToPoints(c + '', ...textStringArgs, { sampleFactor: 0.875 })
		.map(n => createVector(n.x, n.y)
					.add(-textBounds.w * 0.5, textBounds.h * 0.5)
					.sub(8 * textBounds.w * 0.5 + textBounds.advance * 7)
					.mult(scl)
			);
	pts[0].z = -1; // First
	pts[pts.length - 1].z = 1; // Last
	points[c] = pts;
}

function getDate() {
	let date = new Date();
	if(oldDate.toString() === date.toString()) {
		return false;
	}
	oldDate = date;
	textString = timeStringParts
			.map(n => date[n]())
			.map(n => `0${n}`.slice(-2))
			.join(':')
			.split('');
	textPoints = [].concat(...textString.map((n, i) => {
			let offset = (i * textBounds.w * textBounds.advance) * scl;
			return points[n].map(p => p.copy().add(offset, 0));
		}));
	return true;
}

function windowResized() {
	resizeCanvas(windowWidth, windowHeight);
}

class Particle {
	constructor() {
		this.pos = createVector();
		this.vel = createVector();
		this.acc = createVector();
	}
	applyForce(...args) {
		this.acc.add(...args);
		return this;
	}
	attractTo(vec) {
		if(vec === undefined) {
			return this;
		}
		let v = vec	.copy()
					.sub(this.pos)
					.limit(speedLimit);
		this.applyForce(v);
		return this;
	}
	update() {
		let { pos, vel, acc } = this;
		vel.add(acc);
		acc.set(0, 0);
		vel.mult(friction);
		pos.add(vel);
		return this;
	}
	draw() {
		let { pos: { x, y } } = this;
		ctx.moveTo(x + 3, y);
		ctx.arc(x, y, 3, 0, TAU);
		return this;
	}
}
            
          
!
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