Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

              
                <h1>Happy Holidays</h1>
              
            
!

CSS

              
                @import url("https://fonts.googleapis.com/css2?family=Imperial+Script&display=swap");

body {
	margin: 0;
}
canvas {
	width: 100%;
	height: 100%;
}

h1 {
	text-align: center;
	position: absolute;
	top: 4vh;
	width: 100%;
	display: block;
	font-family: "Imperial Script", cursive;
	color: #e63946;
	font-size: 60px;
}

              
            
!

JS

              
                import * as THREE from "https://unpkg.com/[email protected]/build/three.module.js";
import { OrbitControls } from "https://unpkg.com/[email protected]/examples/jsm/controls/OrbitControls.js";
import { MeshSurfaceSampler } from "https://unpkg.com/[email protected]/examples/jsm/math/MeshSurfaceSampler.js";

class Scene {
	constructor(sketch, settings) {
		this.sketch = sketch;
		this.settings = { ...settings };

		this.scene = new THREE.Scene();
		this.scene.background = new THREE.Color(0xa8dadc);
		return this.scene;
	}
}
class Renderer {
	constructor(sketch, settings) {
		this.sketch = sketch;
		this.settings = { ...settings };
		this.renderer = new THREE.WebGLRenderer({ antialias: true });
		this.renderer.setSize(this.sketch.sizes.width, this.sketch.sizes.height);
		this.renderer.setPixelRatio(Math.min(2, window.devicePixelRatio));
		this.renderer.shadowMap.enabled = true;
		this.renderer.outputEncoding = THREE.sRGBEncoding;

		this.renderer.update = this.update.bind(this.sketch);

		return this.renderer;
	}
	update() {
		this.renderer.render(this.scene, this.camera);
	}
}
class Camera {
	constructor(sketch, settings) {
		this.sketch = sketch;
		this.settings = { ...settings };

		this.camera = new THREE.PerspectiveCamera(
			75,
			this.sketch.sizes.width / this.sketch.sizes.height,
			1,
			200
		);
		this.camera.position.x = 0;
		this.camera.position.y = 0;
		this.camera.position.z = 3;
		this.sketch.scene.add(this.camera);

		return this.camera;
	}
}
class Animator {
	constructor(sketch, settings) {
		this.sketch = sketch;
		this.settings = { ...settings };

		this.tasks = [];
	}
	/**
	 *
	 * @param {function} fn
	 */
	add(fn) {
		this.tasks.push(fn);
	}
	animate() {
		requestAnimationFrame(this.animate.bind(this));

		this.tasks.forEach((task) => task());

		this.sketch.renderer.update();
	}
}
class Controls {
	constructor(sketch, settings) {
		this.sketch = sketch;
		this.settings = { ...settings };

		this.controls = new OrbitControls(
			this.sketch.camera,
			this.sketch.renderer.domElement
		);

		return this.controls;
	}
}
class Events {
	constructor(sketch, settings) {
		this.sketch = sketch;
		this.settings = { ...settings };

		this.addEvents();
	}
	addEvents() {
		window.addEventListener("resize", this.onWindowResize.bind(this), false);
	}
	onWindowResize() {
		this.sketch.sizes = {
			width: window.innerWidth,
			height: window.innerHeight
		};

		this.sketch.camera.aspect =
			this.sketch.sizes.width / this.sketch.sizes.height;
		this.sketch.camera.updateProjectionMatrix();
		this.sketch.renderer.setSize(
			this.sketch.sizes.width,
			this.sketch.sizes.height
		);
		this.sketch.renderer.setPixelRatio(Math.min(2, window.devicePixelRatio));
	}
}
class Lights {
	constructor(sketch, settings) {
		this.sketch = sketch;
		this.settings = { ...settings };

		this.ambient();
		this.directional(3, 5, 3);
		this.directional(3, 5, -3);
	}
	ambient() {
		let ambLight = new THREE.AmbientLight(0xffffff, 0.2, 100);
		this.sketch.scene.add(ambLight);
	}
	directional(x, y, z) {
		let dirLight = new THREE.DirectionalLight(0xffffff, 0.6, 100);
		dirLight.position.set(x, y, z);
		this.sketch.scene.add(dirLight);
	}
}
class Loader {
	constructor(sketch, settings) {
		this.settings = {
			load: () => {
				console.log("loaded");
			},
			progress: (itemURL, itemsLoaded, itemsTotal) => {
				console.log("%loaded:", itemsLoaded / itemsTotal);
			},
			...settings
		};
		this.manager = new THREE.LoadingManager(
			() => this.settings.load(),
			(itemURL, itemsLoaded, itemsTotal) =>
				this.settings.progress(itemURL, itemsLoaded, itemsTotal)
		);
	}
}

class Sketch {
	constructor() {
		this.animator = new Animator(this);
		this.sizes = {
			width: window.innerWidth,
			height: window.innerHeight
		};
		this.scene = new Scene(this);
		this.renderer = new Renderer(this);
		this.camera = new Camera(this);
		this.lights = new Lights(this);
		this.controls = new Controls(this);
		this.events = new Events(this);
		this.loader = new Loader(this, {
			load: () => {
				this.addObjects();
			}
		});
		this.clock = new THREE.Clock();
		this.clock.start();
		this.dummy = new THREE.Object3D();
		this.tempPos = new THREE.Vector3();
		this.tempNormal = new THREE.Vector3();
	}
	init() {
		const textureLoader = new THREE.TextureLoader(this.loader.manager);

		this.leafTexture = textureLoader.load(
			"https://assets.codepen.io/5946/leaf.png"
		);

		document.body.appendChild(this.renderer.domElement);
		this.animator.animate();
	}
	createBranch() {
		let leafGeo = new THREE.PlaneBufferGeometry(1, 1, 32, 32);
		let leafMat = new THREE.MeshStandardMaterial({
			map: this.leafTexture,
			transparent: true,
			side: THREE.DoubleSide,
			depthWrite: false
		});

		this.branch = new THREE.Mesh(leafGeo, leafMat);
	}
	createWreath() {
		this.createBranch();
		const wreathGeo = new THREE.TorusGeometry(1, 0.1, 32, 32);
		const wreathMat = new THREE.MeshStandardMaterial();
		const wreathTorus = new THREE.Mesh(wreathGeo, wreathMat);

		// THANKS TO https://coduza.com/blog/surface-sampling-in-three-js/
		const sampler = new MeshSurfaceSampler(wreathTorus).build();

		const branchCount = 300;
		this.wreath = new THREE.InstancedMesh(
			this.branch.geometry,
			this.branch.material,
			branchCount
		);
		this.wreath.instanceMatrix.setUsage(THREE.DynamicDrawUsage);

		for (let i = 0; i < branchCount; i++) {
			sampler.sample(this.tempPos, this.tempNormal);
			this.tempNormal.add(this.tempPos);

			this.dummy.position.copy(this.tempPos);
			this.dummy.lookAt(this.tempNormal);
			this.dummy.updateMatrix();
			this.wreath.setMatrixAt(i, this.dummy.matrix);
		}
	}
	createRibbon() {
		const ribbonMat = new THREE.MeshStandardMaterial({
			color: 0xe63946
		});

		const ribbonShape = new THREE.Shape();
		ribbonShape.moveTo(0, 0);
		ribbonShape.lineTo(0, 0.3);
		ribbonShape.lineTo(0.01, 0.3);
		ribbonShape.lineTo(0.01, 0);

		const ribbonCurve = new THREE.CatmullRomCurve3([
			new THREE.Vector3(-0.2, -0.3, 0.1),
			new THREE.Vector3(0, 0.3, 0.05),
			new THREE.Vector3(0.5, 1, -0.1),
			new THREE.Vector3(0, 1.2, -0.2),
			new THREE.Vector3(-0.5, 1, -0.1),
			new THREE.Vector3(0, 0.3, -0.05),
			new THREE.Vector3(0.2, -0.3, -0.1)
		]);
		const ribbonPoints = ribbonCurve.getPoints(10);

		const ribbonSpline = new THREE.CatmullRomCurve3(ribbonPoints, false);

		// EXTRUDE SETTINGS
		const roadExtrudeSettings = {
			steps: ribbonPoints.length * 20,
			extrudePath: ribbonSpline,
			curveSegments: 36
		};

		// CREATE MESH
		const ribbonGeo = new THREE.ExtrudeGeometry(ribbonShape, roadExtrudeSettings);

		this.ribbon = new THREE.Mesh(ribbonGeo, ribbonMat);
		this.ribbon.position.z = 0.4;
		this.ribbon.position.y = -1.3;
	}
	addObjects() {
		this.createWreath();
		this.createRibbon();

		const complete = new THREE.Group();

		complete.add(this.wreath);
		complete.add(this.ribbon);

		complete.position.y = -0.3;
		this.scene.add(complete);
	}
}

window.sketch = new Sketch();
sketch.init();

              
            
!
999px

Console