Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
790 views
in Technique[技术] by (71.8m points)

javascript - Html2canvas Ignores Object fit for image

I've been trying to make use of html2canvas to take a screenshot of a element

I noticed an image inside the div element that uses the object-fit property becomes stretched after the html2canvas screenshot

Is there a walk around for this. Here is my code

<script>window.addEventListener('load', function () {

      
 var bigCanvas = $("<div>").appendTo('body');
 var scaledElement = $("#Element").clone()
 .css({
  'transform': 'scale(1,1)',
  'transform-origin': '0 0'
 })
 .appendTo(bigCanvas);
 
 var oldWidth = scaledElement.width();
 var oldHeight = scaledElement.height();

 var newWidth = oldWidth * 1;
 var newHeight = oldHeight * 1;
 
 bigCanvas.css({
   'width': newWidth,
   'height': newHeight
 })


 html2canvas(bigCanvas, {
   onrendered: function(canvasq) {
   var image = document.getElementById('image');
  image.style.width='300px';
 
   image.src = canvasq.toDataURL();
   
    bigCanvas.remove() ;
   }
 });}) </script>

While html goes as

<div id="Element" class="container">
  
<div class="bg-light p-3 text-center">

<h3>iPhone 12 Pro Max</h3>

<div class="m-2 shadow" style="height:400px;width:100%;">

<img src="img/agency.jpeg" style="object-fit:cover;height:100%;width:100%;" />
</div>

<h4>Now available for free</h4>

</div>

</div>

<div class="container" style="width:100%;border:2px solid black;overflow:hidden">

 <img src="" id="image" style="max-width:100%;"/>
 </div>
question from:https://stackoverflow.com/questions/65895497/html2canvas-ignores-object-fit-for-image

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...