我正在尝试创建动态 SVG 图形,据我所知,创建动态 SVG 的唯一方法是使用脚本语言,所以我有几个问题,基本上我想将 SVG 加载或嵌入到 HTML 中网页并使用网页中的输入控制图形,而不是在 SVG 文件中硬编码 ECMAscript。我不完全确定是否应该使用嵌入标签或 iframe 来显示 SVG,这是我对 SVG 和脚本编写的疑问:
- 使用
- SVG 可以计算元素属性中的数学表达式吗(只是为了确定)?
I'm trying to create Dynamic SVG graphics, it is my understanding that the only way to create dynamic SVG is to use a scripting language, so I have a few questions, basically I'd like to load or embed the SVG to a HTML web page and control the graphics using Inputs in the web page, rather than hardcoding the ECMAscript in the SVG file. I'm not entirely sure if I should use the embed tag or an iframe for displaying the SVG here are my doubts regarding SVG and scripting:
- Whats the difference (in terms of scripting) in using an
<iframe>
or and <embed>
tag for accessing the SVG elements?, maybe someone can include simple examples.
- Can SVG evaluate math expressions in element attributes(just to be sure)?
发布评论
评论(3)
请勿使用
或
。相反,将 SVG 直接嵌入到 XHTML 中,如下所示:
http://phrogz.net/svg/svg_in_xhtml5.xhtml
这样,您就可以完全访问SVG DOM 作为文档的一部分。如该示例所示,您只需确保使用 SVG 命名空间创建 SVG 元素(但不是属性)。您还必须确保您的网络托管服务商将 xhtml 的内容类型发送为
application/xhtml+xml
或text/xml
,而不是text/html
>。有关 JavaScript 操作 SVG 与 HTML 混合的更多示例,请参阅同一目录中的各种 .xhtml 文件。一个特别引人注目的示例是这个,它动态创建数百个小型 SVG 文件作为内联元素,如下所示文本。
对于你的问题:
一般情况下不行。但是,使用 SMIL Animation 确实允许您指定各种插值方法随着时间的推移的属性。
最后,请注意,您不必将 SVG 放入 HTML 中以使其动态。您可以直接使用 JavaScript 编写 SVG 脚本。例如,看这个测试文件(按绿色按钮开始模拟):
http://phrogz.net/svg/SpringzTest.svg
Don't use either
<iframe>
or<embed>
. Instead, embed your SVG directly in XHTML like so:http://phrogz.net/svg/svg_in_xhtml5.xhtml
With that, you have full access to the SVG DOM as part of your document. As shown in that example, you simply need to be certain to create SVG elements (but not attributes) using the SVG namespace. You must also ensure that your web host is sending the content type for xhtml as
application/xhtml+xml
ortext/xml
, nottext/html
.For more examples of JavaScript manipulating SVG mixed with HTML, see the various .xhtml files in that same directory. A particularly compelling example is this one, which dynamically creates hundreds of small SVG files as inline elements flowing like text.
And to your question:
Not in general, no. However, the usage of SMIL Animation does allow you to specify various interpolation methods of properties over time.
Finally, note that you don't have to put SVG in HTML to make it dynamic. You can script SVG with JavaScript directly. For example, see this test file (press the green button to start simulation):
http://phrogz.net/svg/SpringzTest.svg
:
来源:https://developer.mozilla.org/en/HTML/Element/iframe #Scripting
我们通过
iframe_element.contentWindow
方法访问iframe内容::
示例(查看源代码):https://jwatt.org/svg/demos/scripting-across-embed.html
(两种方法均失败至少在 Chromium 中)
示例(查看源代码):https://jwatt.org/svg/demos/scripting-across-object.html
像
?我在 SVG 规范 中没有找到任何有关它的文字。
编辑
就个人而言,我建议使用
啊哈!
EDIT2
如果您有兴趣如何让标记矢量图形在没有插件的情况下在 Internet Explorer 中工作,那么 矢量标记语言就是这样。
<iframe>
:Source: https://developer.mozilla.org/en/HTML/Element/iframe#Scripting
We access iframe content by
iframe_element.contentWindow
method:<embed>
:Example (view source): https://jwatt.org/svg/demos/scripting-across-embed.html
(both methods fail at least in Chromium)
<object>
Example (view source): https://jwatt.org/svg/demos/scripting-across-object.html
like
<element attribute="48/2*(9+3)"/>
?I did't find a word about it in SVG spec.
EDIT
Personally, I recommend to use
<object>
+ Data URI Scheme and/orobject_element.contentDocument
. I've tested both in Chromium and Firefox.AHA!
<object>
has similar security behavior to<iframe>
: domain, protocol must be same for site and SVG file.EDIT2
If You are interested how to get markup vector graphics to work in Internet Explorer(s) without plug-in(s), then Vector Markup Language is the way.
嗯,这取决于你对动态的理解。在大多数情况下,是的,您可能需要脚本。如果将脚本放入 HTML 或 SVG 文件中,则没有区别,两者都将由同一引擎执行。
您可以使用声明性动画元素(又名 SMIL)创建交互式/动画 svg 内容。您还可以使用 CSS :hover 规则实现简单的悬停效果,或使用 CSS3 过渡 实现简单的悬停效果。
XSLT 还可以用于制作动态的 svg 内容,因为它可以将您的输入转换为其他内容。但它不涵盖交互方面。
您可以通过以下任一方式从包含 svg 元素的 HTML 文件中访问 svg 元素:
theEmbeddingElement.contentDocument
(首选,但不适用于)
或者
theEmbeddingElement.getSVGDocument()
。Well, it depends on what you mean with dynamic. In most cases yes, you'll probably want scripts. There's no difference if you put your script in the HTML or the SVG file, both will be executed by the same engine.
You can create interactive/animated svg content with the declarative animation elements (aka SMIL). You can also do simple hover effects with CSS :hover rules, or transitions with CSS3 Transitions.
XSLT can also be used to make somewhat dynamic svg content, since it can transform your input to something else. It doesn't cover the interaction aspect though.
You can access the svg elements from the HTML file that includes it with either of:
theEmbeddingElement.contentDocument
(preferred, but doesn't work on<embed>
)or alternatively
theEmbeddingElement.getSVGDocument()
.