猿问

如何在javascript中将图像(svg)转换为渲染的svg?

我有一个在单击 div 时加载的图像,如下所示:

<img class="svg" src="{{asset('public/images/my_image.svg') }}" alt="" id='image'>

从后端调用此图像时,它以图像而不是矢量的形式返回(SVG 渲染)。如何使用 Javascript 或任何其他类似工具将其渲染为 SVG?


30秒到达战场
浏览 274回答 2
2回答

白板的微信

使用img带有 in 的标签src本质上会为您向服务器发出 HTTP 请求。在这种情况下,您必须自己提出请求。您可以使用Fetch APIJavaScript 原生的 which 来做到这一点。// This should be the path to your SVG file.// Modify if incorrect.const svgFilePath = 'public/images/my_image.svg';// Select the current image.const image = document.querySelector('.svg');// Create a new dom parser to turn the SVG string into an element.const parser = new DOMParser();// Fetch the file from the server.fetch(svgFilePath)&nbsp; .then(response => response.text())&nbsp; .then(text => {&nbsp; &nbsp; // Turn the raw text into a document with the svg element in it.&nbsp; &nbsp; const parsed = parser.parseFromString(text, 'text/html');&nbsp; &nbsp; // Select the <svg> element from that document.&nbsp; &nbsp; const svg = parsed.querySelector('svg');&nbsp; &nbsp; // If both the image and svg are found, replace the image with the svg.&nbsp; &nbsp; if (image !== null && svg !== null) {&nbsp; &nbsp; &nbsp; image.replaceWith(svg);&nbsp; &nbsp; }&nbsp; });并处理多个文件。在这种情况下,我使用了一个包含对象的数组,这些对象包含id您要获取的图像元素和srcsvg 文件的对象。// Array of object containing the id of the image you want to replace// and the src of the SVG that takes it's place.const svgFiles = [&nbsp; { id: 'image1', src: 'public/images/my_image_1.svg' },&nbsp; { id: 'image2', src: 'public/images/my_image_2.svg' },&nbsp; { id: 'image3', src: 'public/images/my_image_3.svg' },];// Create a new dom parser to turn the SVG string into an element.const parser = new DOMParser();// Loop over each object in the array and use the id and src// with a destructuring assignment.for (const { id, src } of svgFiles) {&nbsp; // Find the image. If it is not there, continue with the&nbsp; // loop to the next iteration.&nbsp; let image = document.getElementById(id);&nbsp; if (image === null) continue;&nbsp; // Fetch the file from the server.&nbsp; fetch(src)&nbsp; &nbsp; .then(response => response.text())&nbsp; &nbsp; .then(text => {&nbsp; &nbsp; &nbsp; // Turn the raw text into a document with the svg element in it.&nbsp; &nbsp; &nbsp; const parsed = parser.parseFromString(text, 'text/html');&nbsp; &nbsp; &nbsp; // Select the <svg> element from that document.&nbsp; &nbsp; &nbsp; const svg = parsed.querySelector('svg');&nbsp; &nbsp; &nbsp; // If the svg is found, replace the image with the svg.&nbsp; &nbsp; &nbsp; // Otherwise, continue the loop.&nbsp; &nbsp; &nbsp; if (svg === null) continue;&nbsp; &nbsp; &nbsp; image.replaceWith(svg);&nbsp; &nbsp; });}

神不在的星期二

我注意到你正在使用asset()函数,所以我猜你不需要它是 JS你可以使用提供刀片指令的Laravel SVG 包。 小巧方便 @svg()<a href="/settings">&nbsp; &nbsp; @svg('cog', 'icon-lg') Settings</a><!-- Renders.. --><a href="/settings">&nbsp; &nbsp; <svg class="icon icon-lg">&nbsp; &nbsp; &nbsp; &nbsp; <path d="..." fill-rule="evenodd"></path>&nbsp; &nbsp; </svg>&nbsp; &nbsp; Settings</a>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答