如何在截图前删除某些元素?

时间:2013-12-15 01:01:38

标签: html2canvas

我可以使用以下示例代码截取页面:

html2canvas(document.body, {
  onrendered: function(canvas) {
    document.body.appendChild(canvas);
  }
});

现在有一些div我在拍摄截图时不想成为页面的一部分吗? 如何防止它们成为屏幕截图的一部分。

我认为克隆元素然后删除元素的一种方法,但是获取克隆的屏幕截图会产生白色屏幕。这是我使用的代码:

html2canvas($(document.body).clone()[0], {
  onrendered: function(canvas) {
    document.body.appendChild(canvas);
  }
});

4 个答案:

答案 0 :(得分:42)

将此属性data-html2canvas-ignore添加到处理屏幕截图时不想拍摄的任何元素。

希望这会对下一个人有所帮助。

答案 1 :(得分:1)

使用该库时,我遇到一个问题,即lib下载了应用程序中的所有图像,这导致应用程序运行缓慢。我使用ignoreElements选项解决了该问题。 这是我的代码:

  var DropAreaElement= document.getElementById("123");
    var config= {
        useCORS: true,
        ignoreElements: function (element) {
            if (element.contains(DropAreaElement) || element.parentElement.nodeName =="HTML" || element == DropAreaElement || element.parentNode == DropAreaElement) {
                console.log("elements that should be taken: ", element)
                return false;
            }else {
                return true;
            }
        }
    };
    html2canvas(DropAreaElement, config).then(function (canvas){
        var imgBase64 = canvas.toDataURL('image/jpeg', 0.1);
        console.log("imgBase64:", imgBase64);
        var imgURL = "data:image/" + imgBase64;
        var triggerDownload = $("<a>").attr("href", imgURL).attr("download", "layout_" + new Date().getTime() + ".jpeg").appendTo("body");
            triggerDownload[0].click();
            triggerDownload.remove();
    }).catch(Delegate.create(this, function (e){
        console.error("getLayoutImageBase64 Exception:", e);
    });

答案 2 :(得分:0)

如果您不想使用属性,html2canvas确实提供了一种删除元素的方法。例如:

html2canvas( document.body, {
    ignoreElements: function( element ) {
        
        /* Remove element with id="MyElementIdHere" */
        if( 'MyElementIdHere' == element.id ) {
            return true;
        }
        
        /* Remove all elements with class="MyClassNameHere" */
        if( element.classList.contains( 'MyClassNameHere' ) ) {
            return true;
        }
        
    }
} ).then( function( canvas ) {
    document.body.appendChild( canvas );
} );

有关更多信息,请参见html2canvas options

答案 3 :(得分:0)

您可以为 HOC<Printable/> 创建 <NonPrintable/> ,您可以使用 <NonPrintable><YourCoolComponent/></NonPrintable> 包装您的组件 这些子组件将被排除。


import React from "react"

interface INonPrintable {
  children: React.ReactChildren
}

/*
HOC - Printable which injects the printId to the React component
which gets us Printable Context to html2canvas => jsPDF
eg:
 <Printable printId="about-you-print">
    <PersonalInfo badEmail={badEmail} />
    <IdentityInfo />
    <AdditonalInfo />
    <AddressInfo
     serviceAddress={serviceAddress}
     billingAddress={this.state.billingAddress}
     setBillingAddress={this.setBillingAddress}
    />
</Printable>

*/
export default function Printable({ printId = "", children, ...restProps  }) {
  return <div print-id={printId} {...restProps}>{children}</div>
}


/*
HOC - NONPrintable which injects the data-html2canvas-ignore to the React component
which gets us Printable Context to html2canvas => jsPDF
eg:
<NonPrintable style={{display:"flex",justifyContent:'space-around'}}>
  <Button
    text="Print PDF using Own utility"
    onClick={this.handlePrintPdf}
    />
    <Button
    text="Print PDF using html2canvas + jsPDF"
    onClick={this.handlePrintwithPDFjs}
    />
</NonPrintable>
*/

export const NonPrintable = ({ children, ...restProps }) => {
  return <div data-html2canvas-ignore {...restProps}>{children}</div>
}