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 esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM 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.
<h1>Spinning and Moving Cube with Controls</h1>
<p>
<ul>
<li>0: reset animation to initial state
<li>1: take one step
<li>g: "go" — start the animation <q>loop</q>
<li>SPC: stop the animation
</ul>
body {
max-width: 100%;
}
/* feel free to style the canvas any way you want. If you want it to
use the entire window, set width: 100% and height: 100%. */
canvas {
width: 80%;
height: 500px;
display: block;
margin: 10px auto;
}
var scene = new THREE.Scene();
var renderer = new THREE.WebGLRenderer();
TW.mainInit(renderer,scene);
TW.cameraSetup(renderer,
scene,
{minx: -2.5, maxx: 2.5,
miny: -2.5, maxy: 2.5,
minz: -2.5, maxz: 2.5});
// parameters of the animation that are controlled by the GUI
var guiParams = {
vx: 0.01, // x rotation velocity
vy: 0.02, // y rotation velocity
vz: 0.04, // z rotation velocity
tx: 0.1, // x velocity
ty: 0.1 // y velocity
};
// global variables whose properties are updated during the animation
var animationState, cube;
// ADD A WIREFRAME CAGE SURROUNDING THE INITIAL POSITION OF THE CUBE,
// AND ADD IT TO THE SCENE
// firstState() resets the cube and rotations to the initial state, adds
// a wireframe cage around the cube, and renders the scene
function firstState() {
animationState = {
time: 0, // total time of the animation
rx: 0, // total x rotation
ry: 0, // total y rotation
rz: 0, // total z rotation
px: 0, // x position
py: 0 // y position
};
if ( cube != null ) {
scene.remove(cube);
}
cube = new THREE.Mesh(new THREE.CubeGeometry(2,2,2),
new THREE.MeshNormalMaterial());
scene.add(cube);
TW.render();
}
firstState();
// updateState() updates cube rotations & positions, and content of animationState
function updateState() {
animationState.time += 1;
// increase the total rotations by the user-specified velocity
animationState.rx += guiParams.vx;
animationState.ry += guiParams.vy;
animationState.rz += guiParams.vz;
// rotate the cube around the x,y,z axes
cube.rotateX(guiParams.vx);
cube.rotateY(guiParams.vy);
cube.rotateZ(guiParams.vz);
// UPDATE THE x,y POSITION OF THE CUBE BASED ON THE tx AND ty PARAMETERS SET BY THE
// USER IN THE GUI - UPDATE BOTH THE animationState AND THE cube.position
}
// oneStep() performs one step of the animation
function oneStep() {
updateState();
TW.render();
}
// stored so that we can cancel the animation if we want
var animationId = null;
// animate() starts the continuous animation loop, and if the position
// of the cube is outside the cage, the animation stops
function animate(timestamp) {
oneStep();
// MODIFY THE LAST CODE STATEMENT HERE TO CAPTURE THE FOLLOWING LOGIC:
// IF THE CUBE'S POSITION IS OUTSIDE THE CAGE
// stopAnimation();
// OTHERWISE
// animationId = requestAnimationFrame(animate);
animationId = requestAnimationFrame(animate);
}
// stopAnimation() halts the animation
function stopAnimation() {
if( animationId != null ) {
cancelAnimationFrame(animationId);
console.log("Cancelled animation using " + animationId);
}
}
// keyboard controls for the animation control
TW.setKeyboardCallback("0", firstState, "reset animation");
TW.setKeyboardCallback("1", oneStep, "advance by one step");
TW.setKeyboardCallback("g", animate, "go: start animation");
TW.setKeyboardCallback(" ", stopAnimation, "stop animation");
// GUI controls enable user to change x,y,z rotation velocities
var gui = new dat.GUI();
gui.add(guiParams, "vx", 0, 0.5);
gui.add(guiParams, "vy", 0, 0.5);
gui.add(guiParams, "vz", 0, 0.5);
gui.add(guiParams, "tx", -0.2, 0.2);
gui.add(guiParams, "ty", -0.2, 0.2);
Also see: Tab Triggers