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 URL's added here will be added as <link>
s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.
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.
If the stylesheet 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 CSS 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.
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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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="webgl-contents"></canvas>
<script id="vs" type="x-shader/x-vertex">
precision highp float;
attribute vec3 position;
void main(void) {
gl_Position = vec4(position, 1.0);
}
</script>
<script id="fs" type="x-shader/x-fragment">
#extension GL_OES_standard_derivatives : enable
precision highp float;
uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;
const float PI = 3.14159265;
const float angle = 60.0;
const float fov = angle * 0.5 * PI / 180.0;
const vec3 lightDir = vec3(0.577, -0.577, 0.577);
vec3 hsv2rgb(vec3 c){
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
float smin(float d1, float d2, float k){
float h = exp(-k * d1) + exp(-k * d2);
return -log(h) / k;
}
float dSphere(vec3 p, float r) {
return length(p) - r;
}
float dBox(vec3 p, vec3 size) {
return length(max(abs(p) - size, 0.0));
}
float dTorus(vec3 p, vec2 t) {
vec2 q = vec2(length(p.xz) - t.x, p.y);
return length(q) - t.y;
}
float dCapsule(vec3 p, vec3 a, vec3 b, float r) {
vec3 pa = p - a, ba = b - a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length( pa - ba*h ) - r;
}
vec3 sphericalPolarCoord(float radius, float radian1, float radian2) {
return vec3(
radius * sin(radian1) * cos(radian2),
radius * sin(radian1) * sin(radian2),
radius * cos(radian1)
);
}
vec3 rotateX(vec3 p, float radian) {
mat3 m = mat3(
1.0, 0.0, 0.0,
0.0, cos(radian), -sin(radian),
0.0, sin(radian), cos(radian)
);
return m * p;
}
vec3 rotateY(vec3 p, float radian) {
mat3 m = mat3(
cos(radian), 0.0, sin(radian),
0.0, 1.0, 0.0,
-sin(radian), 0.0, cos(radian)
);
return m * p;
}
vec3 rotateZ(vec3 p, float radian) {
mat3 m = mat3(
cos(radian), -sin(radian), 0.0,
sin(radian), cos(radian), 0.0,
0.0, 0.0, 1.0
);
return m * p;
}
vec3 rotate(vec3 p, float radian_x, float radian_y, float radian_z) {
mat3 mx = mat3(
1.0, 0.0, 0.0,
0.0, cos(radian_x), -sin(radian_x),
0.0, sin(radian_x), cos(radian_x)
);
mat3 my = mat3(
cos(radian_y), 0.0, sin(radian_y),
0.0, 1.0, 0.0,
-sin(radian_y), 0.0, cos(radian_y)
);
mat3 mz = mat3(
cos(radian_z), -sin(radian_z), 0.0,
sin(radian_z), cos(radian_z), 0.0,
0.0, 0.0, 1.0
);
return mx * my * mz * p;
}
float distanceFunc(vec3 p) {
vec3 p11 = rotate(p, radians(-time), radians(time), radians(time));
vec3 p12 = sphericalPolarCoord(2.0, radians(time), radians(time));
float d1 = dBox(p11 + p12, vec3(1.0));
vec3 p21 = rotate(p, radians(time), radians(-time), radians(time));
vec3 p22 = sphericalPolarCoord(2.0, radians(-time), radians(time));
float d2 = dTorus(p21 + p22, vec2(2.0, 0.3));
vec3 p31 = rotate(p, radians(time * 2.0), radians(time * 2.0), radians(time * -2.0));
vec3 p32 = sphericalPolarCoord(2.0, radians(time), radians(-time));
float d3 = dCapsule(p31 + p32, vec3(1.0), vec3(-1.0), 0.4);
vec3 p4 = sphericalPolarCoord(2.0, radians(-time), radians(-time));
float d4 = dSphere(p + p4, 1.0);
return smin(smin(d1, d2, 2.0), smin(d3, d4, 2.0), 2.0);
}
vec3 getNormal(vec3 p) {
const float d = 0.0001;
return normalize(vec3(
distanceFunc(p + vec3(d, 0.0, 0.0)) - distanceFunc(p + vec3(-d, 0.0, 0.0)),
distanceFunc(p + vec3(0.0, d, 0.0)) - distanceFunc(p + vec3(0.0, -d, 0.0)),
distanceFunc(p + vec3(0.0, 0.0, d)) - distanceFunc(p + vec3(0.0, 0.0, -d))
));
}
void main() {
vec2 p = (gl_FragCoord.xy * 2.0 - resolution) / min(resolution.x, resolution.y);
vec3 cPos = vec3(0.0, 0.0, 10.0);
vec3 cDir = vec3(0.0, 0.0, -1.0);
vec3 cUp = vec3(0.0, 1.0, 0.0);
vec3 cSide = cross(cDir, cUp);
float targetDepth = 1.8;
vec3 ray = normalize(cSide * p.x + cUp * p.y + cDir * targetDepth);
float distance = 0.0;
float rLen = 0.0;
vec3 rPos = cPos;
for(int i = 0; i < 64; i++){
distance = distanceFunc(rPos);
rLen += distance;
rPos = cPos + ray * rLen;
}
vec3 normal = getNormal(rPos);
if(abs(distance) < 0.001){
gl_FragColor = vec4(hsv2rgb(vec3(dot(normal, cUp) / 4.0, 0.5, 0.9)), 1.0);
}else{
gl_FragColor = vec4(0.0);
}
}
</script>
html {
height: 100%;
}
body {
min-height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
const canvas = document.getElementById('webgl-contents');
const gl = canvas.getContext('webgl');
const loadProgram = (gl, vs_src, fs_src) => {
const program = gl.createProgram();
const vs = loadShader(gl.VERTEX_SHADER, gl, vs_src);
const fs = loadShader(gl.FRAGMENT_SHADER, gl, fs_src);
gl.attachShader(program, vs);
gl.attachShader(program, fs);
gl.linkProgram(program);
gl.useProgram(program);
return program;
};
const loadShader = (type, gl, shader_src) => {
const shader = gl.createShader(type);
gl.shaderSource(shader, shader_src);
gl.compileShader(shader);
return shader;
};
const createVBO = (gl, array) => {
const buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, array, gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
return buffer;
};
const createIBO = (gl, array) => {
const buffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, array, gl.STATIC_DRAW);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
return buffer;
};
const setArrayBuffer = (gl, buffer, array, length) => {
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.enableVertexAttribArray(array);
gl.vertexAttribPointer(array, length, gl.FLOAT, false, 0, 0);
};
const vertices = [
-1.0, 1.0, 0.0,
1.0, 1.0, 0.0,
-1.0, -1.0, 0.0,
1.0, -1.0, 0.0,
];
const indecies = [
0, 2, 1,
1, 2, 3,
];
const init = () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
canvas.style.width = `${window.innerWidth}px`;
canvas.style.height = `${window.innerHeight}px`;
if(!gl.getExtension('OES_standard_derivatives')){
console.log('OES_standard_derivatives is not supported');
return;
}
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.enable(gl.DEPTH_TEST);
gl.enable(gl.CULL_FACE);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.viewport(0, 0, canvas.width, canvas.height);
let time = 0;
const program = loadProgram(gl, document.getElementById('vs').textContent, document.getElementById('fs').textContent);
const uni_time = gl.getUniformLocation(program, 'time');
gl.uniform1f(uni_time, time);
const uni_mouse = gl.getUniformLocation(program, 'mouse');
gl.uniform2fv(uni_mouse, [0, 0]);
const uni_resolution = gl.getUniformLocation(program, 'resolution');
gl.uniform2fv(uni_resolution, [window.innerWidth, window.innerHeight]);
const attr_position = gl.getAttribLocation(program, 'position');
const vertex_buffer = createVBO(gl, new Float32Array(vertices));
gl.bindBuffer(gl.ARRAY_BUFFER, null);
const attr_index = gl.getAttribLocation(program, 'index');
const index_buffer = createIBO(gl, new Uint16Array(indecies));
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
const render = () => {
time ++;
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clearDepth(1.0);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.useProgram(program);
setArrayBuffer(gl, vertex_buffer, attr_position, 3);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, index_buffer);
gl.uniform1f(uni_time, time);
gl.uniform2fv(uni_resolution, [window.innerWidth, window.innerHeight]);
gl.drawElements(gl.TRIANGLES, indecies.length, gl.UNSIGNED_SHORT, 0);
gl.bindTexture(gl.TEXTURE_2D, null);
};
const renderLoop = () => {
render();
requestAnimationFrame(renderLoop);
};
renderLoop();
};
init();
Also see: Tab Triggers