简体   繁体   English

如何在没有任何第三方库的情况下使用javascript将html div元素内的所有元素转换为图像

[英]How to convert all elements inside html div element into an image using javascript without any third party library

I am having a div with 3 elements. 我有3个元素的div。 I want to convert that whole div into an image without using third party tool. 我想将整个div转换为图像而不使用第三方工具。 Kindly provide me any suggestion regarding this. 请给我有关此的任何建议。

 <!DOCTYPE html> <html> <body> <div id="div1">DIV 1<br> <svg width="50" height="50"> <path d="M0,0 L50,0 L50,50 Z" style="stroke: #006666; fill:none;"/> </svg> <div>DIV 2</div> <input type="button" value="Button"/> </div> </body> </html> 

This is probably a good starting point. 这可能是一个很好的起点。 I have slightly modified the answer from this question https://stackoverflow.com/a/27232525/8085668 . 我对这个问题的答案做了些微修改, https://stackoverflow.com/a/27232525/8085668 I have just removed the callback and added a click event. 我刚刚删除了回调并添加了click事件。 Please refer to it for a detailed explanation of the code. 请参考它以获取有关代码的详细说明。

 var svgText = document.getElementById("myViewer").outerHTML; var myCanvas = document.getElementById("canvas"); var ctxt = myCanvas.getContext("2d"); var btn = document.getElementsByClassName('convert2Png')[0]; function drawInlineSVG(ctx, rawSVG) { var svg = new Blob([rawSVG], {type:"image/svg+xml;charset=utf-8"}), domURL = self.URL || self.webkitURL || self, url = domURL.createObjectURL(svg), img = new Image; img.onload = function () { ctx.drawImage(this, 0, 0); domURL.revokeObjectURL(url); }; img.src = url; } // usage: btn.addEventListener('click', function() { drawInlineSVG(ctxt, svgText); }); 
 <!DOCTYPE html> <html> <body> <div id="div1">DIV 1<br> <svg width="50" height="50" id="myViewer" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg"> <path d="M0,0 L50,0 L50,50 Z" style="stroke: #006666; fill:none;"/> </svg> <div>DIV 2</div> <input class="convert2Png" type="button" value="Button"/> <canvas id="canvas" width=800 height=600 style="margin-top:10px;"></canvas> </div> </body> </html> 

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

相关问题 如何在不使用第三方库的情况下将 html 字符串转换为反应元素? - How do I convert a html string into a react element without using a third party library? 如何在没有 iframe 的情况下呈现要放置在第三方网页的 div 中的 HTML 文本? - How to render HTML Text To Be Placed Inside third party webpage`s div without iframe? 如何在不使用第三方库的情况下通过 React 更改多个样式元素? - How to change multiple style elements through React without using a third party library? 创建一个无需使用第三方库即可接受脚本(javascript)的控件 - create a control to accept script (javascript) without using third party library 如何删除div和div中的所有元素? Javascript,HTML,CSS - How to delete div and all element inside div? Javascript, html, css 无需安装即可添加第三方 Javascript 库 - adding third party Javascript library without installing Javascript:如何不使用任何第三方就获得内部IP地址? - Javascript: How can we get internal IP address without using any third party? 如何使用 JavaScript 删除 div 元素内的下拉列表中的元素 - How to remove elements in a drop down inside a div element using JavaScript 如何使用 javascript 或 python 允许所有第三方在 chrome 中使用? - How to allow all third party in chrome using javascript or python? 如何实现第三方Javascript库? - How to implement Third Party Javascript Library?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM