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.
<div id="canvas"></div>
<div id="content">
<h1>
<span class="text-plane">Martin Laxenaire</span>
</h1>
<h2>
<span class="text-plane">
Rendering text<br />
to WebGL
</span>
</h2>
<div id="process" class="text-block">
<p>
<span class="text-plane">
This is an example of how we can render whole blocks of text to WebGL thanks to curtains.js and the TextTexture class.
</span>
</p>
<p>
<span class="text-plane">
A WebGL plane is created for all elements that have a "text-plane" class and their text contents are drawn inside a 2D canvas, which is then used as a WebGL texture.
</span>
</p>
</div>
<div id="scroll" class="text-block">
<p>
<span class="text-plane">
We're using an additional shader pass to add a cool effect on scroll that makes you feel like the content is actually dragged.
</span>
</p>
<p>
<span class="text-plane">
Try to scroll down to see what happens!
</span>
</p>
</div>
<div id="lipsum" class="text-block">
<p>
<span class="text-plane">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a dolor posuere nisi tempus rhoncus. Curabitur venenatis velit a tellus porttitor, sed efficitur ipsum volutpat. Nunc ante ante, convallis in commodo eget, semper ac ex. Fusce lobortis risus vel nisl interdum imperdiet. Nulla facilisi
</span>
</p>
<p>
<span class="text-plane">
Cras hendrerit iaculis est at vestibulum. Integer tincidunt mi id metus mollis, in fermentum odio sagittis. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Phasellus in efficitur diam.
</span>
</p>
</div>
</div>
body {
margin: 0;
width: 100%;
min-height: 100vh;
font-family: 'Merriweather Sans', sans-serif;
line-height: 1.2;
color: #0505AF;
font-size: 2vw;
}
#canvas {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
z-index: 1;
}
.text-plane {
display: inline-block;
/* apply negative margins and padding to avoid chars from being cropped */
margin: -1em;
padding: 1em;
/* hide the original text content */
opacity: 0;
}
#content {
width: 100%;
padding: 2.5vw;
box-sizing: border-box;
position: relative;
z-index: 2;
}
#content h1 {
text-align: center;
margin: 25vh 0;
font-size: 6vw;
text-transform: uppercase;
font-family: 'Archivo Black', sans-serif;
font-weight: 400;
line-height: 1;
}
#content h2 {
width: 50%;
margin: 15vh 0 15vh 50%;
font-size: 4vw;
text-transform: uppercase;
font-family: 'Archivo Black', sans-serif;
font-weight: 400;
line-height: 1;
}
.text-block {
width: 50%;
margin: 15vh 0;
}
#scroll {
margin-left: 25%;
}
#lipsum {
margin-left: 50%;
}
import {Curtains, Plane, RenderTarget, ShaderPass} from 'https://cdn.jsdelivr.net/npm/curtainsjs@8.1.2/src/index.mjs';
import {TextTexture} from 'https://gistcdn.githack.com/martinlaxenaire/549b3b01ff4bd9d29ce957edd8b56f16/raw/2f111abf99c8dc63499e894af080c198755d1b7a/TextTexture.js';
const scrollFs = `
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif
varying vec3 vVertexPosition;
varying vec2 vTextureCoord;
uniform sampler2D uRenderTexture;
// lerped scroll deltas
// negative when scrolling down, positive when scrolling up
uniform float uScrollEffect;
// default to 2.5
uniform float uScrollStrength;
void main() {
vec2 scrollTextCoords = vTextureCoord;
float horizontalStretch;
// branching on an uniform is ok
if(uScrollEffect >= 0.0) {
scrollTextCoords.y *= 1.0 + -uScrollEffect * 0.00625 * uScrollStrength;
horizontalStretch = sin(scrollTextCoords.y);
}
else if(uScrollEffect < 0.0) {
scrollTextCoords.y += (scrollTextCoords.y - 1.0) * uScrollEffect * 0.00625 * uScrollStrength;
horizontalStretch = sin(-1.0 * (1.0 - scrollTextCoords.y));
}
scrollTextCoords.x = scrollTextCoords.x * 2.0 - 1.0;
scrollTextCoords.x *= 1.0 + uScrollEffect * 0.0035 * horizontalStretch * uScrollStrength;
scrollTextCoords.x = (scrollTextCoords.x + 1.0) * 0.5;
gl_FragColor = texture2D(uRenderTexture, scrollTextCoords);
}
`;
const vs = `
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif
// default mandatory variables
attribute vec3 aVertexPosition;
attribute vec2 aTextureCoord;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
// custom variables
varying vec3 vVertexPosition;
varying vec2 vTextureCoord;
void main() {
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
// varyings
vVertexPosition = aVertexPosition;
vTextureCoord = aTextureCoord;
}
`;
const fs = `
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif
varying vec3 vVertexPosition;
varying vec2 vTextureCoord;
uniform sampler2D uTexture;
void main( void ) {
gl_FragColor = texture2D(uTexture, vTextureCoord);
}
`;
window.addEventListener('load', () => {
// create curtains instance
const curtains = new Curtains({
container: "canvas",
pixelRatio: Math.min(1.5, window.devicePixelRatio)
});
// track scroll values
const scroll = {
value: 0,
lastValue: 0,
effect: 0,
};
// on success
curtains.onSuccess(() => {
const fonts = {
list: [
'normal 400 1em "Archivo Black", sans-serif',
'normal 300 1em "Merriweather Sans", sans-serif',
],
loaded: 0
};
// load the fonts first
fonts.list.forEach(font => {
document.fonts.load(font).then(() => {
fonts.loaded++;
if(fonts.loaded === fonts.list.length) {
// create our shader pass
const scrollPass = new ShaderPass(curtains, {
fragmentShader: scrollFs,
depth: false,
uniforms: {
scrollEffect: {
name: "uScrollEffect",
type: "1f",
value: scroll.effect,
},
scrollStrength: {
name: "uScrollStrength",
type: "1f",
value: 2.5,
},
}
});
// calculate the lerped scroll effect
scrollPass.onRender(() => {
scroll.lastValue = scroll.value;
scroll.value = curtains.getScrollValues().y;
// clamp delta
scroll.delta = Math.max(-30, Math.min(30, scroll.lastValue - scroll.value));
scroll.effect = curtains.lerp(scroll.effect, scroll.delta, 0.05);
scrollPass.uniforms.scrollEffect.value = scroll.effect;
});
// create our text planes
const textEls = document.querySelectorAll('.text-plane');
textEls.forEach(textEl => {
const textPlane = new Plane(curtains, textEl, {
vertexShader: vs,
fragmentShader: fs
});
// create the text texture and... that's it!
const textTexture = new TextTexture({
plane: textPlane,
textElement: textPlane.htmlElement,
sampler: "uTexture",
resolution: 1.5,
skipFontLoading: true, // we've already loaded the fonts
});
});
}
})
})
});
});
Also see: Tab Triggers