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 class="world"></div>
<div id="credits">
<p> <a href="https://codepen.io/Yakudoo/" target="blank">my other codepens</a> | <a href="https://www.epic.net" target="blank">epic.net</a></p>
</div>
<script type="x-shader/x-fragment" id="fragTunnelShader">
precision highp float;
uniform float time;
uniform vec2 mousePosition;
varying vec2 vUv;
varying float vDisplacement;
varying vec3 vNormal;
float PI = 3.141592;
float rand(vec2 co){
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}
float Hash(in vec2 p, in float scale){
// This is tiling part, adjusts with the scale...
p = mod(p, scale);
return fract(sin(dot(p, vec2(35.6898, 24.3563))) * 353753.373453);
}
float Noise(in vec2 x, in float scale ){
x *= scale;
vec2 p = floor(x);
vec2 f = fract(x);
f = f*f*(3.0-2.0*f);
//f = (1.0-cos(f*3.1415927)) * .5;
float res = mix(mix(Hash(p, scale),
Hash(p + vec2(1.0, 0.0), scale), f.x),
mix(Hash(p + vec2(0.0, 1.0), scale),
Hash(p + vec2(1.0, 1.0), scale), f.x), f.y);
return res;
}
float fBm(in vec2 p, float scale){
float f = 0.0;
// Change starting scale to any integer value...
float amp = 0.25;
for (int i = 0; i < 10; i++){
f += Noise(p, scale) * amp;
amp *= .75;
// Scale must be multiplied by an integer value...
scale *= 2.0;
}
return f;
}
void main () {
float t = time*.05;
vec2 pos = vUv;
pos.y *= .1 ;
pos.x *= 3.0;
float light = dot(mousePosition, -vNormal.xy);
float r = fBm( vec2(sin(t * 1.0) + pos.x, pos.y - (t + 1.0 ) * 2.0), .0625 );
float g = fBm( vec2(sin(t * 2.0) + pos.x, pos.y - (t + 2.2 ) * 3.0), .125);
float b = fBm( vec2(sin(t * 3.0) + pos.x, pos.y - (t + 3.5 ) * 4.0), .250 );
float r1 = fract(r * 20.0 + 10.0 * sin( t * 5.0) );
float g1 = fract(g * 20.0 + 10.0 * sin( t * 5.0 + PI/2.0) );
float b1 = fract(b * 20.0 + 10.0 * sin( t * 5.0 + PI) );
vec3 col = vec3(r1,g1,b1);
col *= sin(vUv.y * PI) * (max(0.2, light + .4));
gl_FragColor = vec4(col, 1.0);
}
</script>
<script type="x-shader/x-vertex" id="vertTunnelShader">
attribute vec3 normal;
attribute vec3 position;
attribute vec2 uv;
uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;
uniform mat3 normalMatrix;
uniform float time;
uniform vec2 mousePosition;
varying vec2 vUv;
varying vec3 vPos;
varying vec3 vNormal;
varying float vDisplacement;
float rand(vec2 co){
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}
float mod289(float x){return x - floor(x * (1.0 / 289.0)) * 289.0;}
vec4 mod289(vec4 x){return x - floor(x * (1.0 / 289.0)) * 289.0;}
vec4 perm(vec4 x){return mod289(((x * 34.0) + 1.0) * x);}
vec2 fade(vec2 t) {return t*t*t*(t*(t*6.0-15.0)+10.0);}
float cnoise(vec2 P){
vec4 Pi = floor(P.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
vec4 Pf = fract(P.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
Pi = mod(Pi, 289.0); // To avoid truncation effects in permutation
vec4 ix = Pi.xzxz;
vec4 iy = Pi.yyww;
vec4 fx = Pf.xzxz;
vec4 fy = Pf.yyww;
vec4 i = perm(perm(ix) + iy);
vec4 gx = 2.0 * fract(i * 0.0243902439) - 1.0; // 1/41 = 0.024...
vec4 gy = abs(gx) - 0.5;
vec4 tx = floor(gx + 0.5);
gx = gx - tx;
vec2 g00 = vec2(gx.x,gy.x);
vec2 g10 = vec2(gx.y,gy.y);
vec2 g01 = vec2(gx.z,gy.z);
vec2 g11 = vec2(gx.w,gy.w);
vec4 norm = 1.79284291400159 - 0.85373472095314 *
vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11));
g00 *= norm.x;
g01 *= norm.y;
g10 *= norm.z;
g11 *= norm.w;
float n00 = dot(g00, vec2(fx.x, fy.x));
float n10 = dot(g10, vec2(fx.y, fy.y));
float n01 = dot(g01, vec2(fx.z, fy.z));
float n11 = dot(g11, vec2(fx.w, fy.w));
vec2 fade_xy = fade(Pf.xy);
vec2 n_x = mix(vec2(n00, n01), vec2(n10, n11), fade_xy.x);
float n_xy = mix(n_x.x, n_x.y, fade_xy.y);
return 2.3 * n_xy;
}
void main() {
vUv = uv;
vPos = position;
vNormal = normal;
float t = time * .1;
float displacement = cnoise( vec2 (normal.x * 2.0 + t, normal.y * 2.0 + t ) ) ;
vNormal *= displacement * 3.0;
vPos += vNormal;
gl_Position = projectionMatrix * modelViewMatrix * vec4 ( vPos, 1.0);
}
</script>
<script type="x-shader/x-fragment" id="fragStarsShader">
precision highp float;
uniform float time;
uniform vec2 mousePosition;
varying vec2 vUv;
varying float vDisplacement;
float PI = 3.141592;
float mod289(float x){return x - floor(x * (1.0 / 289.0)) * 289.0;}
vec4 mod289(vec4 x){return x - floor(x * (1.0 / 289.0)) * 289.0;}
vec4 perm(vec4 x){return mod289(((x * 34.0) + 1.0) * x);}
float rand(vec2 co){
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}
float noise(vec3 p){
vec3 a = floor(p);
vec3 d = p - a;
d = d * d * (3.0 - 2.0 * d);
vec4 b = a.xxyy + vec4(0.0, 1.0, 0.0, 1.0);
vec4 k1 = perm(b.xyxy);
vec4 k2 = perm(k1.xyxy + b.zzww);
vec4 c = k2 + a.zzzz;
vec4 k3 = perm(c);
vec4 k4 = perm(c + 1.0);
vec4 o1 = fract(k3 * (1.0 / 41.0));
vec4 o2 = fract(k4 * (1.0 / 41.0));
vec4 o3 = o2 * d.z + o1 * (1.0 - d.z);
vec2 o4 = o3.yw * d.x + o3.xz * (1.0 - d.x);
return o4.y * d.y + o4.x * (1.0 - d.y);
}
float fbm(vec2 pos, float t){
float r;
r = noise( vec3( pos, t ) * 01.0 ) * 01.0000;
r += noise( vec3( pos, t ) * 02.0 ) * 00.5000;
r += noise( vec3( pos, t ) * 04.0 ) * 00.2500;
r += noise( vec3( pos, t ) * 08.0 ) * 00.1250;
r += noise( vec3( pos, t ) * 16.0 ) * 00.0625;
return r / 1.9375;
}
void main () {
float t = time*.3;// + sin(time) * .2;
vec2 pos = vUv - vec2(.5,.5);
float d = length(pos);
float a = dot(vec2(0.0, 1.0), normalize(pos));
vec2 polCoord = vec2(d,a);
float cloud = fbm( vec2(d * 5.0 - t * 1.0, a * 1.0 + t), t * 5.1);
cloud *= pow( (1.0 - d* 2.0), 3.0) * 3.0;
float r = cloud * mousePosition.x;
float g = cloud * mousePosition.y;
float b = cloud;
vec3 col = vec3(r,g,b);
gl_FragColor = vec4(col, 1.0);
}
</script>
<script type="x-shader/x-vertex" id="vertStarsShader">
attribute vec3 normal;
attribute vec3 position;
attribute vec2 uv;
uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;
uniform mat3 normalMatrix;
uniform float time;
uniform vec2 mousePosition;
varying vec2 vUv;
varying vec3 vPos;
varying float vDisplacement;
float rand(vec2 co){
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}
float mod289(float x){return x - floor(x * (1.0 / 289.0)) * 289.0;}
vec4 mod289(vec4 x){return x - floor(x * (1.0 / 289.0)) * 289.0;}
vec4 perm(vec4 x){return mod289(((x * 34.0) + 1.0) * x);}
vec2 fade(vec2 t) {return t*t*t*(t*(t*6.0-15.0)+10.0);}
float cnoise(vec2 P){
vec4 Pi = floor(P.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
vec4 Pf = fract(P.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
Pi = mod(Pi, 289.0); // To avoid truncation effects in permutation
vec4 ix = Pi.xzxz;
vec4 iy = Pi.yyww;
vec4 fx = Pf.xzxz;
vec4 fy = Pf.yyww;
vec4 i = perm(perm(ix) + iy);
vec4 gx = 2.0 * fract(i * 0.0243902439) - 1.0; // 1/41 = 0.024...
vec4 gy = abs(gx) - 0.5;
vec4 tx = floor(gx + 0.5);
gx = gx - tx;
vec2 g00 = vec2(gx.x,gy.x);
vec2 g10 = vec2(gx.y,gy.y);
vec2 g01 = vec2(gx.z,gy.z);
vec2 g11 = vec2(gx.w,gy.w);
vec4 norm = 1.79284291400159 - 0.85373472095314 *
vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11));
g00 *= norm.x;
g01 *= norm.y;
g10 *= norm.z;
g11 *= norm.w;
float n00 = dot(g00, vec2(fx.x, fy.x));
float n10 = dot(g10, vec2(fx.y, fy.y));
float n01 = dot(g01, vec2(fx.z, fy.z));
float n11 = dot(g11, vec2(fx.w, fy.w));
vec2 fade_xy = fade(Pf.xy);
vec2 n_x = mix(vec2(n00, n01), vec2(n10, n11), fade_xy.x);
float n_xy = mix(n_x.x, n_x.y, fade_xy.y);
return 2.3 * n_xy;
}
void main() {
vUv = uv;
vPos = position;
//float t = time * 2.0;
//float displacement = cnoise( vec2 (normal.x + t, normal.y + t ) ) ;
//vPos += normal * displacement * 2.0;
gl_Position = projectionMatrix * modelViewMatrix * vec4 ( vPos, 1.0);
}
</script>
@import url('https://fonts.googleapis.com/css?family=Josefin+Sans:400');
body{
overflow: hidden;
}
.world{
position: absolute;
width:100%;
height:100%;
background: #000;
}
#credits{
position:absolute;
width:100%;
margin: auto;
bottom:0;
margin-bottom:20px;
font-family:"Josefin Sans", sans-serif;
color:#555555;
font-size:0.7em;
text-transform: uppercase;
text-align : center;
}
#credits a {
color:#999999;
text-decoration: none;
}
class World {
constructor(width, height) {
this.renderer = new THREE.WebGLRenderer({
alpha: true,
antialias: true
});
this.renderer.setPixelRatio(1);
this.renderer.setSize(width, height);
this.container = document.getElementsByClassName("world")[0];
this.scene = new THREE.Scene();
this.width = width;
this.height = height;
this.aspectRatio = width / height;
this.fieldOfView = 50;
var nearPlane = .1;
var farPlane = 20000;
this.camera = new THREE.PerspectiveCamera(this.fieldOfView, this.aspectRatio, nearPlane, farPlane);
this.camera.position.z = 300;
this.container.appendChild(this.renderer.domElement);
this.timer = 0;
this.createPlane();
this.render();
}
createPlane(){
this.tunnelmaterial = new THREE.RawShaderMaterial({
vertexShader: document.getElementById( 'vertTunnelShader' ).textContent,
fragmentShader: document.getElementById('fragTunnelShader').textContent,
side: THREE.BackSide,
uniforms: {
time: { type: 'f', value: 0 },
mousePosition: {type: 'v2', value: new THREE.Vector2( 0.5, 0.5 ) }
}
});
this.tunnelGeometry = new THREE.CylinderGeometry( 40, 40, 250, 50, 1, true);
this.tunnelGeometry.applyMatrix( new THREE.Matrix4().makeRotationX(PI/2));
this.tunnelGeometry.applyMatrix( new THREE.Matrix4().makeTranslation(0,0,125));
this.tunnel = new THREE.Mesh(this.tunnelGeometry, this.tunnelmaterial);
this.scene.add(this.tunnel);
this.starsMaterial = new THREE.RawShaderMaterial({
vertexShader: document.getElementById( 'vertStarsShader' ).textContent,
fragmentShader: document.getElementById('fragStarsShader').textContent,
uniforms: {
time: { type: 'f', value: 0 },
mousePosition: {type: 'v2', value: new THREE.Vector2( 0.5, 0.5 ) }
}
});
this.starsGeometry = new THREE.PlaneGeometry(120,120,1,1);
this.stars = new THREE.Mesh(this.starsGeometry, this.starsMaterial);
this.stars.position.z = -10;
this.scene.add(this.stars);
}
render() {
this.timer+=.01;
this.tunnel.material.uniforms.time.value = this.timer;
this.stars.material.uniforms.time.value = this.timer;
this.renderer.render(this.scene, this.camera);
}
loop() {
this.render();
requestAnimationFrame(this.loop.bind(this));
}
updateSize(w, h) {
this.renderer.setSize(w, h);
this.camera.aspect = w / h;
this.camera.updateProjectionMatrix();
}
mouseMove(mousePos) {
if (this.tunnel){
this.tunnel.material.uniforms.mousePosition.value = new THREE.Vector2(mousePos.px, -mousePos.py);
this.tunnel.rotation.y = -mousePos.px*.05;
this.tunnel.rotation.x = -mousePos.py*.05;
this.stars.material.uniforms.mousePosition.value = new THREE.Vector2(mousePos.px, -mousePos.py);
}
}
};
document.addEventListener("DOMContentLoaded", domIsReady);
let mousePos = {x:0, y:0, px:0, py:0};
let PI = Math.PI;
let world;
function domIsReady() {
world = new World(this.container, this.renderer, window.innerWidth, window.innerHeight);
window.addEventListener('resize', handleWindowResize, false);
document.addEventListener("mousemove", handleMouseMove, false);
handleWindowResize();
world.loop();
}
function handleWindowResize() {
world.updateSize(window.innerWidth, window.innerHeight);
}
function handleMouseMove(e) {
mousePos.x = e.clientX;
mousePos.y = e.clientY;
mousePos.px = mousePos.x / window.innerWidth * 2 - 1;
mousePos.py = mousePos.y / window.innerHeight * 2 - 1;
world.mouseMove(mousePos);
}
Also see: Tab Triggers