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>SVG feDisplacement filter: how to deal with jagged edges</h1>
<svg class="container" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="800px"
        height="540px">
        <defs>
            <filter id="dm-filter1" x="-50%" y="-50%" width="200%" height="200%" color-interpolation-filters="sRGB"
                preserveAspectRatio="none">
                <feImage id="feimage1" preserveAspectRatio="none" result="FEIMG1" href="img/aliasingedges/bezmap.jpg" />
                <feDisplacementMap in="SourceGraphic" in2="FEIMG1" scale="150" xChannelSelector="R"
                    yChannelSelector="B" />
            </filter>
            <filter id="dm-filter2" x="-50%" y="-50%" width="200%" height="200%" color-interpolation-filters="sRGB"
                preserveAspectRatio="none">
                <feImage id="feimage2" preserveAspectRatio="none" result="FEIMG2" />
                <feDisplacementMap in="SourceGraphic" in2="FEIMG2" scale="150" xChannelSelector="R"
                    yChannelSelector="B" />
            </filter>
            <filter id="dm-filter3" x="-50%" y="-50%" width="200%" height="200%" color-interpolation-filters="sRGB"
                preserveAspectRatio="none">
                <feImage id="feimage3" preserveAspectRatio="none" result="FEIMG3" />
                <feDisplacementMap in="SourceGraphic" in2="FEIMG3" scale="150" xChannelSelector="R"
                    yChannelSelector="B" />
                <feGaussianBlur stdDeviation="2.5" />
                <feConvolveMatrix order="3" kernelMatrix="3.3 -3.3  3.3 -3.3 -.25 -3.3 3.3  -3.3  3.3" />
            </filter>
            <filter id="dm-filter4" x="-50%" y="-50%" width="200%" height="200%" color-interpolation-filters="sRGB"
                preserveAspectRatio="none">
                <feImage id="feimage4" preserveAspectRatio="none" result="FEIMG4" />
                <feDisplacementMap in="SourceGraphic" in2="FEIMG4" scale="150" xChannelSelector="R"
                    yChannelSelector="B" />
                <feGaussianBlur stdDeviation="1.5" />
                <feConvolveMatrix order="3" kernelMatrix="2.3 -2.3  2.3 -2.3 -1 -2.3 2.3  -2.3  2.3" />
            </filter>
            <symbol id="polygon">
                <path
                    d="M150,0L174.859,98.379L267.275,56.477L205.858,137.251L296.239,183.378L194.795,185.723L215.083,285.145L150,207.295L84.917,285.145L105.205,185.723L3.761,183.378L94.142,137.251L32.725,56.477L125.141,98.379L150,0Z"
                    style="fill:white;" />
            </symbol>
            <symbol id="monalisa">
                <image href="https://assets.codepen.io/100347/fedm-monalisa.jpg" id="filteredSVG__img" x="0" y="0" height="256px"
                    width="256px" />
            </symbol>
        </defs>
        <text class="info" y="30" x="140" text-anchor="middle">
            <tspan x="120" dy="0">JPG applied as</tspan>
            <tspan x="120" dy="1.4em">displacement map:</tspan>
            <tspan x="120" dy="1.4em">notice the ugly jagged edges.</tspan>
        </text>
        <g class="filter-jpgmap" transform="translate(40 140) scale(.5)">
            <use href="#polygon" />
        </g>
        <text class="info" y="30" x="410" text-anchor="middle">
            <tspan x="410" dy="0">SVG applied as displacement map:</tspan>
            <tspan x="410" dy="1.4em">Edges already look smoother</tspan>
            <tspan x="410" dy="1.4em">but are still way too jaggy.</tspan>
        </text>
        <g class="filter-svgmap" transform="translate(330 140) scale(.5)">
            <use href="#polygon" />
        </g>
        <text class="info" y="30" x="680" text-anchor="middle">
            <tspan x="680" dy="0">SVG map once more,</tspan>
            <tspan x="680" dy="1.4em">this time "fake" antialiasing</tspan>
            <tspan x="680" dy="1.4em">(blur filter + sharpening) was added.</tspan>
            <tspan x="680" dy="1.4em">Not perfekt, but good enough</tspan>
            <tspan x="680" dy="1.4em">for in-between states in animations.</tspan>
        </text>
        <g class="filter-svgmapantialiased-strong" transform="translate(630 140) scale(.5)">
            <use href="#polygon" />
        </g>
        <g class="filter-jpgmap" transform="translate(40 350) scale(.5)">
            <use href="#monalisa" />
        </g>
        <g class="filter-svgmap" transform="translate(330 350) scale(.5)">
            <use href="#monalisa" />
        </g>
        <g class="filter-svgmapantialiased-weak" transform="translate(630 350) scale(.5)">
            <use href="#monalisa" />
        </g>
    </svg>
              
            
!

CSS

              
                html, body {
  height: 100%;font: 0.85em -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', sans-serif;
  color: white;
  text-align: center;
}

body {
  background: black;
}

.container {
  position: relative;
  display: block;
  margin: 0 auto;
  width: 800px;
  height: 540px;
}

.info {
  fill: white;
}

.filter-jpgmap{ 
    filter: url('#dm-filter1');
}

.filter-svgmap { 
    filter: url('#dm-filter2');
}

.filter-svgmapantialiased-strong { 
    filter: url('#dm-filter3');
}

.filter-svgmapantialiased-weak { 
    filter: url('#dm-filter4');
}
              
            
!

JS

              
                class BlobLoader {
    blobContainer = {};

    async load(url) {
        let res;

        if (!this.blobContainer[url]) {
            res = await fetch(url, {
                method: 'GET',
                headers: new Headers(),
                mode: 'cors',
                cache: 'default',
            });

            this.blobContainer[url] = res.blob();
        }

        return this.blobContainer[url];
    }
  
    async readURL(url) {
        const blob = await this.load(url);

        return URL.createObjectURL(blob);
    }
}

const mapBlobs = new BlobLoader();

mapBlobs
    .readURL('https://assets.codepen.io/100347/fedm_bezmap.jpg')
    .then((url) => {
        document.getElementById('feimage1').setAttribute('href', url);
    });

mapBlobs
    .readURL('https://assets.codepen.io/100347/fedm_bezmap-1.svg')
    .then((url) => {
        document.getElementById('feimage2').setAttribute('href', url);
        document.getElementById('feimage3').setAttribute('href', url);
        document.getElementById('feimage4').setAttribute('href', url);
    });
              
            
!
999px

Console