简体   繁体   English

如何在raphael.js文档中添加子svg标签?

[英]How can I add a sub svg tag in a raphael.js document?

I'm using Raphael.js to produce an SVG drawing from Javascript (actually Coffeescript) 我正在使用Raphael.js从Javascript(实际上是Coffeescript)生成SVG绘图

However, I'd like to be able to include subdrawings, automatically scaled, by putting them inside a second <svg> tag nested inside the first. 但是,我希望能够通过将子绘图放入嵌套在第二个<svg>标记中的第二个<svg>标记中,将它们自动缩放。 Unfortunately the Raphael Paper object which allows me to add rects and paths etc. doesn't have an option for adding svgs. 不幸的是,允许我添加矩形和路径等的Raphael Paper对象没有添加svg的选项。

I've tried to add the tag directly to the DOM in javascript with the following code : 我尝试使用以下代码将标记直接添加到javascript中的DOM中:

res = document.createElement("svg")
res.setAttribute("x",x)
res.setAttribute("y",y)
res.setAttribute("width",width)
res.setAttribute("height",height)
res.setAttribute("viewBox","0 0 100 100")
res.innerHTML = someInnerSVG
@paper.canvas.appendChild(res)

This seems to update the DOM as expected, adding my new SVG tag as a child of the main outer SVG. 这似乎可以按预期更新DOM,将我的新SVG标签添加为主要外部SVG的子代。 However, nothing in this inner actually appears on the screen. 但是,此内部的任何内容实际上都不会出现在屏幕上。 (My inner SVG path is scaled within 0 0 100 100 so is within the viewBox.) (我的内部SVG路径在0 0 100 100内缩放,因此在viewBox内。)

The rest of the drawing in the outer SVG, as produced by Raphael, is visible. 由Raphael制作的外部SVG中的其余图形可见。 But nothing of the inner one is. 但是内在的一无是处。

Should what I'm trying be possible? 我正在尝试的可能吗? Any idea what I'm doing wrong? 知道我在做什么错吗?

You should place these sub elements into a Raphael set. 您应该将这些子元素放入Raphael集。 From there, you will be able to apply transformations to the set. 从那里,您将能够将变换应用于集合。 To transform the set, you would call the transform() method and use the s attribute: 要转换集合,您可以调用transform()方法并使用s属性:

set.transform('s[sx],[sy]');

Where sx is the horizontal scale amount and sy is the vertical scale amount. 其中sx是水平刻度量, sy是垂直刻度量。 Remove the brackets when you insert the numbers. 插入数字时,请卸下括号。

Please refer to the documentation here: http://raphaeljs.com/reference.html#Element.transform The methods for elements apply to sets because sets are pseudo elements. 请在此处参考文档: http : //raphaeljs.com/reference.html#Element.transform元素的方法适用于集合,因为集合是伪元素。

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

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