简体   繁体   English

HTML2Canvas-如何捕获所有div?

[英]Html2canvas - how to capture all the div?

I'm using html to canvas for capture div Here is my code : 我使用html画布捕获div这是我的代码:

 $scope.testCanvas = function()
            {
    html2canvas([document.getElementById('test')], {
                          onrendered: function(canvas) {
                              $("#divtest").append(canvas);
                          }
                        });
                });
}

    <div id="test">
    <article id="art2" class="p-Fitness p-Tile u-md-spaceTop p-FitnesssecurityPatch" >
        <section class="p-Tile-section">
            <div class="p-Fitness-impact"></div>
            <div class="p-Fitness-securityGraph"></div>
        </section>
   </article>
   <article id="art2" class="p-Fitness p-Tile u-md-spaceTop p-Fitness-securityPatch" >
        <section class="p-Tile-section">
            <div class="p-Fitness-impact"></div>
            <div class="p-Fitness-securityGraph"></div>
        </section>
    </article>
    </div>

    <div id="divtest">
    </div>

The issue is that just a part of the div is captured the part that visible in the screen - as screenshot . 问题在于,仅捕获了div的一部分,即屏幕上可见的部分-作为屏幕截图。 I need to capture all the div - any idea ? 我需要捕捉所有的div-有什么想法吗?

You are just accessing the div with Id which will give you only a single element. 您只是使用Id访问div,这只会给您一个元素。 To capture all the divs you can use class selector . To capture all the divs you can use class selector add a class to all your divs let say class = 'test' . 向您的所有div添加一个类,让我们说class = 'test' then you can do this. 那么你可以做到这一点。

html2canvas(document.getElementsByClassName('test'), {....

So what you are doing is replacing this 所以,您正在做的是替换这个

[document.getElementById('test')] -- array with one element

With

  document.getElementsByClassName('test')  -- array of selected elements.

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

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