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.
<!-- VertexShader code here -->
<script id="vertexShader" type="x-shader/x-vertex">#version 300 es
precision highp float;
in vec4 vPosition;
void main() {
gl_Position = vPosition;
}
</script>
<!-- FragmentShader code here -->
<script id="fragmentShader" type="x-shader/x-fragment">#version 300 es
precision highp float;
out vec4 fragColor;
uniform vec4 mouse;
uniform vec2 resolution;
uniform float time;
#define R resolution
#define T time
#define M mouse
#define PI 3.141592653
#define PI2 6.283185307
mat2 rot (float a) { return mat2(cos(a),sin(a),-sin(a),cos(a)); }
float hash21( vec2 p ) { return fract(sin(dot(p,vec2(23.43,84.21))) *4832.3234); }
float hash(in float n){return fract(sin(n)*43.54); }
float noise(in vec2 x) {
vec2 p = floor(x);
vec2 f = fract(x);
f = f*f*(3.-2.*f);
float n = p.x + p.y*57.;
float res = mix(mix( hash(n+ 0.1), hash(n+ 1.1),f.x),
mix( hash(n+ 57.1), hash(n+ 58.1),f.x),f.y);
return res;
}
//@iq
float box( vec3 p, vec3 b ){
vec3 q = abs(p)-b;
return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0);
}
float box( in vec2 p, in vec2 b ){
vec2 d = abs(p)-b;
return length(max(d,0.0)) + min(max(d.x,d.y),0.0);
}
vec3 hit,hitPoint;
vec2 gid,sid;
//@blackle domain rep trick using neighbor
vec2 edge(vec2 p) {
vec2 p2 = abs(p);
if (p2.x > p2.y) return vec2((p.x < 0.) ? -1. : 1., 0.);
else return vec2(0., (p.y < 0.) ? -1. : 1.);
}
vec2 map(vec3 p) {
p.y+=4.;
vec2 res = vec2(100.,-1.);
vec2 center = floor(p.xz) + .5;
vec2 tpos = (p.xz - center);
vec2 neighb = center + edge(tpos);
float hs = hash21(center);
float h2 = hash21(center.yx);
float chk = mod(center.y+center.x,2.)*2.-1.;
float height = .25 + .15 * sin(center.y+T *1.25);
height += .25 + .15 * sin(center.x+T *.75);
height += 2.;
float sh = height;
height = chk<.5 ? (1.1*hs)+2.:height;
float tval = hs>.75?(tpos.x+.5):hs>.5?(tpos.y+.5):hs>.25?(1.5-tpos.y):(1.5-tpos.x);
float width = .4825;
vec3 bxs = vec3(width,height+clamp(tval*.4,.0,1.),width);
float me = box(p - vec3(center.x, height,center.y), bxs);
bxs.y = 4.35;
float nx = box(p - vec3(neighb.x, height,neighb.y), bxs);
if (h2>.5&&chk<.5) me = max(me,-box(p-vec3(center.x,height,center.y), vec3(width*.69,height*1.5,width*.69)) );
float bx = min(me, nx);
if(bx<res.x) {
res = vec2(bx,chk>.5?2.:3.);
hit = p-vec3(center.x,height,center.y);
gid = center;
}
float d2 = p.y+5.;
if(d2<res.x) {
res = vec2(d2,1.);
hit = p;
}
vec3 q3 = p-vec3(center.x, height+.25+sh,center.y);
float d3 = length(q3)-.225;
if(d3<res.x && (h2>.7&&chk<.5)) {
res = vec2(d3,5.);
hit = q3;
}
return res;
}
vec3 normal(vec3 p, float t, float mindist){
float e = mindist*t;
vec2 h = vec2(1.,-1.);
return normalize( h.xyy*map( p + h.xyy*e).x +
h.yyx*map( p + h.yyx*e).x +
h.yxy*map( p + h.yxy*e).x +
h.xxx*map( p + h.xxx*e).x );
}
vec3 hue(float t){
vec3 d = vec3(.627,.961,.220);
return .35 + .375*cos(PI2*t*(vec3(.985,.98,.95)+d));
}
vec3 getWood(vec3 p, vec3 clr){
const vec3 wax = normalize(vec3(1,0,1));
vec3 uv = (p + dot(p,wax)*wax*14.5)*1.20;
float wood = 0.;
wood += abs(noise(uv.xz*5. )-.5);
wood += abs(noise(uv.yz*8. )-.5)/2.;
wood += abs(noise(uv.zy*11.)-.5)/4.;
wood = pow(1.-clamp(wood,.0,1.),5.);
return mix(clr,vec3(.204,.075,.016),wood);
}
//@Shane AO
float getao(in vec3 p, in vec3 n){
float sca = 2., occ = 0.;
for( int i = 0; i<5; i++ ){
float hr = float(i + 1)*.05/1.5;
float d = map(p + n*hr).x;
occ += (hr - d)*sca;
sca *= .9;
if(sca>1e5) break;
}
return clamp(1. - occ, 0., 1.);
}
vec3 render(vec3 p, vec3 rd, vec3 ro, float d, float m, inout vec3 n){
n = normal(p,d,1.01);
vec3 lpos = vec3(25,25,-25);
vec3 l = normalize(lpos-p);
float diff = clamp(dot(n,l),.1,1.);
float fresnel = pow(clamp(1.+dot(rd, n), 0., 1.), 9.);
fresnel = mix(.0, .9, fresnel);
float shdw = 1.,t=.2;
for(float i=.0;i<12.;i++) {
float h = map(p + l*t).x;
if( h<1e-4 ) { shdw = .05; break; }
shdw = min(shdw, 75.*h/t);
t += h * .8;
if( shdw<.05 || t>32. ) break;
}
diff = mix(diff,diff*shdw,.25);
float ao = getao(p,n);
diff = mix(diff,diff*ao,.75);
vec3 h = vec3(.08);
if(m==1.) h=getWood(hitPoint,vec3(.506,.314,.094));
if(m==2.) h=mix(vec3(.00),hue(hash21(sid*.0005)+fresnel),fresnel);
if(m==3.) {
float hs = hash21(sid+1.);
vec3 clr = hs>.65?vec3(0.698,0.318,0.063):hs>.5?vec3(0.882,0.529,0.200):hs>.15?vec3(.427,.15,.03):vec3(0.537,0.294,0.122);
h=getWood(hitPoint,clr);
vec2 uv = hitPoint.xz;
float px = .02;
float d = box(uv,vec2(.35));
d=smoothstep(px,-px,abs(abs(d)-.05)-.025);
h=mix(h,h*vec3(.565,.306,.137),d);
}
if(m==5.) h=vec3(.7);
return diff*h;
}
void main( ) {
vec2 F = gl_FragCoord.xy;
vec2 uv = (2.*F.xy-R.xy)/max(R.x,R.y);
vec3 ro = vec3(15,2,3.25),
rd = normalize(vec3(uv,-1));
float y = M.xy == vec2(0) ? 0. : -(M.y/R.y * 1. - .5) * PI;
mat2 rx = rot(.78), ry = rot(y+T*.0325);
ro.yz *= rx;ro.xz *= ry;
rd.yz *= rx;rd.xz *= ry;
vec3 C = vec3(0);
vec3 p = ro + rd;
float atten = 1.,k = 1.,d = 0.;
for(int i=0;i<148;i++)
{
vec2 ray = map(p);
vec3 n=vec3(0);
float m = ray.y;
d = i<25? ray.x*.325:ray.x*.9;
p += rd * d *k;
if (d*d < 1e-7) {
hitPoint = hit;
sid = gid;
C+=render(p,rd,ro,d,ray.y,n)*atten;
if(m==1.||m==3.)break;
p += rd*.01;
k = sign(map(p).x);
if(int(F.x)%2 != int(F.y)%2||m==5.) {
atten *= m==5.?.75:.5;
rd=reflect(-rd,n);
p+=n*.02;
}else{
atten *= .85;
rd=refract(rd,n,.9);
}
}
if(distance(p,rd)>55.) {break;}
}
C = mix( C, C*.5,clamp((uv.y+.1)*1.35,0.,1.));
C = pow(C, vec3(.4545));
fragColor = vec4(C,1.);
}
</script>
<div id="container" />
html {
height: 100%;
}
body {
background: rgb(70, 42, 10);
background: linear-gradient(
180deg,
rgba(70, 42, 10, 1) 0%,
rgba(69, 53, 28, 1) 63%,
rgba(177, 129, 61, 1) 100%
);
overflow: hidden;
padding: 0;
margin: 0;
width: 100%;
height: 100%;
min-height: 100vh;
display: flex;
align-items: center;
position: relative;
}
canvas {
box-sizing: border-box;
padding: 0;
margin: 0;
outline: none;
box-shadow: 2px 20px 13px rgba(0, 0, 0, 0.75);
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 900px;
height: 450px;
z-index: 0;
}
const swidth = 900;
const sheight = 450;
// Fragment & Vertex Shaders in HTML window //
// Mouse Class for movments and attaching to dom //
class Mouse {
constructor(element) {
this.element = element || window;
this.drag = false;
this.cx = swidth / 2;
this.cy = sheight / 2;
this.x = this.cx;
this.y = this.cy;
this.pointer = this.pointer.bind(this);
this.getCoordinates = this.getCoordinates.bind(this);
this.events = ["mouseenter", "mousemove"];
this.events.forEach((eventName) => {
this.element.addEventListener(eventName, this.getCoordinates);
});
this.element.addEventListener("mousedown", () => {
this.drag = true;
});
this.element.addEventListener("mouseup", () => {
this.drag = false;
});
}
getCoordinates(event) {
event.preventDefault();
var rect = this.element.getBoundingClientRect();
const x = event.pageX - rect.left;
const y = event.pageY - rect.top;
if (this.drag) {
this.x = x;
this.y = y;
}
}
pointer() {
return {
x: this.x,
y: this.y
};
}
}
// Boostrap for WebGL and Attaching Shaders //
class Render {
constructor() {
this.start = Date.now();
this.umouse = [0.0, 0.0, 0.0, 0.0];
this.tmouse = [0.0, 0.0, 0.0, 0.0];
// Setup WebGL canvas and surface object //
// Make Canvas and get WebGl2 Context //
let width = swidth; //(this.width = ~~(document.documentElement.clientWidth,window.innerWidth || 0));
let height = sheight; //(this.height = ~~(document.documentElement.clientHeight,window.innerHeight || 0));
const canvas = (this.canvas = document.createElement("canvas"));
const container = document.getElementById("container");
canvas.id = "GLShaders";
canvas.width = width;
canvas.height = height;
this.mouse = new Mouse(canvas);
document.body.appendChild(canvas);
const gl = (this.gl = canvas.getContext("webgl2"));
if (!gl) {
console.warn("WebGL 2 is not available.");
return;
}
// WebGl and WebGl2 Extension //
this.gl.getExtension("OES_standard_derivatives");
this.gl.getExtension("EXT_shader_texture_lod");
this.gl.getExtension("OES_texture_float");
this.gl.getExtension("WEBGL_color_buffer_float");
this.gl.getExtension("OES_texture_float_linear");
this.gl.viewport(0, 0, canvas.width, canvas.height);
this.init();
}
// Shader Bootstrap code //
createShader = (type, source) => {
const shader = this.gl.createShader(type);
this.gl.shaderSource(shader, source);
this.gl.compileShader(shader);
const success = this.gl.getShaderParameter(shader, this.gl.COMPILE_STATUS);
if (!success) {
console.log(this.gl.getShaderInfoLog(shader));
this.gl.deleteShader(shader);
return false;
}
return shader;
};
createWebGL = (vertexSource, fragmentSource) => {
// Setup Vertext/Fragment Shader functions
this.vertexShader = this.createShader(this.gl.VERTEX_SHADER, vertexSource);
this.fragmentShader = this.createShader(
this.gl.FRAGMENT_SHADER,
fragmentSource
);
// Setup Program and Attach Shader functions
this.program = this.gl.createProgram();
this.gl.attachShader(this.program, this.vertexShader);
this.gl.attachShader(this.program, this.fragmentShader);
this.gl.linkProgram(this.program);
this.gl.useProgram(this.program);
if (!this.gl.getProgramParameter(this.program, this.gl.LINK_STATUS)) {
console.warn(
"Unable to initialize the shader program: " +
this.gl.getProgramInfoLog(this.program)
);
return null;
}
// Create and Bind buffer //
const buffer = this.gl.createBuffer();
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, buffer);
this.gl.bufferData(
this.gl.ARRAY_BUFFER,
new Float32Array([-1, 1, -1, -1, 1, -1, 1, 1]),
this.gl.STATIC_DRAW
);
const vPosition = this.gl.getAttribLocation(this.program, "vPosition");
this.gl.enableVertexAttribArray(vPosition);
this.gl.vertexAttribPointer(
vPosition,
2, // size: 2 components per iteration
this.gl.FLOAT, // type: the data is 32bit floats
false, // normalize: don't normalize the data
0, // stride: 0 = move forward size * sizeof(type) each iteration to get the next position
0 // start at the beginning of the buffer
);
this.clearCanvas();
this.importUniforms();
};
clearCanvas = () => {
this.gl.clearColor(0, 0, 0, 0);
this.gl.clear(this.gl.COLOR_BUFFER_BIT);
};
// add other uniforms here
importUniforms = () => {
const width = swidth;
const height = sheight;
this.resolution = new Float32Array([width, height]);
this.gl.uniform2fv(
this.gl.getUniformLocation(this.program, "resolution"),
this.resolution
);
// get the uniform ins from the shader fragments
this.ut = this.gl.getUniformLocation(this.program, "time");
this.ms = this.gl.getUniformLocation(this.program, "mouse");
};
// things that need to be updated per frame
updateUniforms = () => {
let tm = (Date.now() - this.start) / 1000;
//prevent time from getting too big
if (tm > 2000) this.start = Date.now();
// this.gl.uniform1f(this.ut,150.230);
this.gl.uniform1f(this.ut, (Date.now() - this.start) / 1000);
const mouse = this.mouse.pointer();
this.umouse = [mouse.x, mouse.y, 0];
const factor = 0.15;
this.tmouse[0] =
this.tmouse[0] - (this.tmouse[0] - this.umouse[0]) * factor;
this.tmouse[1] =
this.tmouse[1] - (this.tmouse[1] - this.umouse[1]) * factor;
this.tmouse[2] = mouse.z ? 1 : 0;
this.gl.uniform4fv(this.ms, this.tmouse);
this.gl.drawArrays(
this.gl.TRIANGLE_FAN, // primitiveType
0, // Offset
4 // Count
);
};
// setup shaders and send to render loop
init = () => {
this.createWebGL(
document.getElementById("vertexShader").textContent,
document.getElementById("fragmentShader").textContent
);
this.renderLoop();
};
renderLoop = () => {
this.updateUniforms();
this.animation = window.requestAnimationFrame(this.renderLoop);
};
}
const demo = new Render(document.body);
Also see: Tab Triggers