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

              
                #canvas-wrapper(aria-label='dandelion-dahlia flowers')
              
            
!

CSS

              
                * {
	user-select: none;
}

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

JS

              
                /*
 * DANDELIONS DAHLIAS
 * Made with ThreeJS - Enjoy!
 *
 * Use cursor to move around the floating animation. 
 * On mobile touch + drag screen.
 *
 * Hover the flowers to change their material and light effect.
 * On mobile touch the flowers to change them, and touch anywhere else on the screen to go back to default material.
 *
 * If you have any performance tips on this code, I'm all ears!
 *
 * #043 - #100DaysOfCode
 * By ilithya | 2020
 * https://www.ilithya.rocks/
 * https://twitter.com/ilithya_net
 */

const nearDist = 1;
const farDist = 1000;

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
	75,
	window.innerWidth / window.innerHeight,
	nearDist,
	farDist
);
camera.position.set(0, 0, 450);

const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setClearColor("hotpink");
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.querySelector("#canvas-wrapper").appendChild(renderer.domElement);

// CREATE FLOWER
const petalRadius = 100;
const geometry = new THREE.TetrahedronBufferGeometry(petalRadius);
const material = new THREE.MeshNormalMaterial();
const group = new THREE.Group();

// CREATES TubeBufferGeometry
const CustomSinCurve = function(scale) {
	THREE.Curve.call(this);
	this.scale = scale === undefined ? 1 : scale;
};
CustomSinCurve.prototype = Object.create(THREE.Curve.prototype);
CustomSinCurve.prototype.constructor = CustomSinCurve;
CustomSinCurve.prototype.getPoint = function (t) {
	const tx = t * 3 - 1.5;
	const ty = Math.sin(2 * Math.PI * t);
	const tz = 0;

	return new THREE.Vector3(tx, ty, tz).multiplyScalar(this.scale);
};

const Flower = function (group, distance = null) {
	this.group = group;
	this.dist = distance;

	this.group.position.x = petalRadius * -1;
	this.group.position.y = Math.round(petalRadius / 2);
	this.group.rotation.z = 250;

	if (distance !== null) {
		this.group.position.x = Math.round(this.dist / 2);
		this.group.position.y = Math.round(petalRadius * -0.5);
		this.group.position.z = 10;

		this.group.rotation.x = Math.round(this.dist / -2);
		this.group.rotation.z = Math.round(this.dist * 2);
	}
};

// CREATE PETALS
Flower.prototype.create = function () {
	const petalCount = 180;
	for (let i = 0; i < petalCount; i++) {
		const mesh = new THREE.Mesh(geometry, material);
		const tau = 2 * Math.PI;

		mesh.rotation.x = Math.random() * tau;
		mesh.rotation.y = Math.random() * tau;

		mesh.matrixAutoUpdate = false;
		mesh.updateMatrix();

		this.group.add(mesh);
	}
};

// CREATE STEM'S FLOWER
Flower.prototype.createStem = function () {
	// Let's use the TubeBufferGeometry created earlier
	const path = new CustomSinCurve(petalRadius);
	const geometryStem = new THREE.TubeBufferGeometry(path, 1, 5, 20, false);
	const meshStem = new THREE.Mesh(geometryStem, material);
	
	meshStem.position.x = petalRadius;

	this.group.add(meshStem);
};

const f1group = new THREE.Group();
const f1 = new Flower(f1group);
f1.create();
f1.createStem();

const f2group = new THREE.Group();
const f2dist = Math.floor(farDist / 5);
const f2 = new Flower(f2group, f2dist);
f2.create();
f2.createStem();

group.add(f1group);
group.add(f2group);

scene.add(group);

// CREATE RAYCASTING FOR INTERACTION EFFECTS
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();

const colorMaterial = "#ff0080"; // dark pink
const newMaterial = new THREE.MeshPhongMaterial({
	color: colorMaterial,
	shininess: 60,
});

// Add light for MeshPhongMaterial to be visible in scene
const light = new THREE.DirectionalLight();
light.position.set(0, 0.5, 1);
scene.add(light);

// CREATE PART OF THE MOUSE OVER/TOUCH EFFECT
let mouseX = 0;
let mouseY = 0;
const mouseFX = {
	coordinates(cX, cY) {
		mouseX = cX - window.innerWidth / 2;
		mouseY = cY - window.innerHeight / 2;
		
		const limit = 700;
		if (mouseX >= limit || mouseX <= (limit*-1)) mouseX = limit;
	},
	// PART OF RAYCASTING
	calcObjPickingRay(gp) {
		const ob = gp.children;
		const intersects = raycaster.intersectObjects(ob, true);

		ob.forEach((el) => {
			el.material = intersects.length > 0 ? newMaterial : material;
			el.material.needsUpdate = true;
		});
	},
	// PART OF RAYCASTING
	addMouseOver(cX, cY) {		
		mouse.x = (cX / window.innerWidth) * 2 - 1;
		mouse.y = -(cY / window.innerHeight) * 2 + 1;

		// Update the picking ray with the camera and mouse position
		raycaster.setFromCamera(mouse, camera);

		// Calculate objects intersecting the picking ray
		const flowerGroups = [f1group, f2group];
		flowerGroups.forEach((el) => {
			mouseFX.calcObjPickingRay(el);
		});
	},
	onMouseMove(e) {
		mouseFX.addMouseOver(e.clientX, e.clientY);
		mouseFX.coordinates(e.clientX, e.clientY);
	},
	onTouchMove(e) {
		const touchX = e.changedTouches[0].clientX;
		const touchY = e.changedTouches[0].clientY;
		mouseFX.coordinates(touchX, touchY);
	}
};
document.addEventListener("mousemove", mouseFX.onMouseMove);
document.addEventListener("touchmove", mouseFX.onTouchMove);

// RESIZE CANVAS
const onWindowResize = () => {
	const w = window.innerWidth;
	const h = window.innerHeight;

	camera.aspect = w / h;
	camera.updateProjectionMatrix();

	renderer.setSize(w, h);
};
window.addEventListener("resize", onWindowResize);

// CREATE ANIMATIONS
const createAnimRotation = () => {
	const t = Date.now() * 0.001;
	const rx = Math.sin(t * 0.7);
	const ry = Math.sin(t * 0.3);
	
	group.rotation.x = rx;
	group.rotation.y = ry;
};

// RENDER 3D GRAPHIC
const render = () => {
	camera.position.x += (mouseX - camera.position.x) * 0.05;
	camera.position.y += (mouseY * -1 - camera.position.y) * 0.05;
	camera.lookAt(scene.position);

	createAnimRotation();

	renderer.render(scene, camera);

	requestAnimationFrame(render);
};
render();
              
            
!
999px

Console