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.
<span class="info">**please <strong>RELOAD</strong> if this glitches</span>
<div class="support">
<a href="https://twitter.com/DevLoop01" target="_blank"><i class="fab fa-twitter-square"></i></a>
<a href="https://dribbble.com/devloop01" target="_blank"><i class="fab fa-dribbble"></i></a>
</div>
@import url('https://fonts.googleapis.com/css2?family=Montserrat&display=swap');
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
width: 100%;
height: 100vh;
overflow: hidden;
}
.info{
position: absolute;
bottom: 15px;
left: 50%;
transform: translateX(-50%);
color: #fff;
font-family: 'Montserrat';
font-size: 0.7rem;
z-index: 100;
opacity: 0.65;
}
.support{
position: absolute;
right: 10px;
bottom: 10px;
padding: 10px;
display: flex;
a{
margin: 0 10px;
color: #fff;
font-size: 1.8rem;
backface-visibility: hidden;
transition: all 150ms ease;
&:hover{
transform: scale(1.1);
}
}
}
console.clear()
// ***** PLEASE RELOAD THE PAGE IF THIS GLITCHES *****
// First Things First. This animation is not by me. This is just an implementation. Animation is originally created by Mat Voyce
// Animation Link 👉 [ https://www.instagram.com/p/CC34wWEB0sC/ ]
// This took a lot of time as it involves shaders, obviously I have not written the shader code from scratch 😅 (I'm a noob in shaders)
// Here on Codepen you might find it hard to understand the code & how I linked dependencies,I recommend you to checking out the Github Repo.
// Here 👉 [ https://github.com/devloop01/twisting-mesh ]
// I took the shader code and most of the implementations from here 👉 [ https://github.com/marioecg/codrops-kinetic-typo ]
// Thanks to Mario Carrillo (@marioecg) to create such an awesome codrops artcle/demo 👉 [ https://tympanus.net/codrops/2020/06/02/kinetic-typography-with-three-js/ ]
// Definetly give him a follow if you are not doing it already
// Enough Talk Let the code do the magic. ENJOY
// ******** SHADER CODE ************* //
const vertexShader = `
varying vec2 vUv;
uniform float twistAmount;
mat4 rotation3d(vec3 axis, float angle) {
axis = normalize(axis);
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;
return mat4(
oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0,
0.0, 0.0, 0.0, 1.0
);
}
vec3 rotate(vec3 v, vec3 axis, float angle) {
return (rotation3d(axis, angle) * vec4(v, 1.0)).xyz;
}
void main() {
vUv = uv;
vec3 pos = position;
vec3 axis = vec3(1., 0., 0.);
float twist = -twistAmount;
float angle = pos.x * twist;
vec3 transformed = rotate(pos, axis, angle);
gl_Position = projectionMatrix * modelViewMatrix * vec4(transformed, 1.);
}
`;
const fragMentShader = `
varying vec2 vUv;
uniform sampler2D uTexture;
void main() {
vec2 uv = fract(vUv * 1. - vec2(0.05, 0.));
vec3 texture = texture2D(uTexture, uv).rgb;
gl_FragColor = vec4(texture, 1.);
}
`;
// ******** SCENE CODE ************* //
class GL {
constructor() {
this.width = window.innerWidth;
this.height = window.innerHeight;
this.createScene();
this.createCamera();
this.init();
}
createScene() {
this.renderer = new THREE.WebGLRenderer({
alpha: true,
antialias: true,
});
this.renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.5));
this.renderer.setSize(this.width, this.height);
this.renderer.setClearColor(0x001845, 1);
this.scene = new THREE.Scene();
}
createCamera() {
this.camera = new THREE.PerspectiveCamera(
45,
this.width / this.height,
1,
1000
);
this.camera.position.z = 5;
this.camera.lookAt(new THREE.Vector3());
}
render() {
this.renderer.render(this.scene, this.camera);
}
animate() {
requestAnimationFrame(this.animate.bind(this));
this.render();
}
addEvents() {
window.addEventListener("resize", this.resize.bind(this));
}
init() {
this.addToDom();
this.animate();
this.addEvents();
}
addToDom() {
const canvas = this.renderer.domElement;
document.body.appendChild(canvas);
}
resize() {
this.width = window.innerWidth;
this.height = window.innerHeight;
this.camera.aspect = this.width / this.height;
this.camera.updateProjectionMatrix();
this.renderer.setSize(this.width, this.height);
}
}
// INIT THE SCENE
const _GL = new GL();
// ******** THE TYPE CODE (ohh my God GSAP is sooo fun) ************* //
class Type extends THREE.Object3D {
init(options) {
this.opts = {
word: options.word,
color: options.color,
fill: options.fill,
wordPosition: options.position.texture,
wordScale: options.scale,
position: options.position.mesh,
rotation: options.rotation || [0, 0, 0],
geometry: options.geometry,
vertex: options.shaders.vertex,
fragment: options.shaders.fragment,
fontFile: options.font.file,
fontAtlas: options.font.atlas,
};
// Create geometry of packed glyphs
loadBmfont(options.font.file, (err, font) => {
this.fontGeometry = threeBmfontText({
font,
text: this.opts.word,
});
// Load texture containing font glyps
this.loader = new THREE.TextureLoader();
this.loader.crossOrigin = 'Anonymous';
this.loader.load(this.opts.fontAtlas, (texture) => {
this.fontMaterial = new THREE.RawShaderMaterial(
createMSDFShader ({
map: texture,
side: THREE.DoubleSide,
transparent: true,
negate: false,
color: this.opts.color,
})
);
this.createRenderTarget();
this.createMesh();
this.twistAndFlip();
});
});
}
createRenderTarget() {
this.rt = new THREE.WebGLRenderTarget(
window.innerWidth,
window.innerHeight
);
this.rtCamera = new THREE.PerspectiveCamera(45, 1, 0.1, 1000);
this.rtCamera.position.z = 2.3;
this.rtScene = new THREE.Scene();
this.rtScene.background = new THREE.Color(this.opts.fill);
this.text = new THREE.Mesh(this.fontGeometry, this.fontMaterial);
this.text.position.set(...this.opts.wordPosition);
this.text.rotation.set(Math.PI, 0, 0);
this.text.scale.set(...this.opts.wordScale);
this.rtScene.add(this.text);
}
createMesh() {
this.geometry = this.opts.geometry;
this.material = new THREE.ShaderMaterial({
vertexShader: this.opts.vertex,
fragmentShader: this.opts.fragment,
uniforms: {
twistAmount: { value: 0 },
uTexture: { value: this.rt.texture },
},
defines: {
PI: Math.PI,
},
side: THREE.DoubleSide,
});
this.mesh = new THREE.Mesh(this.geometry, this.material);
this.mesh.position.set(...this.opts.position);
this.mesh.rotation.set(...this.opts.rotation);
this.mesh.lookAt(new THREE.Vector3());
this.mesh.onBeforeRender = (renderer) => {
renderer.setRenderTarget(this.rt);
renderer.render(this.rtScene, this.rtCamera);
renderer.setRenderTarget(null);
};
this.add(this.mesh);
_GL.scene.add(this);
}
twistAndFlip() {
const twistAmount = 1;
const rotationAngleFactor = 58;
const tlDefaults = {
duration: 0.5,
ease: "elastic.out(0.8, 1.5)",
};
const tl = gsap.timeline({
delay: 1,
repeat: -1,
repeatDelay: 0.5,
defaults: tlDefaults,
});
tl.addLabel("start", 0);
tl.addLabel("startEnd", tlDefaults.duration);
tl.addLabel("endStart", tlDefaults.duration * 2 + 0.5);
tl.addLabel("end", tlDefaults.duration * 3 + 0.5);
tl.to(
this.material.uniforms.twistAmount,
{
value: twistAmount,
},
"start"
)
.to(
this.mesh.rotation,
{
x: THREE.MathUtils.degToRad(
twistAmount * rotationAngleFactor
),
},
"start"
)
.to(
this.material.uniforms.twistAmount,
{
value: 0,
},
"startEnd"
)
.to(
this.mesh.rotation,
{
x: this.mesh.rotation.x - THREE.MathUtils.degToRad(90),
},
"startEnd"
)
.to(
this.material.uniforms.twistAmount,
{
value: twistAmount,
},
"endStart"
)
.to(
this.mesh.rotation,
{
x:
this.mesh.rotation.x -
THREE.MathUtils.degToRad(
twistAmount * rotationAngleFactor * 0.55
),
},
"endStart"
)
.set(
this.mesh.rotation,
{
z: Math.PI,
},
"end"
)
.to(
this.material.uniforms.twistAmount,
{
value: 0,
},
"end"
)
.to(
this.mesh.rotation,
{
x: this.mesh.rotation.x - THREE.MathUtils.degToRad(180),
},
"end"
);
}
}
// ******** INITIALIZE THE "TYPE" INSTANCES ************* //
const OPTION_DEFAULTS = {
position: {
texture: [-0.945, -0.5, 0],
mesh: [0, 0, 0],
},
scale: [0.012, 0.04, 1],
shaders: {
vertex: vertexShader,
fragment: fragMentShader,
},
font: {
file: 'https://raw.githubusercontent.com/devloop01/twisting-mesh/gh-pages/fonts/ArchivoBlack-Regular.fnt',
atlas: 'https://raw.githubusercontent.com/devloop01/twisting-mesh/gh-pages/fonts/ArchivoBlack-Regular.png',
},
};
const options = [
{
word: "HOME",
color: "#ffffff",
fill: "#0466c8",
geometry: new THREE.BoxGeometry(2, 1, 1, 64, 64, 64),
...OPTION_DEFAULTS,
},
{
word: "WORK",
color: "#0466c8",
fill: "#ffffff",
geometry: new THREE.BoxGeometry(1.9995, 1.0015, 0.9995, 64, 64, 64),
...OPTION_DEFAULTS,
},
];
options.forEach((option) => {
let type = new Type();
type.init(option);
});
Also see: Tab Triggers