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

HTML

              
                //- We can VJ with this pen!
//- ------------------------
//- Desktop & Mobile:
//- Canvas click or touch switches the shapes' order + color background and stroke

// Desktop:
//- Left + right arrow keys switch the shapes' order 
//- 'B' key swaps color background and stroke
              
            
!

CSS

              
                body {
	background-color: hotpink;
	margin: 0;
	padding: 0;
	overflow: hidden;
}
              
            
!

JS

              
                /* 
 * VJ SHAPE SHIFTER
 * Made with p5 - Enjoy!
 *
 * This is a remix from another pen I did: 
 * https://codepen.io/ilithya/pen/eYYVOmj
 *
 * Only the shape morph functions are a remix from original code by Golan Levin's Circle Morphing repository:
 * https://github.com/golanlevin/circle-morphing
 *
 * #codevember 07 - SHAPE
 * #023 - #100DaysOfCode
 * By ilithya | 2019
 */

const colorLight = 'hotpink';
const colorDark = '#fffffd'; // white

let bgColor = colorLight;
let strokeColor = colorDark;

function setup() {
	createCanvas(windowWidth, windowHeight);
	fill(strokeColor);
	frameRate(4);
}

let createMorphShapes = () => shape.runMorphGroupOne();
function draw() {
	background(bgColor);
	stroke(strokeColor);

	createMorphShapes();
	createMorphShapes();
	createMorphShapes();
}

// Let's VJ a little :)
// Only Desktop:
// We can switch the shapes' order with the arrow keys
// And swap color background and stroke with the 'b' key
function keyPressed() {
	if (keyCode === RIGHT_ARROW) {
		morph.nextMorphGroup();
	} else if (keyCode === LEFT_ARROW) {
		morph.prevMorphGroup();
	} else if (keyCode === 66) {
		bgColor = bgColor === colorLight ? colorDark : colorLight;
		strokeColor = strokeColor === colorDark ? colorLight : colorDark;
	}
}

// Desktop and Mobile:
// We can switch the shapes' order + color background and stroke with a canvas click or touch
function mousePressed() {
	changeShapesAndBg();
}
function touchStarted() {
	changeShapesAndBg();
}

const shape = {
	circle: 30,
	square: 4,
	triangle: 3,
	timing: 400,
	size() {
		return width / 10;
	},
	amount(t) {
		// 0.8 - By Golan Levin, January 2017
		return pow(0.5 + 0.5 * sin(millis() / t), 2);
	},
	cx() { 
		return width / 2;
	},
	cy() { 
		return height / 2;
	},
	circleToTriangle(shapeSize) {
		// Based on Golan Levin's morphing code, January 2017
		const third = TWO_PI / random(this.triangle/2, this.triangle*2);
		const a = this.amount(this.timing);

		beginShape();
		for (let j = 0; j < this.triangle; j++) {
			for (let i = 0; i <= this.circle; i++) {
				const angCenter = (j + 0.5) * third;
				const angA = angCenter - a * 0.5 * third;
				const angB = angCenter + a * 0.5 * third;
				const t = map(i, 0, this.circle, angA, angB) + HALF_PI;
				const px = this.cx() + shapeSize * cos(t);
				const py = this.cy() + shapeSize * sin(t);
				vertex(px, py);
			}
		}
		endShape(CLOSE);
	},
	triangleToSquare(shapeSize) {
		// Based on Golan Levin's morphing code, January 2017
		const quarter = TWO_PI / random(this.square/2, this.square*2);	
		const a = this.amount(this.timing);

		beginShape();
		for (let j = 0; j < this.square; j++) {
			for (let i = 0; i <= this.triangle; i++) {
				const angCenter = (i + 0.5) * quarter;
				const angA = angCenter - a * 0.5 * quarter;
				const angB = angCenter + a * 0.5 * quarter;
				const t = map(i, 0, this.triangle, angA, angB) + HALF_PI;
				const px = this.cx() - shapeSize * cos(t);
				const py = this.cy() - shapeSize * sin(t);
				vertex(px, py);
			}
		}
		endShape(CLOSE);
	},
	squareToCircle(shapeSize) {
		// Based on Golan Levin's morphing code, January 2017
		const quarter = TWO_PI / random(this.square/2, this.square*2);	
		const a = this.amount(this.timing * -1);

		beginShape();
		for (let j = 0; j < this.square; j++) {
			for (let i = 0; i <= this.square; i++) {
				const angCenter = (i + 0.5) * quarter;
				const angA = angCenter - a * 0.5 * quarter;
				const angB = angCenter + a * 0.5 * quarter;
				const t = map(i, 0, this.square/2, angA, angB) + HALF_PI;
				const px = this.cx() + shapeSize * cos(t);
				const py = this.cy() + shapeSize * sin(t);
				vertex(px, py);
			}
		}
		endShape(CLOSE);
	},
	runMorphGroupOne() {
		const s = this.size();
		
		this.circleToTriangle(s*1.5);
	
		push();
		translate(s*-2.5, 0);
		this.triangleToSquare(s*1.05);
		pop();

		push();
		translate(s*2.5, s/-5);
		this.squareToCircle(s);
		pop();
	},
	runMorphGroupTwo() {
		const s = this.size();
		
		this.squareToCircle(s*1.2);
	
		push();
		translate(s*-1.1, s/4);
		this.circleToTriangle(s);
		pop();

		push();
		translate(s*1.2, s/-3);
		this.triangleToSquare(s*1.05);
		pop();
	},
	runMorphGroupThree() {
		const s = this.size();
		
		this.triangleToSquare(s);
	
		push();
		translate(s*-1.3, s*-0.5);
		this.squareToCircle(s);
		pop();

		push();
		translate(s*1.5, s*0.5);
		this.circleToTriangle(s/1.5);
		pop();
	},
};

let morphIndex = 0;
const morph = {
	array: [
		// Existing shape group morphing functions
		'runMorphGroupOne', 
		'runMorphGroupTwo', 
		'runMorphGroupThree', 
	],
	updateMorphGroup() {
		const currentMorph = this.array[morphIndex];
		const updateMorph = new Function(`shape.${currentMorph}()`);
		// Let's switch the morph function
		createMorphShapes = () => updateMorph();	
	},
	nextMorphGroup() {
		// Update morph array index after last selection
		morphIndex = morphIndex >= this.array.length - 1 ? 0 : ++morphIndex;

		this.updateMorphGroup();
	},
	prevMorphGroup() {
		// Update morph array index after last selection
		morphIndex = morphIndex >= 1 ? --morphIndex : this.array.length - 1;

		this.updateMorphGroup();	
	},
};

const changeShapesAndBg = () => {
	morph.nextMorphGroup();
	bgColor = bgColor === colorLight ? colorDark : colorLight;
	strokeColor = strokeColor === colorDark ? colorLight : colorDark;
};
              
            
!
999px

Console