将内联svg保存为图像文件

时间:2015-07-11 16:53:03

标签: html svg onclick png inline

我有一个内联SVG代码,根据用户对colors / css的选择进行更改。

我希望能够将行svg保存为外部文件onclick(svg或png或jpeg)。

示例:http://jsfiddle.net/WebbySmart/fof8jqxx/

<svg height="100" width="100">
  <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
</svg>

<div style="clear:both;"></div>
<br>
<button>Save Image</button>

有没有办法做到这一点?非常感谢 - 谢谢。

1 个答案:

答案 0 :(得分:1)

找到一个完美的解决方案。

http://jsfiddle.net/LznLjxq7/

var btn = document.querySelector('button');
var svg = document.querySelector('svg');
var canvas = document.querySelector('canvas');

function triggerDownload (imgURI) {
  var evt = new MouseEvent('click', {
    view: window,
    bubbles: false,
    cancelable: true
  });

  var a = document.createElement('a');
  a.setAttribute('download', 'MY_COOL_IMAGE.png');
  a.setAttribute('href', imgURI);
  a.setAttribute('target', '_blank');

  a.dispatchEvent(evt);
}

btn.addEventListener('click', function () {
  var canvas = document.getElementById('canvas');
  var ctx = canvas.getContext('2d');
  var data = (new XMLSerializer()).serializeToString(svg);
  var DOMURL = window.URL || window.webkitURL || window;

  var img = new Image();
  var svgBlob = new Blob([data], {type: 'image/svg+xml;charset=utf-8'});
  var url = DOMURL.createObjectURL(svgBlob);

  img.onload = function () {
    ctx.drawImage(img, 0, 0);
    DOMURL.revokeObjectURL(url);

    var imgURI = canvas
        .toDataURL('image/png')
        .replace('image/png', 'image/octet-stream');

    triggerDownload(imgURI);
  };

  img.src = url;
});

Stright javascript

#include <iostream>
#include <string>
using namespace std;
int n(char s)
{
    switch (s)
    {
    case '0':
        return 0;
    case '1':
        return 1;
    case '2':
        return 2;
    case '3':
        return 3;
    case '4':
        return 4;
    case '5':
        return 5;
    case '6':
        return 6;
    case '7':
        return 7;
    case '8':
        return 8;
    case '9':
        return 9;
    }
}
int getnumber(string a)
{
    string num = "";
    for (int i = 0; i < a.size(); i++)
    {
        if (isdigit(a[i]))
            num += a[i];
    }
    if (num.size() == 1)
    {
        return n(num[0]);
    }
    if (num.size() == 2)
    {
        return (n(num[0]) * 10) + (n(num[1]));
    }

    if (num.size() == 3)
    {
        return (n(num[0]) * 100) + (n(num[1]) * 10) + (n(num[2]));
    }
}
bool matrix2(long  int**p, int r)
{

    int c = 0, u = 0, ss = 0;
    for (int i = 0; i < r; i++)
    {
        for (int j = 0; j < r; j++)
        {
            if (p[i][j]<0)
                return false;
        }
    }
    long  int*arrL = new long  int[r];
    long  int*arrR = new long  int[r];
    for (int i = 0; i < r; i++)
    {
        for (int j = 0; j < i; j++)
        {
            arrL[c++] = p[i][j];
            ss++;
        }
    }   
    for (int i = 0; i < r; i++)
    {
        for (int j = i + 1; j < r; j++)
        {
            arrR[u++] = p[i][j];
        }
    }
    for (int i = 0; i < ss; i++)
    {
        int q = ss - i - 1;
        long  int a = arrR[i];
        long  int b = arrL[q];
        if (!(a == b))
        {
            delete[]arrL;
            delete[]arrR;
            return false;
        }
        }


    delete[]arrL;
    delete[]arrR;
    return true;
}
int main()
{
    int t;
    cin >> t;
    for (int num = 0; num < t;num++)
    {

        int yy = num + 1;
        string dimension;
        cin.ignore();
        getline(cin, dimension);
        int r = getnumber(dimension);
        long  int**p = new  long int*[r];
        for (int w = 0; w < r; w++){
            p[w] = new  long int[r];
        }
        for (int w = 0; w < r; w++)
        {
            for (int ww = 0; ww < r; ww++)
            {
                cin >> p[w][ww];
            }
        }
    bool result = matrix2(p, r);
        if (result)
        {
            cout << "Test #" << yy << ": Symmetric." << endl;
        }
        else
        {
            cout << "Test #" << yy << ": Non-symmetric." << endl;
        }
        for (int i = 0; i < r; i++)
        {
            delete[]p[i];
        }
        delete[] p;
    }

    return 0;

}

参考来自: Save inline SVG as JPEG/PNG/SVG