HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div class="overlay">
(please wait...)
</div>
body {
margin: 0;
}
.overlay {
position: absolute;
top: 10px;
left: 50%;
z-index: 1;
transform: translateX(-50%);
color: #ffffff;
background: #000000;
border-radius: 2px;
padding: 10px;
}
import * as THREE from '//cdn.rawgit.com/mrdoob/three.js/master/build/three.module.js';
import { GLTFLoader } from '//cdn.rawgit.com/mrdoob/three.js/master/examples/jsm/loaders/GLTFLoader.js';
import { FBXLoader } from '//cdn.rawgit.com/mrdoob/three.js/master/examples/jsm/loaders/FBXLoader.js';
// NOTE Toggle between rendering cubes and models
const RENDER_OBJECTS = 'gltf'; // box | gltf | fbx
const MAKE_MEMORY_LEAK = true;
const cubeTexture = '//cdn.wtlstudio.com/common-ptr.wtlstudio.com/3d61522d-79ba-4ffe-98a0-864c3a091e87.png';
const gltfModel = '//cdn.wtlstudio.com/sample.wtlstudio.com/48315172-1012-4127-9e52-ed8738ba5e37.glb';
// NOTE Setup - safe to skip to next comment
let meshes = [];
let limit = 0;
let pending = 0;
const gltfLoader = new GLTFLoader();
const fbxLoader = new FBXLoader();
const textureLoader = new THREE.TextureLoader();
const overlayDiv = document.querySelector('.overlay');
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeff5555);
scene.add(new THREE.AmbientLight(0xffffff, 1.0));
const camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight);
camera.position.set(-2, 2, 10);
// NOTE Dispose helper
const dispose = (object) => {
if (!object || typeof object !== 'object') {
return;
}
if (object.children) {
// Dispose all children first
object.children.forEach(child => dispose(child));
}
if (object.parent) {
// Remove from parent to prevent rerender
object.parent.remove(object);
}
if (object.geometry) {
// Dispose geometry
object.geometry.dispose();
}
if (object.material) {
// Dispose material and all possible textures
Object.keys(object.material).forEach(key => {
if (object.material[key] && typeof object.material[key].dispose === 'function') {
object.material[key].dispose();
}
});
object.material.dispose();
}
if (typeof object.dispose === 'function') {
// Dispose object itself
object.dispose();
}
};
const animate = () => {
// Create 20 meshes, then call startDisposing() below
if (limit < 20 && pending === 0) {
if (RENDER_OBJECTS === 'gltf') {
// Fetch new GLTF model
gltfLoader.load(gltfModel, gltf => {
// Do this:
if (!MAKE_MEMORY_LEAK) {
const children = [];
gltf.scene.children.forEach(child => children.push(child));
scene.add(...children);
meshes.push(...children);
}
// Instead of this (memory leak):
if (MAKE_MEMORY_LEAK) {
scene.add(gltf.scene);
meshes.push(gltf.scene);
}
pending -= 1;
});
} else if (RENDER_OBJECTS === 'fbx') {
// Fetch new box model (this FBX has no texture)
fbxLoader.load('//cdn.wtlstudio.com/sample.wtlstudio.com/f1dd4202-bbd2-4d38-baaa-b04c6c7acee3.fbx', (model) => {
model.scale.setScalar(0.01);
model.position.copy(new THREE.Vector3().random());
scene.add(model);
meshes.push(model);
pending -= 1;
});
} else if (RENDER_OBJECTS === 'box') {
// Create sample textured cube
const model = new THREE.Mesh(
new THREE.BoxBufferGeometry(2.0, 2.0, 2.0),
new THREE.MeshBasicMaterial({
map: textureLoader.load(cubeTexture, (texture) => {
model.position.copy(new THREE.Vector3().random());
scene.add(model);
meshes.push(model);
})
})
);
pending -= 1;
}
limit += 1;
pending += 1;
}
if (limit === 20 && pending === 0) {
limit = 21;
startDisposing();
}
if (limit > 0) {
overlayDiv.innerHTML = `
three: r${THREE.REVISION}<br/>
mem: ${JSON.stringify(renderer.info.memory)}<br/>
models: ${meshes.length} / ${Math.min(limit, 20)} (pend.: ${pending})
`;
}
renderer.render(scene, camera);
requestAnimationFrame(animate);
};
const startDisposing = () => {
// Delay disposing to ensure 100% of materials and textures are loaded
setTimeout(() => {
while (meshes.length > 0) {
dispose(meshes.pop());
}
}, 3000);
};
animate();
document.body.appendChild(renderer.domElement);
Also see: Tab Triggers