圖片轉(zhuǎn)svg

基于html的圖片轉(zhuǎn)svg秋忙,效果很好彩掐,大于500k的效果不是很好,網(wǎng)上看到的灰追,鏈接忘了堵幽,貼代碼吧狗超,直接保存成html文件就可以運行
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>HTML5 把圖像轉(zhuǎn)換成SVG文件</title>
<style>
body {
padding: 1em;
padding-bottom: 2em;
max-width: 35em;
margin: auto;
font-size: 14px;
line-height: 1.4;
}

    h1 {
        display: block;
        margin: 0 0 0.5em;
    }

        h1 svg {
            margin: 0;
        }

    p {
        margin: 0 0 1em;
    }

    a {
        color: #f05555;
    }

    img {
        vertical-align: middle;
    }

    button {
        background: #ddd;
        border: none;
        -webkit-appearance: none;
        -moz-appearance: none;
        appearance: none;
    }

    button,
    input[type=file] {
        padding: 0.5em;
        cursor: pointer;
    }

    svg,
    label,
    input[type=file] {
        display: block;
        margin: 1em auto;
        text-align: center;
    }

        label small {
            display: block;
        }

    svg {
        max-width: 100%;
    }

    .choices {
        background: #eee;
        display: -webkit-box;
        display: -webkit-flex;
        display: -ms-flexbox;
        display: flex;
        -webkit-box-align: stretch;
        -webkit-align-items: stretch;
        -ms-flex-align: stretch;
        align-items: stretch;
    }

        .choices > * {
            -webkit-box-flex: 1;
            -webkit-flex: 1 1 50%;
            -ms-flex: 1 1 50%;
            flex: 1 1 50%;
        }

    .download,
    .outputSize {
        display: inline-block;
        text-align: center;
        margin: 1em auto 0;
        padding: 0.25em 0.5em;
        font-size: 0.8em;
    }

    .download {
        background-color: #f05555;
        color: #fff;
    }

    #output {
        text-align: center;
    }

    #outputRaw {
        display: block;
        background: #eee;
        white-space: pre-wrap;
        padding: 0.5em;
        font-size: 0.75em;
    }

    .inspired {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        padding: 0.5em;
        width: 100%;
        background: #fff;
        background: rgba(255, 255, 255, 0.8);
        text-align: center;
    }
</style>

</head>
<body>
<h1>
<svg xmlns="https://www.w3.org/2000/svg" viewbox="0 -0.5 61 11.5" shape-rendering="crispEdges">
<title>Pixels to SVG</title>
<path stroke="#f05555" d="M0 0h61M0 1h61M0 2h2M5 2h2M8 2h1M10 2h3M14 2h1M19 2h1M21 2h5M29 2h2M41 2h3M47 2h1M49 2h3M53 2h2M58 2h3M0 3h2M3 3h2M6 3h1M8 3h1M10 3h3M14 3h1M16 3h4M21 3h4M26 3h5M33 3h1M41 3h2M44 3h4M49 3h3M53 3h1M55 3h6M0 4h2M3 4h2M6 4h1M8 4h2M11 4h1M13 4h2M18 4h2M21 4h4M26 4h5M32 4h3M37 4h2M41 4h2M44 4h4M49 4h3M53 4h1M55 4h6M0 5h2M5 5h2M8 5h3M12 5h3M16 5h4M21 5h5M28 5h3M33 5h1M36 5h1M39 5h1M41 5h3M46 5h2M49 5h3M53 5h1M55 5h1M58 5h3M0 6h2M3 6h4M8 6h2M11 6h1M13 6h2M16 6h4M21 6h7M29 6h2M33 6h1M36 6h1M39 6h1M41 6h5M47 6h2M50 6h1M52 6h2M55 6h3M59 6h2M0 7h2M3 7h4M8 7h1M10 7h3M14 7h1M16 7h4M21 7h7M29 7h2M34 7h1M37 7h2M41 7h5M47 7h2M50 7h1M52 7h2M55 7h3M59 7h2M0 8h2M3 8h4M8 8h1M10 8h3M14 8h1M19 8h1M24 8h1M28 8h3M41 8h2M46 8h4M51 8h4M58 8h3M0 9h61M0 10h61" />
<path stroke="#ffffff" d="M2 2h3M7 2h1M9 2h1M13 2h1M15 2h4M20 2h1M26 2h3M31 2h10M44 2h3M48 2h1M52 2h1M55 2h3M2 3h1M5 3h1M7 3h1M9 3h1M13 3h1M15 3h1M20 3h1M25 3h1M31 3h2M34 3h7M43 3h1M48 3h1M52 3h1M54 3h1M2 4h1M5 4h1M7 4h1M10 4h1M12 4h1M15 4h3M20 4h1M25 4h1M31 4h1M35 4h2M39 4h2M43 4h1M48 4h1M52 4h1M54 4h1M2 5h3M7 5h1M11 5h1M15 5h1M20 5h1M26 5h2M31 5h2M34 5h2M37 5h2M40 5h1M44 5h2M48 5h1M52 5h1M54 5h1M56 5h2M2 6h1M7 6h1M10 6h1M12 6h1M15 6h1M20 6h1M28 6h1M31 6h2M34 6h2M37 6h2M40 6h1M46 6h1M49 6h1M51 6h1M54 6h1M58 6h1M2 7h1M7 7h1M9 7h1M13 7h1M15 7h1M20 7h1M28 7h1M31 7h3M35 7h2M39 7h2M46 7h1M49 7h1M51 7h1M54 7h1M58 7h1M2 8h1M7 8h1M9 8h1M13 8h1M15 8h4M20 8h4M25 8h3M31 8h10M43 8h3M50 8h1M55 8h3" />
</svg>
</h1>
<p>
Need a pixel-perfect scalable image, but all you have is a low-res GIF? You could use <a target="_blank"><code>image-rendering: pixelated</code></a> and hope the browser will scale it
right, or you could use this tool to convert a raster image to SVG.
</p>
<p>
Each color is merged into one <code>path</code>, optimized for combining horizontal runs where possible to keep file size down. Works best with 8-bit images, or graphics where colors are limited and the dimensions are relatively small. Large or complex
images may lock the browser.
</p>
<div class="choices">
<input type="file" id="upload" />

    <button id="test">
        or try with a test image:
        <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAASCAMAAABcgh8DAAAADFBMVEUAAAD/MRjGYwD/lFpY2Im6AAAAAXRSTlMAQObYZgAAAExJREFUeNptzgkKwCAQQ1Enuf+dm8wYbIsfF56CuC6VO0pDACS4tij1DN8WLLUmTltA+Spq5gSmwphEsTRANgXHphXnz7Pk5e929l8Pl0sBHu8kwiYAAAAASUVORK5CYII="
            id="testImage" />
    </button>
</div>
<!--<label><input type="checkbox" id="includeDimensions" /> Include width/height on SVG? <small>viewBox will always be included, but omitting the width/height allows the SVG to scale</small></label>-->
<div id="output"></div>
<pre contenteditable="true" id="outputRaw"></pre>
<div class="inspired">
    <em>Inspired by <a  target="_blank">px2svg</a></em>
</div>
<script>
    console.clear();
    function each(obj, callback) {
        var length = obj.length,
            likeArray = (length === 0 || (length > 0 && (length - 1) in obj)),
            i = 0;

        if (likeArray) {
            for (; i < length; i++) {
                if (callback.call(obj[i], i, obj[i]) === false) {
                    break;
                }
            }
        } else {
            for (i in obj) {
                if (callback.call(obj[i], i, obj[i]) === false) {
                    break;
                }
            }
        }
    }
    function byteCount(s) {
        return encodeURI(s).split(/%..|./).length - 1;
    }

    function componentToHex(c) {
        var hex = c.toString(16);
        return hex.length == 1 ? "0" + hex : hex;
    }

    function getColor(r, g, b, a) {
        if (a === undefined || a === 255) {
            return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
        }
        if (a === 0) {
            return false;
        }
        return 'rgba(' + r + ',' + g + ',' + b + ',' + (a / 255) + ')';
    }
    // Optimized for horizontal lines
    function makePathData(x, y, w) {
        return ('M' + x + ' ' + y + 'h' + w + '');
    }

    function path(color, data) {
        return '<path stroke="' + color + '" d="' + data + '" />\n';
    }

    var canvas = document.createElement("canvas");
    var ctx = canvas.getContext("2d");

    var uploader = document.getElementById('upload');
    var outputDiv = document.getElementById('output');
    var outputRaw = document.getElementById('outputRaw');

    function processImage(src) {
        var img = new Image();

        img.onload = function () {
            var width = img.width;
            var height = img.height;

            canvas.width = width;
            canvas.height = height;
            ctx.drawImage(img, 0, 0);

            // viewBox starts at -0.5 to accomodate stroke's middle-origin to prevent having to include 0.5 on each path move
            var output = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -0.5 ' + width + ' ' + height + '" shape-rendering="crispEdges">\n';

            var colors = {},
                x = 0,
                y = 0,
                p, color;

            for (y = 0; y < height; y++) {
                for (x = 0; x < width; x++) {
                    p = ctx.getImageData(x, y, 1, 1).data;
                    color = getColor(p[0], p[1], p[2], p[3]);
                    if (color) {
                        colors[color] = colors[color] || [];
                        colors[color].push([x, y]);
                    }
                }
            }

            // Loop through each color
            each(colors, function (i, value) {
                if (i === false) {
                    return;
                }
                var paths = [];
                var curPath;
                var w = 1;

                // Loops through each color's pixels to optimize paths
                each(value, function () {

                    if (curPath && this[1] === curPath[1] && this[0] === (curPath[0] + w)) {
                        w++;
                    } else {
                        if (curPath) {
                            paths.push(makePathData(curPath[0], curPath[1], w));
                            w = 1;
                        }
                        curPath = this;
                    }

                });
                paths.push(makePathData(curPath[0], curPath[1], w)); // Finish last path

                output += path(i, paths.join(''));
            });

            output += '</svg>';

            outputDiv.innerHTML = '<em class="outputSize">Output size (bytes): ' + byteCount(output) + '</em>' + '<a href="data:Application/octet-stream,' + encodeURIComponent(output) + '" download="pixels.svg"><span class="download">Download SVG</span>' + output + '</a>';
            outputRaw.innerHTML = output.replace(/</g, '<').replace(/>/g, '>');
        }
        img.src = (src.target ? src.target.result : src);
    }

    function loadImage(e) {
        var reader = new FileReader();
        reader.onload = processImage;

        file = (e.target.files || uploader.files)[0];

        if (file) {
            reader.readAsDataURL(file);
        }
    }
    uploader.onclick = uploader.onchange = loadImage;
    var test = document.getElementById('test');
    var testImage = document.getElementById('testImage');
    test.onclick = function () {
        processImage(testImage.src);
    }
</script>

</body>
</html>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市朴下,隨后出現(xiàn)的幾起案子努咐,更是在濱河造成了極大的恐慌,老刑警劉巖殴胧,帶你破解...
    沈念sama閱讀 216,496評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件渗稍,死亡現(xiàn)場離奇詭異,居然都是意外死亡团滥,警方通過查閱死者的電腦和手機竿屹,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來灸姊,“玉大人拱燃,你說我怎么就攤上這事×撸” “怎么了碗誉?”我有些...
    開封第一講書人閱讀 162,632評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長父晶。 經(jīng)常有香客問我哮缺,道長,這世上最難降的妖魔是什么诱建? 我笑而不...
    開封第一講書人閱讀 58,180評論 1 292
  • 正文 為了忘掉前任蝴蜓,我火速辦了婚禮,結(jié)果婚禮上俺猿,老公的妹妹穿的比我還像新娘茎匠。我一直安慰自己,他們只是感情好押袍,可當(dāng)我...
    茶點故事閱讀 67,198評論 6 388
  • 文/花漫 我一把揭開白布诵冒。 她就那樣靜靜地躺著,像睡著了一般谊惭。 火紅的嫁衣襯著肌膚如雪汽馋。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,165評論 1 299
  • 那天圈盔,我揣著相機與錄音豹芯,去河邊找鬼。 笑死驱敲,一個胖子當(dāng)著我的面吹牛铁蹈,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播众眨,決...
    沈念sama閱讀 40,052評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼握牧,長吁一口氣:“原來是場噩夢啊……” “哼容诬!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起沿腰,我...
    開封第一講書人閱讀 38,910評論 0 274
  • 序言:老撾萬榮一對情侶失蹤览徒,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后颂龙,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體习蓬,經(jīng)...
    沈念sama閱讀 45,324評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,542評論 2 332
  • 正文 我和宋清朗相戀三年措嵌,在試婚紗的時候發(fā)現(xiàn)自己被綠了友雳。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,711評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡铅匹,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出饺藤,到底是詐尸還是另有隱情包斑,我是刑警寧澤,帶...
    沈念sama閱讀 35,424評論 5 343
  • 正文 年R本政府宣布涕俗,位于F島的核電站罗丰,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏再姑。R本人自食惡果不足惜萌抵,卻給世界環(huán)境...
    茶點故事閱讀 41,017評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望元镀。 院中可真熱鬧绍填,春花似錦、人聲如沸栖疑。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽遇革。三九已至卿闹,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間萝快,已是汗流浹背锻霎。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留揪漩,地道東北人旋恼。 一個月前我還...
    沈念sama閱讀 47,722評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像氢拥,于是被迫代替她去往敵國和親蚌铜。 傳聞我的和親對象是個殘疾皇子锨侯,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,611評論 2 353

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,322評論 0 10
  • 前言: 項目需求是這樣的:將網(wǎng)頁上面的外聯(lián)的svg圖像,變成內(nèi)嵌的svg圖像冬殃。從而我們可以寫一些css樣式控制這個...
    merrylmr閱讀 4,135評論 0 1
  • 前言: 之前做svg動畫的時候用到圖片轉(zhuǎn)svg path路徑囚痴,網(wǎng)上找了很多相關(guān)的, 也用過幾個在線轉(zhuǎn)換的網(wǎng)址审葬,...
    lpz0715閱讀 59,468評論 1 21
  • 一深滚、習(xí)慣的養(yǎng)成 1.啟動按鈕(為什么要養(yǎng)成這個習(xí)慣,內(nèi)在啟動和外在啟動) 2.行動—執(zhí)行既定的安排 3.激勵計劃—...
    獨上蘭舟_bac7閱讀 258評論 0 0
  • 這幾天一直在網(wǎng)上搜索:怎么賺到一百萬?事實上我也明白不可能找到方法官册!老外比較務(wù)實生兆,只會說可能賺到的行業(yè)!而國人膝宁,大...
    35歲的阿牛閱讀 160評論 0 0