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 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.
<!-- 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.14159265359
#define PI2 6.28318530718
#define MAX_DIST 30.00
#define MIN_DIST 0.001
float hash21(vec2 a){ return fract(sin(dot(a, vec2(27.609, 57.583)))*43758.5453); }
mat2 rot(float a) { return mat2(cos(a),sin(a),-sin(a),cos(a)); }
//@iq https://iquilezles.org/www/articles/palettes/palettes.htm
vec3 hue(float t){
vec3 d = vec3(0.510,0.584,0.349);
return .45+.4*cos( PI2*t*vec3(.95,.97,.88)*d );
}
//@iq cylinder
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);
}
const float sz = 2.;
const float hl = sz*.5;
const vec2 boxSize = vec2(sz*.465,.15);
const float density = 16.;
//global
vec3 hit,ghp;
vec2 cellId,gid;
float lpscale,movement;
mat2 turn;
vec2 map(vec3 q){
vec2 res = vec2(1e5,0.);
vec2 p = q.xz;
p*=turn;
float r = length(p);
p = vec2(log(r), atan(p.y, p.x));
p *= lpscale;
float mul = r/lpscale;
p.y -= hl;
p.x += .0 + movement;
vec2 id = floor((p+hl)/sz) - 1.5;
p = mod(p+hl,sz)-hl;
vec3 lp = vec3(p.x, max(0.0, q.y/mul), p.y);
float bx = box(lp,boxSize.xyx)-.035;
if(bx<res.x) {
res = vec2(bx*mul,2.);
gid = id;
ghp = lp;
}
return res;
}
// Tetrahedron technique @iq
// https://www.iquilezles.org/www/articles/normalsSDF
vec3 normal(vec3 p, float t)
{
float e = MIN_DIST*t;
vec2 h =vec2(1,-1)*.5773;
vec3 n = 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;
return normalize(n);
}
vec3 truchet(vec2 vuv) {
float px = fwidth(length(vuv)/PI);
vec2 id = cellId;
vec2 grid = vuv;
float hs = hash21(id);
if(hs>.5) grid.x*=-1.;
vec3 h = vec3(0);
vec3 bc= vec3(1);
float chk = mod(id.y + id.x,2.) * 2. - 1.;
vec2 d2 = vec2(length(grid-hl), length(grid+hl));
vec2 gx = d2.x<d2.y? vec2(grid-hl) : vec2(grid+hl);
float circle = length(gx)-hl;
float circle2 = abs(abs(circle)-.125)-(.085+.065*sin(vuv.x*3.25) );
circle2=abs(abs(circle2)-.04)-.02;
circle2=smoothstep(-px,px,circle2);
// color flip for every other one and then ones
// thats are flipped by the hash
circle=(chk>0.^^ hs>.5) ? smoothstep(px,-px,circle) : smoothstep(-px,px,circle);
vec2 sx = abs(grid)-hl;
float cbx = length(sx)-.35;
cbx=abs(abs(cbx)-.075)-(.025+.015*sin(vuv.x*3.25));
cbx=smoothstep(px,-px,cbx);
h = mix(h, bc, min(circle2,circle));
h = mix(h, bc, cbx);
return h;
}
void main()
{
// pre-cal
// dont you know you have to chew
// your variables before using
lpscale = floor(density)/PI;
movement = time*lpscale * .123;
turn = rot(T*5.*PI/180.);
vec2 F = gl_FragCoord.xy;
vec2 uv = (2.* F.xy-R.xy)/max(R.x,R.y);
vec3 ro = vec3(0, 0, 8);
vec3 rd = normalize(vec3(uv, -1.0));
// mouse //
float x = M.xy==vec2(0) ? 0. : -(M.y/R.y*.25-.125)*PI;
float y = M.xy==vec2(0) ? 0. : -(M.x/R.x*.5-.25)*PI;
mat2 rx =rot((-.75+.2*sin(T*.1))+x);
mat2 ry =rot((.8*sin(T*.3))+y);
ro.zy*=rx;rd.zy*=rx;
ro.xz*=ry;rd.xz*=ry;
vec3 C = vec3(0);
float m = 0.;
float d = 0.;
vec3 p = ro;
for(int i=0;i<100;i++)
{
p = ro + rd * d;
vec2 ray = map(p);
if(abs(ray.x)<MIN_DIST*d||d>MAX_DIST)break;
d += i<64? ray.x*.5: ray.x;
m = ray.y;
}
hit = ghp;
cellId = gid;
float alpha = 0.;
if(d<MAX_DIST)
{
vec3 n = normal(p,d);
vec3 lpos = vec3(0,8,.5)*lpscale;
vec3 l = normalize(lpos-p);
float diff = clamp(dot(n,l),0.,1.);
vec3 view = normalize(p - ro);
vec3 ret = reflect(normalize(lpos), n);
float spec = 0.75 * pow(max(dot(view, ret), 0.), 24.);
vec3 h = vec3(.05);
if(m==2.) h = truchet(hit.xz)* hue(100.+cellId.x*.1);
C = h * diff + spec;
}
C = mix(vec3(0),C,exp(-.00125*d*d*d));
C=pow(C, vec3(.4545));
// Output to screen
fragColor = vec4(C,1.0);
}
</script>
<div id="container" />
html {
height: 100%;
}
img {
display: none;
}
body {
background: #000;
overflow: hidden;
padding: 0;
margin: 0;
width: 100%;
height: 100%;
text-align: center;
}
canvas {
height: 100%;
width: 100%;
margin: auto;
}
// Boostrap for WebGL and Attaching Shaders //
// Fragment & Vertex Shaders in HTML window //
class Render {
constructor() {
this.start = Date.now();
// Setup WebGL canvas and surface object //
// Make Canvas and get WebGl2 Context //
let width = (this.width = ~~(document.documentElement.clientWidth,
window.innerWidth || 0));
let height = (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;
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);
// always nice to let people resize
window.addEventListener(
"resize",
() => {
let width = ~~(document.documentElement.clientWidth,
window.innerWidth || 0);
let height = ~~(document.documentElement.clientHeight,
window.innerHeight || 0);
this.canvas.width = width;
this.canvas.height = height;
this.gl.viewport(0, 0, this.canvas.width, this.canvas.height);
this.resolution = new Float32Array([width, height]);
this.gl.uniform2fv(
this.gl.getUniformLocation(this.program, "resolution"),
this.resolution
);
this.clearCanvas();
},
false
);
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 = ~~(document.documentElement.clientWidth,
window.innerWidth || 0);
const height = ~~(document.documentElement.clientHeight,
window.innerHeight || 0);
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");
};
// 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, (Date.now() - this.start) / 1000);
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