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

Text tags in SVG overlap (duplicated / doubling) #541

Closed
hyojin opened this issue Feb 19, 2015 · 24 comments
Closed

Text tags in SVG overlap (duplicated / doubling) #541

hyojin opened this issue Feb 19, 2015 · 24 comments

Comments

@hyojin
Copy link
Contributor

hyojin commented Feb 19, 2015

(Sorry, I am not good at english)

Hello,
I am using this awesome script to render SVG tags
There is some text overlapping when I render the text tag in SVG to canvas
Text tag be processed as a Text node and also SVG
You can see that easily with transform property
2015-02-19 15 08 29_2
Here is sample code (with html2canvas 0.5.0-alpha2)

<html>
<head>
  <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
  <script type="text/javascript" src="js/html2canvas.js"></script>
</head>
<body>
  <svg width="100" height="100">
    <g transform="translate(20,20)">
      <text dy=".45em" y="10" x="0" dx="-.8em" transform="translate(0,0) rotate(-70)" style="font-size:12px;">Text</text>
    </g>
  </svg>
  <script>
    $(document).ready(function() {
      html2canvas(document.body, {
        onrendered: function(canvas) {
          document.body.appendChild(canvas);
        }
      });
    })
  </script>
</body>
</html

And Here is my quick change

NodeParser.prototype.getChildren = function(parentContainer) {
    return flatten([].filter.call(parentContainer.node.childNodes, renderableNode).map(function(node) {
        var container = [node.nodeType === Node.TEXT_NODE && node.parentElement.tagName !== "text" ? new TextContainer(node, parentContainer) : new NodeContainer(node, parentContainer)].filter(nonIgnoredElement);
        return node.nodeType === Node.ELEMENT_NODE && container.length && node.tagName !== "TEXTAREA" ? (container[0].isElementVisible() ? container.concat(this.getChildren(container[0])) : []) : container;
    }, this));
};

I hope this will help

@josnabattula
Copy link

Thank you#Hyojin. That helps me.

hyojin added a commit to hyojin/html2canvas that referenced this issue Jun 23, 2015
hyojin added a commit to hyojin/html2canvas that referenced this issue Jun 23, 2015
hyojin added a commit to hyojin/html2canvas that referenced this issue Jun 23, 2015
@natee
Copy link

natee commented Jul 23, 2015

Thanks ,help me a lot!

ncoquelet pushed a commit to ncoquelet/html2canvas that referenced this issue Aug 4, 2015
@ncoquelet
Copy link

Thanks for your fix !

you can also check on tspan tagName :
node.nodeType === Node.TEXT_NODE && node.parentElement.tagName !== "text" && node.parentElement.tagName !== "tspan"

@hyojin
Copy link
Contributor Author

hyojin commented Aug 5, 2015

@ncoquelet I updated my code. Thanks!

@ncoquelet
Copy link

@hyojin
After test it on IE, your fix doesn't works because parentElement property is not populate on the current node. Use parentNode instead ! In any case, it will be more clear because we are into the NodeParser and it talk about node.

Otherwise, instead test on tag name, i change my code to test the instance of parentNode and process the current node has a TextContainer only if the parentNode isn't a SVGElement.

It works on chrome and ie11. on firefox i can't test it because of blank canvas bug !

My code

NodeParser.prototype.getChildren = function(parentContainer) {
    return flatten([].filter.call(parentContainer.node.childNodes, renderableNode).map(function(node) {
        var container = [node.nodeType === Node.TEXT_NODE && !(node.parentNode instanceof SVGElement) ? new TextContainer(node, parentContainer) : new NodeContainer(node, parentContainer)].filter(nonIgnoredElement);
        return node.nodeType === Node.ELEMENT_NODE && container.length && node.tagName !== "TEXTAREA" ? (container[0].isElementVisible() ? container.concat(this.getChildren(container[0])) : []) : container;
    }, this));
};

I hope this helps.

@ncoquelet
Copy link

fix my firefox problem, works on ff too :-)

ncoquelet pushed a commit to ncoquelet/html2canvas that referenced this issue Aug 7, 2015
@hyojin
Copy link
Contributor Author

hyojin commented Aug 12, 2015

More clear!
I also checked that it works well on ff.
I merged your fix and pull request was updated. Thanks :)

@berendberendsen
Copy link

berendberendsen commented Jul 17, 2016

@hyojin @ncoquelet thanks for the investigation into this and the fix. I ran into the exact same problem the other day and I wasn't entirely sure what was causing it.

@Suchetana10
Copy link

I am not sure where I am getting confused here but I am getting the exception as "Uncaught ReferenceError: NodeParser is not defined"

@hyojin
Copy link
Contributor Author

hyojin commented Aug 5, 2016

@Suchetana10 When did you get the exception? in loading script?

@vishwasvadavi
Copy link

@hyojin thank you.. worked for me

@nugen
Copy link

nugen commented Feb 13, 2017

@hyojin thanks! works perfectly for me too

@hyojin
Copy link
Contributor Author

hyojin commented Feb 14, 2017

@vishwasvadavi @nugen I'm happy to hear that! :)

@hyojin hyojin closed this as completed Feb 14, 2017
@hyojin hyojin changed the title Text tag in SVG overlaps Text tags in SVG overlap (duplicated / doubling) Feb 14, 2017
@hyojin
Copy link
Contributor Author

hyojin commented Feb 14, 2017

Some people still have the issue with text element, I change this issue opened and I will send PR again.

@hyojin hyojin reopened this Feb 14, 2017
@seemasingh123
Copy link

<script type="text/javascript"> $(function(){ $('#PDFTABLE11').mouseover(function(){ div_content = document.querySelector("#container2") html2canvas(div_content).then(function(canvas) { data = canvas.toDataURL('image/png?'); save_img(data); }); }); }); function save_img(data){ $.post('save_jpg.php', {data: data}, function(res){ if(res != ''){ if(yes){ location.href =phe.php; } } else{ alert('something wrong'); } }); } </script>

image

@theinventor
Copy link

Does anyone have a build of the library with this fix in it that I can test? Seeing same issue on highcharts page..

@Nywlem11
Copy link

Nywlem11 commented Aug 2, 2017

Is this build available with the fix? I am still facing this issue with highcharts.

@sgsmittal
Copy link

Is this fix available in build or we have to create an override ?

@niklasvh
Copy link
Owner

Is this still an issue with v1.0.0? If so, could you please share an example on jsfiddle.

@no-response no-response bot closed this as completed Dec 12, 2017
@no-response
Copy link

no-response bot commented Dec 12, 2017

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.

@karannagupta
Copy link

karannagupta commented Sep 10, 2019

Dear @niklasvh ,

Thank you for this amazing library. I am able to reproduce this issue with the latest version - v1.0.0-rc.3

I have created an example on CodePen here (Chrome only) where you will see that some of the text nodes (in the Graph title) are duplicated.

Steps to reproduce -

  1. Please open the CodePen link - https://codepen.io/karannagupta/pen/RXpddB in Chrome only
  2. Click on Copy to Clipboard button
  3. Wait for the animation to finish (or see console for copied message)
  4. Paste the graph in paint or a document (ctrl + v)

The graph titles are added on Line#193

@hyojin , would you please open the issue again?

Regards,
Karan


text-duplication

@hyojin
Copy link
Contributor Author

hyojin commented Sep 10, 2019

Hi @karannagupta
Could you try my branch to check whether it comes from the same reason?

@karannagupta
Copy link

Hi @hyojin

With your version of the library (v0.5.0-alpha2) it doesn't work. You can test your library on my Pen here

Also, I would like to use the latest version of html2canvas. I also don't need to use html2canvas.svg then.

Regards,
Karan

@karannagupta
Copy link

Turns out it is an issue with Unicode fonts

image

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

No branches or pull requests