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.
<!-- Shader Session with cacheflowe | 2022
*
* cacheflowe:
* https://cacheflowe.com
* https://www.instagram.com/cacheflowe/
*
* We're curiouslyminded:
* https://www.curiouslyminded.xyz
* https://www.twitch.tv/curiouslyminded
* https://www.youtube.com/curiouslyminded -->
<div id="shadercollab"></div>
<script id="vertex" type="x-shader/x-vertex">
void main() { gl_Position = vec4(position, 1.0); }
</script>
<script id="fragment" type="x-shader/x-fragment">
precision highp float;
uniform vec2 u_resolution;
uniform float u_time;
////////////////////////
// helpers
////////////////////////
#define PI 3.14159265358
#define TWO_PI 6.28318530718
vec2 rotateCoord(vec2 uv, float rads) {
uv *= mat2(cos(rads), sin(rads), -sin(rads), cos(rads));
return uv;
}
float saw(float rads) {
rads += PI * 0.5;
float percent = fract(rads/PI);
float dir = sign(sin(rads));
return dir * (2. * percent - 1.);
}
float oscBetween(float low, float high, float time, float offset) {
float range = abs(high - low);
float halfRange = range / 2.;
float midPoint = low + halfRange;
return midPoint + halfRange * sin(offset + time);
}
////////////////////////
// patterns
////////////////////////
vec3 drawChevronStripes(vec2 uv) {
// rotate
float rotate = oscBetween(-1., 1., u_time/2., 0.);
uv = rotateCoord(uv, rotate * -1.);
uv.y *= u_resolution.y / u_resolution.x;
// build params
float altTime = u_time * 0.5;
float chevronAmp = 0.06;
float freqAmp = oscBetween(0., 1., u_time/2., 0.);
float freq = 10. + freqAmp * 20.;
float zoom = oscBetween(0., 1., u_time/3., PI);
float numLines = 20. + zoom * 100.;
float x = uv.x;
// lerp between saw & sin
float sawWaveDisp = saw(x * freq);
float sinWaveDisp = sin(x * freq);
uv.y += chevronAmp * mix(sawWaveDisp, sinWaveDisp, 0.5 + 0.5 * sin(altTime));
float col = 0.5 + 0.5 * sin(uv.y * numLines);
return vec3(0.0, col, 0.0);
}
vec3 drawWarpVortex(vec2 uv) {
float rotate = oscBetween(-1., 1., u_time/3., 0.);
float altTime = u_time * 0.05;
float rads = atan(uv.x, uv.y) + rotate;
float zoom = oscBetween(0.3, 1., u_time/3., PI);
float dist = length(uv) * zoom;
float spinAmp = oscBetween(-2., 2., u_time/4., 0.);
float spinFreq = oscBetween(0.3, 5., u_time/3., PI);;
rads += sin(altTime + dist * spinFreq) * spinAmp * (1. - dist/8.);
float radialStripes = 24.;
float col = 0.5 + 0.5 * sin(rads * radialStripes);
return vec3(col);
}
vec3 newPattern(vec2 uv) {
float rotate = oscBetween(-1., 1., u_time/2., 0.);
uv = rotateCoord(uv, rotate * -1.);
uv.y *= u_resolution.y / u_resolution.x;
float stripes = fract(
uv.y * 5.0 + u_time
);
return vec3(stripes, 0., 0.);
}
////////////////////////
// main! combine the patterns
////////////////////////
void main()
{
// Centered pixel coordinates
//c2 uv = (-iResolution.xy + 2.0*fragCoord)/iResolution.y;
vec2 uv = (gl_FragCoord.xy - (u_resolution.xy * .5)) / u_resolution.yy;
uv *= 3.; // zoom a bit
// oscillated pattern mix
float drawFunc1Mix = oscBetween(0.5, 1., u_time/3., 0.);
float drawFunc2Mix = oscBetween(0.5, 1., u_time/2., PI/2.);
float drawFunc3Mix = oscBetween(0.5, 1., u_time/1., 0.);
// sum of patterns
vec3 col = vec3(0.);
col += drawFunc1Mix * drawChevronStripes(uv);
col += drawFunc2Mix * drawWarpVortex(uv);
col += drawFunc3Mix * newPattern(uv);
// test individual patterns
//col = newPattern(uv);
// "threshold" combine patterns & output
col = smoothstep(0.45, 0.55, col);
gl_FragColor = vec4(col, 1.0);
}
</script>
$c1: #000; // black
$c2: #fff; // white
$c3: #811121; // dark red
$c4: yellow;
* {
user-select: none;
}
body {
height: 100vh;
background-color: $c1;
margin: 0;
padding: 0;
overflow: hidden;
position: relative;
&.is-debugging {
overflow: scroll;
}
}
.debug-drawer {
width: auto;
position: absolute;
top: 0;
left: 0;
border: 4px solid gray;
pre {
background-color: $c1; // black
color: $c2; // white
padding: 5px;
margin: 0;
.error-line {
background: $c3;
}
.error {
color: $c4;
font-style: italic;
}
}
}
/*
* SHADER SESSION WITH CACHEFLOWE
* NOV 2022
*
* cacheflowe:
* https://cacheflowe.com
* https://www.instagram.com/cacheflowe/
*
* We're curiouslyminded:
* https://www.curiouslyminded.xyz
* https://www.twitch.tv/curiouslyminded
* https://www.youtube.com/curiouslyminded
*
*
* GLSL Debugger in JS by Sean Zellmer:
* https://twitter.com/lejeunerenard
*
*/
let camera, scene, renderer, clock;
let uniforms;
function shaderErrorView (errors, code) {
// Alter code to display error
const codeLines = code.trim().split('\n');
const maxGutter = Math.floor(Math.log10(codeLines.length));
const errorLines = codeLines.map(function (line, i) {
i += 1; // Adjust for 1-indexed errors
const lineNumWidth = Math.floor(Math.log10(i));
const gutter = ' '.repeat(maxGutter - lineNumWidth) + i;
// Error lineNumber is based on non-trimmed code which includes an empty first line
const lineErrors = errors.filter((error) => error.lineNumber - 1 === i);
const lineClass = lineErrors.length ? 'error-line' : '';
let errorMessages = lineErrors.map((e) => '<span class="error">' + e.message + '</span>').join(', ');
if (errorMessages !== '') errorMessages = ' ' + errorMessages;
return `<span class="${lineClass}">${gutter}| ${line}${errorMessages}</span>`;
});
const container = document.createElement('div');
container.classList.add('debug-drawer');
document.body.classList.add('is-debugging');
const pre = document.createElement('pre');
const codeContainer = document.createElement('code');
codeContainer.innerHTML = errorLines.join('\n');
pre.appendChild(codeContainer);
container.appendChild(pre);
return container;
}
function checkForShaderErrors (renderer) {
var errors = [];
const currentScript = 'fragmentShader';
var programs = renderer.info.programs;
valid = true;
var parseMessage = /^(?:ERROR|WARNING): \d+:(\d+): (.*)/gm; // Fixed threejs regex by adding `m` flag
for (var i = 0, n = programs.length; i !== n; ++i) {
var diagnostics = programs[ i ].diagnostics;
if ( diagnostics === undefined ) continue;
if ( ! diagnostics.runnable ) valid = false;
var shaderInfo = diagnostics[ currentScript ];
var lineOffset = shaderInfo.prefix.split( /\r\n|\r|\n/ ).length;
while(true) {
var parseResult = parseMessage.exec( shaderInfo.log );
if ( parseResult === null ) break;
errors.push({
lineNumber: parseResult[ 1 ] - lineOffset,
message: parseResult[ 2 ]
});
} // messages
break;
} // programs
return errors;
}
function init() {
const container = document.getElementById("shadercollab");
clock = new THREE.Clock();
camera = new THREE.Camera();
camera.position.z = 1;
scene = new THREE.Scene();
const geometry = new THREE.PlaneBufferGeometry(2, 2);
uniforms = {
u_time: { type: "f", value: 1.0 },
u_resolution: { type: "v2", value: new THREE.Vector2() },
u_mouse: { type: "v2", value: new THREE.Vector2() },
};
const material = new THREE.ShaderMaterial({
uniforms,
vertexShader: document.getElementById("vertex").textContent,
fragmentShader: document.getElementById("fragment").textContent
});
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio(window.devicePixelRatio);
container.appendChild(renderer.domElement);
onWindowResize();
window.addEventListener("resize", onWindowResize);
}
function onWindowResize() {
renderer.setSize(window.innerWidth, window.innerHeight);
uniforms.u_resolution.value.x = renderer.domElement.width;
uniforms.u_resolution.value.y = renderer.domElement.height;
}
let runOnce = false;
function render() {
uniforms.u_time.value = clock.getElapsedTime();
renderer.render(scene, camera);
if (!runOnce) {
const errors = checkForShaderErrors(renderer);
if (errors.length) {
const overlay = shaderErrorView(errors, document.getElementById("fragment").textContent);
document.body.appendChild(overlay);
}
runOnce = true;
}
}
function animate() {
render();
requestAnimationFrame(animate);
}
init();
animate();
document.onmousemove = function (e) {
uniforms.u_mouse.value.x = e.pageX;
uniforms.u_mouse.value.y = e.pageY;
};
Also see: Tab Triggers