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.
<div id="container"></div>
body {
background-image: starfield(3, 30);
background-repeat: repeat;
background-size: 400px 400px;
background-color: #000;
margin: 0;
overflow: hidden;
padding: 0;
}
/**
* @file
* The main scene.
*/
/**
* Define constants.
*/
const TEXTURE_PATH = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/123879/';
/**
* Set our global variables.
*/
var camera,
scene,
renderer,
effect,
controls,
element,
container,
rotationPoint;
var textureFlare0;
var textureFlare2;
var textureFlare3;
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
init();
animate();
/**
* Initializer function.
*/
function init() {
// Build the container
container = document.createElement( 'div' );
document.body.appendChild( container );
// Create the scene.
scene = new THREE.Scene();
// Create a rotation point.
rotationPoint = new THREE.Object3D();
rotationPoint.position.set( 0, 0, 1000 );
scene.add(rotationPoint);
// Create the camera.
camera = new THREE.PerspectiveCamera(
50, // Angle
window.innerWidth / window.innerHeight, // Aspect Ratio.
1, // Near view.
23000 // Far view.
);
rotationPoint.add( camera );
// Build the renderer.
renderer = new THREE.WebGLRenderer( { antialias: true, alpha: true } );
element = renderer.domElement;
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.shadowMap.enabled;
container.appendChild( element );
// Build the controls.
controls = new THREE.OrbitControls( camera, element );
controls.enablePan = true;
controls.enableZoom = true;
controls.maxDistance = 4000;
controls.minDistance = 1000;
controls.target.copy( new THREE.Vector3( 0, 0, -100 ) );
function setOrientationControls(e) {
if (!e.alpha) {
return;
}
controls = new THREE.DeviceOrientationControls( camera );
controls.connect();
window.removeEventListener('deviceorientation', setOrientationControls, true);
}
window.addEventListener('deviceorientation', setOrientationControls, true);
// Ambient lights
var ambient = new THREE.AmbientLight( 0x222222 );
scene.add( ambient );
// The sun.
var light = new THREE.PointLight( 0xffffff, 1, 10000, 0 );
light.position.set( -8000, 0, 0 );
scene.add( light );
// Add the skymap.
addSkybox();
// Add the sun.
createSun(-11600, 0, 0);
// Create a lensflare effect.
createLensflare(-11400, 0, 0);
// Add the sun.
createSun(-11600, 4000, 5500);
// Create a lensflare effect.
createLensflare(-11400, 4000, 5500);
// Add the sun.
createSun(11600, 0, 0);
// Create a lensflare effect.
createLensflare(11400, 0, 0);
// Add the sun.
createSun(11600, -4000, 5500);
// Create a lensflare effect.
createLensflare(11400, -4000, 5500);
// Add the sun.
createSun(0, 0, -11600);
// Create a lensflare effect.
createLensflare(0, 0, -11400);
// Add the sun.
createSun(5500, 4000, -11600);
// Create a lensflare effect.
createLensflare(5500, 4000, -11400);
// Add the sun.
createSun(0, 0, 11600);
// Create a lensflare effect.
createLensflare(0, 0, 11400);
// Add the sun.
createSun(11600, 0, 11600);
// Create a lensflare effect.
createLensflare(11400, 0, 11400);
// Add the sun.
createSun(11600, 0, -11600);
// Create a lensflare effect.
createLensflare(11400, 0, -11400);
// Add the sun.
createSun(-11600, 0, -11600);
// Create a lensflare effect.
createLensflare(-11400, 0, -11400);
// Add the sun.
createSun(-11600, 0, 11600);
// Create a lensflare effect.
createLensflare(-11400, 0, 11400);
// Add the sun.
createSun(0, -11600, 0);
// Create a lensflare effect.
createLensflare(0, -11400, 0);
// Add the sun.
createSun(0, 11600, 0);
// Create a lensflare effect.
createLensflare(0, 11400, 0);
window.addEventListener('resize', onWindowResize, false);
}
/**
* Events to fire upon window resizing.
*/
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
/**
* Add the sun to the scene.
*/
function createSun(x, y, z) {
// Add the Sun sphere model.
var sunGeometry = new THREE.SphereGeometry( 100, 16, 16 );
// Create the Sun materials.
var sunMaterial = new THREE.MeshLambertMaterial({
color: '#ffff55',
emissive: '#ffff55',
});
sun = new THREE.Mesh( sunGeometry, sunMaterial );
sun.castShadow = false;
sun.receiveShadow = false;
sun.position.set( x, y, z );
sun.rotation.y = Math.PI;
// Add the Sun to the scene.
scene.add( sun );
}
/**
* Updates to apply to the scene while running.
*/
function update() {
camera.updateProjectionMatrix();
rotationPoint.rotation.y -= 0.0015;
}
/**
* Render the scene.
*/
function render() {
renderer.render(scene, camera);
}
/**
* Animate the scene.
*/
function animate() {
requestAnimationFrame(animate);
update();
render();
}
/**
* Add the skybox, the stars wrapper.
*/
function addSkybox() {
var urlPrefix = TEXTURE_PATH;
var urls = [
urlPrefix + 'test.jpg',
urlPrefix + 'test.jpg',
urlPrefix + 'test.jpg',
urlPrefix + 'test.jpg',
urlPrefix + 'test.jpg',
urlPrefix + 'test.jpg',
];
var loader = new THREE.CubeTextureLoader();
loader.setCrossOrigin( 'https://s.codepen.io' );
var textureCube = loader.load( urls );
textureCube.format = THREE.RGBFormat;
var shader = THREE.ShaderLib[ "cube" ];
shader.uniforms[ "tCube" ].value = textureCube;
var material = new THREE.ShaderMaterial( {
fragmentShader: shader.fragmentShader,
vertexShader: shader.vertexShader,
uniforms: shader.uniforms,
depthWrite: false,
side: THREE.BackSide
} );
var geometry = new THREE.BoxGeometry( 20000, 20000, 20000 );
var skybox = new THREE.Mesh( geometry, material );
//skybox.position.x = -30;
scene.add( skybox );
}
/**
* Create the lens flare effect.
*
* Code from https://threejs.org/examples/webgl_lensflares.html
*/
function createLensflare(x, y, z) {
var textureLoader = new THREE.TextureLoader();
textureLoader.setCrossOrigin( 'https://s.codepen.io' );
textureFlare0 = textureLoader.load( TEXTURE_PATH + "sun.png" );
textureFlare2 = textureLoader.load( TEXTURE_PATH + "lensflare2.png" );
textureFlare3 = textureLoader.load( TEXTURE_PATH + "lensflare3.png" );
addLight( 0.55, 0.9, 0.5, x, y, z );
}
/**
* Add the lens flare to the scene.
*
* Code from https://threejs.org/examples/webgl_lensflares.html
*/
function addLight( h, s, l, x, y, z ) {
var flareColor = new THREE.Color( 0xffffff );
flareColor.setHSL( h, s, l + 0.5 );
var lensFlare = new THREE.LensFlare( textureFlare0, 700, 0.0, THREE.AdditiveBlending, flareColor );
lensFlare.add( textureFlare3, 60, 0.6, THREE.AdditiveBlending );
lensFlare.add( textureFlare3, 70, 0.7, THREE.AdditiveBlending );
lensFlare.add( textureFlare3, 120, 0.9, THREE.AdditiveBlending );
lensFlare.add( textureFlare3, 70, 1.0, THREE.AdditiveBlending );
lensFlare.customUpdateCallback = lensFlareUpdateCallback;
lensFlare.position.set( x, y, z );
scene.add( lensFlare );
}
/**
* Update the lens flare.
*
* Code from https://threejs.org/examples/webgl_lensflares.html
*/
function lensFlareUpdateCallback( object ) {
var f, fl = object.lensFlares.length;
var flare;
var vecX = -object.positionScreen.x * 2;
var vecY = -object.positionScreen.y * 2;
for( f = 0; f < fl; f++ ) {
flare = object.lensFlares[ f ];
flare.x = object.positionScreen.x + vecX * flare.distance;
flare.y = object.positionScreen.y + vecY * flare.distance;
flare.rotation = 0;
}
object.lensFlares[ 2 ].y += 0.025;
object.lensFlares[ 3 ].rotation = object.positionScreen.x * 0.5 + THREE.Math.degToRad( 45 );
}
function onDocumentMouseMove( event ) {
event.preventDefault();
//mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
//mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
}
Also see: Tab Triggers