使用<canvas>

时间:2015-08-26 15:35:13

标签: javascript canvas svg

我尝试将大型SVG(它的数据网址大约为750000 - 1000000个字符)转换为PNG,方法是将其数据网址传递到图片并放入画布中,但是图像只加载大约1/4的SVG。

通过创建:

var svg_xml = (new XMLSerializer()).serializeToString(svg),
    url = 'data:image/svg+xml;base64,' + btoa(svg_xml);

var img = new Image();
img.width = 730;
img.height = 300;
img.onload = function(){
    var canvas = document.create('canvas');
    canvas.width = 730;
    canvas.height = 300;

    var ctx = canvas.getContext('2d');
    ctx.drawImage(img, 0, 0, 730, 300);

    callbackFn(canvas.toDataURL('image/png');
}
img.src = url

修改

我尝试使用canvg绘制SVG,但DataURL生成的结果为空白图像:

var svg_xml = (new XMLSerializer()).serializeToString(svg);
var canvas = document.createElement('canvas');
canvas.width = 730;
canvas.height = 300;

var ctx = canvas.getContext('2d');
ctx.drawSvg(svg_xml, 0, 0, 730, 300);
callbackFn(canvas.toDataURL('image/png');

我使用的方法有什么问题吗?

进一步修改

我现在相当确信画布无法绘制整个图像,因为我尝试实现Blob解决方案同样的效果:

var svg_xml = (new XMLSerializer()).serializeToString(svg),
    blob = new Blob([svg_xml], {type:'image/svg+xml;charset=utf-8'}),
    url = window.URL.createObjectURL(blob);

var img = new Image();
img.width = 730;
img.height = 300;
img.onload = function(){
    var canvas = document.create('canvas');
    canvas.width = 730;
    canvas.height = 300;

    var ctx = canvas.getContext('2d');
    ctx.drawImage(img, 0, 0, 730, 300);

    window.URL.revokeObjectURL(url);
    callbackFn(canvas.toDataURL('image/png');
}
img.src = url   

图像再次加载正常并转到URL(在它被撤销之前)也会显示图像。

画布dataURL的长度不一致,所以我不认为最大化,有没有办法检测画布大小?该应用程序仅支持Chrome和FireFox。

1 个答案:

答案 0 :(得分:2)

Working fiddle.

HTML:

    <div>
        <svg xmlns="http://www.w3.org/2000/svg"
              width="526" height="233">
          <rect x="13" y="14" width="500" height="200" rx="50" ry="100"
              fill="none" stroke="blue" stroke-width="10" />
        </svg>
        <a id='imgId'>Save</a>
    </div>
    <canvas></canvas>

JavaScript的:

var svg = document.getElementsByTagName('svg')[0];
var svg_xml = (new XMLSerializer()).serializeToString(svg),
    blob = new Blob([svg_xml], {type:'image/svg+xml;charset=utf-8'}),
    url = window.URL.createObjectURL(blob);

var img = new Image();
img.width = 730;
img.height = 300;
img.onload = function(){
    var canvas = document.createElement('canvas');
    canvas.width = 730;
    canvas.height = 300;

    var ctx = canvas.getContext('2d');
    ctx.drawImage(img, 0, 0, 730, 300);

    window.URL.revokeObjectURL(url);
    var canvasdata = canvas.toDataURL('image/png');
    var a = document.getElementById('imgId');
    a.download = "export_" + Date.now() + ".png";
    a.href=canvasdata;   
}
img.src = url

您的问题:

为什么图像只加载大约1/4的SVG?

<强>答案:

请遵循以下规则:

    {li> x <rect/> + width <rect/>&lt; = width <svg/> {li> y <rect/> + height <rect/>&lt; = height <svg/>

    例如:

    <svg xmlns="http://www.w3.org/2000/svg" width="500" height="200">
         <rect x="10" y="10" width="550" height="250" rx="50" ry="100"
                      fill="none" stroke="blue" stroke-width="10" />
    </svg>
    

    此处x = <rect/>的{​​{1}},宽度10 = <rect/>,宽度550 = <svg>

    所以,500 + 10&gt; 550

    在这种情况下,图像将被部分渲染。

    希望这能解决您的问题。