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

              
                <h1>Effect of different Image Maps in SVG feDisplacement Filter</h1>
<section class="image-wrapper">
        <svg class="image-wrapper__image" width="256" height="256" viewBox="0 0 256 256" xmlns="http://www.w3.org/2000/svg">
    <defs>
        <style type="text/css">
            .filtered{
                filter: url(#filter);
                }
        </style>
        <filter id="filter" width="1" height="1" x="0" y="0" color-interpolation-filters="sRGB">
            <feImage id="feimage" width="100%" height="100%" x="0" y="0" result="10_MAP" href="https://assets.codepen.io/100347/fedm_identity.png"/>
            <feDisplacementMap id="displacement" in="SourceGraphic" in2="10_MAP" scale="100" xChannelSelector="R" yChannelSelector="B"/>
        </filter>
    </defs>
    <image id="map" href="https://assets.codepen.io/100347/fedm_pattern-a.svg" width="100%" height="100%" class="filtered" />
</svg>
        <section class="image-wrapper__sources" id="viewsource">
            <code class="image-wrapper__code" id="filtercode">
            </code>
            <img class="image-wrapper__map" id="map-preview" width="256" height="256"
                src="https://assets.codepen.io/100347/fedm_identity.png" alt="Preview of displacement-map" />
            <button id="toggleview" class="image-wrapper__toggleview">Show Code</button>
        </section>
    </section>

    <section class="map-scale-wrapper">
        <div class="map-scale-wrapper__value" id="scaleval">scale: 100</div>
        <input class="map-scale-wrapper__scale-range" id="scale" type="range" min="-256" max="256" steps="4" value="100"
            size="50" />
    </section>
    <ul class="map-select" id="thumbs">
        <li class="map-select__thumb">
            <input type="radio" name="radio" id="thumb-a" checked="checked" />
            <label class="map-select__label" for="thumb-a">
                <figure class="map-select__figure">
                    <img class="map-select__image" src="https://assets.codepen.io/100347/fedm_identity.png" width="100%"
                        item-height="100%" />
                    <figcaption class="map-select__title">
                        Identity Map
                    </figcaption>
                </figure>
            </label>
        </li>
        <li class="map-select__thumb">
            <input type="radio" name="radio" id="thumb-b" />
            <label class="map-select__label" for="thumb-b">
                <figure class="map-select__figure">
                    <img class="map-select__image" src="https://assets.codepen.io/100347/fedm_distort-100.jpg" width="100%"
                        item-height="100%" />
                    <figcaption class="map-select__title">
                        Distort -100
                    </figcaption>
                </figure>
            </label>
        </li>
        <li class="map-select__thumb">
            <input type="radio" name="radio" id="thumb-c" />
            <label class="map-select__label" for="thumb-c">
                <figure class="map-select__figure">
                    <img class="map-select__image" src="https://assets.codepen.io/100347/fedm_distortplus100.jpg" width="100%"
                        item-height="100%" />
                    <figcaption class="map-select__title">
                        Distort +100
                    </figcaption>
                </figure>
            </label>
        </li>
        <li class="map-select__thumb">
            <input type="radio" name="radio" id="thumb-d" />
            <label class="map-select__label" for="thumb-d">
                <figure class="map-select__figure">
                    <img class="map-select__image" src="https://assets.codepen.io/100347/fedm_canvas.jpg" width="100%"
                        item-height="100%" />
                    <figcaption class="map-select__title">
                        Canvas
                    </figcaption>
                </figure>
            </label>
        </li>
        <li class="map-select__thumb">
            <input type="radio" name="radio" id="thumb-e" />
            <label class="map-select__label" for="thumb-e">
                <figure class="map-select__figure">
                    <img class="map-select__image" src="https://assets.codepen.io/100347/fedm_cracks.jpg" width="100%"
                        item-height="100%" />
                    <figcaption class="map-select__title">
                        Cracks
                    </figcaption>
                </figure>
            </label>
        </li>
        <li class="map-select__thumb">
            <input type="radio" name="radio" id="thumb-f" />
            <label class="map-select__label" for="thumb-f">
                <figure class="map-select__figure">
                    <img class="map-select__image" src="https://assets.codepen.io/100347/fedm_crystal.jpg" width="100%"
                        item-height="100%" />
                    <figcaption class="map-select__title">
                        Crystal
                    </figcaption>
                </figure>
            </label>
        </li>
        <li class="map-select__thumb">
            <input type="radio" name="radio" id="thumb-g" />
            <label class="map-select__label" for="thumb-g">
                <figure class="map-select__figure">
                    <img class="map-select__image" src="https://assets.codepen.io/100347/fedm_glass.jpg" width="100%" item-height="100%" />
                    <figcaption class="map-select__title">
                        Glass
                    </figcaption>
                </figure>
            </label>
        </li>
        <li class="map-select__thumb">
            <input type="radio" name="radio" id="thumb-h" />
            <label class="map-select__label" for="thumb-h">
                <figure class="map-select__figure">
                    <img class="map-select__image" src="https://assets.codepen.io/100347/fedm_oceanwaves.jpg" width="100%"
                        item-height="100%" />
                    <figcaption class="map-select__title">
                        Ocean Waves
                    </figcaption>
                </figure>
            </label>
        </li>
        <li class="map-select__thumb">
            <input type="radio" name="radio" id="thumb-i" />
            <label class="map-select__label" for="thumb-i">
                <figure class="map-select__figure">
                    <img class="map-select__image" src="https://assets.codepen.io/100347/fedm_oscillation.jpg" width="100%"
                        item-height="100%" />
                    <figcaption class="map-select__title">
                        Oscillation
                    </figcaption>
                </figure>
            </label>
        </li>
        <li class="map-select__thumb">
            <input type="radio" name="radio" id="thumb-j" />
            <label class="map-select__label" for="thumb-j">
                <figure class="map-select__figure">
                    <img class="map-select__image" src="https://assets.codepen.io/100347/fedm_polar2circle.jpg" width="100%"
                        item-height="100%" />
                    <figcaption class="map-select__title">
                        Polar Square to Circle
                    </figcaption>
                </figure>
            </label>
        </li>
        <li class="map-select__thumb">
            <input type="radio" name="radio" id="thumb-k" />
            <label class="map-select__label" for="thumb-k">
                <figure class="map-select__figure">
                    <img class="map-select__image" src="https://assets.codepen.io/100347/fedm_polar2square.jpg" width="100%"
                        item-height="100%" />
                    <figcaption class="map-select__title">
                        Polar Circle to Square
                    </figcaption>
                </figure>
            </label>
        </li>
        <li class="map-select__thumb">
            <input type="radio" name="radio" id="thumb-l" />
            <label class="map-select__label" for="thumb-l">
                <figure class="map-select__figure">
                    <img class="map-select__image" src="https://assets.codepen.io/100347/fedm_raster.jpg" width="100%"
                        item-height="100%" />
                    <figcaption class="map-select__title">
                        Raster
                    </figcaption>
                </figure>
            </label>
        </li>
        <li class="map-select__thumb">
            <input type="radio" name="radio" id="thumb-m" />
            <label class="map-select__label" for="thumb-m">
                <figure class="map-select__figure">
                    <img class="map-select__image" src="https://assets.codepen.io/100347/fedm_ripple.jpg" width="100%"
                        item-height="100%" />
                    <figcaption class="map-select__title">
                        Ripple
                    </figcaption>
                </figure>
            </label>
        </li>
        <li class="map-select__thumb">
            <input type="radio" name="radio" id="thumb-n" />
            <label class="map-select__label" for="thumb-n">
                <figure class="map-select__figure">
                    <img class="map-select__image" src="https://assets.codepen.io/100347/fedm_tiles.jpg" width="100%" item-height="100%" />
                    <figcaption class="map-select__title">
                        Tiles
                    </figcaption>
                </figure>
            </label>
        </li>
        <li class="map-select__thumb">
            <input type="radio" name="radio" id="thumb-o" />
            <label class="map-select__label" for="thumb-o">
                <figure class="map-select__figure">
                    <img class="map-select__image" src="https://assets.codepen.io/100347/fedm_twirl.jpg" width="100%" item-height="100%" />
                    <figcaption class="map-select__title">
                        Twirl
                    </figcaption>
                </figure>
            </label>
        </li>
        <li class="map-select__thumb">
            <input type="radio" name="radio" id="thumb-p" />
            <label class="map-select__label" for="thumb-p">
                <figure class="map-select__figure">
                    <img class="map-select__image" src="https://assets.codepen.io/100347/fedm_waves.jpg" width="100%" item-height="100%" />
                    <figcaption class="map-select__title">
                        Waves
                    </figcaption>
                </figure>
            </label>
        </li>
    </ul>
    <section class="map-channelselector-wrapper" id="channel">
        <fieldset class="map-channelselector-wrapper__group">
            <span class="map-channelselector-wrapper__title">x-channel selector</span>
            <input type="radio" name="xchannel" id="redx" value="R" checked="checked" data-channel="x" />
            <label class="map-channelselector-wrapper__label--red" for="redx">red</label>
            <input type="radio" name="xchannel" id="bluex" value="B" data-channel="x" />
            <label class="map-channelselector-wrapper__label--blue" for="bluex">green</label>
        </fieldset>
        <fieldset class="map-channelselector-wrapper__group">
            <span class="map-channelselector-wrapper__title">y-channel selector</span>
            <input type="radio" name="ychannel" id="redy" value="R" data-channel="y" />
            <label class="map-channelselector-wrapper__label--red" for="redy">red</label>
            <input type="radio" name="ychannel" id="bluey" value="B" checked="checked" data-channel="y" />
            <label class="map-channelselector-wrapper__label--blue" for="bluey">green</label>
        </fieldset>
    </section>
              
            
!

CSS

              
                @use postcss-preset-env;

*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    height: 100%;
}

body {
    margin: 0;
    padding: 1.25em;
    height: 100%;
    color: white;
    font: 0.85em -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', sans-serif;
    min-width: 320px;
}

p {
    margin-bottom: 1em;
}

.filter {
    position: absolute;
    top: 0;
    left: -1;
    width: 1px;
    height: 1px;
    visibility: hidden;
}

::selection {
    background: #b400ff;
}

input[type='checkbox'],
input[type='radio'] {
    display: none;
}

html {
    height: 100%;
}

body {
    background: #071a2f;
    color: white;
}

h1 {
  font-size: 16px;
  text-align: center;
}

.image-wrapper {
    display: block;
    display: flex;
    justify-content: space-between;
    width: 512px;
    height: 256px;
    margin: 20px auto;
    text-align: center;
}

.image-wrapper__sources,
.image-wrapper__sources--toggle,
.image-wrapper__map,
.image-wrapper__code,
.image-wrapper__image {
    display: inline-block;
    position: relative;
    width: 256px;
    height: 256px;
    background-color: rgba(255, 255, 255, 0.4);
    overflow: hidden;
}

.image-wrapper__map,
.image-wrapper__code {
    position: absolute;
    left: 0;
    top: 0;
    transition: transform 0.3s ease-out;
}

.image-wrapper__code {
    background: black;
    padding: 10px;
    text-align: left;
    white-space: pre-wrap;
    font-size: 11px;
}

.image-wrapper__toggleview {
    display: block;
    position: absolute;
    right: 0;
    max-width: 14em;
    top: 50%;
    transform: translateY(-50%);
    background-color: transparent;
    color: transparent;
    height: 40px;
    line-height: 40px;
    padding: 0 46px 0 6px;
    border: none;
    cursor: pointer;
    transition: background-color 0.2s, color 0.2s, top 0.2s, padding-right 0.2s, max-width 0.2s;
    outline: none;

    &:hover {
        outline: none;
        background-color: black;
        color: white;
    }

    &::after {
        display: block;
        position: absolute;
        right: 0;
        top: 0;
        width: 40px;
        height: 40px;
        background: black;
        color: white;
        text-align: center;
        font-size: 18px;
        line-height: 40px;
        content: '</>';
        overflow: hidden;
    }

    &:before {
        position: absolute;
        display: block;
        max-width: 0;
        background-color: rgba(255, 255, 255, 0.4);
        color: white;
        height: 40px;
        overflow: hidden;
        right: 0;
        top: 0;
        content: 'show map';
        white-space: pre;
        opacity: 0;
        transition: opacity 0.2s, max-width 0.3s, padding 0.3s;
    }
}

.image-wrapper__sources--toggle .image-wrapper__toggleview {
    bottom: 0;
    background: white;
    max-width: 0;
    top: 240px;
    color: black;
    padding: 0;

    &::before {
        max-width: 114em;
        opacity: 1;
        line-height: 36px;
        padding: 0 6px;
    }

    &:hover::before {
        background-color: white;
        color: black;
    }

    &::after {
        width: 0;
    }
}

.image-wrapper__code {
    left: -100%;
}

.image-wrapper__sources--toggle {
    & .image-wrapper__code,
    & .image-wrapper__map {
        transform: translateX(100%);
    }
}

.map-select {
    display: block;
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    width: 512px;
    margin: 0 auto;
    position: relative;
}

.map-select__thumbs {
    display: block;
    position: relative;
    list-style-type: none;
}

.map-select__thumb {
    display: inline-block;
    position: relative;
    width: 60px;
    height: 60px;
    flex: 0 1 60px;
    margin: 0 4px 4px 0;

    &::nth-child(8) {
        margin-right: 0;
    }
}

.map-select__label {
    cursor: pointer;
    display: block;
    position: relative;
    width: 100%;
    height: 100%;
    border: solid rgba(255, 255, 255, 0.75) 0px;
    transition: border-width 0.3s;
}

input:checked + .map-select__label {
  border-width: 2px;
}

.map-select__figure {
    width: 100%;
    height: 100%;
}

input:checked + .map-select__label .map-select__figure {
    width: 56px;
    height: 56px;
}

.map-select__image {
    width: 100%;
    height: 100%;
}

.map-select__title {
    display: block;
    position: absolute;
    padding: 5px;
    font-size: 13px;
    color: black;
    text-align: center;
    left: 50%;
    top: 0;
    width: 70px;
    visibility: hidden;
    opacity: 0;
    transform: translate(-50%, -120%);
    background: white;
    transition: opacity 0.5s, visibility 0.5s;
    transition-delay: 0, 1.3s;

    &::after {
        display: block;
        width: 10px;
        height: 10px;
        background: white;
        position: absolute;
        left: 50%;
        bottom: -8px;
        transform: rotate(45deg) translateX(-50%);
        content: '';
    }

    @nest .map-select__label:hover & {
        opacity: 1;
        visibility: visible;
    }
}

.map-channelselector-wrapper,
.map-scale-wrapper {
    display: block;
    display: flex;
    width: 512px;
    margin: 40px auto;
    position: relative;
    text-align: right;
}

.map-scale-wrapper__value {
    background-color: rgba(255, 255, 255, 0.2);
    width: 162px;
    text-align: center;
    margin-right: 1px;
    padding: 0 0.5em;
    line-height: 20px;
}

.map-channelselector-wrapper {
    justify-content: flex-start;
    align-items: center;
    align-content: center;
    text-align: left;
    height: 36px;
}

.map-channelselector-wrapper__group {
    display: block;
    width: 50%;
    flex: 0 1 50%;
    border: none;
}

.map-channelselector-wrapper__title {
    display: inline-block;
    outline: none;
    margin-right: 8px;
}

.map-channelselector-wrapper__label--blue,
.map-channelselector-wrapper__label--red {
    display: inline-block;
    width: 10px;
    height: 10px;
    border: solid white 1px;
    background: #ff0000;
    text-indent: -100px;
    font-size: 0;
    color: transparent;
    vertical-align: middle;
    margin-right: 8px;
    transition: width 0.2s ease-out, height 0.2s ease-out;
    cursor: pointer;
}

:checked + .map-channelselector-wrapper__label--blue,
:checked + .map-channelselector-wrapper__label--red {
    width: 14px;
    height: 14px;
    cursor: auto;
}

.map-channelselector-wrapper__label--blue {
    background: #0000ff;
}

input[type='range'] {
    display: block;
    -webkit-appearance: none;
    background-color: rgba(255, 255, 255, 0.5);
    margin-top: 0;
    width: 100%;
}

input[type='range']:focus {
    outline: none;
}

input[type='range']::-webkit-slider-thumb {
    -webkit-appearance: none;
    background-color: white;
    width: 30px;
    height: 20px;
    cursor: pointer;
}

input[type='range']::-moz-range-track {
    background-color: #ffaa00;
    border: none;
    margin: 0;
    padding: 0;
    outline: 0;
}

input[type='range']::-moz-range-thumb {
    background-color: white;
    width: 30px;
    height: 20px;
    cursor: pointer;
    border: none;
    border-radius: 0;
    margin: 0;
}

input[type='range']::-ms-fill-lower {
    background: #ff3632;
}
input[type='range']::-ms-fill-upper {
    background: #ffaa00;
}
input[type='range']::-ms-thumb {
    background: white;
}
input[type='range']::-ms-ticks-after {
    display: none;
    color: blue;
}
input[type='range']::-ms-ticks-before {
    display: none;
    color: black;
}
input[type='range']::-ms-track {
    padding: 0;
    cursor: pointer;
}
input[type='range']::-ms-tooltip {
    display: none;
}

              
            
!

JS

              
                'use strict';

const toggleButton = document.getElementById('toggleview');
const sourceWrapper = document.getElementById('viewsource');
const codeWrapper = document.getElementById('filtercode');
const feImage = document.getElementById('feimage');
const displacement = document.getElementById('displacement');
const map = document.getElementById('map-preview');
const slider = document.getElementById('scale');
const thumbs = document.getElementById('thumbs');
const scaleVal = document.getElementById('scaleval');
const mapThumbs = document.querySelectorAll('.map-select__image');
const radios = document
    .getElementById('channel')
    .querySelectorAll('input[type=radio]');
const channelSelector = {
    x: 'R',
    y: 'B',
};
let displacementScale = 100;
let mapSrc = 'https://assets.codepen.io/100347/fedm_identity.png';

for(let thumb of mapThumbs) {
  const url = thumb.getAttribute('src');
  
  fetch(url)
  .then((response) => {
      return response.blob();
  })
  .then((blob) => {
      const objURL = URL.createObjectURL(blob);
    
      thumb.setAttribute('src', objURL);
    
      if(/identity/g.test(url)){
        feImage.setAttribute('href', objURL);
        map.setAttribute('src', objURL);
      }
  });
}

for (let i = 0; i < radios.length; i++) {
    radios[i].addEventListener('change', swapChannel);
}

toggleButton.addEventListener('click', toggleCode);
thumbs.addEventListener('click', swapMap);
slider.addEventListener('input', setMapScale, false);
slider.addEventListener('change', setMapScale, false);

function toggleCode(e) {
    let cn = sourceWrapper.className;

    if (/--toggle/gi.test(cn)) {
        cn = cn.replace('--toggle', '');
    } else {
        cn += '--toggle';
    }
    sourceWrapper.className = cn;
}

function updateCode() {
    const code = `<filter width="1" height="1" x="0" y="0" color-interpolation-filters="sRGB">
    <feImage id="feimage"
     width="100%" height="100%"
     x="0" y="0"
     result="10_MAP"
     href="${mapSrc}"
    />
    <feDisplacementMap
     in="SourceGraphic" 
     in2="10_MAP"
     scale="${displacementScale}" 
     xChannelSelector="${channelSelector.x}"
     yChannelSelector="${channelSelector.y}'
    />
</filter>`;

    codeWrapper.innerText = code;
}

function swapChannel(e) {
    const target = e.target;
    const channel = target.dataset.channel;

    channelSelector[channel] = target.value;
    displacement.setAttribute(
        `${channel}ChannelSelector`,
        channelSelector[channel]
    );
    updateCode();
}

function swapMap(e) {
    const target = e.target;

    if (target.src) {
        let src = target.src;
        mapSrc = src.match(/\/([^\/]+)$/g)[0].replace('/', '');
        map.setAttribute('src', src);
        feImage.setAttribute('href', src);
    }

    updateCode();
}

function setMapScale(e) {
    displacementScale = e.target.value;
    displacement.setAttribute('scale', displacementScale);
    scaleVal.innerHTML = 'scale: ' + displacementScale;
    updateCode();
}

updateCode();

              
            
!
999px

Console