首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >html2canvas缺少背景

html2canvas缺少背景
EN

Stack Overflow用户
提问于 2014-03-22 04:46:02
回答 3查看 25.9K关注 0票数 2

我正在使用html2canvas从我的html页面生成图像。

当我输入我的url http://prompter.rareapps.org/prompt/prompt-save2.php?p=123

在html2canvas网站(http://html2canvas.hertzen.com/screenshots.html)的网页渲染器中,我的页面是用背景直接渲染的。

但是当我生成自己的画布时,我没有背景。以下是我的代码

代码语言:javascript
复制
<script type="text/javascript">
$( document ).ready(function() {
    html2canvas(document.body, {
        allowTaint: true,
        taintTest: false,
        onrendered: function(canvas) {
        window.location.href = canvas.toDataURL('image/jpeg');
      }
    });
});

我错过了什么?

谢谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-04-10 23:45:27

此问题已在0.5.0版本中修复(但0.5.0版本尚未准备好使用)。

但是有很多问题正在被纠正,基本功能还没有实现。

注意: html2canvas没有预测0.5.0版本的日期注意:版本0.4.1不再接收更新

问题是0.5.0版本还没有准备好使用,我重新编写了你的html,请参阅:

代码语言:javascript
复制
<!DOCTYPE html><!--// Never put a space/break-line before the DOCTYPE -->
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta charset="UTF-8">

    <script type="text/javascript" src="html2canvas.js"></script>
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>

    <style type="text/css">
        html, body {
            margin: 0;
            padding: 0;
            height: 100%;
        }
        body {
            font-family: Georgia, serif;
            font-size: 4em;
        }
        p {
            padding: 5px 0;
            margin: 0;
        }

        #main {
            margin: 0 auto;
            width: 100%;
            min-height: 100%;
            font-weight: bold;
            background: white url('prompt-save-bg.jpg') center top repeat-y;
            max-width: 1263px; /* background-limit*/
            text-align: center;
        }
        #article {
            padding: 5em 2em 0 2em;
            font-weight: bold;
        }
        #article p.info {
            font-size: 0.2em; padding: 40em 0;
        }
    </style>

    <script type="text/javascript">
    //<!CDATA[
    function save(canvas) {/*html2canvas-0.5.0 work with Promise.js*/
        window.open(canvas.toDataURL());
    }
    $(document).ready(function(){
        $('#save-image').click(function () {
            html2canvas(document.body, {
                allowTaint: true,
                logging: true,
                taintTest: false,
                onrendered: save /*0.4.1*/
            }).then(save);/*0.5.0-rc*/
        });
    });
    //]]>
    </script>
</head>
<body>
    <div id="main">
        <input value="Generate Image" id="save-image" download="YourFileName.jpg" type="button">

        <div id="article">
            <h2>AUBREY</h2>

            <p>And Aubrey was her name</p>
            <p>A not so very ordinary girl or name</p>
            <p>But who's to blame?</p>
            <p>For a love that wouldn't bloom</p>
            <p>For the hearts that never played in tune</p>
            <p>Like a lovely melody that everyone can sing</p>
            <p>Take away the words that rhyme, it doesn't mean a thing</p>
            <p>&nbsp;</p>
            <p>And Aubrey was her name</p>
            <p>We tripped the light and danced together to the moon</p>
            <p>But where was June?</p>
            <p>No, it never came around</p>
            <p>If it did it never made a sound</p>
            <p>Maybe I was absent or was listening too fast</p>
            <p>Catching all the words but then the meaning going past</p>
            <p>&nbsp;</p>
            <p>But God I miss the girl</p>
            <p>And I'd go a thousand times around the world just to be</p>
            <p>Closer to her than to me</p>
            <p>&nbsp;</p>
            <p>And Aubrey was her name</p>
            <p>I never knew her but I loved her just the same</p>
            <p>I loved her name</p>
            <p>Wish that I had found the way</p>
            <p>And the reasons that would make her stay</p>
            <p>I have learned to lead a life apart from all the rest</p>
            <p>If I can't have the one I want, I'll do without the best</p>
            <p>&nbsp;</p>
            <p>But how I miss the girl</p>
            <p>And I'd go a million times around the world just to say</p>
            <p>She had been mine for a day</p>

            <p class="info">
                Prompter generated by RAREAPPS: http://prompter.rareapps.org
            </p>
        </div>
    </div>
</body>
</html>

  • 在Chrome和火狐的0.4.10.5.0-rc.
  • Worked中工作。

注意,我创建了一个名为save()的函数,因此您可以在onrendered (版本0.4.1)或then (版本0.5.0-rc)之间切换。

我放了你的背景和一个<DIV>没有改变<html><body>,因为"html2canvas“不够聪明,不能理解窗口中overflow:的操作。

请注意,最后一段中的margin:; (<p>)在"html2canvas“和"Webkit”浏览器中都造成了问题,所以我使用了padding:

记住,有不止一种方法可以做同样的事情。

票数 4
EN

Stack Overflow用户

发布于 2016-11-11 20:41:38

当前最新版本的0.4.1版本中,背景大小属性设置为封面的背景图片存在问题。

这个页面中的“fperich”提供的解决方案解决了我的问题:https://github.com/niklasvh/html2canvas/issues/265

根据他的解决方案,只需使用以下语句更新版本0.4.1中的第350行:

代码语言:javascript
复制
topPos = isNaN(parseInt(bgposition[1], 10)) ? topPos : parseInt(bgposition[1], 10); 
票数 2
EN

Stack Overflow用户

发布于 2016-07-14 00:39:30

明确设置背景样式,例如。如果

代码语言:javascript
复制
...
 html2canvas(document.body,
        {
            onrendered: function(canvas){
                 var imgData = canvas.toDataURL("image/jpeg");
                var doc = new jsPDF('p','mm');
                doc.addImage(imgData, 'JPEG', 15, 40, 180, 160);
                doc.save($.now()+'.pdf');
        }

pdf具有空白(黑色)背景,

现在,如果您添加CSS样式

代码语言:javascript
复制
 body {
         background-color: white;
     }

空白(黑色)背景已更正

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22569210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档