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.
<main>
<div class="frame">
<h3 class="frame__logo">GL.</h3>
<button class="frame__button">Menu</button>
<p class="frame__artist">
<span>Artist — <a href="https://www.instagram.com/adxmboni/" target="_blank">Adam Rosol</a></span>
</p>
<p class="frame__credits">
<span>Wave effect inspired from — <a href="https://www.nightingale.world/" target="_blank">nightingale.world</a></span>
</p>
</div>
<div class="slider">
<div class="slider__image--wrapper">
<img class="slider__image slider__image--1" src="https://raw.githubusercontent.com/devloop01/webgl-slider/main/src/img/retro.jpg" alt="image 01" />
<img class="slider__image slider__image--2" src="https://raw.githubusercontent.com/devloop01/webgl-slider/main/src/img/modern.jpg" alt="image 02" />
<img class="slider__image slider__image--3" src="https://raw.githubusercontent.com/devloop01/webgl-slider/main/src/img/metal.jpg" alt="image 03" />
<img class="slider__image slider__image--4" src="https://raw.githubusercontent.com/devloop01/webgl-slider/main/src/img/emotion.jpg" alt="image 04" />
</div>
<div class="slider__slide-info--wrapper">
<div class="slider__silde-info slide--current">
<h2 class="slide__index" data-splitting>01</h2>
<div class="slide__text--wrapper">
<h2 class="slide__text slide__text--title" data-splitting>RETRO</h2>
<p class="slide__text slide__text--description">
Your assumptions are your windows on the world. Scrub them off every once in a while, or the light won't
come in.
</p>
</div>
</div>
<div class="slider__silde-info">
<h2 class="slide__index" data-splitting>02</h2>
<div class="slide__text--wrapper">
<h2 class="slide__text slide__text--title" data-splitting>MODERN</h2>
<p class="slide__text slide__text--description">
In this modern world it is easy to be complex but difficult to be simple. At least try to keep your mind
simple.
</p>
</div>
</div>
<div class="slider__silde-info">
<h2 class="slide__index" data-splitting>03</h2>
<div class="slide__text--wrapper">
<h2 class="slide__text slide__text--title" data-splitting>METAL</h2>
<p class="slide__text slide__text--description">
You simply have to turn your back on a culture that has gone sterile and dead and get with the program of a
living world and the imagination.
</p>
</div>
</div>
<div class="slider__silde-info">
<h2 class="slide__index" data-splitting>04</h2>
<div class="slide__text--wrapper">
<h2 class="slide__text slide__text--title" data-splitting>EMOTION</h2>
<p class="slide__text slide__text--description">
Every man has his secret sorrows which the world knows not; and often times we call a man cold when he is
only sad.
</p>
</div>
</div>
</div>
<nav class="slider__nav--wrapper">
<button class="slider__nav slider__nav--prev">
<svg>
<path d="M 47.5547,8.05465 H 4.94603 L 10.0165,3.00878 C 10.5822,2.44568 10.5844,1.53058 10.0213,0.964816 9.45823,0.398976 8.54305,0.396881 7.97729,0.959902 L 0.425238,8.47552 c -5.06e-4,4.4e-4 -8.67e-4,9.4e-4 -0.0013,0.00138 -0.564323,0.56309 -0.566129,1.48115 -1.45e-4,2.0461 5.06e-4,5e-4 8.67e-4,10e-4 0.001301,0.0014 L 7.97714,18.04 c 0.5657,0.563 1.48087,0.561 2.04406,-0.0049 0.5631,-0.5658 0.5609,-1.4809 -0.0049,-2.044 L 4.94603,10.9453 H 47.5547 C 48.353,10.9453 49,10.2982 49,9.49996 49,8.70172 48.353,8.05465 47.5547,8.05465 Z" fill="white" />
</svg>
</button>
<button class="slider__nav slider__nav--next">
<svg>
<path d="M 1.44529,10.9454 H 44.054 l -5.0705,5.0458 c -0.5657,0.5631 -0.5679,1.4782 -0.0048,2.044 0.5631,0.5658 1.4782,0.5679 2.044,0.0049 l 7.5521,-7.5156 c 5e-4,-5e-4 8e-4,-10e-4 0.0013,-0.0014 0.5643,-0.56309 0.5661,-1.48115 1e-4,-2.04613 -5e-4,-4.3e-4 -9e-4,-9.3e-4 -0.0013,-0.00137 L 41.0229,0.959983 C 40.4572,0.397033 39.542,0.398984 38.9788,0.964896 38.4157,1.53066 38.4179,2.44576 38.9837,3.00886 L 44.054,8.05473 H 1.44528 C 0.64704,8.05473 -2.75561e-5,8.7018 -2.74863e-5,9.50004 -2.74165e-5,10.2983 0.647041,10.9454 1.44529,10.9454 Z" fill="white" />
</svg>
</button>
</nav>
</div>
</main>
<svg class="cursor cursor--large" width="60" height="60" viewBox="0 0 60 60">
<circle class="cursor__inner" cx="30" cy="30" r="20" />
</svg>
<svg class="cursor cursor--small" width="60" height="60" viewBox="0 0 60 60">
<circle class="cursor__inner" cx="30" cy="30" r="5" />
</svg>
<svg class="cursor cursor--close" width="40" height="40" viewBox="0 0 512 512">
<line x1="368" y1="368" x2="144" y2="144" />
<line x1="368" y1="144" x2="144" y2="368" />
</svg>
<!-- this effect is taken from -- https://codepen.io/sandstedt/pen/GRZeywP?editors=0100 -->
<div class="loading__wrapper">
<h4 class="loading__text">
<span class="loading__text--inner">Loading...</span>
</h4>
<h1 class="text__wrapper">
<span class="text__row">
<span class="text">WELCOME</span>
</span>
<span class="text__row text__row--sibling" aria-hidden="true">
<span class="text">WELCOME</span>
</span>
<span class="text__row text__row--sibling" aria-hidden="true">
<span class="text">WELCOME</span>
</span>
<span class="text__row text__row--sibling" aria-hidden="true">
<span class="text">WELCOME</span>
</span>
<span class="text__row text__row--sibling" aria-hidden="true">
<span class="text">WELCOME</span>
</span>
<span class="text__row text__row--sibling" aria-hidden="true">
<span class="text">WELCOME</span>
</span>
</h1>
<div class="bg__transition--slide"></div>
</div>
<!-- //////// SHADER CODE //////// -->
<script id="vertexShader" type="x-shader/x-vertex">
precision mediump float;
varying vec2 vUv;
varying float vWave;
uniform float uTime;
uniform float uAmplitude;
uniform float uProgDirection;
uniform float uMouseOverAmp;
uniform float uRadius;
uniform vec2 uMeshSize;
uniform vec2 uMousePos;
uniform bool uAnimating;
uniform bool uTranslating;
float mapVal(in float n,in float start1,in float stop1,in float start2,in float stop2){
return((n-start1)/(stop1-start1))*(stop2-start2)+start2;
}
void main(){
vec3 pos=position;
vUv=uv;
vec2 center=vUv-uMousePos;
center.x*=uMeshSize.x/uMeshSize.y;
float dist=length(center);
float radius=uRadius;
float mask=smoothstep(radius,radius*5.,dist);
float d=mapVal(mask,-1.,1.,-1.,0.);
if(uAnimating){
pos.z=sin(pos.x*5.+uTime*10.*uProgDirection)*uAmplitude;
pos.z*=2.5;
}else{
pos.z=d*uMouseOverAmp;
pos.z*=15.;
}
if(uTranslating){
pos.z=sin(pos.y*6.+uTime*10.)*uAmplitude;
pos.z*=3.5;
}
vWave=pos.z;
gl_Position=projectionMatrix*modelViewMatrix*vec4(pos,1.);
}
</script>
<script id="fragmentShader" type="x-shader/x-fragment">
precision mediump float;
varying vec2 vUv;
varying float vWave;
uniform float uTime;
uniform float uProg;
uniform float uProgDirection;
uniform sampler2D uCurrTex;
uniform sampler2D uNextTex;
uniform vec2 uMeshSize;
uniform vec2 uImageSize;
vec2 backgroundCoverUv(vec2 uv,vec2 canvasSize,vec2 textureSize){
vec2 ratio=vec2(
min((canvasSize.x/canvasSize.y)/(textureSize.x/textureSize.y),1.),
min((canvasSize.y/canvasSize.x)/(textureSize.y/textureSize.x),1.)
);
vec2 uvWithRatio=uv*ratio;
return vec2(
uvWithRatio.x+(1.-ratio.x)*.5,
uvWithRatio.y+(1.-ratio.y)*.5
);
}
void main(){
vec2 texUv=backgroundCoverUv(vUv,uMeshSize,uImageSize);
float x=uProg;
float y;
if(uProgDirection==1.)y=(x*2.+(vUv.x-1.));
else y=((x*2.)-vUv.x);
x=smoothstep(0.,1.,y);
float w=vWave;
float r1=texture2D(uCurrTex,texUv+w*.04).r;
float g1=texture2D(uCurrTex,texUv+w*.01).g;
float b1=texture2D(uCurrTex,texUv+w*-.03).b;
vec3 tex1=vec3(r1,g1,b1);
float r2=texture2D(uNextTex,texUv+w*.04).r;
float g2=texture2D(uNextTex,texUv+w*.01).g;
float b2=texture2D(uNextTex,texUv+w*-.03).b;
vec3 tex2=vec3(r2,g2,b2);
float scaleUp=(.4+.6*(1.-uProg));
float scaleDown=(.6+.4*uProg);
vec4 f1=mix(
texture2D(uCurrTex,texUv*(1.-x)*scaleUp+vec2(.15)*uProg),
texture2D(uNextTex,texUv*x*scaleDown),
x);
vec3 f2=mix(tex1,tex2,x);
vec4 final=mix(f1,vec4(f2,1.),.12);
gl_FragColor=final;
}
</script>
// fonts
@import url("https://fonts.googleapis.com/css2?family=Red+Rose:wght@300;400;700&display=swap");
@import url("https://fonts.googleapis.com/css2?family=Lato:wght@300&display=swap");
:root {
--cursor-stroke: #5631e9;
--cursor-fill: transparent;
--cursor-stroke-width: 1px;
--base-image-width: 450px;
--base-image-height: 600px;
--image-width: 450px;
--image-height: 600px;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
font-size: 16px;
}
body {
background: #1f1322;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
main {
width: 100%;
height: 100%;
}
button {
outline: none;
border: none;
background: none;
color: inherit;
font-family: inherit;
}
a {
text-decoration: none;
color: inherit;
}
.slider {
width: 100%;
height: 100%;
position: relative;
overflow: hidden;
}
.slider__image--wrapper {
position: absolute;
left: 50%;
top: 50%;
width: var(--image-width);
height: var(--image-height);
transform: translate(-50%, -50%);
overflow: hidden;
z-index: 1;
user-select: none;
.slider__image {
position: absolute;
left: 50%;
transform: translateX(-50%);
top: 0;
height: 100%;
display: none;
}
}
.slider__slide-info--wrapper {
pointer-events: none;
position: absolute;
left: 50%;
top: 50%;
width: var(--image-width);
height: var(--image-height);
transform: translate(-50%, -50%);
z-index: 2;
.slider__silde-info {
color: #fff;
pointer-events: auto;
.slide__text--wrapper {
position: absolute;
bottom: 0;
left: 105%;
}
.slide__index,
.slide__text--title {
font-family: "Red Rose";
font-weight: 700;
}
.slide__index {
position: absolute;
left: 0;
top: 0;
transform: translate(-60%, -20%);
display: inline-block;
font-size: 14rem;
overflow: hidden;
line-height: 80%;
&--text {
display: block;
}
}
.slide__text--title {
font-size: 7rem;
transform: translateX(-35%);
overflow: hidden;
line-height: 80%;
color: transparent;
-webkit-text-stroke: 2px #fff;
.word {
display: flex;
}
}
.slide__text--description {
width: 350px;
font-family: "Lato";
font-size: 1rem;
line-height: 140%;
.line {
overflow: hidden;
}
}
.slide__index,
.slide__text--title,
.slide__text--description {
pointer-events: none;
}
&:not(.slide--current) {
.slide__index,
.slide__text--title,
.slide__text--description {
pointer-events: none;
opacity: 0;
}
}
}
}
.slider__nav {
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 50px;
height: 20px;
cursor: pointer;
z-index: 12;
svg {
display: block;
width: 100%;
height: 100%;
transition: transform 250ms ease;
transform: translateX(var(--tr-x, 0px));
}
&--prev {
left: 25vw;
&:hover {
svg {
--tr-x: -10px;
}
&:active svg {
--tr-x: -5px;
}
}
}
&--next {
right: 25vw;
&:hover {
svg {
--tr-x: 10px;
}
&:active svg {
--tr-x: 5px;
}
}
}
}
.dom-gl {
position: fixed;
z-index: -1;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.cursor {
display: none;
}
.frame {
position: fixed;
width: 100%;
height: 100%;
padding: 2rem 3rem;
color: #fff;
font-family: "Red Rose";
font-weight: 300;
z-index: 50;
display: grid;
justify-content: space-between;
align-content: space-between;
grid-template-columns: repeat(3, 1fr);
grid-template-areas:
"logo .. btn"
"artist ... credits";
pointer-events: none;
> * {
pointer-events: auto;
}
&__logo {
grid-area: logo;
justify-self: left;
}
&__button {
grid-area: btn;
justify-self: right;
}
&__artist {
grid-area: artist;
justify-self: left;
}
&__credits {
grid-area: credits;
justify-self: right;
}
&__credits,
&__artist {
font-size: 0.8rem;
overflow: hidden;
span {
display: inline-block;
}
a {
font-weight: 400;
opacity: 0.8;
&:hover {
opacity: 1;
}
}
}
}
.loading__wrapper {
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100vh;
z-index: 1000;
display: flex;
justify-content: center;
align-items: center;
.loading__text {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 2vmin;
font-family: "Red Rose", sans-serif;
color: #fff;
&--inner {
animation: blink 1s infinite alternate-reverse;
}
@keyframes blink {
from {
opacity: 1;
}
to {
opacity: 0.6;
}
}
}
.text__wrapper {
position: relative;
font-size: 10vmin;
font-family: "Red Rose", sans-serif;
text-transform: uppercase;
font-display: swap;
color: #fff;
.text__row {
display: block;
.text {
display: block;
user-select: none;
opacity: 0;
}
&:nth-child(1) {
clip-path: polygon(0% 75%, 100% 75%, 100% 100%, 0% 100%);
}
&:nth-child(2) {
clip-path: polygon(0% 50%, 100% 50%, 100% 75.5%, 0% 75.5%);
}
&:nth-child(3) {
clip-path: polygon(0% 25%, 100% 25%, 100% 50.5%, 0% 50.5%);
}
&:nth-child(4) {
clip-path: polygon(0% 0%, 100% 0%, 100% 25.5%, 0% 25.5%);
}
&:nth-child(5) {
clip-path: polygon(0% -25%, 100% -25%, 100% 0.5%, 0% 0.5%);
}
&:nth-child(6) {
clip-path: polygon(0% -50%, 100% -50%, 100% -24.5%, 0% -24.5%);
}
&.text__row--sibling {
position: absolute;
top: 0;
left: 0;
user-select: none;
}
}
}
.bg__transition--slide {
background: #0e0e0e;
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
z-index: -1;
}
}
@media (any-pointer: fine) {
.cursor {
position: fixed;
top: 0;
left: 0;
display: block;
pointer-events: none;
mix-blend-mode: difference;
z-index: 100;
&--large .cursor__inner {
fill: var(--cursor-fill);
stroke: var(--cursor-stroke);
stroke-width: var(--cursor-stroke-width);
opacity: 0.7;
}
&--small .cursor__inner {
fill: var(--cursor-stroke);
stroke: var(--cursor-fill);
opacity: 0.7;
}
&--close {
fill: none;
stroke: var(--cursor-stroke);
stroke-linecap: round;
stroke-linejoin: round;
stroke-width: 32px;
mix-blend-mode: difference;
}
}
}
// some dumb media queries - IDK not the best css I have written here
@media only screen and (max-width: 64em) {
.slider__slide-info--wrapper {
.slider__silde-info {
.slide__text--title {
transform: translateX(-80%);
}
.slide__text--description {
opacity: 0;
height: 0;
}
}
}
.frame {
&__credits {
display: none;
}
}
}
@media only screen and (max-width: 53em) {
:root {
--image-width: calc(var(--base-image-width) / 1.125);
--image-height: calc(var(--base-image-height) / 1.125);
}
.slider__slide-info--wrapper {
.slider__silde-info {
.slide__index {
transform: translate(-60%, 0%);
font-size: 10rem;
}
}
}
}
@media only screen and (max-width: 32em) {
:root {
--image-width: calc(var(--base-image-width) / 1.5);
--image-height: calc(var(--base-image-height) / 1.5);
}
.slider__slide-info--wrapper {
.slider__silde-info {
.slide__index {
font-size: 7rem;
}
.slide__text--title {
transform: translateX(-60%);
font-size: 5rem;
}
}
}
.frame {
&__artist {
display: none;
}
}
}
// After experimenting with shaders it was time to create something interactive.
// So I came up with this. It is built using THREEJS & Shaders.
// The cool wavy effect is inspired from -> www.nightingale.world/
// Github Repo --> https://github.com/devloop01/webgl-slider
// Full page version --> https://web-gl-slider.netlify.app/
// I hope you enjoy the experience :)
console.clear();
let nMouse = new THREE.Vector2();
window.addEventListener("mousemove", (event) => {
event.preventDefault();
nMouse.x = (event.clientX / window.innerWidth) * 2 - 1;
nMouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
});
let mouseOver = false,
mouseDown = false;
const vertexShader = document.getElementById("vertexShader").innerHTML;
const fragmentShader = document.getElementById("fragmentShader").innerHTML;
const planeGeometry = new THREE.PlaneBufferGeometry(1, 1, 32, 32);
const planeMaterial = new THREE.ShaderMaterial({
vertexShader,
fragmentShader
});
// -------- UTILITY FUNCTIONS & CLASSES [START] --------
const lerp = (a, b, n) => (1 - n) * a + n * b;
const getMousePos = (e) => {
let posx = 0;
let posy = 0;
if (!e) e = window.event;
if (e.pageX || e.pageY) {
posx = e.pageX;
posy = e.pageY;
} else if (e.clientX || e.clientY) {
posx = e.clientX + body.scrollLeft + document.documentElement.scrollLeft;
posy = e.clientY + body.scrollTop + document.documentElement.scrollTop;
}
return { x: posx, y: posy };
};
function preloadImages(selector) {
return new Promise((resolve, reject) => {
imagesLoaded(selector, { background: true }, resolve);
});
}
class Mouse {
constructor() {
this.position = {
x: 0,
y: 0
};
this.isMoving = false;
this.mouseEvent = {
previous: null,
current: null
};
this.initEvents();
this.updateMovingState();
}
initEvents() {
window.addEventListener("mousemove", (ev) => {
this.mouseEvent.current = ev;
this.position = getMousePos(ev);
});
}
updateMovingState() {
setInterval(() => {
if (this.mouseEvent.previous && this.mouseEvent.current) {
const moveX = Math.abs(
this.mouseEvent.current.screenX - this.mouseEvent.previous.screenX
);
const moveY = Math.abs(
this.mouseEvent.current.screenY - this.mouseEvent.previous.screenY
);
const movement = Math.sqrt(moveX * moveX + moveY * moveY);
if (movement == 0) this.isMoving = false;
else this.isMoving = true;
}
this.mouseEvent.previous = this.mouseEvent.current;
}, 100);
}
}
class Splitter {
constructor(el) {
this.DOM = { el };
this.DOMElComputedStyles = getComputedStyle(this.DOM.el);
this.init();
}
init() {
const lines = this.split();
this.clearElement();
this.insertLines(lines);
}
split() {
const maxwidth = this.DOM.el.getBoundingClientRect().width;
const textContent = this.DOM.el.innerText;
const words = textContent.split(" ");
const lines = [];
let curline = [];
const fontWeight = this.DOMElComputedStyles["font-weight"];
const fontSize = this.DOMElComputedStyles["font-size"];
const fontFamily = this.DOMElComputedStyles["font-family"];
const canvasEl = document.createElement("canvas");
const ghost =
"OffscreenCanvas" in window
? canvasEl.transferControlToOffscreen()
: canvasEl;
const context = ghost.getContext("2d");
context.font = `${fontWeight} ${fontSize} ${fontFamily}`;
for (let i = 0; i < words.length; i++) {
curline.push(words[i]);
if (context.measureText(curline.join(" ")).width >= maxwidth) {
const cache = curline.pop();
lines.push(curline.join(" "));
curline = [cache];
}
}
lines.push(curline.join(" "));
return lines;
}
insertLines(lines) {
this.linesEl = document.createElement("span");
this.linesEl.className = "lines";
this.linesEl.style.display = "block";
lines.forEach((line) => {
const lineEl = document.createElement("span");
const lineInnerTextEl = document.createElement("span");
lineEl.className = "line";
lineInnerTextEl.className = "line--innertext";
lineEl.style.display = "block";
lineInnerTextEl.style.display = "block";
lineInnerTextEl.innerText = line;
lineEl.appendChild(lineInnerTextEl);
this.linesEl.appendChild(lineEl);
});
this.DOM.el.appendChild(this.linesEl);
}
clearElement() {
this.DOM.el.innerHTML = "";
}
}
// -------- UTILITY FUNCTIONS & CLASSES [END] --------
// -------- GL CLASSES [START] --------
class GL {
constructor() {
this.scene = new THREE.Scene();
this.camera = new THREE.PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
0.1,
100
);
this.camera.position.z = 50;
this.renderer = new THREE.WebGLRenderer({
alpha: true,
antialias: true
});
this.renderer.setPixelRatio(
gsap.utils.clamp(1.5, 1, window.devicePixelRatio)
);
this.renderer.setSize(window.innerWidth, window.innerHeight);
this.renderer.setClearColor(0xf2f2f2, 0);
this.clock = new THREE.Clock();
this.init();
}
init() {
this.addToDom();
this.addEvents();
this.run();
}
addToDom() {
const canvas = this.renderer.domElement;
canvas.classList.add("dom-gl");
document.body.appendChild(canvas);
}
addEvents() {
window.addEventListener("resize", this.resize.bind(this));
requestAnimationFrame(() => this.run());
}
resize() {
this.renderer.setSize(window.innerWidth, window.innerHeight);
this.camera.updateProjectionMatrix();
for (let i = 0; i < this.scene.children.length; i++) {
const plane = this.scene.children[i];
if (plane.resize) plane.resize();
}
}
run() {
let elapsed = this.clock.getElapsedTime();
for (let i = 0; i < this.scene.children.length; i++) {
const plane = this.scene.children[i];
if (plane.updateTime) plane.updateTime(elapsed);
}
this.render();
}
render() {
this.renderer.render(this.scene, this.camera);
requestAnimationFrame(() => this.run());
}
}
const Gl = new GL();
class GlObject extends THREE.Object3D {
init(el) {
this.el = el;
this.resize();
}
resize() {
this.setBounds();
}
setBounds() {
this.rect = this.el.getBoundingClientRect();
this.bounds = {
left: this.rect.left,
top: this.rect.top + window.scrollY,
width: this.rect.width,
height: this.rect.height
};
this.updateSize();
this.updatePosition();
}
updateSize() {
this.camUnit = this.calculateUnitSize(Gl.camera.position.z - this.position.z);
const x = this.bounds.width / window.innerWidth;
const y = this.bounds.height / window.innerHeight;
if (!x || !y) return;
this.scale.x = this.camUnit.width * x;
this.scale.y = this.camUnit.height * y;
}
calculateUnitSize(distance = this.position.z) {
const vFov = (Gl.camera.fov * Math.PI) / 180;
const height = 2 * Math.tan(vFov / 2) * distance;
const width = height * Gl.camera.aspect;
return { width, height };
}
updateY(y = 0) {
const { top, height } = this.bounds;
this.position.y = this.camUnit.height / 2 - this.scale.y / 2;
this.position.y -= ((top - y) / window.innerHeight) * this.camUnit.height;
this.progress = gsap.utils.clamp(
0,
1,
1 - (-y + top + height) / (window.innerHeight + height)
);
}
updateX(x = 0) {
const { left } = this.bounds;
this.position.x = -(this.camUnit.width / 2) + this.scale.x / 2;
this.position.x += ((left + x) / window.innerWidth) * this.camUnit.width;
}
updatePosition(y) {
this.updateY(y);
this.updateX(0);
}
}
class GlSlider extends GlObject {
init(el) {
super.init(el);
this.geometry = planeGeometry;
this.material = planeMaterial.clone();
this.material.uniforms = {
uCurrTex: { value: 0 },
uNextTex: { value: 0 },
uTime: { value: 0 },
uProg: { value: 0 },
uAmplitude: { value: 0 },
uProgDirection: { value: 0 },
uMeshSize: { value: [this.rect.width, this.rect.height] },
uImageSize: { value: [0, 0] },
uMousePos: { value: [0, 0] },
uMouseOverAmp: { value: 0 },
uAnimating: { value: false },
uRadius: { value: 0.08 },
uTranslating: { value: true }
};
this.imageScale = 1;
this.textures = [];
this.raycaster = new THREE.Raycaster();
this.mouse = new THREE.Vector2();
this.mouseLerpAmount = 0.1;
this.state = {
animating: false,
current: 0
};
this.mesh = new THREE.Mesh(this.geometry, this.material);
this.add(this.mesh);
Gl.scene.add(this);
this.loadTextures();
this.addEvents();
}
loadTextures() {
const manager = new THREE.LoadingManager(() => {
this.material.uniforms.uCurrTex.value = this.textures[0];
});
const loader = new THREE.TextureLoader(manager);
const imgs = [...this.el.querySelectorAll("img")];
imgs.forEach((img) => {
loader.load(img.src, (texture) => {
texture.minFilter = THREE.LinearFilter;
texture.generateMipmaps = false;
this.material.uniforms.uImageSize.value = [
img.naturalWidth,
img.naturalHeight
];
this.textures.push(texture);
});
});
}
switchTextures(index, direction) {
if (this.state.animating) return;
gsap
.timeline({
onStart: () => {
this.state.animating = true;
this.material.uniforms.uAnimating.value = true;
this.material.uniforms.uProgDirection.value = direction;
this.material.uniforms.uNextTex.value = this.textures[index];
},
onComplete: () => {
this.state.animating = false;
this.material.uniforms.uAnimating.value = false;
this.material.uniforms.uCurrTex.value = this.textures[index];
this.currentAmp = 0;
}
})
.fromTo(
this.material.uniforms.uProg,
{
value: 0
},
{
value: 1,
duration: 1,
ease: "ease.out"
},
0
)
.fromTo(
this.material.uniforms.uAmplitude,
{
value: 0
},
{
duration: 0.8,
value: 1,
repeat: 1,
yoyo: true,
yoyoEase: "sine.out",
ease: "expo.out"
},
0
);
}
updateTime(time) {
this.material.uniforms.uTime.value = time;
this.run();
}
addEvents() {
this.el.addEventListener("mouseenter", () => (mouseOver = true));
this.el.addEventListener("mouseleave", () => (mouseOver = false));
this.el.addEventListener("mousedown", () => (mouseDown = true));
this.el.addEventListener("mouseup", () => (mouseDown = false));
}
scaleImage(direction) {
const imageTl = gsap.timeline({
defaults: {
duration: 1.2,
ease: "elastic.out(1, 1)",
onUpdate: () => {
this.resize();
}
}
});
if (direction == "up") {
imageTl.to(this.el, {
scale: window.innerHeight / 600
});
} else if (direction == "down") {
imageTl.to(this.el, {
scale: 1
});
}
}
run() {
let m = mouseOver ? nMouse : new THREE.Vector2(0, 0);
this.mouse.lerp(m, this.mouseLerpAmount);
this.raycaster.setFromCamera(this.mouse, Gl.camera);
let intersects = this.raycaster.intersectObject(this.mesh);
if (intersects.length > 0) {
this.material.uniforms.uMousePos.value = [
intersects[0].uv.x,
intersects[0].uv.y
];
}
if (mouseOver) {
this.material.uniforms.uMouseOverAmp.value = THREE.MathUtils.lerp(
this.material.uniforms.uMouseOverAmp.value,
1,
0.08
);
this.mouseLerpAmount = THREE.MathUtils.lerp(this.mouseLerpAmount, 0.1, 0.5);
} else {
this.material.uniforms.uMouseOverAmp.value = THREE.MathUtils.lerp(
this.material.uniforms.uMouseOverAmp.value,
0,
0.08
);
this.mouseLerpAmount = THREE.MathUtils.lerp(this.mouseLerpAmount, 0, 0.5);
}
if (mouseOver && mouseDown) {
this.material.uniforms.uRadius.value = THREE.MathUtils.lerp(
this.material.uniforms.uRadius.value,
1,
0.01
);
} else if (mouseOver && !mouseDown) {
this.material.uniforms.uRadius.value = THREE.MathUtils.lerp(
this.material.uniforms.uRadius.value,
0.08,
0.08
);
}
if (this.state.animating) {
this.material.uniforms.uMouseOverAmp.value = THREE.MathUtils.lerp(
this.material.uniforms.uMouseOverAmp.value,
0,
0.1
);
}
}
}
// -------- GL CLASSES [END] --------
// -------- MAIN CLASSES [START] --------
let mouse = new Mouse();
class Cursor {
constructor(el) {
this.DOM = { el: el };
this.DOM.el.style.opacity = 0;
this.bounds = this.DOM.el.getBoundingClientRect();
this.renderedStyles = {
tx: { previous: 0, current: 0, amt: 0.2 },
ty: { previous: 0, current: 0, amt: 0.2 },
scale: { previous: 0, current: 1, amt: 0.2 },
opacity: { previous: 0, current: 1, amt: 0.15 }
};
}
init() {
this.onMouseMoveEv = () => {
this.renderedStyles.tx.previous = this.renderedStyles.tx.current =
mouse.position.x - this.bounds.width / 2;
this.renderedStyles.ty.previous = this.renderedStyles.ty.previous =
mouse.position.y - this.bounds.height / 2;
requestAnimationFrame(() => this.render());
window.removeEventListener("mousemove", this.onMouseMoveEv);
};
window.addEventListener("mousemove", this.onMouseMoveEv);
}
setTranslateLerpAmount(amount) {
this.renderedStyles["tx"].amt = amount;
this.renderedStyles["ty"].amt = amount;
return this;
}
scale(amount = 1) {
this.renderedStyles["scale"].current = amount;
return this;
}
opaque(amount = 1) {
this.renderedStyles["opacity"].current = amount;
return this;
}
render() {
this.renderedStyles["tx"].current = mouse.position.x - this.bounds.width / 2;
this.renderedStyles["ty"].current = mouse.position.y - this.bounds.height / 2;
for (const key in this.renderedStyles) {
this.renderedStyles[key].previous = lerp(
this.renderedStyles[key].previous,
this.renderedStyles[key].current,
this.renderedStyles[key].amt
);
}
gsap.set(this.DOM.el, {
translateX: this.renderedStyles["tx"].previous,
translateY: this.renderedStyles["ty"].previous,
scale: this.renderedStyles["scale"].previous,
opacity: this.renderedStyles["opacity"].previous
});
requestAnimationFrame(() => this.render());
}
}
class Cursors {
constructor() {
this.DOM = {};
this.DOM.cursorEls = {
large: document.querySelector(".cursor--large"),
small: document.querySelector(".cursor--small"),
close: document.querySelector(".cursor--close")
};
this.cursors = {
large: new Cursor(this.DOM.cursorEls.large),
small: new Cursor(this.DOM.cursorEls.small),
close: new Cursor(this.DOM.cursorEls.close)
};
this.cursors.small.setTranslateLerpAmount(0.85);
this.cursors.close.opaque(0).scale(0.5).setTranslateLerpAmount(0.5);
}
init() {
Object.values(this.cursors).forEach((cursor) => {
cursor.init();
});
this.initEvents();
}
initEvents() {
this.initEventsOnElements();
this.initEventsOnImage();
}
initEventsOnElements() {
const onMouseEnter = () => {
this.cursors.large.scale(2).opaque(0);
this.cursors.small.scale(5);
};
const onMouseLeave = () => {
this.cursors.large.scale(1).opaque(1);
this.cursors.small.scale(1);
};
const onMouseDown = () => {
this.cursors.small.scale(4);
};
const onMouseUp = () => {
this.cursors.small.scale(5);
};
[
...document.querySelectorAll("a"),
...document.querySelectorAll("button")
].forEach((element) => {
element.addEventListener("mouseenter", onMouseEnter);
element.addEventListener("mouseleave", onMouseLeave);
element.addEventListener("mousedown", onMouseDown);
element.addEventListener("mouseup", onMouseUp);
});
}
initEventsOnImage() {
const imageWrapper = document.querySelector(".slider__image--wrapper");
const onMouseDown = () => {
this.cursors.large.scale(2).opaque(0);
this.cursors.small.scale(5);
};
const onMouseUp = () => {
this.cursors.large.scale(1).opaque(1);
this.cursors.small.scale(1);
};
imageWrapper.addEventListener("mousedown", onMouseDown);
imageWrapper.addEventListener("mouseup", onMouseUp);
}
initEventsOnSlider(slider) {
const imageWrapper = document.querySelector(".slider__image--wrapper");
const onMouseEnter = () => {
this.cursors.large.scale(2).opaque(0);
this.cursors.small.scale(5).setTranslateLerpAmount(0.25);
this.cursors.close.opaque(1).scale(1);
};
const onMouseLeave = () => {
this.cursors.large.scale(1).opaque(1);
this.cursors.small.scale(1).setTranslateLerpAmount(0.85);
this.cursors.close.opaque(0).scale(0.5);
};
slider.onFullscreen(() => {
onMouseEnter();
imageWrapper.addEventListener("mouseenter", onMouseEnter);
imageWrapper.addEventListener("mouseleave", onMouseLeave);
});
slider.offFullscreen(() => {
onMouseLeave();
imageWrapper.removeEventListener("mouseenter", onMouseEnter);
imageWrapper.removeEventListener("mouseleave", onMouseLeave);
});
}
}
class Slideinfo {
constructor(el) {
this.DOM = { el: el };
this.DOM.text = {
index: this.DOM.el.querySelectorAll(".slide__index .char"),
title: this.DOM.el.querySelectorAll(".slide__text--title .char"),
description: this.DOM.el.querySelector(".slide__text--description")
};
const split = new Splitter(this.DOM.text.description);
const lines = [...split.linesEl.children].map((c) => [...c.children][0]);
this.DOM.text.descriptionLines = lines;
}
}
let clicked = false;
class Slideshow {
constructor(el) {
this.DOM = { el };
this.DOM.imageWrapperEl = this.DOM.el.querySelector(
".slider__image--wrapper"
);
this.DOM.navigation = {
prev: this.DOM.el.querySelector(".slider__nav--prev"),
next: this.DOM.el.querySelector(".slider__nav--next")
};
this.slideInfos = [];
[...this.DOM.el.querySelectorAll(".slider__silde-info")].forEach((slide) =>
this.slideInfos.push(new Slideinfo(slide))
);
this.current = 0;
this.slidesTotal = this.slideInfos.length;
this.GlSlider = new GlSlider();
this.GlSlider.init(document.querySelector(".slider__image--wrapper"));
this.initEvents();
}
init() {
const currentSlideInfo = this.slideInfos[this.current];
gsap.set([currentSlideInfo.DOM.text.index, currentSlideInfo.DOM.text.title], {
yPercent: 120,
rotation: -3,
stagger: -0.02
});
gsap.set(currentSlideInfo.DOM.text.descriptionLines, {
yPercent: 100,
stagger: 0.05
});
gsap.set(this.DOM.navigation.prev, {
translateX: 100,
opacity: 0
});
gsap.set(this.DOM.navigation.next, {
translateX: -100,
opacity: 0
});
gsap.set(this.DOM.imageWrapperEl, {
translateY: "150%",
onUpdate: () => {
this.GlSlider.setBounds();
}
});
}
initAnimation() {
const currentSlideInfo = this.slideInfos[this.current];
const tl = gsap
.timeline({
defaults: { duration: 1, ease: "power4.inOut" },
delay: 0.25
})
.addLabel("start", 0)
.addLabel("upcoming", 1.25);
tl
.to(
this.DOM.imageWrapperEl,
{
duration: 1.25,
translateY: 0,
ease: "sine.out",
onUpdate: () => {
this.GlSlider.setBounds();
}
},
"start"
)
.to(
this.GlSlider.material.uniforms.uAmplitude,
{
duration: 1,
value: 1,
repeat: 1,
yoyo: true,
yoyoEase: "sine.out",
ease: "expo.out",
onComplete: () => {
this.GlSlider.material.uniforms.uTranslating = false;
}
},
"start"
)
.to(
[currentSlideInfo.DOM.text.index, currentSlideInfo.DOM.text.title],
{
yPercent: 0,
rotation: 0,
stagger: -0.02
},
"upcoming"
)
.to(
currentSlideInfo.DOM.text.descriptionLines,
{
yPercent: 0,
stagger: 0.05
},
"upcoming"
)
.to(
[this.DOM.navigation.prev, this.DOM.navigation.next],
{
translateX: 0,
opacity: 1
},
"upcoming"
);
}
initEvents() {
this.onClickPrevEv = () => this.navigate("prev");
this.onClickNextEv = () => this.navigate("next");
this.onImageClickEv = () => {
if (this.isAnimating) return;
clicked = !clicked;
const currentSlideInfo = this.slideInfos[this.current];
const tl = gsap
.timeline({
defaults: { duration: 1, ease: "power4.inOut" },
onStart: () => {
this.isAnimating = true;
if (clicked) {
this.GlSlider.scaleImage("up");
if (this.onFullscreenCallbackFn) this.onFullscreenCallbackFn();
} else {
this.GlSlider.scaleImage("down");
if (this.offFullscreenCallbackFn) this.offFullscreenCallbackFn();
}
},
onComplete: () => {
this.isAnimating = false;
}
})
.addLabel("start", clicked ? 0 : 0.2);
tl
.fromTo(
[currentSlideInfo.DOM.text.index, currentSlideInfo.DOM.text.title],
{
yPercent: clicked ? 0 : 120,
rotation: clicked ? 0 : -3
},
{
yPercent: clicked ? -120 : 0,
rotation: clicked ? 3 : 0,
stagger: clicked ? 0.02 : -0.02
},
"start"
)
.fromTo(
currentSlideInfo.DOM.text.descriptionLines,
{
yPercent: clicked ? 0 : 100
},
{
yPercent: clicked ? -100 : 0,
stagger: 0.05
},
"start"
)
.fromTo(
this.DOM.navigation.prev,
{
translateX: clicked ? 0 : 100,
opacity: clicked ? 1 : 0
},
{
translateX: clicked ? -100 : 0,
opacity: clicked ? 0 : 1
},
"start"
)
.fromTo(
this.DOM.navigation.next,
{
translateX: clicked ? 0 : -100,
opacity: clicked ? 1 : 0
},
{
translateX: clicked ? 100 : 0,
opacity: clicked ? 0 : 1
},
"start"
)
.set([this.DOM.navigation.prev, this.DOM.navigation.next], {
pointerEvents: clicked ? "none" : "auto"
});
};
this.DOM.navigation.prev.addEventListener("click", () =>
this.onClickPrevEv()
);
this.DOM.navigation.next.addEventListener("click", () =>
this.onClickNextEv()
);
this.DOM.imageWrapperEl.addEventListener("click", () =>
this.onImageClickEv()
);
}
onSlideChange(callback) {
if (typeof callback == "function") {
this.onSlideChangeCallbackFn = callback;
}
}
onFullscreen(callback) {
if (typeof callback == "function") {
this.onFullscreenCallbackFn = callback;
}
}
offFullscreen(callback) {
if (typeof callback == "function") {
this.offFullscreenCallbackFn = callback;
}
}
navigate(direction) {
if (this.GlSlider.state.animating) return;
const incrementSlideIndex = (val) => {
if (val > 0 && this.current + val < this.slidesTotal) {
this.current += val;
} else if (val > 0) {
this.current = 0;
} else if (val < 0 && this.current + val < 0) {
this.current = this.slidesTotal - 1;
} else {
this.current += val;
}
};
const increment = direction == "prev" ? -1 : 1;
const currentSlideInfo = this.slideInfos[this.current];
incrementSlideIndex(increment);
const nextSlideInfo = this.slideInfos[this.current];
this.GlSlider.switchTextures(this.current, increment);
gsap
.timeline({
defaults: { duration: 1, ease: "power4.inOut" },
onStart: () => {
this.GlSlider.switchTextures(this.current, increment);
if (this.onSlideChangeCallbackFn)
this.onSlideChangeCallbackFn(this.current);
this.isAnimating = true;
},
onComplete: () => {
currentSlideInfo.DOM.el.classList.remove("slide--current");
this.isAnimating = false;
}
})
.addLabel("start", 0)
.to(
[currentSlideInfo.DOM.text.index, currentSlideInfo.DOM.text.title],
{
yPercent: direction === "next" ? -120 : 120,
rotation: direction === "next" ? 3 : -3,
stagger: direction === "next" ? 0.02 : -0.02
},
"start"
)
.to(
currentSlideInfo.DOM.text.descriptionLines,
{
yPercent: direction === "next" ? -100 : 100,
stagger: direction === "next" ? 0.05 : -0.05
},
"start"
)
.addLabel("upcoming", 0.4)
.add(() => {
gsap.set([nextSlideInfo.DOM.text.index, nextSlideInfo.DOM.text.title], {
yPercent: direction === "next" ? 120 : -120,
rotation: direction === "next" ? -3 : 3
});
gsap.set(nextSlideInfo.DOM.text.descriptionLines, {
yPercent: direction === "next" ? 100 : -100
});
nextSlideInfo.DOM.el.classList.add("slide--current");
}, "upcoming")
.to(
[nextSlideInfo.DOM.text.index, nextSlideInfo.DOM.text.title],
{
yPercent: 0,
rotation: 0,
stagger: direction === "next" ? 0.02 : -0.02
},
"upcoming"
)
.to(
nextSlideInfo.DOM.text.descriptionLines,
{
yPercent: 0,
stagger: direction === "next" ? 0.05 : -0.05
},
"upcoming"
);
}
}
// -------- MAIN CLASSES [END] --------
// -------- MAIN CODE [START] --------
const cursors = new Cursors();
Splitting();
const bgColors = ["#1f1322", "#27172e", "#454d53", "#2d1f2d"];
const masterTl = gsap.timeline();
preloadImages(document.querySelectorAll(".slider__image")).then(() => {
const slider = new Slideshow(document.querySelector(".slider"));
slider.init();
const loadedAnimationTl = gsap
.timeline({
onStart: () => {
gsap.set(".text__row .text", { autoAlpha: 1 });
}
})
.to(".loading__text", {
duration: 1,
opacity: 0
})
.from(".text__row .text", {
duration: 3,
translateY: (i) => -100 + i * -25 + "%",
ease: "expo.out",
stagger: 0.1
})
.to(".text__row .text", {
duration: 3,
translateY: (i) => 100 + i * 25 + "%",
ease: "expo.in",
stagger: 0.25
})
.to(".bg__transition--slide", {
duration: 1,
scaleY: 0,
transformOrigin: "top center",
ease: "expo.out",
onComplete: () => {
slider.initAnimation();
gsap.set(".loading__wrapper", {
pointerEvents: "none",
autoAlpha: 0
});
}
});
const pageAnimationTl = gsap
.timeline({
delay: loadedAnimationTl.duration(),
onComplete: () => {
cursors.init();
cursors.initEventsOnSlider(slider);
}
})
.from(
[
".frame__logo",
".frame__button",
".frame__artist > span",
".frame__credits > span"
],
{
duration: 1,
opacity: 0,
yPercent: 100,
stagger: 0.1,
ease: "expo.out"
}
);
masterTl.add(loadedAnimationTl, 0);
masterTl.add(pageAnimationTl, pageAnimationTl.duration() - 0.5);
slider.onSlideChange((currentSlideIndex) => {
gsap.to("body", {
duration: 1.2,
backgroundColor: bgColors[currentSlideIndex]
});
});
});
// -------- MAIN CODE [END] --------
Also see: Tab Triggers