简体   繁体   English

html2canvas 不适用于伪元素

[英]html2canvas not working for pseudo element

I am working on saving image of div.我正在保存 div 的图像。 but div has pseudo element , however i come to know that html2canvas does not support pseudo element.但是 div 有伪元素,但是我知道 html2canvas 不支持伪元素。

How to solve it ?如何解决? Is there any other library to save div as image ?有没有其他库可以将 div 保存为图像?

I am using below link to create a tree structure :我正在使用以下链接创建树结构:
https://codepen.io/P233/pen/Kzbsi https://codepen.io/P233/pen/Kzbsi
and i want to save this as image.我想将其保存为图像。

For this purpose I am using html2canvas为此,我使用 html2canvas

$(document).ready(function() {
  html2canvas($("#home1"), {
    onrendered: function(canvas) {
      var image = new Image();
      image.src = divByteArray;
      document.getElementById('image').appendChild(image);

      //window.open(divByteArray);          
      /* $("#test").attr('href', canvas.toDataURL("image/png"));
                    $("#test").attr('download', 'checkFile.png');
                    $("#test")[0].click(); */
    }
  });
});

Please don't put your comment on function bracket.请不要将您的评论放在函数括号上。 i am not putting whole function.我没有把整个功能。
I just want to know if there is another library which save div as image?我只想知道是否有另一个库将 div 保存为图像?

您可以使用html2canvas.jscanvas2image将 div 转换为画布并转换为图像。

html2canvas seems to work just well with your code : https://codepen.io/anon/pen/gMOmpB html2canvas 似乎与您的代码配合得很好: https ://codepen.io/anon/pen/gMOmpB

I wonder how you did " come to know " that it doesn't work with pseudo-elements?我想知道你是如何“知道”它不适用于伪元素的?

A simpler example, which doesn't work in stack-snippet... :一个更简单的例子,它在 stack-snippet... 中不起作用

https://jsfiddle.net/whtsavpp/ https://jsfiddle.net/whtsavpp/

html2canvas(d).then(function(c){document.body.appendChild(c)})
div:after{content:'hello'}
canvas{border: 1px solid black;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.5.0-beta4/html2canvas.js"></script>
<div id="d"></div>

In browsers other than Google chrome, getComputedStyle() method on pseudo elements pick the border style from main element.在 Google chrome 以外的浏览器中,伪元素上的getComputedStyle()方法从主元素中选择边框样式。 so you will have to include the border style on main element.所以你必须在主元素上包含边框样式。

Here is the trick, change this code:这是技巧,更改此代码:

div::before {
content: 'Hello',
border-top: 1px solid green;
}

to:到:

div {
border: 0 solid;
}

div::before {
content: 'Hello',
border-top: 1px solid green;
}

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

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