Изображение, созданное из html, неправильно помещено на холст - PullRequest
0 голосов
/ 15 ноября 2018

Я использую html2canvas.js для создания скриншота из html и jspdf.js для создания pdf.Мой HTML-код выглядит следующим образом:

<div id="panel-item-all-annotations">
   <div class="panel-body" id="" style="width: 100%;">
         {{--some html with image--}}
   </div>
</div>
<canvas width="800" height="500" style="display: block;"></canvas>

Я использую холст для размещения изображения div # panel-item-all-annotations с помощью кода JS, как показано ниже

html2canvas(document.querySelector("#panel-item-all-annotations"), {canvas: canvas}).then(function(canvas) {
        console.log('Drew on the existing canvas');
});

Но этонеправильно размещает изображение на холсте.Он размещается в самом низу и справа от холста, как показано ниже Improper image place on canvas

html2canvas.js url: https://html2canvas.hertzen.com/ jspdf: https://github.com/MrRio/jsPDF

1 Ответ

0 голосов
/ 15 ноября 2018

Я не уверен, почему он делает это с тобой. Там должно быть что-то еще происходит. Разобрав его до супер-простых вещей, кажется, работает. Вот пример:

https://lab -keqpanlmmw.now.sh

Вот код, но он не будет работать здесь в StackOverflow.

function go() {
  let input = document.querySelector("#input");
  let output = document.querySelector("#output");
  html2canvas(input, {
    canvas: output
  });
}
#input {
  background-color: orange;
  color: white;
  padding: 1em;
  font-family: sans-serif;
  max-width: 200px;
}

#output {
  border: 1px solid black;
  width: 300px;
  height: 200px;
  margin-top: 1em;
}
<script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
<div id="input">
  {{--some html with image--}}
</div>
<div>
  <button onclick="go()">Go</button>
</div>
<div>
  <canvas id="output"></canvas>
</div>
...