html2canvas not rendering image (externally hosted images)

十年热恋 提交于 2021-02-19 01:46:49

问题


I have created a game in which users pick 4 images from 4 separate slideshows to create a composite image, which I then want to be able to export to a 'gallery' function on my site. I am attempting to use html2canvas but am encountering some problems

this is my jquery:

var shotit = function() {
$('.bodyWrap').html2canvas({
onrendered : function(canvas) {
var img = canvas.toDataURL();
    $('<img>',{src:img}).appendTo($('body'));
        }
    }); }

body wrap is a really long html element with lots of child elements in it, but it basically goes like this

<div class="bodyWrap">

        <div class="header">
          <h1>TITLE</h1>
        </div>

            <div class="slideshowWrapper">
              <div class="slideshow">
                    <div class="slideshowWrapper">
                <div class="slideshow">
                    <div class="slide">
                       <img class="img2" src="https://i.imgur.com/mnJDmlS.jpg" alt="head1">
<div class="emailButton">
 <a class="emailLink" href="mailto:emailaddres@email.com?Subject=Let's mail"target="_blank"><br>Name of Artist</a>
</div>
                    </div>
                    <div class="slide">
                        <img https://picture.jpg>
                    </div>
                      <div class="prevControl"></div>
                      <div class="nextControl"></div>
              <div class="slideshow">
                    <div class="slide">
                        <img https://picture.jpg>
                    </div>
                    <div class="slide">
                        <img https://picture.jpg>
                    </div>
                      <div class="prevControl"></div>
                      <div class="nextControl"></div>
              <div class="slideshow">
                    <div class="slide">
                        <img https://picture.jpg>
                    </div>
                    <div class="slide">
                        <img https://picture.jpg>
                    </div>
                      <div class="prevControl"></div>
                      <div class="nextControl"></div>
              <div class="slideshow">
                  <div class="slide">
                        <img https://picture.jpg>
                  </div>
                  <div class="slide">
                        <img https://picture.jpg>
                  </div>
                      <div class="prevControl"></div>
                      <div class="nextControl"></div>
                  </div>
              </div>
            </div>
</div>

the images are hosted on externally imgur and not on my website.

and then a button which looks like this (for taking the pic)

<a href="#" onClick="shotit()">take picture</a>

sorry thats probably a bit much code but i just don't know where the problem is!!

anyway, any help or pointers gratefully received! thanks


回答1:


Try this:

<div class="bodyWrap">
  <div class="header">
    <h1>TITLE</h1>
  </div>
  <div class="slideshowWrapper">
    <div class="slideshow">
      <div class="slide">
        <img src="https://picture.jpg">
      </div>
      <div class="slide">
        <img src="https://picture.jpg">
      </div>
      <div class="prevControl"></div>
      <div class="nextControl"></div>
      <div class="slideshow">
        <div class="slide">
          <img src="https://picture.jpg">
        </div>
        <div class="slide">
          <img src="https://picture.jpg">
        </div>
        <div class="prevControl"></div>
        <div class="nextControl"></div>
        <div class="slideshow">
          <div class="slide">
            <img src="https://picture.jpg">
          </div>
          <div class="slide">
            <img src="https://picture.jpg">
          </div>
          <div class="prevControl"></div>
          <div class="nextControl"></div>
          <div class="slideshow">
            <div class="slide">
              <img src="https://picture.jpg">
            </div>
            <div class="slide">
              <img src="https://picture.jpg">
            </div>
            <div class="prevControl"></div>
            <div class="nextControl"></div>
          </div>
        </div>
      </div>
    </div>
    <a href="#" onClick="shotit(event)">take picture</a>
    <script src="jquery-2.1.1.min.js"></script>
    <script src="html2canvas.js"></script>
    <script>
      var shotit = function(e)
      {
        e.preventDefault();
        html2canvas($('.bodyWrap'), {
          onrendered: function(canvas)
          {
            document.body.appendChild(canvas);
          },
          logging:true
        });
      };
    </script>



回答2:


You can do so by passing one of the option in html2canvas function.

html2canvas(document.body,
{
useCORS: true, //By passing this option in function Cross origin images will be rendered properly in the downloaded version of the PDF
onrendered: function (canvas) {
 //your functions here
}
});


来源:https://stackoverflow.com/questions/30372417/html2canvas-not-rendering-image-externally-hosted-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!