简体   繁体   English

如何设置样式/操作 SVG USE 元素的内容

[英]How to style/manipulate contents of SVG USE element

I have three SVG images on a page.我在一个页面上有三个 SVG 图像。 One is an "image editor" where you can enter some text, an icon, drag and scale it around, etc.一个是“图像编辑器”,您可以在其中输入一些文本、图标、拖动和缩放等。

The other two SVG's use the content from the top SVG through a USE element and displays a preview of the end result.其他两个 SVG 通过 USE 元素使用顶部 SVG 的内容,并显示最终结果的预览。

编辑器截图

When I edit something in the editor, the two preview automatically update.当我在编辑器中编辑内容时,两个预览会自动更新。 Perfect.完美的。 But I don't want to show the bounding box, transform tools, guides, etc in the bottom two preview SVG's.但我不想在底部的两个预览 SVG 中显示边界框、变换工具、指南等。

Is there a way to style (CSS) or manipulate (JS) the (shadow)DOM of those SVG's to not display these extra elements?有没有办法对这些 SVG 的(阴影)DOM 进行样式设置(CSS)或操作(JS)以不显示这些额外元素?

Alter your editor code so that the drawing elements go into one group and the handle elements go into another group.更改您的编辑器代码,使绘图元素进入一组,而手柄元素进入另一组。 Then in your clone SVGs have your <use> elements reference the first group.然后在您的克隆 SVG 中,您的<use>元素引用第一组。

 svg { border: solid 1px black; }
 <svg width="500" height="300" viewBox="0 0 500 300"> <g id="drawing"> <ellipse cx="250" cy="150" rx="200" ry="100" fill="orange"/> </g> <g id="handles"> <rect x="50" y="50" width="400" height="200" fill="none" stroke="blue" stroke-dasharray="10"/> </g> </svg> <br/> <svg width="250" height="150" viewBox="0 0 500 300"> <use xlink:href="#drawing"/> </svg>

Just like @paulLeBeau answer i suggest putting it outside a group of elements.就像@paulLeBeau 的回答一样,我建议将它放在一组元素之外。
The javascript code here will put the handles on all the corners of the group element.此处的 javascript 代码会将句柄放在组元素的所有角上。 ( edit-group in this example). (本例中的edit-group )。
If you have a group that you allready draw to you can simply put that group into this code and it should make the handles.如果你有一个你已经画好的组,你可以简单地将该组放入此代码中,它应该制作句柄。

 function setAttributes(elem, attrs) { for (var key in attrs) { elem.setAttribute(key, attrs[key]); } } document.addEventListener("DOMContentLoaded", function(event) { var svgns = "http://www.w3.org/2000/svg" var svgdoc = document.getElementById("edit-svg"); var editgroup = svgdoc.getElementById("edit-group"); var grouprect = editgroup.getBBox(); var handle = document.createElementNS(svgns, "rect"); setAttributes(handle, { "transform": "translate(-1.5 -1.5)", "fill": "none", "stroke": "black", "stroke-width": "0.5", "width": "3", "height": "3", "x": grouprect.x, "y": grouprect.y }); svgdoc.appendChild(handle); var handle2 = document.createElementNS(svgns, "rect"); setAttributes(handle2, { "transform": "translate(-1.5 -1.5)", "fill": "none", "stroke": "black", "stroke-width": "0.5", "width": "3", "height": "3", "x": grouprect.x + grouprect.width, "y": grouprect.y }); svgdoc.appendChild(handle2); var handle3 = document.createElementNS(svgns, "rect"); setAttributes(handle3, { "transform": "translate(-1.5 -1.5)", "fill": "none", "stroke": "black", "stroke-width": "0.5", "width": "3", "height": "3", "x": grouprect.x, "y": grouprect.y + grouprect.height }); svgdoc.appendChild(handle3); var handle4 = document.createElementNS(svgns, "rect"); setAttributes(handle4, { "transform": "translate(-1.5 -1.5)", "fill": "none", "stroke": "black", "stroke-width": "0.5", "width": "3", "height": "3", "x": grouprect.x + grouprect.width, "y": grouprect.y + grouprect.height, }); svgdoc.appendChild(handle4); var dotted = document.createElementNS(svgns, "rect"); setAttributes(dotted, { "fill": "none", "stroke": "black", "stroke-width": "0.5", "stroke-dasharray": "5", "width": grouprect.width, "height": grouprect.height, "x": grouprect.x, "y": grouprect.y }); svgdoc.appendChild(dotted); });
 .main { border: 1px solid black; } #edit-svg { width: 500px; } .preview svg { border: 1px solid black; }
 <div class="main"> <svg id="edit-svg" viewbox="0 0 110 50" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <g id="edit-group"> <text x=5 y=40 font-size="10" stroke="firebrick">Ask us almost anything</text> </g> </svg> </div> <div class="preview"> <h2>Preview</h2> <svg width="500px" viewBox="0 0 110 50"> <use x="0" y="0" xlink:href="#edit-group" /> </svg> </div>

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

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