svg preview - OhMinsSup/tip-review GitHub Wiki

https://codepen.io/copist/pen/VvmLbZ

$(function(){
    var svgText = $('#svg-txt'),
        svgSize = $('#svg-size'),
        svgFixedSize = $('.svg-fixed-size'),
        svgPreviewPlaceholder = $('.svg-preview-placeholder'),
        svgPreview = $('#svg-preview'),
        fileinfo = $('#fileinfo');

    if ('' == svgSize.val()) {
        svgSize.val(48);
    }

    function render() {
        var size = svgSize.val();
        if (size>200)
        {
          svgPreviewPlaceholder.addClass('large'); 
        } else
        {
          svgPreviewPlaceholder.removeClass('large');
        }
        svgPreview.html(svgText.val());
        svgPreview.css({width: size + 'px', height: size + 'px'})
        $('svg', svgPreview).css({width: '100%', height: '100%'})
    }

    function resize(event, element) {
        svgFixedSize.removeClass('active');
        $(this).addClass('active');
        svgSize.val($(this).text())
        render();
    }

    function readFile(evt) {
        //Retrieve the first (and only!) File from the FileList object
        var file = evt.target.files[0];

        if (file) {
            var reader = new FileReader();
            reader.onload = function(e) {
                var contents = e.target.result;
                fileinfo.html("<pre>Got the file: " +
                    "name: " + file.name + "\n" +
                    "type: " + file.type + "\n" +
                    "size: " + file.size + " bytes\n" +
                    "starts with: " + contents.substr(0, contents.indexOf("n") + '</pre>')
                );

                if (file.type.indexOf('svg')>0) {
                    svgText.val(contents)
                    svgPreview.html(svgText.val());
                    render();
                } else {
                    fileinfo.html('<div class="ui red message"><i class="icon red alert"></i> Wrong file type. Only SVG files allowed</div>');
                }
            }
            reader.readAsText(file);
        } else {
            fileinfo.html('<div class="ui red message"><i class="icon red alert"></i> Failed to load file</div>');
        }
    }
  
        svgText.val('<svg version="1.0" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 48 48" enable-background="new 0 0 48 48" xml:space="preserve">\
            <path fill="#4CAF50" d="M40,14H8l3.8,28.3c0.1,1,1,1.7,2,1.7h20.5c1,0,1.8-0.7,2-1.7L40,14z"></path>\
            <g>\
    <path fill="#81C784" d="M42,14H6v-3c0-2.2,1.8-4,4-4h28c2.2,0,4,1.8,4,4V14z"></path>\
    <path fill="#81C784" d="M37.2,10H10.8l1.7-4.7c0.3-0.8,1-1.3,1.9-1.3h19.2c0.8,0,1.6,0.5,1.9,1.3L37.2,10z"></path>\
    </g>\
    <path fill="#E8F5E9" d="M28,28.5c1.2-1.1,2-2.7,2-4.5c0-3.3-2.7-6-6-6c-3.3,0-6,2.7-6,6c0,1.8,0.8,3.4,2,4.5c-1.2,1.1-2,2.7-2,4.5\
    c0,3.3,2.7,6,6,6c3.3,0,6-2.7,6-6C30,31.2,29.2,29.6,28,28.5z M24,36c-1.7,0-3-1.3-3-3c0-1.7,1.3-3,3-3c1.7,0,3,1.3,3,3\
    C27,34.7,25.7,36,24,36z M24,27c-1.7,0-3-1.3-3-3c0-1.7,1.3-3,3-3c1.7,0,3,1.3,3,3C27,25.7,25.7,27,24,27z"></path>\
    </svg>');

    svgText.bind('keyup', render).bind('blur', render);
    svgSize.bind('keyup', render).bind('blur', render);
    svgFixedSize.bind('click', resize);

    render();

    // Check for the various File API support.
    if (window.File && window.FileReader && window.FileList && window.Blob) {
        $('#svg-file').show().bind('change', readFile);
    } else {
        fileinfo.html('<div class="ui red message"><i class="icon red alert"></i> The File APIs are not fully supported by your browser.</div>');
    }
});

$('.dropdown').dropdown();
<div class="ui container">
  <h1 class="ui header">Preview SVG files
    <a class="ui blue label" href="https://developer.mozilla.org/en-US/docs/Web/API/FileReader">FileReader from File API</a>
    <span class="ui green label">no upload to server requied</span>
  </h1>
  <div class="ui header">SVG icon text:</div>
    <div class="ui form">
        <div class="ui field">
            <textarea id="svg-txt" placeholder="<svg><!-- plain svg --></svg>"></textarea>
        </div>
        <div class="ui field">
            <input type="file" id="svg-file" />
        </div>
        <div class="ui field">
            <div id="fileinfo"></div>
        </div>
    </div>

    <div class="ui header">Icon size</div>
    <div class="ui segment">
        <div class="ui top attached menu">
            <div class="ui dropdown item">
                Windows 8/Metro<i class="dropdown icon"></i>
                <div class="menu">
                    <a class="item svg-fixed-size">26</a>
                    <a class="item svg-fixed-size">32</a>
                    <a class="item svg-fixed-size">64</a>
                    <a class="item svg-fixed-size">128</a>
                    <a class="item svg-fixed-size">256</a>
                    <a class="item svg-fixed-size">512</a>
                </div>
            </div>

            <div class="ui dropdown item">
                iOS7<i class="dropdown icon"></i>
                <div class="menu">
                    <a class="item svg-fixed-size">25</a>
                    <a class="item svg-fixed-size">50</a>
                    <a class="item svg-fixed-size">75</a>
                    <a class="item svg-fixed-size">100</a>
                    <a class="item svg-fixed-size">32</a>
                    <a class="item svg-fixed-size">128</a>
                    <a class="item svg-fixed-size">256</a>
                    <a class="item svg-fixed-size">512</a>
                </div>
            </div>

            <div class="ui dropdown item">
                Android<i class="dropdown icon"></i>
                <div class="menu">
                    <a class="item svg-fixed-size">24</a>
                    <a class="item svg-fixed-size">36</a>
                    <a class="item svg-fixed-size">48</a>
                    <a class="item svg-fixed-size">96</a>
                    <a class="item svg-fixed-size">32</a>
                    <a class="item svg-fixed-size">64</a>
                    <a class="item svg-fixed-size">128</a>
                    <a class="item svg-fixed-size">256</a>
                    <a class="item svg-fixed-size">512</a>
                </div>
            </div>

            <div class="right menu">
                <div class="ui right aligned category search item">
                    <div class="ui right labeled input">
                        <input type="text" id="svg-size" value="48">
                        <div class="ui basic label">px</div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="ui header">SVG icon preview</div>
    <div class="ui segment svg-preview-container">
        <div class="svg-preview-placeholder">
            <div class="ui image" id="svg-preview"></div>
        </div>
    </div>
</div>
<div class="ui bottom fixed menu">
    <a class="ui item" href="http://webmentor.pro/">
      <img class="ui avatar image" src="http://copist.ru/wp-content/uploads/2013/09/19789.jpg"> Pavel Volyntsev</a>
    <a class="ui item icons8" href="https://icons8.com/">Icons8</a>
</div>
body {
  background: #eee;
}

.ui.container {
  margin-top: 40px;
  margin-bottom: 80px; 
}

.svg-preview-placeholder.large .ui.image {
  background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI2MCIgaGVpZ2h0PSI2MCI+CjxyZWN0IHdpZHRoPSI2MCIgaGVpZ2h0PSI2MCIgZmlsbD0iI2ZmZiI+PC9yZWN0Pgo8cmVjdCB3aWR0aD0iNDIuNDIiIGhlaWdodD0iNDIuNDIiIHRyYW5zZm9ybT0idHJhbnNsYXRlKDMwIDApIHJvdGF0ZSg0NSkiIGZpbGw9IiM0NDQiPjwvcmVjdD4KPC9zdmc+");
}

.svg-preview-placeholder .ui.image {
  background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyMCIgaGVpZ2h0PSIyMCI+CjxyZWN0IHdpZHRoPSIyMCIgaGVpZ2h0PSIyMCIgZmlsbD0iI2ZmZiI+PC9yZWN0Pgo8cmVjdCB3aWR0aD0iMTAiIGhlaWdodD0iMTAiIGZpbGw9IiNjY2MiPjwvcmVjdD4KPHJlY3QgeD0iMTAiIHk9IjEwIiB3aWR0aD0iMTAiIGhlaWdodD0iMTAiIGZpbGw9IiNjY2MiPjwvcmVjdD4KPC9zdmc+");
}

.ui.item.icons8 {
  background: #32C24D;
  font: #ffffff;
}

#svg-preview {
  display: block;
  margin: 10px auto;
  border: 1px solid #999;
}
⚠️ **GitHub.com Fallback** ⚠️