Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Element styling only for html2canvas? #701

Closed
thesciz opened this issue Oct 19, 2015 · 6 comments
Closed

Element styling only for html2canvas? #701

thesciz opened this issue Oct 19, 2015 · 6 comments
Labels

Comments

@thesciz
Copy link

thesciz commented Oct 19, 2015

I'm using the handy data-html2canvas-ignore tag on a few elements, but Firefox is giving me problems with elements with negative margins. I'd like to be able to add an html2canvas-only style tag for a particular element, such as: data-html2canvas-style="margin-left: 0".

Has anyone else had any luck doing something like this?

@niklasvh
Copy link
Owner

You can modify the contents of the document (which is a clone of the original) that will get rendered with the onclone option, for example:

html2canvas(document.body, {onclone: function(document) {
  document.querySelector('.something').style.marginLeft = 0;
}}).then(...)

@thesciz
Copy link
Author

thesciz commented Oct 20, 2015

Thanks Nik! Greatly appreciate your work on this library. Is this in the documentation?

@niklasvh
Copy link
Owner

No, unfortunately not. The documentation is greatly lacking in many respects at the moment...

@saksham-sg01
Copy link

My downloaded image isn't getting the proper styling so I'm using computed-style-to-inline-style to render it properly by converting all the styling to inline css. But the issue with that is it's affecting the original dom. So I want to do it for the cloned element instead inside the onclone function but it's not working and the image isn't properly styled. Any idea why it's happening?

@akyenze
Copy link

akyenze commented Jun 27, 2019

I tried to do the same thing, here is how I implemented it and all my styles were properly downloaded:

const computedStyleToInlineStyle = require("computed-style-to-inline-style");

 html2canvas(document.querySelector(".someclass"), {onclone: function(document) {
     computedStyleToInlineStyle(document.querySelector(".someclass"), {
          recursive: true
      });
    }}).then(canvas => {
        const imgData = canvas.toDataURL('image/png');
        ...
    });

@reslear
Copy link

reslear commented Jun 5, 2023

another way, my solution:

add to element for example :
data-html2canvas-class="!transform-none"

      onclone: (document) => {
        const els = document.querySelectorAll(
          '[data-html2canvas-class]',
        ) as NodeListOf<HTMLElement>

        els.forEach((el) => {
          const payloadClasses = el.getAttribute('data-html2canvas-class')
          if (payloadClasses) {
            el.className = [el.className.trim(), payloadClasses.trim()].join(
              ' ',
            )
          }
        })
      },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants