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.
<canvas id="canvas"></canvas>
<!-- name badge -->
<div id='name-card-container'>
<div id='name-card'>
<div class='dark-background'><span class='emoji'>👀</span><a href='http://bit.ly/creative_coding' target='_blank'>More pens</a> by <a href='http://bit.ly/codepen_yitliu' target='_blank'>@yitliu</a></div><div class='light-background'><span class='emoji'>🕊</span><a href='http://bit.ly/twitter_yitliu' target='_blank'>Twitter</a></div>
</div>
</div>
#canvas {
background:#248079;
}
/* Name Badge */
html, body {
margin: 0;
padding: 0;
overflow:hidden;
}
#name-card-container{
position:fixed;
bottom:0px;
height:38px;
width:100%;
display:flex;
justify-content:center;
}
#name-card {
display:flex;
justify-content:center;
font-family:Avenir;
font-size: 14px;
font-weight:500;
line-height:38px;
border-radius:4px 4px 0 0;
overflow:hidden;
box-shadow: 0 0 10px rgba(0,0,0,0.10), 0 0 2px rgba(0,0,0,0.20);
}
#name-card > div.dark-background{
background:#1d1e22;
padding:0 10px 0 20px;
height:100%;
color:#fff;
}
#name-card > div.light-background{
background:#fff;
padding:0 20px 0 10px;
height:100%;
}
#name-card span.emoji{
font-size:18px;
}
#name-card a {
text-decoration:none;
}
#name-card a:hover{
text-decoration: underline;
}
#name-card a:visited{
text-decoration: none;
}
#name-card div.dark-background a{
color:#fff;
}
#name-card div.light-background a{
color:#000;
}
//Create color palette
var Colors = {
cyan: 0x248079,
brown: 0xA98F78,
brownDark: 0x9A6169,
green: 0x65BB61,
greenLight: 0xABD66A,
blue:0x6BC6FF
};
var scene = new THREE.Scene();
var h = window.innerHeight,
w = window.innerWidth;
var aspectRatio = w / h,
fieldOfView = 25,
nearPlane = .1,
farPlane = 1000;
var camera = new THREE.PerspectiveCamera(
fieldOfView,
aspectRatio,
nearPlane,
farPlane);
var renderer = new THREE.WebGLRenderer({canvas:canvas,alpha: true, antialias: true});
const dpi = window.devicePixelRatio;
renderer.setSize( w*dpi, h*dpi );
const theCanvas = document.getElementById('canvas');
theCanvas.style.width = `${w}px`;
theCanvas.style.height = `${h}px`;
renderer.shadowMapEnabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.appendChild( renderer.domElement );
camera.position.set(-5,6,8);
// camera.position.set(0,0,8); // front
// camera.position.set(-10,.2,0); //left
// camera.position.set(0,10,0); //top
// camera.position.y=4;
camera.lookAt(new THREE.Vector3(0,0,0));
//Ambient light
var light = new THREE.AmbientLight( 0xffffff ,.5);
var shadowLight = new THREE.DirectionalLight(0xffffff, .5);
shadowLight.position.set(200, 200, 200);
shadowLight.castShadow = true;
var backLight = new THREE.DirectionalLight(0xffffff, .2);
backLight.position.set(-100, 200, 50);
backLight.castShadow = true;
scene.add(backLight);
scene.add(light);
scene.add(shadowLight);
// grassland left
var geometry_left = new THREE.BoxGeometry( 2, .2, 2 );
var material_grass = new THREE.MeshLambertMaterial( { color: Colors.greenLight } );
var ground_left = new THREE.Mesh( geometry_left, material_grass );
ground_left.position.set(-1,0.1,0);
scene.add( ground_left );
customizeShadow(ground_left,.25) // mess, opacity
//river
var geometry_river = new THREE.BoxGeometry( 1, .1, 2 );
var material_river = new THREE.MeshLambertMaterial( { color: Colors.blue } );
var river = new THREE.Mesh( geometry_river, material_river );
river.position.set(.5,.1,0);
scene.add( river );
customizeShadow(river,.08) // mess, opacity
//river bed
var geometry_bed = new THREE.BoxGeometry( 1, .05, 2 );
var bed = new THREE.Mesh( geometry_bed , material_grass );
bed.position.set(.5,.025,0);
scene.add( bed );
//grassland right
var geometry_right = new THREE.BoxGeometry( 1, .2, 2 );
var ground_right = new THREE.Mesh( geometry_right, material_grass );
ground_right.position.set(1.5,0.1,0);
scene.add( ground_right );
customizeShadow(ground_right,.25) // mess, opacity
var tree=function(x,z){
this.x=x;
this.z=z;
//trunk
var material_trunk = new THREE.MeshLambertMaterial({ color: Colors.brownDark });
var geometry_trunk = new THREE.BoxGeometry( .15, .15, .15 );
var trunk = new THREE.Mesh( geometry_trunk, material_trunk );
trunk.position.set(this.x,.275,this.z);
trunk.castShadow = true;
trunk.receiveShadow = true;
scene.add( trunk );
//leaves
var geometry_leaves = new THREE.BoxGeometry( .25, .4, .25 );
var material_leaves = new THREE.MeshLambertMaterial({ color: Colors.green});
var leaves = new THREE.Mesh( geometry_leaves, material_leaves );
leaves.position.set(this.x,.2+.15+.4/2,this.z);
leaves.castShadow = true;
customizeShadow(leaves,.25) // mess, opacity
scene.add( leaves );
}
//left
tree(-1.75,-.85);
tree(-1.75,-.15);
tree(-1.5,-.5);
tree(-1.5,.4);
tree(-1.25,-.85);
tree(-1.25,.75);
tree(-.75,-.85);
tree(-.75,-.25);
tree(-.25,-.85);
//right
tree(1.25,-.85);
tree(1.25,.75);
tree(1.5,-.5);
tree(1.75,-.85);
tree(1.75,.35);
function customizeShadow(t,a){ //opacity, target mesh
var material_shadow = new THREE.ShadowMaterial({opacity:a});
var mesh_shadow = new THREE.Mesh( t.geometry, material_shadow );
mesh_shadow.position.set(t.position.x,t.position.y,t.position.z);
mesh_shadow.receiveShadow = true;
scene.add( mesh_shadow );
}
var material_wood = new THREE.MeshLambertMaterial({ color: Colors.brown });
//bridge - wood block
for (var i=0;i<6;i++){
var geometry_block = new THREE.BoxGeometry( .15, .02, .4 );
var block = new THREE.Mesh( geometry_block, material_wood );
block.position.set(0+.2*i,.21,.2);
block.castShadow = true;
block.receiveShadow = true;
scene.add( block );
}
//bridge - rail
var geometry_rail_v = new THREE.BoxGeometry( .04,.3,.04 );
var rail_1 = new THREE.Mesh( geometry_rail_v, material_wood );
rail_1.position.set(-.1,.35,.4);
rail_1.castShadow = true;
customizeShadow(rail_1,.2);
scene.add( rail_1 );
var rail_2 = new THREE.Mesh( geometry_rail_v, material_wood );
rail_2.position.set(1.1,.35,.4);
rail_2.castShadow = true;
customizeShadow(rail_2,.2);
scene.add( rail_2 );
var rail_3 = new THREE.Mesh( geometry_rail_v, material_wood );
rail_3.position.set(-.1,.35,0);
rail_3.castShadow = true;
customizeShadow(rail_3,.2);
scene.add( rail_3 );
var rail_4 = new THREE.Mesh( geometry_rail_v, material_wood );
rail_4.position.set(1.1,.35,0);
rail_4.castShadow = true;
customizeShadow(rail_4,.2);
scene.add( rail_4 );
var geometry_rail_h = new THREE.BoxGeometry( 1.2,.04,.04 );
var rail_h1 = new THREE.Mesh( geometry_rail_h, material_wood );
rail_h1.position.set(0.5,.42,.4);
rail_h1.castShadow = true;
customizeShadow(rail_h1,.2);
scene.add( rail_h1 );
var rail_h2 = new THREE.Mesh( geometry_rail_h, material_wood );
rail_h2.position.set(0.5,.42,0);
rail_h2.castShadow = true;
customizeShadow(rail_h2,.2);
scene.add( rail_h2 );
var Drop=function(){
this.geometry = new THREE.BoxGeometry(.1, .1, .1 );
this.drop= new THREE.Mesh( this.geometry, material_river );
this.drop.position.set(Math.random(.1,.9),0.1,1+(Math.random()-.5)*.1);
scene.add( this.drop );
this.speed=0;
this.lifespan=(Math.random()*50)+50;
this.update=function(){
this.speed+=.0007;
this.lifespan--;
this.drop.position.x+=(.5-this.drop.position.x)/70;
this.drop.position.y-=this.speed;
}
}
var drops=[];
var count=0;
var render = function() {
requestAnimationFrame( render );
if(count%3==0){
for(var i=0;i<5;i++){
drops.push(new Drop());
}
}
count++;
for(var i=0;i<drops.length;i++){
drops[i].update();
if(drops[i].lifespan<0){
scene.remove(scene.getObjectById(drops[i].drop.id));
drops.splice(i,1);
}
}
renderer.render( scene, camera );
}
render();
Also see: Tab Triggers