Immersive media transitions—turning one image into another—are a staple of showcase galleries, and many of the best examples lean on WebGL and texture mapping to drive a displacement or dissolve. Both effects need the same three ingredients: the source media you're transitioning from, the target media, and a map supplying a per-pixel value that controls when and how far each pixel flips from one to the other. That map can be a prebuilt image or a <canvas> painted with noise. The kampos library handles the GPU plumbing, leaving you to supply the noise and tune the visuals.

Scene setup and image loading

Start with a minimal DOM holding two images (videos work just as well) plus enough JavaScript to confirm both are ready before you touch them.

<main>
  <section>
    <figure>
      <canvas id="target">
        <img id="source-from" src="path/to/first.jpg" alt="My first image" />
        <img id="source-to" alt="My second image" />
      </canvas>
    <figure>
  </section>
</main>

With the stage in place, bring in the two media sources.

// Notify when our images are ready
function loadImage (src) {
  return new Promise(resolve => {
    const img = new Image();
    img.onload = function () {
      resolve(this);
    };
    img.src = src;
  });
}
// Get the image URLs
const imageFromSrc = document.querySelector('#source-from').src;
const imageToSrc = document.querySelector('#source-to').dataset.src;
// Load images  and keep their promises so we know when to start
const promisedImages = [
  loadImage(imageFromSrc),
  loadImage(imageToSrc)
];

Building the dissolve map

Generate the map by rendering noise onto a canvas. A Classic Perlin noise run through a turbulence effect—which layers noise at different scales—produces a grayscale texture, functioning much like the SVG feTurbulence filter.

const turbulence = kampos.effects.turbulence({ noise: kampos.noise.perlinNoise });

Next, configure the turbulence parameters. With the following settings you get a liquid, blobby texture in which the first image appears to sink into the second. Raising CELL_FACTOR densifies the pattern into smaller blobs, while octaves=1 is what keeps it blobby. The amplitude is normalized to at least the media's longer side so the texture stretches cleanly across the image.

// Depending of course on the size of the target canvas
const WIDTH = 854;
const HEIGHT = 480;
const CELL_FACTOR = 2;
const AMPLITUDE = CELL_FACTOR / WIDTH;

turbulence.frequency = {x: AMPLITUDE, y: AMPLITUDE};
turbulence.octaves = 1;
turbulence.isFractal = true;

Render the map using the on-page canvas for inspection:

const mapTarget = document.querySelector('#target'); // instead of document.createElement('canvas');
mapTarget.width = WIDTH;
mapTarget.height = HEIGHT;

const dissolveMap = new kampos.Kampos({
  target: mapTarget,
  effects: [turbulence],
  noSource: true
});
dissolveMap.draw();

Adjusting the noise

Changing the parameters changes the character of the result. For something smoke-like rather than liquid-like, try these:

const CELL_FACTOR = 4; // instead of 2

And then this:

turbulence.octaves = 8; // instead of 1

Superimposing eight levels of noise instead of one yields a far denser, more detailed pattern.

Wiring up the transition

Construct the transition effect itself:

const dissolve = kampos.transitions.dissolve();
dissolve.map = mapTarget;
dissolve.high = 0.03; // for liquid-like effect

The high value is the key to the liquid look. The transition uses a step function to decide which media a pixel shows, smoothing the transition across the step so edges stay soft instead of jagged. Keeping the low edge at its default 0.0, a range of 0.0 to 0.03 is abrupt—effectively clipping from one medium to the other. Widening it to something like 0.0 to 0.5 gives more partial transparency, blending the images as partial opacity would and producing a smoky, cloudy result.

Before proceeding, swap the document canvas for a fresh one created off the DOM:

const mapTarget = document.createElement('canvas');

Compositing and playback

Instantiate the compositor:

const target = document.querySelector('#target');
const hippo = new kampos.Kampos({target, effects: [dissolve]});

Then fetch the images and play it:

Promise.all(promisedImages).then(([fromImage, toImage]) => {
  hippo.setSource({media: fromImage, width, height});
  dissolve.to = toImage;
  hippo.play(time => {
    // a sin() to play in a loop
    dissolve.progress = Math.abs(Math.sin(time * 4e-4)); // multiply time by a factor to slow it down a bit
  });
});

Variations

For a more smoke-like effect, retune the parameters:

const CELL_FACTOR = 4;
turbulence.octaves = 8;

For a smoother transition, raise the high edge of the step function:

dissolve.high = 0.3;

Animating the noise

For one final twist, animate the map itself. kampos does not refresh the dissolve map texture by default, so enable per-frame updates:

dissolve.textures[1].update = true;

Then, each frame, advance the turbulence time property and redraw. Slow the transition itself so the noise is visible shifting mid-effect:

hippo.play(time => {
  turbulence.time = time * 2;
  dissolveMap.draw();
  // Notice that the time factor is smaller here
  dissolve.progress = Math.abs(Math.sin(time * 2e-4));
});

Where to take it

This is one recipe for kampos-based media transitions; the parameters above combine in many ways depending on the impression you want. Some starting points:

  • Swapping site or section backgrounds
  • Transitions between backgrounds in an image carousel
  • Background changes triggered by click or hover
  • Retiring a custom poster image once a video starts playing