screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <script src="https://unpkg.com/vue@3.2.12/dist/vue.global.js"></script> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css" /> </head> <body> <div id="app"> <h3 class="heading">Vue Js SVG Download as Png or Download Svg Code</h3> <div class="svg-container"> <div class="svg-icon" v-html="svg"></div> <button class="button svg-download" @click="downloadSvg"> Download SVG </button> <!-- Added SVG download button --> <button class="button png-download" @click="downloadPng"> Download PNG </button> </div> </div> <script type="module"> const app = Vue.createApp({ data() { return { svg: `<svg xmlns="http://www.w3.org/2000/svg" width="300" height="200" fill="red" class="bi bi-facebook" viewBox="0 0 16 16"> <path d="M16 8.049c0-4.446-3.582-8.05-8-8.05C3.58 0-.002 3.603-.002 8.05c0 4.017 2.926 7.347 6.75 7.951v-5.625h-2.03V8.05H6.75V6.275c0-2.017 1.195-3.131 3.022-3.131.876 0 1.791.157 1.791.157v1.98h-1.009c-.993 0-1.303.621-1.303 1.258v1.51h2.218l-.354 2.326H9.25V16c3.824-.604 6.75-3.934 6.75-7.951z"/> </svg>`, }; }, methods: { downloadSvg() { const svgData = this.svg; const svgBlob = new Blob([svgData], { type: "image/svg+xml" }); const svgUrl = URL.createObjectURL(svgBlob); const downloadLink = document.createElement("a"); downloadLink.href = svgUrl; downloadLink.download = "image.svg"; downloadLink.click(); URL.revokeObjectURL(svgUrl); }, downloadPng() { const svgData = this.svg; const svgBlob = new Blob([svgData], { type: "image/svg+xml" }); const svgUrl = URL.createObjectURL(svgBlob); const svgImage = new Image(); svgImage.onload = () => { const canvas = document.createElement("canvas"); canvas.width = svgImage.width; canvas.height = svgImage.height; const canvasCtx = canvas.getContext("2d"); canvasCtx.drawImage(svgImage, 0, 0); canvas.toBlob((blob) => { const pngUrl = URL.createObjectURL(blob); const downloadLink = document.createElement("a"); downloadLink.href = pngUrl; downloadLink.download = "image.png"; downloadLink.click(); URL.revokeObjectURL(pngUrl); }, "image/png"); }; svgImage.src = svgUrl; }, }, }); app.mount("#app"); </script> <style scoped> /* CSS for the app container */ #app { display: flex; flex-direction: column; align-items: center; justify-content: center; font-family: Arial, sans-serif; } /* CSS for the svg container */ .svg-container { display: flex; flex-direction: column; align-items: center; margin-bottom: 5px; } /* CSS for the svg download button */ .button { background-color: #4caf50; color: #fff; border: none; padding: 10px 20px; font-size: 16px; cursor: pointer; border-radius: 4px; margin-bottom: 10px; } /* CSS for the png download button */ .png-download { background-color: #2196f3; margin-left: 10px; } /* CSS for button hover effect */ .button:hover { opacity: 0.8; } </style> </body> </html>