简体   繁体   English

Canvas导出到png不会导出backgroundImage

[英]Canvas export to png does not export backgroundImage

I am trying to export my final render of canvas everything works just fine exception when i set an SVG backgroundImage is completely ignored on export. 我试图导出我的最终渲染画布一切正常,当我设置SVG backgroundImage导出时完全忽略。

This is how i set the background image 这就是我设置背景图片的方式

fabric.Object.prototype.transparentCorners = true;
fabric.loadSVGFromURL(svg, function (objects, options) {        
    var group = fabric.util.groupSVGElements(objects, options);
    group.set({
        left: 0,
        top: 0,
        scaleY: 1.2,
        scaleX: 1.2,
        fill : front_color
    });
    //canvas.add(group);
    canvas.setBackgroundImage(group, canvas.renderAll.bind(canvas), {
        backgroundImageOpacity: 0.5,
        backgroundImageStretch: true,
        color: front_color,
        repeat: 'repeat'
    });
    canvas.setBackgroundColor(fill_color);
    canvas.renderAll();
});

and this is how i export everything 这就是我出口一切的方式

window.open(canvas.toDataURL()); 

Your example works fine for me in Chrome & Firefox, but fails in Internet Explorer. 您的示例在Chrome和Firefox中适用于我,但在Internet Explorer中失败。

Chrome and Firefox are fine with exporting SVG drawings on the canvas. Chrome和Firefox可以在画布上导出SVG图纸。

Internet Explorer will turn off canvas.toDataURL if you draw SVG to the canvas. 如果将SVG绘制到画布,Internet Explorer将关闭canvas.toDataURL

This is for security reasons--to prevent stealing user's info by exporting their secret info drawn on a canvas. 这是出于安全原因 - 通过导出在画布上绘制的秘密信息来防止窃取用户的信息。 This restriction may be lifted in new version of IE, but for now in IE you can't draw SVG to the canvas and then export the canvas with .toDataURL . 这个限制可能会在IE的新版本中解除,但是现在在IE中你无法将SVG绘制到画布上,然后使用.toDataURL导出画布。

The simplest workaround is to convert your SVG background into .png or .jpg format. 最简单的解决方法是将SVG背景转换为.png或.jpg格式。 Internet Explorer will allow .toDataURL if the background image is in those formats. 如果背景图像采用这些格式,Internet Explorer将允许.toDataURL

[ Addition per comment about resolution ] [关于决议的评论]

Webpage images (& everything on the page viewport) are always denoted in pixels. 网页图像(以及页面视口中的所有内容)始终以像素表示。

If you want to export the canvas for printing (which needs a higher pixel count than your on-screen canvas) then you can use FabricJS's toDataURLWithMultiplier method which increases the amount of pixels on the exported dataURL. 如果要导出用于打印的画布(需要比屏幕画布更高的像素数),则可以使用FabricJS的toDataURLWithMultiplier方法,这会增加导出的dataURL上的像素数量。 It works by taking a single pixel and creating a 2x2 set of the same pixel (or 3x3, etc). 它的工作原理是采用单个像素并创建相同像素的2x2集(或3x3等)。 Link about toDataURLWithMultiplier : http://fabricjs.com/docs/fabric.StaticCanvas.html . 关于toDataURLWithMultiplier链接: httptoDataURLWithMultiplier

That would let you substitute a larger .png image for your SVG image. 这样您就可以用更大的.png图像替换SVG图像。 As noted previously, using .png instead of SVG is currently the best cross-browser solution. 如前所述,使用.png而不是SVG是目前最好的跨浏览器解决方案。 ;-) ;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM