Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

Save Automatically?

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.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

            
              <div id="app">
    <div class="left">
        <ui-textbox v-model="input" multi-line rows="10" help="Paste the source of an SVG document">SVG source</ui-textbox>
    </div>
    <div class="right">
        <div class="right-top">
            <h4>Preview</h4>
            <div class="output" :style="style"></div>
        </div>
        <div class="right-bottom">
            <h4>CSS</h4>
            <pre class="source"><code v-text="cssOutput"></code></pre>
            
            <h4>Gradients</h4>
            <pre class="source"><code v-text="output"></code></pre>
        </div>
    </div>
</div>
            
          
!

CSS

            
              $monospace = SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;

* {
    box-sizing: border-box;
    font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,Fira Sans,Droid Sans,Helvetica,Arial,sans-serif;
}

html {
    font-size: 16px;
}

body {
    background-color: beige;
    margin: 0;
    padding: 60px;
}

.ui-textbox__textarea {
    background-color: #eee;
    padding: 8px;
    margin-top: 8px;
    font-family: $monospace;
}


h4 {
    margin: 0;
    margin-bottom: 16px;
}

#app {
    display: flex;
    background-color: white;
    width: 1200px;
    max-width: 100%;
    margin: 0 auto;
    padding: 48px;
    box-shadow: 0 3px 8px 0 rgba(0,0,0,0.2);
    border-radius: 2px;
    min-height: 500px;
}

.left {
    width: 50%;
    padding-right: 24px;
}

.right {
    display: flex;
    flex-direction: column;
    width: 50%;
    padding-left: 24px;
    // border-left: 1px solid #EE;
}

.right-top,
.right-bottom {
    height: 50%;
}

.right-top {
    margin-bottom: 24px;
}

.output {
    width: 180px;
    height: 180px;
}

pre {
    display: block;
    font-size: 87.5%;
    margin-top: 0;
    margin-bottom: 1rem;
    overflow: auto;
    background-color: #eee;
    border: 1px solid #DDD;
    padding: 8px;
}

code {
    color: #e83e8c;
    word-break: break-word;
    font-family: $monospace;
}

            
          
!

JS

            
              new Vue({
    el: '#app',
    
    data: {
        input: '<linearGradient xmlns="http://www.w3.org/2000/svg" gradientUnits="userSpaceOnUse" x1="17.5001" y1="32" x2="17.5001" y2="2.9711"><stop offset="0" style="stop-color:#FCB3A4"/><stop offset="1" style="stop-color:#DA5899"/></linearGradient>'
    },
    
    computed: {
        output() {
            return parseInput(this.input)
                .map(gradient => {
                    if (defined(gradient.angle) && gradient.stops.length > 0) {
                        const angle = round(Number(gradient.angle));
                        return `linear-gradient(${angle}deg, ${
                            gradient.stops
                                .map(stop => `${stop.color} ${stop.offset}%`)
                                .join(', ')
                        })`;
                    }
                
                    return undefined;
                })
                .filter(gradient => defined(gradient))
                .join(',\r\n');
        },
        
        style() {
            return this.output && this.output.trim().length > 0
                ?  { 'background-image': this.output }
                : undefined;
        },
        
        cssOutput() {
            return this.style 
                ? `.gradient {\r\n    background-image: ${this.style['background-image']};\r\n}`
                : '';
        }
    }
});

function parseInput(input) {
    const parser = new DOMParser();
    const doc = parser.parseFromString(input, 'image/svg+xml');
    
    return Array.from(doc.querySelectorAll('linearGradient'))
        .map(lg => {
            const angle = getGradientAngle(lg);
            const stops = getGradientStops(lg);
        
            return {
                angle,
                stops: stops.filter(s => { return defined(s) && defined(s.offset) && defined(s.color); })
            };
        });
}


function getGradientAngle(lg) {
    let x1 = lg.getAttribute('x1');
    let y1 = lg.getAttribute('y1');
    let x2 = lg.getAttribute('x2');
    let y2 = lg.getAttribute('y2');
    
    if (x1.trim().endsWith('%')) {
        x1 = Number(x1.trim().replace('%', ''));
    }
    
    if (y1.trim().endsWith('%')) {
        y1 = Number(y1.trim().replace('%', ''));
    }
    
    if (x2.trim().endsWith('%')) {
        x2 = Number(x2.trim().replace('%', ''));
    }
    
    if (y2.trim().endsWith('%')) {
        y2 = Number(y2.trim().replace('%', ''));
    }
    
    const x = x2 - x1;
    const y = y2 - y1;
    
    // Use 90deg for gradients only in the x-direction
    if (y === 0) {
        return 90;
    }
    
    const angleRad = Math.atan(y/x);
    
    return angleRad * 180 / Math.PI;
}

function getGradientStops(lg) {
    return Array.from(lg.querySelectorAll('stop'))
        .map(stop => {
            let offset = stop.getAttribute('offset');
        
            if (offset === null) {
                return undefined;
            }
        
            offset = offset.trim()
        
            if (offset.endsWith('%')) {
                offset = offset.replace('%', '');
            } else {
                // Assume 0-1 as percentage when offset has no %
                offset = Number(offset) * 100;
            }
        
            let color;
            let opacity;

            // Try to find the color using `stop-color` and `stop-opacity`
            if (stop.hasAttribute('stop-color')) {
                color = stop.getAttribute('stop-color');
                opacity = stop.getAttribute('stop-opacity');

                if (opacity) {
                    const rgb = hexToRgb(color);

                    if (rgb) {
                        const { r, g, b } = rgb;
                        color = `rgba(${r}, ${g}, ${b}, ${opacity})`;
                    }
                }
            }
        
            // Try to find the color using `style`
            if (!color && stop.hasAttribute('style')) {
                const styles = getStyles(stop.getAttribute('style'));
                color = styles['stop-color'];
            }
                
            return { offset, color };
        });
}

function hexToRgb(hex) {
    const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
    
    return result ? {
        r: parseInt(result[1], 16),
        g: parseInt(result[2], 16),
        b: parseInt(result[3], 16)
    } : undefined;
}

function defined(value) {
    return value !== undefined;
}

function getStyles(string) {
    const el = document.createElement('div');
    
    el.setAttribute('style', string);
    
    return el.style;
}

function round(value) {
    return Math.round(value * 100) / 100;
}

            
          
!
999px

Console