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 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 x1Attr = removeUnit((lg.getAttribute('x1') || '').trim());
    let x2Attr = removeUnit((lg.getAttribute('x2') || '').trim());
    let y1Attr = removeUnit((lg.getAttribute('y1') || '').trim());
    let y2Attr = removeUnit((lg.getAttribute('y2') || '').trim());
    
    // Defaults: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/linearGradient
    // thanks @nVitius :)
    const x1 = toNumber(x1Attr, 0);
    const x2 = toNumber(x2Attr, 100);
    const y1 = toNumber(y1Attr, 0);
    const y2 = toNumber(y2Attr, 0);
        
    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 removeUnit(value) {
    if (value.includes('%')) {
        return value.replace(/%/g, '');
    }
    
    return value;
}

function toNumber(value, defaultValue) {
    const n = Number(value);
    return Number.isNaN(n) ? defaultValue : n;
}

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