tsayen / dom-to-image

Generates an image from a DOM node using HTML5 canvas
Other
10.32k stars 1.68k forks source link

Position:Absolute not being proccesed #147

Open DanielZambranoC opened 7 years ago

DanielZambranoC commented 7 years ago

Trying to print a result from JqueryOrgChart

domtoimg

Even better than html2canvas, but position absolute not being interpreted

shamahoque commented 7 years ago

I ran into a similar problem with the position:absolute elements not being rendered accurately in the image.

Explicitly declaring position:relative on the outer/parent element solved the problem for me.

Example:

.outer {
    position: relative;
}
.inner {
    position: absolute;
}
<div class="outer">
  <div class="inner">...</div>
  <div class="inner">...</div>
</div>
narayanachitrakars commented 6 years ago

Same issue occuring from my side any one can help ?

lawsumisu commented 6 years ago

Under the hood, this library uses window.getComputedStyle() to set the style of the DOM Element you are converting. I believe the issue is that when you set position: absolute, window.getComputedStyle() will also set left, right, bottom, top to have pixel values based on where the element would be positioned by default. This can cause your image to basically be rendered "off screen" if the pixel values are too off. You can overwrite this style via the options param by doing (as an example):

options.style = {
  left: '0',
  right: '0',
  bottom: '0',
  top: '0'
}

I had a similar problem, and those are the settings I used to fix it, but if those don't work, I would consider doing calling getComputedStyle() on your element to see what the positioning is, and from there figure out what they should be, and then use options.style to overwrite them to get a nice image.

terry623 commented 5 years ago

@shamahoque Thanks for your answer It works for me!

stellarsailor commented 3 years ago

@shamahoque Thank you so much!!!

benpinchas commented 2 years ago

For me the problem was that the children were positioned absolute and we placed them on the relative parent with trasnfrom: translate(100px, 100px)

So I had to iterate over these child and replace for example: trasnfrom: translate(100px, 100px) to: left: 100px top: 100px

This fixed my issue