javascript - html2canvas打印带有位置绝对值的svg失败

标签 javascript html css html2canvas

请运行下面的代码,您可以看到:红线没有打印到 Canvas 上。位置绝对值有什么问题?我该如何解决?谢谢。

<!DOCTYPE html>
<html>
  <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
      <div id="Screenshot" style="height:200px; width:300px;">
          <p>This is it</p>
          <svg width="138" height="14"  version="1.1" xmlns="http://www.w3.org/2000/svg" >
            <path d="M 5 0 C 71 10 71 10 137 0 " transform="translate(0,3.4488784721473897)" version="1.1" xmlns="http://www.w3.org/2000/svg" fill="none" stroke="#89bcde" style="" stroke-width="1"></path>
            <path version="1.1" xmlns="http://www.w3.org/2000/svg" d="M5,0 L15.63509460627212,-3.4488784721473897 L11.159991667025153,0.9313445453647529 L14.140159541481182,6.438748601729261 L5,0" stroke="#89bcde" fill="#89bcde" transform="translate(0,3.4488784721473897)"></path>
          </svg>
          <svg width="138" height="14" style="position:absolute;left:10px;top:100px" version="1.1" xmlns="http://www.w3.org/2000/svg">
            <path d="M 5 0 C 71 10 71 10 137 0 " transform="translate(0,3.4488784721473897)" pointer-events="visibleStroke" version="1.1" xmlns="http://www.w3.org/2000/svg" fill="none" stroke="red" style="" stroke-width="1"></path>
            <path version="1.1" xmlns="http://www.w3.org/2000/svg" d="M5,0 L15.63509460627212,-3.4488784721473897 L11.159991667025153,0.9313445453647529 L14.140159541481182,6.438748601729261 L5,0"  stroke="red" fill="#89bcde" transform="translate(0,3.4488784721473897)"></path>
          </svg>
      </div>
      <hr />
      <input type="button" value="test" onclick="TestCV()" />
      <div id="test">

      </div>
      <!--<script src="../src/html2canvas.js"></script>-->
      <script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
    <script type="text/javascript">
        function TestCV() {
            html2canvas(document.getElementById("Screenshot")).then(function (canvas) {
                document.getElementById("test").appendChild(canvas);
            });
        }
    </script>
  </body>
</html>

最佳答案

像这样在 svg 之前尝试追加和跨度:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>

    <div id="Screenshot" style="height:200px; width:300px;">
        <p>This is it</p>
        <svg style="display:block" width="138" height="14" version="1.1" xmlns="http://www.w3.org/2000/svg">
            <path d="M 5 0 C 71 10 71 10 137 0 " transform="translate(0,3.4488784721473897)" version="1.1" xmlns="http://www.w3.org/2000/svg" fill="none" stroke="#89bcde" style="" stroke-width="1"></path>
            <path version="1.1" xmlns="http://www.w3.org/2000/svg" d="M5,0 L15.63509460627212,-3.4488784721473897 L11.159991667025153,0.9313445453647529 L14.140159541481182,6.438748601729261 L5,0" stroke="#89bcde" fill="#89bcde" transform="translate(0,3.4488784721473897)"></path>
        </svg>
        <svg style="position:absolute;left:10px;top:100px" width="1389" height="14" version="1.1" xmlns="http://www.w3.org/2000/svg">
            <path d="M 5 0 C 71 10 71 10 137 0 " transform="translate(0,3.4488784721473897)" pointer-events="visibleStroke" version="1.1" xmlns="http://www.w3.org/2000/svg" fill="none" stroke="red" style="" stroke-width="1"></path>
            <path version="1.1" xmlns="http://www.w3.org/2000/svg" d="M5,0 L15.63509460627212,-3.4488784721473897 L11.159991667025153,0.9313445453647529 L14.140159541481182,6.438748601729261 L5,0" stroke="red" fill="#89bcde" transform="translate(0,3.4488784721473897)"></path>
        </svg>
    </div>
    <hr />
    <input type="button" value="test" onclick="TestCV()" />
    <div id="test">
    </div>
    <div id="temporady" >
    </div>
    <!--<script src="../src/html2canvas.js"></script>-->

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
    <script type="text/javascript">
        //==============================================
        function TestCV() {
            AddCss();
            var x = $("#Screenshot")[0];
            html2canvas(x).then(function (canvas) {
                document.getElementById("test").appendChild(canvas);
            });
            setTimeout(function () {
                RemoveCss();
            }, 200);
        }
        function AddCss() {
            var css;
            $("#Screenshot").find("svg").each(function () {
                var style = $(this).attr("style");
                $(this).wrap("<spand style='"+style+"'></spand>");
                $(this).removeAttr("style");
            });
        }
        function RemoveCss() {
            $("#Screenshot").find("svg").each(function () {
                 css = $(this).parent().attr("style");
                 $(this).attr('style', css);
                 $(this).unwrap();
            });
        }
    </script>
</body>
</html>

关于javascript - html2canvas打印带有位置绝对值的svg失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48201286/

相关文章:

javascript - 哪个最适合 jquery 选择类名与 id 的性能

javascript - JQuery Lightbox - 对不是链接的图像进行分组

html - 使用 twitter bootstrap 将容器设置为精确宽度

javascript - Firefox 无法处理我的 jQuery

javascript - Bootstrap 3 下拉动画/缓动

javascript - Ckeditor Inline 动态修改工具栏以实现内容编辑

html - CSS 在 CSS 文件中的工作方式与在 HTML 中的工作方式不同

javascript - 为什么菜单 JavaScript 效果不起作用?

javascript - 有什么办法可以阻止 Javascript 默默地失败吗?

html - 如何在不影响文本本身的情况下保持宽度小于文本宽度的背景?