Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="wrapper">
  <div class="canvas"></div>
  <div class="slides multi-textures">
    <img
         style="display: none" src="https://raw.githubusercontent.com/ivanalbizu/webgl-swiper/main/src/img/displacement4.jpg" data-sampler="displacement" />
  </div>
</div>

<div class="open-modal">
  <button class="js-open-modal" type="button">
    <svg
         xmlns="http://www.w3.org/2000/svg"
         width="24"
         height="24"
         viewBox="0 0 24 24"
         fill="none"
         stroke="#000000"
         stroke-width="1.5"
         stroke-linecap="round"
         stroke-linejoin="round">
      <line x1="4" y1="21" x2="4" y2="14"></line>
      <line x1="4" y1="10" x2="4" y2="3"></line>
      <line x1="12" y1="21" x2="12" y2="12"></line>
      <line x1="12" y1="8" x2="12" y2="3"></line>
      <line x1="20" y1="21" x2="20" y2="16"></line>
      <line x1="20" y1="12" x2="20" y2="3"></line>
      <line x1="1" y1="14" x2="7" y2="14"></line>
      <line x1="9" y1="8" x2="15" y2="8"></line>
      <line x1="17" y1="16" x2="23" y2="16"></line>
    </svg>
  </button>
  <button class="js-close-modal" type="button">
    <svg
         xmlns="http://www.w3.org/2000/svg"
         width="24"
         height="24"
         viewBox="0 0 24 24"
         fill="none"
         stroke="#000000"
         stroke-width="1.5"
         stroke-linecap="round"
         stroke-linejoin="round">
      <line x1="18" y1="6" x2="6" y2="18"></line>
      <line x1="6" y1="6" x2="18" y2="18"></line>
    </svg>
  </button>
</div>

<div class="overlap-swiper">
  <div class="swiper swiper-container">
    <div class="swiper-wrapper">
      <div class="swiper-slide">
        <img width="160" height="100" src="https://raw.githubusercontent.com/ivanalbizu/webgl-swiper/main/src/img/img-01.jpg" crossorigin="anonymous" alt="" />
      </div>
      <div class="swiper-slide">
        <img width="160" height="100" src="https://raw.githubusercontent.com/ivanalbizu/webgl-swiper/main/src/img/img-02.jpg" crossorigin="anonymous" alt="" />
      </div>
      <div class="swiper-slide">
        <img width="160" height="100" src="https://raw.githubusercontent.com/ivanalbizu/webgl-swiper/main/src/img/img-03.jpg" crossorigin="anonymous" alt="" />
      </div>
      <div class="swiper-slide">
        <img width="160" height="100" src="https://raw.githubusercontent.com/ivanalbizu/webgl-swiper/main/src/img/img-04.jpg" crossorigin="anonymous" alt="" />
      </div>
      <div class="swiper-slide">
        <img width="160" height="100" src="https://raw.githubusercontent.com/ivanalbizu/webgl-swiper/main/src/img/img-05.jpg" crossorigin="anonymous" alt="" />
      </div>
      <div class="swiper-slide">
        <img width="160" height="100" src="https://raw.githubusercontent.com/ivanalbizu/webgl-swiper/main/src/img/img-06.jpg" crossorigin="anonymous" alt="" />
      </div>
      <div class="swiper-slide">
        <img width="160" height="100" src="https://raw.githubusercontent.com/ivanalbizu/webgl-swiper/main/src/img/img-07.jpg" crossorigin="anonymous" alt="" />
      </div>
    </div>
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
  </div>
</div>

<div class="modal">
  <div class="modal__content">
    <img
         src="https://raw.githubusercontent.com/ivanalbizu/webgl-swiper/main/src/img/displacement4.jpg"
         crossorigin="anonymous" data-setting
         class="active"
         alt="" />
    <img src="https://raw.githubusercontent.com/ivanalbizu/webgl-swiper/main/src/img/displacement.jpg" crossorigin="anonymous" data-setting alt="" />
    <img src="https://raw.githubusercontent.com/ivanalbizu/webgl-swiper/main/src/img/displacement1.jpg" crossorigin="anonymous" data-setting alt="" />
    <img src="https://raw.githubusercontent.com/ivanalbizu/webgl-swiper/main/src/img/displacement2.jpg" crossorigin="anonymous" data-setting alt="" />
    <img src="https://raw.githubusercontent.com/ivanalbizu/webgl-swiper/main/src/img/displacement3.png" crossorigin="anonymous" data-setting alt="" />
    <img src="https://raw.githubusercontent.com/ivanalbizu/webgl-swiper/main/src/img/displacement5.png" crossorigin="anonymous" data-setting alt="" />
    <img src="https://raw.githubusercontent.com/ivanalbizu/webgl-swiper/main/src/img/displacement6.jpg" crossorigin="anonymous" data-setting alt="" />
    <img src="https://raw.githubusercontent.com/ivanalbizu/webgl-swiper/main/src/img/displacement7.jpg" crossorigin="anonymous" data-setting alt="" />
    <img src="https://raw.githubusercontent.com/ivanalbizu/webgl-swiper/main/src/img/displacement8.jpg" crossorigin="anonymous" data-setting alt="" />
    <img src="https://raw.githubusercontent.com/ivanalbizu/webgl-swiper/main/src/img/displacement9.jpg" crossorigin="anonymous" data-setting alt="" />
    <img src="https://raw.githubusercontent.com/ivanalbizu/webgl-swiper/main/src/img/displacement10.jpg" crossorigin="anonymous" data-setting alt="" />
    <img src="https://raw.githubusercontent.com/ivanalbizu/webgl-swiper/main/src/img/displacement11.jpg" crossorigin="anonymous" data-setting alt="" />
    <img src="https://raw.githubusercontent.com/ivanalbizu/webgl-swiper/main/src/img/displacement12.png" crossorigin="anonymous" data-setting alt="" />
    <img src="https://raw.githubusercontent.com/ivanalbizu/webgl-swiper/main/src/img/displacement13.jpg" crossorigin="anonymous" data-setting alt="" />
    <img src="https://raw.githubusercontent.com/ivanalbizu/webgl-swiper/main/src/img/displacement14.jpg" crossorigin="anonymous" data-setting alt="" />
    <img src="https://raw.githubusercontent.com/ivanalbizu/webgl-swiper/main/src/img/displacement15.png" crossorigin="anonymous" data-setting alt="" />
  </div>
</div>
              
            
!

CSS

              
                html,
body {
  min-height: 100%;
}
body {
  margin: 0;
  font-family: sans-serif;
  background: #ffffff;
  overflow: hidden;
}
.slides {
  display: flex;
  flex-wrap: wrap;
}
.wrapper {
  width: 100%;
  height: 100vh;
  overflow: hidden;
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: 1fr;
  grid-gap: 0;
}
.canvas {
  height: 100vh;
  width: 100vw;
  z-index: 10;
  grid-area: 1 / 1 / 1 / 1;
}
.multi-textures {
  grid-area: 1 / 1 / 1 / 1;
  z-index: 15;
  align-content: center;
  img {
    display: none;
    min-width: 100%;
    min-height: 100%;
    object-fit: cover;
  }
}
.no-curtains {
  .multi-textures {
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  &.image-1 .multi-textures img:nth-child(2) {
    display: block;
  }
  &.image-2 .multi-textures img:nth-child(3) {
    display: block;
  }
  &.image-3 .multi-textures img:nth-child(4) {
    display: block;
  }
  &.image-4 .multi-textures img:nth-child(5) {
    display: block;
  }
  &.image-5 .multi-textures img:nth-child(6) {
    display: block;
  }
}

.nav {
  display: flex;
  justify-content: space-between;
  width: 100%;
}
.btn {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: rgba(0, 0, 0, 0.9);
  color: #fff;
  border: 1px solid rgba(0, 0, 0, 0.2);
  width: 4em;
  height: 4em;
  margin: 0.5em 0.2em;
  cursor: pointer;
  transition: all 0.7s ease-in 0s;
  svg,
  svg path {
    pointer-events: none;
  }
}

// Not necessary. Use to show example textures animations
.open-modal {
  position: fixed;
  top: 4px;
  right: 18px;
  background-color: rgba(0, 0, 0, 0.9);
  z-index: 18;
  button {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    cursor: pointer;
  }
  .js-close-modal {
    display: none;
  }
}

.modal {
  position: fixed;
  right: 0;
  width: 200px;
  top: 0;
  bottom: 0;
  z-index: 17;
  overflow-y: auto;
  transform: translateX(200px);
  transition: transform 0.3s ease-in-out;
  img {
    max-width: 100%;
    z-index: 19;
  }

  &__content {
    display: flex;
    flex-wrap: wrap;
    overflow-y: auto;
    background-color: rgba(0, 0, 0, 0.85);
    padding: 0.5em;
    img {
      border: 2px dashed #fff;
      &:not(:last-of-type) {
        margin-bottom: 0.75em;
      }
      &.active {
        border-style: solid;
      }
    }
  }
}
.modal-active {
  .open-modal {
    .js-close-modal {
      display: flex;
    }
    .js-open-modal {
      display: none;
    }
  }
  .modal {
    transform: translateX(0px);
    transition: transform 0.3s ease-in-out;
  }
}

.overlap-swiper {
  position: absolute;
  bottom: 4px;
  right: 0;
  z-index: 80;
  // 320 360
  // 540 1440
  max-width: clamp(320px, calc(20vw + 246px), 540px);
  filter: drop-shadow(0px 0px 4px rgba(0, 0, 0, 0.5));
}
.swiper-slide {
  width: auto;

  img {
    display: block;
    max-width: 100%;
    border: 1.5px solid #fff;
    border-radius: 3px;
    object-fit: cover;
  }
}

              
            
!

JS

              
                import { Curtains, Plane } from 'https://cdn.jsdelivr.net/npm/curtainsjs@8.1.4/src/index.mjs';
const fragment = `
precision mediump float;

varying vec3 vVertexPosition;
varying vec2 vTextureCoord;
varying vec2 vActiveTextureCoord;
varying vec2 vNextTextureCoord;

// custom uniforms
uniform float uTransitionTimer;

// our textures samplers
// notice how it matches the sampler attributes of the textures we created dynamically
uniform sampler2D activeTexture;
uniform sampler2D nextTexture;
uniform sampler2D displacement;

void main() {
    // our displacement texture
    vec4 displacementTexture = texture2D(displacement, vTextureCoord);

    // slides transitions based on displacement and transition timer
    vec2 firstDisplacementCoords = vActiveTextureCoord + displacementTexture.r * ((cos((uTransitionTimer + 90.0) / (90.0 / 3.141592)) + 1.0) / 1.25);
    vec4 firstDistortedColor = texture2D(activeTexture, vec2(vActiveTextureCoord.x, firstDisplacementCoords.y));

    // same as above but we substract the effect
    vec2 secondDisplacementCoords = vNextTextureCoord - displacementTexture.r * ((cos(uTransitionTimer / (90.0 / 3.141592)) + 1.0) / 1.5);
    vec4 secondDistortedColor = texture2D(nextTexture, vec2(vNextTextureCoord.x, secondDisplacementCoords.y));

    // mix both texture
    vec4 finalColor = mix(firstDistortedColor, secondDistortedColor, 1.0 - ((cos(uTransitionTimer / (90.0 / 3.141592)) + 1.0) / 2.0));

    // handling premultiplied alpha
    finalColor = vec4(finalColor.rgb * finalColor.a, finalColor.a);

    gl_FragColor = finalColor;
}
`;

const vertex = `
precision mediump float;

// default mandatory variables
attribute vec3 aVertexPosition;
attribute vec2 aTextureCoord;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;

// textures matrices
uniform mat4 activeTextureMatrix;
uniform mat4 nextTextureMatrix;

// varyings : notice we've got 3 texture coords varyings
// displacement texture / visible texture / upcoming texture
varying vec3 vVertexPosition;
varying vec2 vTextureCoord; // displacement
varying vec2 vActiveTextureCoord;
varying vec2 vNextTextureCoord;

// custom uniforms
uniform float uTransitionTimer;


void main() {
    gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);

    // varyings
    // use original texture coords for our displacement
    vTextureCoord = aTextureCoord;
    vActiveTextureCoord = (activeTextureMatrix * vec4(aTextureCoord, 0.0, 1.0)).xy;
    vNextTextureCoord = (nextTextureMatrix * vec4(aTextureCoord, 0.0, 1.0)).xy;

    vVertexPosition = aVertexPosition;
}
`;

import Swiper from 'https://cdn.jsdelivr.net/npm/swiper@8/swiper-bundle.esm.browser.min.js';

const swiperEl = document.querySelector('.swiper');
const wrapper = document.querySelector('.wrapper');
const canvas = wrapper.querySelector('.canvas');
const planeElement = wrapper.querySelector('.multi-textures');

const swiper = new Swiper(swiperEl, {
  slidesPerView: 'auto',
  spaceBetween: 16,
  loop: true,
  slideToClickedSlide: true,

  navigation: {
    nextEl: '.swiper-button-next',
    prevEl: '.swiper-button-prev',
  },

  on: {
    beforeInit: () => {
      swiperEl.querySelectorAll('img').forEach((img) => {
        planeElement.appendChild(img.cloneNode());
      });
    },
  },
});

class WebglSlides {
  constructor(set) {
    this.swiper = set.swiper;
    this.canvas = set.canvas;

    this.planeElement = set.planeElement;
    this.multiTexturesPlane = null;
    this.slidesState = {
      isChanging: false,
      transitionTimer: 0,
    };
    this.params = {
      vertexShader: vertex,
      fragmentShader: fragment,
      uniforms: {
        transitionTimer: {
          name: 'uTransitionTimer',
          type: '1f',
          value: 0,
        },
      },
    };

    this.init();
  }

  init() {
    this.setupCurtains();
    this.initPlane();
    this.update();
  }

  setupCurtains() {
    this.curtains = new Curtains({
      container: this.canvas,
      watchScroll: false,
      pixelRatio: Math.min(1.5, window.devicePixelRatio),
    });
    this.curtains.onError(() => this.error());
    this.curtains.onContextLost(() => this.restoreContext());
  }

  initPlane() {
    this.multiTexturesPlane = new Plane(
      this.curtains,
      this.planeElement,
      this.params
    );

    this.multiTexturesPlane
      .onLoading((texture) => {
        texture.setMinFilter(this.curtains.gl.LINEAR_MIPMAP_NEAREST);
      })
      .onReady(() => {
        const activeTexture = this.multiTexturesPlane.createTexture({
          sampler: 'activeTexture',
          fromTexture:
            this.multiTexturesPlane.textures[this.swiper.realIndex + 1],
        });
        const nextTexture = this.multiTexturesPlane.createTexture({
          sampler: 'nextTexture',
          fromTexture:
            this.multiTexturesPlane.textures[this.swiper.realIndex + 2],
        });

        this.initEvent(activeTexture, nextTexture);
      });
  }

  update() {
    this.multiTexturesPlane.onRender(() => {
      if (this.slidesState.isChanging) {
        this.slidesState.transitionTimer +=
          (90 - this.slidesState.transitionTimer) * 0.04;

        if (
          this.slidesState.transitionTimer >= 88.5 &&
          this.slidesState.transitionTimer !== 90
        ) {
          this.slidesState.transitionTimer = 90;
        }
      }

      this.multiTexturesPlane.uniforms.transitionTimer.value =
        this.slidesState.transitionTimer;
    });
  }

  initEvent(activeTexture, nextTexture) {
    this.swiper.on('realIndexChange', () => {
      if (!this.slidesState.isChanging) {
        this.curtains.enableDrawing();

        this.slidesState.isChanging = true;
        this.swiper.disable();

        nextTexture.setSource(
          this.multiTexturesPlane.images[this.swiper.realIndex + 1]
        );

        setTimeout(() => {
          this.curtains.disableDrawing();

          this.slidesState.isChanging = false;
          activeTexture.setSource(
            this.multiTexturesPlane.images[this.swiper.realIndex + 1]
          );

          this.slidesState.transitionTimer = 0;
          this.swiper.enable();
        }, 1700);
      }
    });
  }

  error() {
    document.body.classList.add('no-curtains', 'image-1');
  }

  restoreContext() {
    this.curtains.restoreContext();
  }

  // Not necesary, only for change Displacements Texture
  removePlanes() {
    this.curtains.dispose();
  }
}

window.addEventListener('load', () => {
  let slide = new WebglSlides({
    swiper,
    canvas,
    planeElement,
  });

  // Down, not necesary, only for change Displacements Texture
  document.querySelector('.js-open-modal').addEventListener('click', () => {
    document.body.classList.add('modal-active');
  });
  document.querySelector('.js-close-modal').addEventListener('click', () => {
    document.body.classList.remove('modal-active');
  });

  const settings = document.querySelectorAll('[data-setting]');
  settings.forEach((setting) => {
    setting.addEventListener('click', (event) => {
      const target = event.target;
      const path = target.getAttribute('src');
      settings.forEach((setting) => setting.classList.remove('active'));
      target.classList.add('active');
      document.querySelector('[data-sampler]').src = path;

      slide.removePlanes();
      slide = new WebglSlides({
        swiper,
        canvas,
        planeElement,
      });
    });
  });
});

              
            
!
999px

Console