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="webgl" width="500" height="1758"></canvas>
<script id="vertexShader" type="x-shader/x-vertex">
attribute vec4 a_position;
uniform mat4 u_modelViewMatrix;
uniform mat4 u_projectionMatrix;
void main() {
gl_Position = a_position;
}
</script>
<script id="fragmentShader" type="x-shader/x-fragment">
precision highp float;
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
uniform sampler2D u_noise;
uniform sampler2D u_buffer;
uniform bool u_bufferpass;
#define PI 3.14159265359
#define TAU 6.28318530718
// These awesome complex Math functions curtesy of
// https://github.com/mkovacs/reim/blob/master/reim.glsl
vec2 cCis(float r);
vec2 cLog(vec2 c); // principal value
vec2 cInv(vec2 c);
float cArg(vec2 c);
float cAbs(vec2 c);
vec2 cMul(vec2 a, vec2 b);
vec2 cDiv(vec2 a, vec2 b);
vec2 cCis(float r)
{
return vec2( cos(r), sin(r) );
}
vec2 cExp(vec2 c)
{
return exp(c.x) * cCis(c.y);
}
vec2 cConj(vec2 c)
{
return vec2(c.x, -c.y);
}
vec2 cInv(vec2 c)
{
return cConj(c) / dot(c, c);
}
vec2 cLog(vec2 c)
{
return vec2( log( cAbs(c) ), cArg(c) );
}
float cArg(vec2 c)
{
return atan(c.y, c.x);
}
float cAbs(vec2 c)
{
return length(c);
}
vec2 cMul(vec2 a, vec2 b)
{
return vec2(a.x*b.x - a.y*b.y, a.x*b.y + a.y*b.x);
}
vec2 cDiv(vec2 a, vec2 b)
{
return cMul(a, cInv(b));
}
float r1 = 0.1;
float r2 = 0.3;
vec2 Droste(vec2 uv) {
// r1 = .1 + u_mouse.x;
r2 = .15 + max(u_mouse.y + .5, -.0);
// float c = cos(u_time);
// float s = sin(u_time);
// uv *= mat2(c, -s, s, c);
// 5. Take the tiled strips back to ordinary space.
uv = cLog(uv);
// 4. Scale and rotate the strips
float scale = log(r2/r1);
float angle = atan(scale/PI);
uv = cDiv(uv, cExp(vec2(0,angle))*cos(angle));
// 3. this simulates zooming in the tile
// uv -= u_time;
// 2. Tile the strips
uv.x = mod(uv.x,log(r2/r1));
// 1. Take the annulus to a strip
uv = cExp(uv)*r1;
return uv;
}
vec3 hash3( vec2 p ) {
vec3 q = vec3( dot(p,vec2(127.1,311.7)),
dot(p,vec2(269.5,183.3)),
dot(p,vec2(419.2,371.9)) );
return fract(sin(q)*43758.5453);
}
vec2 getScreenSpace() {
vec2 uv = (gl_FragCoord.xy - 0.5 * u_resolution.xy) / min(u_resolution.y, u_resolution.x);
return uv;
}
const float colours = 3.;
const vec4 colour1 = vec4(.1,.2,.8, 1.);
const vec4 colour2 = vec4(.8,.3,.2, 1.);
const vec4 colour3 = vec4(.1,.7,.2, 1.);
vec4 getColour(float r) {
float or = r;
r = floor(r*(colours+1.));
if(r == 0.) {
return colour1;
} else if(r == 1.) {
return colour2+vec4(0, (sin(u_time*3. + or*10.) * or + or), 0., 0.);
} else if(r == 2.) {
return colour3;
}
}
vec4 render(vec2 uv) {
// uv *= 10.;
// uv.x += u_time;
float row = floor(uv.y);
if(mod(row, 2.) == 0.) return vec4(0,0,0,1);
vec4 rowval = texture2D(u_noise, vec2(.5, row/110.));
float nf = rowval.r;
nf *= nf;
nf *= 5.;
uv.x += u_time * nf * 3.;
float noiseloopval = sin(uv.x*PI*.1)*floor(uv.y);
noiseloopval = mod(uv.x*row, row*2.);
vec2 uvid = floor(vec2( floor(noiseloopval), uv.y ));
vec3 uvseed = hash3(uvid/PI);
float shapefield = sin(fract(uv.y)*3.) * sin(fract(uv.x)*10.);
vec4 colour = getColour(rowval.g*(1.-rowval.g*(sin(u_time*3.)*.5+.5))) * smoothstep(.2,.6,shapefield);
colour += smoothstep(.9,1.,shapefield);
colour += smoothstep(.99,1.,shapefield)*5.;
// colour *= uvseed.x;
return mix(vec4(0,0,0,1), colour, colour.a);
}
vec4 render_effect(vec2 uv, vec4 prev) {
vec2 polar = vec2(atan(uv.x, uv.y)/PI, length(uv));
vec4 c = render(polar);
c += render(polar * vec2(2., .6) + vec2(0.,1./.6));
c += render(polar * vec2(1., 1.2) + vec2(0.,1./1.2));
uv = Droste(getScreenSpace())*20.;
polar = vec2(atan(uv.x, uv.y)/PI, length(uv));
c += render(polar * vec2(1., 2.2) + vec2(0.,2./2.2));
return c;
}
void main() {
vec4 prev = texture2D(u_buffer, gl_FragCoord.xy/u_resolution);
if(u_bufferpass) {
vec2 uv = Droste(getScreenSpace())*(u_mouse.x+.5) * 10.;
// uv = Droste(uv*getScreenSpace())*10.;
gl_FragColor = prev * .94 + render_effect(uv, prev) * .05;
} else {
gl_FragColor = prev;
}
}
</script>
body {
margin:0;
}
canvas {
position: fixed;
}
console.clear();
w = 1024;
w = 1024;
const twodWebGL = new WTCGL(
document.querySelector('canvas#webgl'),
document.querySelector('script#vertexShader').textContent,
document.querySelector('script#fragmentShader').textContent,
window.innerWidth,
window.innerHeight,
2
);
twodWebGL.startTime = -100 + Math.random() * 50;
let fb1 = twodWebGL.addFrameBuffer(window.innerWidth, window.innerHeight, WTCGL.IMAGETYPE_REGULAR, WTCGL.TEXTYPE_HALF_FLOAT_OES);
let fb2 = twodWebGL.addFrameBuffer(window.innerWidth, window.innerHeight, WTCGL.IMAGETYPE_REGULAR, WTCGL.TEXTYPE_HALF_FLOAT_OES);
let activeFB = fb1;
let timeout;
window.addEventListener('resize', () => {
clearTimeout(timeout);
timeout = setTimeout(() => {
twodWebGL.resize(window.innerWidth, window.innerHeight);
fb1 = twodWebGL.addFrameBuffer(window.innerWidth, window.innerHeight, WTCGL.IMAGETYPE_REGULAR, WTCGL.TEXTYPE_HALF_FLOAT_OES);
fb2 = twodWebGL.addFrameBuffer(window.innerWidth, window.innerHeight, WTCGL.IMAGETYPE_REGULAR, WTCGL.TEXTYPE_HALF_FLOAT_OES);
}, 100);
});
twodWebGL.onRun = (delta) => {
let _ctx = twodWebGL._ctx;
// find the active texture based on the index
uniform = _ctx.getUniformLocation(twodWebGL._program, `u_buffer`);
// Set the texture unit to the uniform
_ctx.uniform1i(uniform, 5);
_ctx.activeTexture(_ctx.TEXTURE5);
// Finally, bind the texture
_ctx.bindTexture(_ctx.TEXTURE_2D, activeFB.frameTexture);
activeFB = activeFB === fb1 ? fb2 : fb1;
twodWebGL.addUniform('bufferpass', WTCGL.TYPE_BOOL, true);
// twodWebGL.resize(1024, 1024);
twodWebGL.render(activeFB);
twodWebGL.addUniform('bufferpass', WTCGL.TYPE_BOOL, false);
// twodWebGL.resize(window.innerWidth, window.innerHeight);
oldmousepos[0] += (mousepos[0] - oldmousepos[0])*.02;
oldmousepos[1] += (mousepos[1] - oldmousepos[1])*.02;
// oldmousepos = mousepos;
twodWebGL.addUniform('mouse', WTCGL.TYPE_V2, oldmousepos);
}
// track mouse move
let oldmousepos = [0,0];
let mousepos = [0,0];
const u_mousepos = twodWebGL.addUniform('mouse', WTCGL.TYPE_V2, mousepos);
window.addEventListener('pointermove', (e) => {
let ratio = window.innerHeight / window.innerWidth;
if(window.innerHeight > window.innerWidth) {
mousepos[0] = (e.pageX - window.innerWidth / 2) / window.innerWidth;
mousepos[1] = (e.pageY - window.innerHeight / 2) / window.innerHeight * -1 * ratio;
} else {
mousepos[0] = (e.pageX - window.innerWidth / 2) / window.innerWidth / ratio;
mousepos[1] = (e.pageY - window.innerHeight / 2) / window.innerHeight * -1;
}
});
// Load all our textures. We only initiate the instance once all images are loaded.
const textures = [
{
name: 'noise',
url: 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/982762/noise.png',
type: WTCGL.IMAGETYPE_TILE,
img: null
}
];
const loadImage = function (imageObject) {
let img = document.createElement('img');
img.crossOrigin="anonymous";
return new Promise((resolve, reject) => {
img.addEventListener('load', (e) => {
imageObject.img = img;
resolve(imageObject);
});
img.addEventListener('error', (e) => {
reject(e);
});
img.src = imageObject.url
});
}
const loadTextures = function(textures) {
return new Promise((resolve, reject) => {
const loadTexture = (pointer) => {
if(pointer >= textures.length || pointer > 10) {
resolve(textures);
return;
};
const imageObject = textures[pointer];
const p = loadImage(imageObject);
p.then(
(result) => {
twodWebGL.addTexture(result.name, result.type, result.img);
},
(error) => {
console.log('error', error)
}).finally((e) => {
loadTexture(pointer+1);
});
}
loadTexture(0);
});
}
loadTextures(textures).then(
(result) => {
twodWebGL.initTextures();
// twodWebGL.render();
twodWebGL.running = true;
},
(error) => {
console.log('error');
}
);
Also see: Tab Triggers