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.
Private Pens are hidden everywhere on CodePen, except to you. You can still share them and other people can see them, they just can't find them through searching or browsing.
Upgrade to PROIf 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.
Templates are Pens that can be used to start other Pens quickly from the create menu. The new Pen will copy all the code and settings from the template and make a new Pen (that is not a fork). You can view all of your templates, or learn more in the documentation.
Any Pen can act as a template (even if you don't flip the toggle above) with a special URL you can use yourself or share with others. Here's this Pen's template URL:
Screenshots of Pens are shown in mobile browsers, RSS feeds, to users who chose images instead of iframes, and in social media sharing.
This Pen is using the default Screenshot, generated by CodePen. Upgrade to PRO to upload your own thumbnail that will be displayed on previews of this pen throughout the site and when sharing to social media.
Upgrade to PRO
<!--
An attempt to try out a technique mentioned by @minionsart and @ciaccodavide on Twitter:
https://twitter.com/minionsart/status/964257071423283200
https://twitter.com/ciaccodavide/status/964407412634472448
-->
<script type="x-shader/x-fragment" id="shader-flame-fragment">
uniform vec3 colorStart;
uniform vec3 colorEnd;
uniform sampler2D textureGradient;
uniform sampler2D textureNoise;
uniform float threshold;
varying vec2 vUv;
void main() {
float gradient = texture2D(textureGradient, vUv).r;
float noise = texture2D(textureNoise, vUv).r;
float alpha = gradient * noise;
if (alpha < threshold) discard;
float alphaRender = (1.0 - threshold) * alpha;
vec3 color = mix(colorStart, colorEnd, alphaRender);
gl_FragColor = vec4(color, 1.0);
}
</script>
<script type="x-shader/x-fragment" id="shader-gradient-fragment">
uniform float power;
varying vec2 vUv;
void main() {
float darkness = pow(1.0 - vUv.y, power);
gl_FragColor = vec4(darkness, darkness, darkness, 1.0);
}
</script>
<script type="x-shader/x-fragment" id="shader-noise-fragment">
uniform int octaves;
uniform float persistence;
uniform float scale;
uniform float speed;
uniform float time;
varying vec2 vUv;
const int OCTAVES_MAX = 16;
float noiseOctaves(int octaves, vec2 position, float persistence, vec2 frequency) {
if (octaves == 0) return 0.0;
float amplitude = 1.0;
float amplitudeSum = 0.0;
float noise = 0.0;
for (int i = 0; i < OCTAVES_MAX; i++) {
if (i >= octaves) break;
float octave = float(i);
float t = octave / float(octaves);
noise = noise + pnoise(position * frequency, frequency) * amplitude;
amplitudeSum = amplitudeSum + amplitude;
amplitude = amplitude * persistence;
frequency = 2.0 * frequency;
}
return (1.0 + noise / amplitudeSum) * 0.5;
}
void main() {
vec2 position = vec2(0.0, -time) * speed + vUv;
float noise = noiseOctaves(octaves, position, persistence, vec2(scale, scale));
gl_FragColor = vec4(noise, noise, noise, 1.0);
}
</script>
<script type="x-shader/x-vertex" id="shader-passthrough-vertex">
varying vec2 vUv;
void main() {
vUv = uv.xy;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
</script>
<script type="x-shader/x-fragment" id="shader-texture-fragment">
uniform sampler2D texture;
varying vec2 vUv;
void main() {
gl_FragColor = texture2D(texture, vUv);
}
</script>
<script type="x-shader/x-generic" id="shader-noise-2d">
//
// GLSL textureless classic 2D noise "cnoise",
// with an RSL-style periodic variant "pnoise".
// Author: Stefan Gustavson ([email protected])
// Version: 2011-08-22
//
// Many thanks to Ian McEwan of Ashima Arts for the
// ideas for permutation and gradient selection.
//
// Copyright (c) 2011 Stefan Gustavson. All rights reserved.
// Distributed under the MIT license. See LICENSE file.
// https://github.com/stegu/webgl-noise
//
vec4 mod289(vec4 x)
{
return x - floor(x * (1.0 / 289.0)) * 289.0;
}
vec4 permute(vec4 x)
{
return mod289(((x*34.0)+1.0)*x);
}
vec4 taylorInvSqrt(vec4 r)
{
return 1.79284291400159 - 0.85373472095314 * r;
}
vec2 fade(vec2 t) {
return t*t*t*(t*(t*6.0-15.0)+10.0);
}
// Classic Perlin noise
float cnoise(vec2 P)
{
vec4 Pi = floor(P.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
vec4 Pf = fract(P.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
Pi = mod289(Pi); // To avoid truncation effects in permutation
vec4 ix = Pi.xzxz;
vec4 iy = Pi.yyww;
vec4 fx = Pf.xzxz;
vec4 fy = Pf.yyww;
vec4 i = permute(permute(ix) + iy);
vec4 gx = fract(i * (1.0 / 41.0)) * 2.0 - 1.0 ;
vec4 gy = abs(gx) - 0.5 ;
vec4 tx = floor(gx + 0.5);
gx = gx - tx;
vec2 g00 = vec2(gx.x,gy.x);
vec2 g10 = vec2(gx.y,gy.y);
vec2 g01 = vec2(gx.z,gy.z);
vec2 g11 = vec2(gx.w,gy.w);
vec4 norm = taylorInvSqrt(vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
g00 *= norm.x;
g01 *= norm.y;
g10 *= norm.z;
g11 *= norm.w;
float n00 = dot(g00, vec2(fx.x, fy.x));
float n10 = dot(g10, vec2(fx.y, fy.y));
float n01 = dot(g01, vec2(fx.z, fy.z));
float n11 = dot(g11, vec2(fx.w, fy.w));
vec2 fade_xy = fade(Pf.xy);
vec2 n_x = mix(vec2(n00, n01), vec2(n10, n11), fade_xy.x);
float n_xy = mix(n_x.x, n_x.y, fade_xy.y);
return 2.3 * n_xy;
}
// Classic Perlin noise, periodic variant
float pnoise(vec2 P, vec2 rep)
{
vec4 Pi = floor(P.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
vec4 Pf = fract(P.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
Pi = mod(Pi, rep.xyxy); // To create noise with explicit period
Pi = mod289(Pi); // To avoid truncation effects in permutation
vec4 ix = Pi.xzxz;
vec4 iy = Pi.yyww;
vec4 fx = Pf.xzxz;
vec4 fy = Pf.yyww;
vec4 i = permute(permute(ix) + iy);
vec4 gx = fract(i * (1.0 / 41.0)) * 2.0 - 1.0 ;
vec4 gy = abs(gx) - 0.5 ;
vec4 tx = floor(gx + 0.5);
gx = gx - tx;
vec2 g00 = vec2(gx.x,gy.x);
vec2 g10 = vec2(gx.y,gy.y);
vec2 g01 = vec2(gx.z,gy.z);
vec2 g11 = vec2(gx.w,gy.w);
vec4 norm = taylorInvSqrt(vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
g00 *= norm.x;
g01 *= norm.y;
g10 *= norm.z;
g11 *= norm.w;
float n00 = dot(g00, vec2(fx.x, fy.x));
float n10 = dot(g10, vec2(fx.y, fy.y));
float n01 = dot(g01, vec2(fx.z, fy.z));
float n11 = dot(g11, vec2(fx.w, fy.w));
vec2 fade_xy = fade(Pf.xy);
vec2 n_x = mix(vec2(n00, n01), vec2(n10, n11), fade_xy.x);
float n_xy = mix(n_x.x, n_x.y, fade_xy.y);
return 2.3 * n_xy;
}
</script>
canvas {
display: block;
}
class ShaderChunkLibrary {
static get flameFragment() {
return document.getElementById('shader-flame-fragment').textContent;
}
static get noise2DPeriodic() {
return document.getElementById('shader-noise-2d').textContent;
}
static get gradientFragment() {
return document.getElementById('shader-gradient-fragment').textContent;
}
static get noiseFragment() {
return ShaderChunkLibrary.noise2DPeriodic + document.getElementById('shader-noise-fragment').textContent;
}
static get passThroughVertex() {
return document.getElementById('shader-passthrough-vertex').textContent;
}
static get textureFragment() {
return document.getElementById('shader-texture-fragment').textContent;
}
}
class BufferRenderer {
constructor(material) {
this.camera = new THREE.OrthographicCamera(-0.5, 0.5, 0.5, -0.5, 1, 3);
this.camera.position.z = 2;
this.material = material;
this.plane = BufferRenderer.createPlane(material);
this.scene = new THREE.Scene();
this.scene.add(this.plane);
}
render(renderer, renderTarget) {
renderer.render(this.scene, this.camera, renderTarget);
}
static createPlane(material) {
const geometry = new THREE.PlaneBufferGeometry(1, 1, 1, 1);
return new THREE.Mesh(geometry, material);
}
}
class Application {
static get RENDERER_OPTIONS() {
return {
antialias: true,
};
}
constructor() {
this.bufferRenderers = Application.createBufferRenderers();
this.buffers = new Array(3).fill().map(_ => new THREE.WebGLRenderTarget(1024, 1024, {
wrapS: THREE.RepeatWrapping,
wrapT: THREE.RepeatWrapping,
}));
this.cylinder = Application.createCylinder();
this.planes = Application.createPlanes(32, 8);
this.camera = new THREE.PerspectiveCamera(45, 1, 1, 512);
this.camera.position.z = 256;
this.renderer = new THREE.WebGLRenderer(Application.RENDERER_OPTIONS);
this.scene = new THREE.Scene();
this.scene.add(this.cylinder, ...Object.values(this.planes));
this.controls = new THREE.OrbitControls(this.camera, this.renderer.domElement);
this.controls.update();
}
setSize(width, height) {
this.camera.aspect = width / height;
this.camera.updateProjectionMatrix();
this.renderer.setSize(width, height);
}
render(time) {
this.controls.update();
this.bufferRenderers.gradient.render(this.renderer, this.buffers[0]);
this.bufferRenderers.noise.material.uniforms.time.value = time;
this.bufferRenderers.noise.render(this.renderer, this.buffers[1]);
this.planes.gradient.material.uniforms.texture.value = this.buffers[0].texture;
this.planes.noise.material.uniforms.texture.value = this.buffers[1].texture;
this.cylinder.material.uniforms.textureGradient.value = this.buffers[0].texture;
this.cylinder.material.uniforms.textureNoise.value = this.buffers[1].texture;
this.renderer.render(this.scene, this.camera);
}
static createBuffer(size) {
return THREE.WebGLRenderTarget(size, size, {
wrapS: THREE.RepeatWrapping,
wrapT: THREE.RepeatWrapping,
});
}
static createBufferRenderers() {
const materialBasic = new THREE.MeshBasicMaterial();
const materialGradient = new THREE.ShaderMaterial({
fragmentShader: ShaderChunkLibrary.gradientFragment,
vertexShader: ShaderChunkLibrary.passThroughVertex,
uniforms: {
power: { value: 1 },
},
});
const materialNoise = new THREE.ShaderMaterial({
fragmentShader: ShaderChunkLibrary.noiseFragment,
vertexShader: ShaderChunkLibrary.passThroughVertex,
uniforms: {
octaves: { value: 1 },
persistence: { value: 1 },
scale: { value: 1 },
speed: { value: 1 },
time: { value: 0 },
}
});
return {
gradient: new BufferRenderer(materialGradient),
noise: new BufferRenderer(materialNoise),
};
}
static createCylinder() {
const geometry = new THREE.CylinderBufferGeometry(16, 16, 64, 16, 1, true);
const material = new THREE.ShaderMaterial({
blending: THREE.AdditiveBlending,
depthTest: false,
fragmentShader: ShaderChunkLibrary.flameFragment,
vertexShader: ShaderChunkLibrary.passThroughVertex,
side: THREE.DoubleSide,
transparent: true,
uniforms: {
colorStart: { value: null },
colorEnd: { value: null },
textureGradient: { value: null },
textureNoise: { value: null },
threshold: { value: 0 }
},
});
return new THREE.Mesh(geometry, material);
}
static createPlanes(size, spacing = 0) {
const createMaterial = () => new THREE.ShaderMaterial({
fragmentShader: ShaderChunkLibrary.textureFragment,
vertexShader: ShaderChunkLibrary.passThroughVertex,
side: THREE.DoubleSide,
uniforms: {
texture: { value: null },
},
});
const planes = {
noise: Application.createTexturePlane(size, createMaterial()),
gradient: Application.createTexturePlane(size, createMaterial()),
};
Object.values(planes).forEach((p, i, arr) => p.position.set(((i + 0.5) - (arr.length * 0.5)) * (size + spacing), size, 0));
return planes;
}
static createTexturePlane(size, material) {
const geometry = new THREE.PlaneBufferGeometry(size, size, 1, 1);
return new THREE.Mesh(geometry, material);
}
}
const animate = (callback) => {
const update = (time) => {
requestAnimationFrame(update);
callback(time / 1000);
};
update(performance.now());
};
const createUpdateSettingsCallback = (options) => () => {
app.cylinder.material.uniforms.colorStart.value = new THREE.Color(options.composition.colorStart);
app.cylinder.material.uniforms.colorEnd.value = new THREE.Color(options.composition.colorEnd);
app.cylinder.material.uniforms.threshold.value = options.composition.threshold;
app.bufferRenderers.gradient.material.uniforms.power.value = options.gradient.power;
app.bufferRenderers.noise.material.uniforms.octaves.value = options.noise.octaves;
app.bufferRenderers.noise.material.uniforms.persistence.value = options.noise.persistence;
app.bufferRenderers.noise.material.uniforms.scale.value = options.noise.scale;
app.bufferRenderers.noise.material.uniforms.speed.value = options.noise.speed;
};
const app = new Application();
app.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(app.renderer.domElement);
const gui = new dat.GUI();
const guiOptions = {
composition: {
colorStart: '#F00',
colorEnd: '#FF0',
threshold: 0.25,
},
gradient: {
power: 1.5,
},
noise: {
octaves: 8,
persistence: 0.5,
scale: 16,
speed: 0.75,
},
};
const updateSettings = createUpdateSettingsCallback(guiOptions);
const guiComposition = gui.addFolder('Composition');
guiComposition.addColor(guiOptions.composition, 'colorStart').onChange(updateSettings);
guiComposition.addColor(guiOptions.composition, 'colorEnd').onChange(updateSettings);
guiComposition.add(guiOptions.composition, 'threshold', 0, 1, 0.05).onChange(updateSettings);
const guiGradient = gui.addFolder('Gradient');
guiGradient.add(guiOptions.gradient, 'power', 0, 3, 0.25).onChange(updateSettings);
const guiNoise = gui.addFolder('Noise');
guiNoise.add(guiOptions.noise, 'octaves', 1, 16, 1).onChange(updateSettings);
guiNoise.add(guiOptions.noise, 'persistence', 0, 1, 0.05).onChange(updateSettings);
guiNoise.add(guiOptions.noise, 'scale', 1, 16, 1).onChange(updateSettings);
guiNoise.add(guiOptions.noise, 'speed', -2, 2, 0.1).onChange(updateSettings);
guiComposition.open();
guiGradient.open();
guiNoise.open();
window.addEventListener('resize', () => app.setSize(window.innerWidth, window.innerHeight));
updateSettings();
animate(time => app.render(time));
Also see: Tab Triggers