繁体   English   中英

html2canvas等待图像加载

[英]html2canvas wait for images to load

我有一段时间了这个问题,我似乎找不到解决办法。

我正在使用最新的html2canvas js插件来拍摄用flotcharts绘制的图表的屏幕截图,然后通过隐藏的输入提交base64屏幕截图。 问题是图表的div也有一些图像,并且html2canvas在图像加载前返回了base64字符串。 我可以将setTimeout放在提交上,直到它们加载完毕,但显然chrome打开我提交的页面作为弹出窗口(这真的不行,因为我们的客户不知道如何允许弹出窗口)。 所以这就是我尝试过的,但是图像没有预加载(由于html2canvas的异步特性)

function getPNGBase64forHtml($container) {
    var imageString;

    html2canvas($container, {
        useCORS: true,
        onrendered: function(canvas) {
            imageString = canvas.toDataURL('image/png');
        }
    });

    return imageString;
}

而且这(这可以,但是不能及时加载图像):

function getPNGBase64forHtml($container) {
    var h2canvas = html2canvas($container);
    var queue = h2canvas.parse();
    var canvas = h2canvas.render(queue);

    return canvas.toDataURL('image/png');
}

因此,问题在于等待图像加载到html2canvas中,然后执行其余的东西。 如果有人可以帮助,将不胜感激,我向您致敬的先生和女士们! :)

编辑

这是我捕获的部分的html,所有这些都在打印容器div中。 时间轴预测中的箭头(仅显示一个)没有被捕获,因为它是图像,其他所有操作都可以:

<div id="timeline-outer-container">
    <div id="timeline-container" class="flot-container">
        <div id="timeline-chart" class="flot-chart">
            <canvas class="flot-base" width="888" height="335" ></canvas>
            <div class="flot-text" >
                <div class="flot-x-axis flot-x1-axis xAxis x1Axis">
                    <div class="flot-tick-label tickLabel" >Q1</div>
                    <div class="flot-tick-label tickLabel">Q2</div>
                    ...
                </div>
                <div class="flot-y-axis flot-y1-axis yAxis y1Axis">
                    <div class="flot-tick-label tickLabel">75</div>
                    <div class="flot-tick-label tickLabel">100</div>
                    ...
                </div>
            </div>
            <canvas class="flot-overlay" width="888" height="335"></canvas>
            <div class="axis-label xaxis">Zeitraum</div>
            <div class="axis-label yaxis rotate-90">Anzahl</div>
            <div id="zoom-out-button" class="timeline-zoom-button"><i class="fa fa-zoom-out"></i></div>
            <div id="zoom-in-button" class="timeline-zoom-button"><i class="fa fa-zoom-in"></i></div>
            <div id="zoom-default-button" class="timeline-zoom-button"><i class="fa fa-repeat"></i></div>
        </div>
    </div>
</div>

<div id="timeline-prognose">
    <img id="timeline-arrow-up" class="timeline-arrows" src="/portal//images/arrows/up.png" alt="">
    <img id="timeline-arrow-down" class="timeline-arrows" src="/portal//images/arrows/down.png" alt="">
</div>

因此,由于您使用的是远程图像,因此可以使用以下修复程序在脚本中进行一些修改:

function getPNGBase64forHtml() {
    var imageString;

    html2canvas(document.body, {
        useCORS: true,
        logging : true, //Enable log (use Web Console for get Errors and Warnings)
        proxy :"html2canvasproxy.php",
        onrendered: function(canvas) {
            var img = new Image();
            img.onload = function() {
                img.onload = null;
                document.body.appendChild(img);
            };

            img.onerror = function() {
                img.onerror = null;
                if(window.console.log) {
                    window.console.log("Not loaded image from canvas.toDataURL");
                } else {
                    alert("Not loaded image from canvas.toDataURL");
                }
            };

            imageString = canvas.toDataURL('image/png');
        }
    });

    return imageString;
}

如果您使用的是php,则代理设置必须使用以下脚本: html2canvas-php-proxy

否则,对于.NET项目,您可以使用以下脚本资源:

带有asp-classic的 html2canvas代理-vb带有asp.net的html2canvas代理-csharp

如果您决定使用本地图像,则不会出现此错误。

希望它对您有用,这是此html2canvas错误的原始线程https://github.com/niklasvh/html2canvas/issues/145

暂无
暂无

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

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