简体   繁体   English

使用 DomToImage 后背景图像捕获一半

[英]Background image captures in half after using DomToImage

So I'm trying out Dom-to-Image but ran into a deadend.所以我正在尝试 Dom-to-Image 但遇到了死胡同。 I have this div with a fixed width and height of 700px and 500 px respectively.我有一个固定widthheight分别为700px像素和500像素的 div。 This is the div which I want to take a screenshot of with Dom-to-Image plugin.这是我想用Dom-to-Image插件截屏的 div。

When the div is left-aligned on the viewport, the screen capture is perfect and I can download the image.div在视口上left-aligned时,屏幕截图是完美的,我可以下载图像。 But if I make the div center with margin: 0 auto;但是,如果我将 div 中心margin: 0 auto; , then I get one-third of the screenshot and not the full image. ,然后我得到三分之一的屏幕截图,而不是完整的图像。

Here's the issue when it is centered:这是居中时的问题:

在此处输入图像描述

What I want:我想要的是:

No matter where my div is placed, I only want to take the screenshot of that full div.无论我的 div 放在哪里,我只想截取那个完整 div 的屏幕截图。 How can I do that?我怎样才能做到这一点?

Here's the code:这是代码:

 $(document).ready(() => { const imgDiv = document.querySelector('#div'); const downloadBtn = document.querySelector('#download'); $(downloadBtn).click(() => { domtoimage.toBlob(imgDiv).then( blob => { saveAs(blob, 'myImage.png'); }); }); });
 #div { width: 700px; height: 500px; background-image: url('https://img.glyphs.co/img?q=85&w=900&src=aHR0cHM6Ly9zMy5tZWRpYWxvb3QuY29tL3Jlc291cmNlcy9HZW9tZXRyaWMtQmFja2dyb3VuZC1QYXR0ZXJucy1QcmV2aWV3LTJiLmpwZw=='); background-position: center center; background-size: cover; display: flex; justify-content: center; align-items: center; margin: 0 auto; }.text-block { display: flex; flex-direction: column; z-index: 9999; }.text, .name { color: #fff; text-align: center; margin: 0; }.text { padding: 0 40px; margin-bottom: 20px; font-size: 15px; }.name { font-size: 8px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.0/FileSaver.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/dom-to-image/2.6.0/dom-to-image.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="div"> <div class="text-block"> <p class="text">Booooooooooooooossss </p> <p class="name">s</p> </div> </div> <button id="download">Download</button>

As said in the docs:如文档中所述:

This library uses a feature of SVG that allows having arbitrary HTML content inside of the <foreignObject> tag.该库使用 SVG 的一个特性,该特性允许在<foreignObject>标记内包含任意 HTML 内容。

So it basically copies the element with the css applied to it and runs them again within the <foreignObject> .所以它基本上复制了应用了 css 的元素,并在<foreignObject>中再次运行它们。

I assume this is done as such in case there's a background or some kind of coloration applied to the element, They probably didn't think much about the css properties that would alter placement or I just didn't read everything in the documentation.我假设这样做是为了以防元素有背景或某种颜色,他们可能没有过多考虑会改变位置的 css 属性,或者我只是没有阅读文档中的所有内容。

With that said, To avoid this You will have to nest your element.话虽如此,为了避免这种情况,您将不得不嵌套您的元素。

All your css property that alter the position of the element should be put on the container, everything else can stay on the DOM element you want to convert to an image.所有改变元素 position 的 css 属性都应该放在容器上,其他所有东西都可以留在要转换为图像的 DOM 元素上。

 var NodeToImage = document.querySelector('[NodeToImage]'); domtoimage.toPng(NodeToImage).then(function(dataUrl) { var img = new Image(); img.src = dataUrl; document.body.appendChild(img); }).catch(function(error) { console.error('oops, something went wrong,'; error); });
 /* Container to nest the DOM element */ /* margin is applied to it instead*/ [ctr] { width: 100px; height: 100px; margin: 20px; } /* This is the DOM element to be converted */ [NodeToImage] { background: orange; height: 100%; /* To match the height */ } img { border: 1px solid;/* For the preview*/ }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/dom-to-image/2.6.0/dom-to-image.min.js" integrity="sha512-01CJ9/g7e8cUmY0DFTMcUw/ikS799FHiOA0eyHsUWfOetgbx/t6oV4otQ5zXKQyIrQGTHSmRVPIgrgLcZi/WMA==" crossorigin="anonymous"></script> <div ctr> <div NodeToImage></div> </div>


Side Note: I tried to look through the issues on Github, and couldn't find anything related to this, maybe i missed it.旁注:我试图查看 Github 上的问题,但找不到与此相关的任何内容,也许我错过了。 You might want to make one你可能想做一个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM