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.
<button id='VR' class='button toggleVR' onclick='toggleVR()' title='Toggle VR Mode for Mobile Devices Only'>
<svg id="Layer_1" xmlns="http://www.w3.org/2000/svg" viewBox="-12 -12 48 48"><path fill="#fff" d="M23.058 5.038H.942c-.522 0-.942.422-.942.94V18.02c0 .522.423.942.942.942H8.8c.26 0 .492-.174.564-.425.88-3.028 1.062-4.754 2.724-4.754 1.66 0 1.905 1.973 2.685 4.766.068.243.29.41.543.41h7.743c.52 0 .94-.424.94-.943V5.982c0-.522-.42-.944-.942-.944zm-16.43 9.467c-1.384 0-2.504-1.12-2.504-2.504s1.12-2.503 2.504-2.503 2.5 1.12 2.5 2.504-1.12 2.505-2.5 2.505zm10.744 0c-1.384 0-2.504-1.12-2.504-2.504s1.12-2.503 2.504-2.503c1.383 0 2.504 1.12 2.504 2.504s-1.12 2.505-2.504 2.505z"/></svg>
</button>
<section>
<h1><span class="low">Polar Bear Facts</span> 101:</h1>
<p>Adult polar bears have no natural predators except other polar bears. Cubs less than one year old sometimes are prey to wolves and other carnivores. Newborn cubs may be cannibalized by malnourished mothers or adult male polar bears.</p>
<small>source: <br>
<a href="https://seaworld.org/animals/all-about/polar-bear/longevity/" target="_blank">seaworld.org</a><br>
</section>
<div class="btn-group">
<span>RL</span>
<span>RR</span>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r124/three.min.js"></script>
<script src="https://baronwatts.com/scripts/LegacyJSONLoader.js"></script>
<script src="https://unpkg.com/[email protected]/examples/js/loaders/GLTFLoader.js"></script>
<script src="https://unpkg.com/[email protected]/examples/js/controls/DeviceOrientationControls.js"></script>
<script src="https://unpkg.com/[email protected]/examples/js/effects/StereoEffect.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.11.2/TweenMax.min.js"></script>
@import url("https://fonts.googleapis.com/css?family=Roboto+Condensed|Playfair+Display:700i|Didact+Gothic");
html,
body {
overflow: hidden;
background: black;
font-family: monospace;
font-family: "Roboto", sans-serif;
}
html {
box-sizing: border-box;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
* {
margin: 0;
padding: 0;
}
h1 {
font-family: "Roboto Condensed", "Helvetica Neue", Helvetica, Arial,
sans-serif;
font-size: 30px;
line-height: 1;
}
h3 {
font-weight: lighter;
}
section {
color: white;
max-width: 275px;
width: 100%;
display: none;
font-size: 14px;
}
p {
color: rgba(255, 255, 255, 0.5);
}
a {
color: white;
}
span.low {
color: skyblue;
}
.btn-group {
display: flex;
-webkit-display: flex;
justify-content: space-between;
-webkit-justify-content: space-between;
width: 100%;
padding: 50px 25px;
position: absolute;
left: 0;
bottom: 0;
visibility: hidden;
}
.btn-group span {
width: 75px;
height: 75px;
border-radius: 50%;
background: white;
background: #222;
color: white;
text-align: center;
display: flex;
-webkit-display: flex;
align-items: center;
-webkit-align-items: center;
justify-content: center;
-webkit-justify-content: center;
font-weight: bold;
font-size: 30px;
box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.5);
}
#VR {
position: fixed;
top: 20px;
right: 20px;
outline: none;
border: none;
width: 60px;
height: 40px;
z-index: 100;
background: rgba(135, 206, 250, 0.75);
display: flex;
-webkit-display: flex;
justify-content: center;
-webkit-justify-content: center;
align-items: center;
-webkit-align-items: center;
visibility: hidden;
}
#VR svg {
width: 60px;
height: 35px;
transform: scale(2);
-webkit-transform: scale(2);
fill: white;
}
button {
background: none;
}
button:hover {
cursor: pointer;
}
@media (min-width: 1100px) {
section {
display: block;
position: absolute;
bottom: 50px;
left: 50px;
}
}
/* Show only on touchscreen devices */
@media (pointer: coarse) {
.btn-group,
#VR {
visibility: visible;
}
.hide {
visibility: hidden;
}
}
const randnum = (min, max) => Math.round(Math.random() * (max - min) + min);
//=========================================================================================== scene
var scene = new THREE.Scene();
//=========================================================================================== camera
var camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
//=========================================================================================== canvas
renderer = new THREE.WebGLRenderer({
alpha: true,
antialias: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMapEnabled = true; //Shadow
renderer.shadowMapSoft = true; // Shadow
renderer.shadowMapType = THREE.PCFShadowMap; //Shadow
document.body.appendChild(renderer.domElement);
//=========================================================================================== add VR
renderer.setPixelRatio(window.devicePixelRatio); //VR
var effect = new THREE.StereoEffect(renderer); //VR
effect.setSize(window.innerWidth, window.innerHeight); //VR
var VR = false;
function toggleVR() {
if (VR) {
VR = false;
camera.rotation.reorder( 'ZYX' );
camera.lookAt(0,0,0);
} else {
VR = true;
controls = new THREE.DeviceOrientationControls(camera);
requestFullscreen(document.documentElement);
}
renderer.setSize(window.innerWidth, window.innerHeight);
}
//=========================================================================================== resize
window.addEventListener("resize", function() {
let width = window.innerWidth;
let height = window.innerHeight;
renderer.setSize(width, height);
camera.aspect = width / height;
camera.updateProjectionMatrix();
});
//=========================================================================================== fog
scene.fog = new THREE.FogExp2(new THREE.Color("black"), 0.0075);
//=========================================================================================== light
var sphereLight = new THREE.SphereGeometry(.05);
var sphereLightMaterial = new THREE.MeshBasicMaterial({
color: new THREE.Color("white")
});
var sphereLightMesh = new THREE.Mesh(sphereLight, sphereLightMaterial);
sphereLightMesh.castShadow = true;
sphereLightMesh.position.set(0,2.5,0)
//scene.add(sphereLightMesh);
var distance = 10;
var intensity = 2.5;
var pointLight2 = new THREE.PointLight(new THREE.Color('white'), intensity/4, distance);
pointLight2.position.set(0, 0, -10.5);
scene.add(pointLight2);
var pointLight3 = new THREE.PointLight(new THREE.Color('#02a0ac'), intensity, distance);
pointLight3.position.set(0, 0, 5);
scene.add(pointLight3);
var pointLight4 = new THREE.PointLight(new THREE.Color('purple'), intensity, distance);
pointLight4.position.set(0, 0, 2.5);
scene.add(pointLight4);
var light = new THREE.DirectionalLight( new THREE.Color('white'), 1, 400 );
light.position.set( 0, .10, 4 ); //push ligth back to cast shadow
light.castShadow = true;
scene.add( light );
//Set up shadow properties for the light
light.shadow.mapSize.width = 512; // default
light.shadow.mapSize.height = 512; // default
light.shadow.camera.near = 0.5; // default
light.shadow.camera.far = 500; // default
//=========================================================================================== floor
var groundMaterial = new THREE.MeshPhongMaterial({
color: new THREE.Color('#fff'),
specular: new THREE.Color('skyblue'),
shininess: 50,
});
var groundGeo = new THREE.PlaneGeometry(200, 200);
var ground = new THREE.Mesh(groundGeo, groundMaterial);
ground.position.set(0, 0, 0);
ground.rotation.x = (-Math.PI / 2);
ground.receiveShadow = true;
scene.add(ground);
//===================================================== add model
var loader = new THREE.LegacyJSONLoader();
loader.load("https://raw.githubusercontent.com/baronwatts/models/master/rocks.js", function(geometry, materials) {
new Array(100).fill(null).map( (d, i) => {
x = Math.cos(i/100 * Math.PI * 2) * randnum(10,50);
z = randnum(0,50);
y = -.1;
var obj = new THREE.Mesh(geometry, materials);
obj.scale.set(1,1,1);
obj.position.set(x, y, z);
obj.rotateY(Math.PI/randnum(0,180));
scene.add(obj);
});
});
//===================================================== add model
var loader = new THREE.LegacyJSONLoader();
loader.load("https://raw.githubusercontent.com/baronwatts/models/master/glacier.js", function(geometry, materials) {
new Array(8).fill(null).map( (d, i) => {
x = Math.cos(i/8 * Math.PI * 2) * randnum(50,75);
z = Math.sin(i/8 * Math.PI * 2) * randnum(50,75);
y = 0;
var obj = new THREE.Mesh(geometry, materials);
obj.scale.set(12, randnum(2,5), 12);
obj.castShadow = true;
obj.position.set(x, y, z);
obj.rotateY(Math.PI/Math.random());
scene.add(obj);
});
});
//===================================================== add model
var loader = new THREE.LegacyJSONLoader();
loader.load("https://raw.githubusercontent.com/baronwatts/models/master/iceberg.js", function(geometry, materials) {
var obj = new THREE.Mesh(geometry, materials);
obj.scale.set(4,1,4);
obj.castShadow = true;
obj.position.set(-25, 0, 5);
obj.rotateY(-Math.PI);
scene.add(obj);
});
//===================================================== add sky particles
var textureLoader = new THREE.TextureLoader();
textureLoader.crossOrigin = ''; //allow cross origin loading
const imageSrc = textureLoader.load('https://raw.githubusercontent.com/baronwatts/models/master/snowflake.png');
const shaderPoint = THREE.ShaderLib.points;
uniforms = THREE.UniformsUtils.clone(shaderPoint.uniforms);
uniforms.map.value = imageSrc;
var matts = new THREE.PointsMaterial({
size: 0.75,
color: new THREE.Color("skyblue"),
map: uniforms.map.value,
blending: THREE.AdditiveBlending,
depthWrite: false,
transparent: true,
opacity: 0.75
});
var geo = new THREE.Geometry();
for ( var i = 0; i < 500; i ++ ) {
var star = new THREE.Vector3();
geo.vertices.push( star );
}
var sparks = new THREE.Points(geo, matts );
sparks.scale.set(1,1,1);
scene.add(sparks);
sparks.geometry.vertices.map((d,i)=>{
d.y = randnum(15,20);
d.x = randnum(-75, 75);
d.z = randnum(-75, 75);
});
//===================================================== add model
if (window.innerWidth > 768) {
var leafs = [];
loader = new THREE.LegacyJSONLoader();
loader.load('https://raw.githubusercontent.com/baronwatts/models/master/single-leaf.js', function(geometry, materials) {
//create leafs
new Array(500).fill(null).map( (d, i) => {
var matt = new THREE.MeshLambertMaterial({
transparent: true,
opacity: 1,
side: THREE.DoubleSide,
color: new THREE.Color('silver')
});
var particle = new THREE.Mesh(geometry, matt);
particle.position.set(randnum(-10, randnum(10, 20)), 20, randnum(-20, 20));
particle.scale.set(.5, .5, .5);
particle.rotateY(Math.random() * 180);
scene.add(particle);5
leafs.push(particle);
});
leafs.map((d, i) => {
//position
if (i % 2 == 0) {
leafs[i].position.y = 0;
} else {
TweenMax.to(leafs[i].position, 10, {
y: 0,
x: randnum(-10, 10),
ease: Power2.Linear,
delay: 0.025 * i,
repeat: -1
}, 1);
}
//rotation
if (i % 2 == 0) {
leafs[i].rotation.y = 0;
} else {
TweenMax.to(leafs[i], 5, {
rotationY: '+=25',
ease: Power2.Linear,
delay: 0.025 * i,
repeat: -1
}, 1);
}
}); //end leafs
});
} //end if
//===================================================== models
var mixers = [];
var characterGroup = new THREE.Group();
scene.add(characterGroup);
loadModels();
function loadModels() {
const loader = new THREE.GLTFLoader();
loader.load( 'https://raw.githubusercontent.com/baronwatts/models/master/PolarBear.glb', function(gltf){
var model = gltf.scene.children[ 0 ];
model.position.set( 4, 0, -10 );
model.scale.set( .025, .025, .025 );
model.rotateZ(Math.PI);
var animation = gltf.animations[ 0 ];
var mixer = new THREE.AnimationMixer( model );
mixers.push( mixer );
var action = mixer.clipAction( animation );
action.play();
gltf.scene.traverse(function(node) {
if (node instanceof THREE.Mesh) {
node.castShadow = true;
node.material.side = THREE.DoubleSide;
}
});
scene.add( model );
});
loader.load( 'https://raw.githubusercontent.com/baronwatts/models/master/PolarBear.glb', function(gltf){
var model = gltf.scene.children[ 0 ];
model.position.set( -2, 0, -10 );
model.scale.set( .025, .025, .025 );
var animation = gltf.animations[ 1 ];
var mixer = new THREE.AnimationMixer( model );
mixers.push( mixer );
var action = mixer.clipAction( animation );
action.play();
gltf.scene.traverse(function(node) {
if (node instanceof THREE.Mesh) {
node.castShadow = true;
node.material.side = THREE.DoubleSide;
}
});
scene.add( model );
});
loader.load( 'https://raw.githubusercontent.com/baronwatts/models/master/PolarCub.glb', function(gltf){
var model = gltf.scene.children[ 0 ];
model.position.set( 25, 0, -15 );
model.scale.set( .05, .05, .05 );
model.rotateZ(-Math.PI);
var animation = gltf.animations[ 0 ];
var mixer = new THREE.AnimationMixer( model );
mixers.push( mixer );
var action = mixer.clipAction( animation );
action.play();
gltf.scene.traverse(function(node) {
if (node instanceof THREE.Mesh) {
node.castShadow = true;
node.material.side = THREE.DoubleSide;
}
});
characterGroup.add( model );
});
}//end loadModels
//=========================================================================================== model
loader = new THREE.LegacyJSONLoader();
loader.load('https://raw.githubusercontent.com/baronwatts/models/master/igloo.js', function(geometry, materials) {
var matt = new THREE.MeshPhongMaterial({
vertexColors: THREE.FaceColors,
transparent: true,
opacity: 1,
side: THREE.DoubleSide,
color: new THREE.Color('white'),
specular: new THREE.Color('skyblue'),
//specular: new THREE.Color('#333'),
shininess: 50,
});
var wall = new THREE.Mesh(geometry, matt);
wall.position.set(0, 0, 0);
wall.rotateY(Math.PI);
wall.scale.set(.1, .1, .1);
scene.add(wall);
});
//=========================================================================================== full screen
var requestFullscreen = function(ele) {
if (ele.requestFullscreen) {
ele.requestFullscreen();
} else if (ele.webkitRequestFullscreen) {
ele.webkitRequestFullscreen();
} else if (ele.mozRequestFullScreen) {
ele.mozRequestFullScreen();
} else if (ele.msRequestFullscreen) {
ele.msRequestFullscreen();
} else {
console.log('Fullscreen API is not supported.');
}
}
var exitFullscreen = function(ele) {
if (ele.exitFullscreen) {
ele.exitFullscreen();
} else if (ele.webkitExitFullscreen) {
ele.webkitExitFullscreen();
} else if (ele.mozCancelFullScreen) {
ele.mozCancelFullScreen();
} else if (ele.msExitFullscreen) {
ele.msExitFullscreen();
} else {
console.log('Fullscreen API is not supported.');
}
}
//=========================================================================================== add tweening
//https://greensock.com/forums/topic/16993-threejs-properties/
Object.defineProperties(THREE.Object3D.prototype, {
x: {
get: function() {
return this.position.x;
},
set: function(v) {
this.position.x = v;
}
},
y: {
get: function() {
return this.position.y;
},
set: function(v) {
this.position.y = v;
}
},
z: {
get: function() {
return this.position.z;
},
set: function(v) {
this.position.z = v;
}
},
rotationZ: {
get: function() {
return this.rotation.x;
},
set: function(v) {
this.rotation.x = v;
}
},
rotationY: {
get: function() {
return this.rotation.y;
},
set: function(v) {
this.rotation.y = v;
}
},
rotationX: {
get: function() {
return this.rotation.z;
},
set: function(v) {
this.rotation.z = v;
}
}
});
//=========================================================================================== add Animation
let angle = 0,
lastTime = null,
u_frame = 0,
clock = new THREE.Clock(),
count = 0,
prevTime = Date.now(),
phase = 0;
function moveCharacter(){
characterGroup.position.x < -85 ? (characterGroup.position.x = 85) : (characterGroup.position.x -= .15);
}
function moveLights(){
phase += 0.03;
sphereLightMesh.position.z = 5 - Math.cos(phase) * 5;
sphereLightMesh.position.x = Math.sin(phase) * 5;
pointLight3.position.copy(sphereLightMesh.position);
}
//===================================================== mouse
var mouseX = 0;
var mouseY = 0;
var zoomIn = 20;
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
function onDocumentMouseMove(event) {
mouseX = ( event.clientX - window.innerWidth / 2 ) / zoomIn;
mouseY = ( event.clientY - window.innerHeight / 2 ) / zoomIn;
}
(function animate() {
//update models
const delta = clock.getDelta();
mixers.forEach( ( mixer ) => { mixer.update( delta * 1.25 ); } );
moveCharacter();
moveLights();
//VR Mode
if(VR){
effect.render(scene, camera);
controls.update();
document.querySelector('.btn-group').classList.add('hide');
}else{
renderer.render(scene, camera);
camera.position.x += ( mouseX - camera.position.x ) * .05;
camera.lookAt( scene.position );
document.querySelector('.btn-group').classList.remove('hide');
}
requestAnimationFrame(animate);
})();
//set camera position
camera.position.y = 3;
camera.position.z = -25;
camera.position.x = 50;
Also see: Tab Triggers