【问题标题】:canvas image gets blurry画布图像变得模糊
【发布时间】:2017-03-30 19:32:02
【问题描述】:

https://jsfiddle.net/Abhi_Solanki/9yktct3z/3/ 这里小提琴链接..

我要做的是在以 PDF 格式或 SVG 格式导出时显示与 highcharts 相同的输出,事实上在这段代码中我有使用了用于生成 Highcharts 的 SVG 文件的相同函数,其输出对我来说似乎是正确的

但是当我将该变量传递到 canvg.js 文件中调用 canvg 时出现问题图像变得模糊

我希望输出保持不变,而像素不会变得模糊,因为它在 highcharts pdf 中是相同的。因为我必须在 pdf 文件中显示该图表图像,所以请帮忙。代码如下。


var svgres = chart.getSVG();
var svgArr = [],
			top = 0,
			height = 0,
			width = 0,
			col=0;
			svgCustDim = 400;
			var svgWidth = 600,
			svg = svgres.replace('<svg', '<g transform="translate(' + col * svgWidth + ',' + top  + ')" ');
			svg = svg.replace('</svg>', '</g>');
			svg = '<svg height="' + svgCustDim + '" width="' + svgCustDim*(3)+ '" version="1.1" xmlns="http://www.w3.org/2000/svg">' + svg + '</svg>';
canvg('canvas', svg);
var canvas = document.getElementById('canvas');    
return canvas.toDataURL("image/jpeg");

【问题讨论】:

    标签: highcharts html2canvas pdfmake


    【解决方案1】:

    github 上演示页面中使用的 canvg 版本有问题。如果我从这里https://github.com/canvg/canvg/blob/master/canvg.js 获取脚本并将其粘贴到小提琴中,它可以正常工作。

    你可以在这里https://github.com/canvg/canvg/issues/486阅读更多信息。

    另外你的画布比图片大很多,你可以取图表的宽度和高度,使画布与图表的尺寸相同。

    svgCustDim = chart.chartHeight;
    var svgWidth = chart.chartWidth,
    svg = svgres.replace('<svg', '<g');
    svg = svg.replace('</svg>', '</g>');
    svg = '<svg height="' + svgCustDim + '" width="' + svgWidth+ '" version="1.1" xmlns="http://www.w3.org/2000/svg">' + svg + '</svg>';
    

    这里是从上面的 url 中提取的 canvg.js 和更改,以便您的画布与图表的尺寸相同 https://jsfiddle.net/9yktct3z/6/

    请注意,缩小版 (canvg.min.js) 似乎是

    如果您想要更高分辨率的图像,请从图表容器中删除高度和宽度

    <div id="container" style="margin: 0 auto"></div>
    

    并在图表选项中设置它们,我使用 2000*2000 但你玩弄这些值

    chart: {
        height: 2000,
        width: 2000,
        plotBackgroundColor: null,
        plotBorderWidth: null,
        plotShadow: false,
        type: 'pie'
    },
    

    这是完整的小提琴https://jsfiddle.net/9yktct3z/10/

    【讨论】:

    • 谢谢@LiviuBoboia,它很有帮助.. 还有一件事我想问的是,您知道在 PDF 文件中显示图像的任何方法并且它不会变得模糊,因为仍然使用这个新的canvg文件JPEG或PNG图像变得模糊,而在SVG分辨率和像素一切都保持不变......

      如果有任何解决方案,这将是非常有帮助的......

      谢谢。
    • 如何添加生成图像?因为如果我将画布的结果放在图像中,它看起来很好jsfiddle.net/9yktct3z/7
    • 首先我生成 SVG,然后将该数据传递到 canvg 以创建画布,然后他们通过 toDataURL 方法获取图像 URL,我使用 pdfmake.min.js 生成 PDF,默认情况下我使用图像标签以在其中显示图像。
    • var svgres = chart.getSVG(); var svgArr = [], top = 0, height = 0, width = 0, col=0; svgCustDim = 400; var svgWidth = 600, svg = svgres.replace('&lt;svg', '&lt;g transform="translate(' + col * svgWidth + ',' + top + ')" '); svg = svg.replace('&lt;/svg&gt;', '&lt;/g&gt;'); svg = '&lt;svg height="' + svgCustDim + '" width="' + svgCustDim*(3)+ '" version="1.1" xmlns="http://www.w3.org/2000/svg"&gt;' + svg + '&lt;/svg&gt;'; canvg('canvas', svgres); var canvas = document.getElementById('canvas'); return canvas.toDataURL("image/svg+xml"); }
    • 代码在你原来的小提琴中,请创建一个生成pdf的小提琴
    猜你喜欢
    • 1970-01-01
    • 2013-07-25
    • 2021-02-19
    • 1970-01-01
    • 2015-11-01
    • 2011-08-04
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多