使用
  • 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:

    1. 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.
    2. Can SVG evaluate math expressions in element attributes(just to be sure)?

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(3

    马蹄踏│碎落叶 2024-11-08 17:02:54

    请勿使用

    这样,您就可以完全访问SVG DOM 作为文档的一部分。如该示例所示,您只需确保使用 SVG 命名空间创建 SVG 元素(但不是属性)。您还必须确保您的网络托管服务商将 xhtml 的内容类型发送为 application/xhtml+xmltext/xml,而不是 text/html >。

    phrogz$ curl --silent -I http://phrogz.net/svg/svg_in_xhtml5.xhtml | grep "Type"
    Content-Type: application/xhtml+xml
    

    有关 JavaScript 操作 SVG 与 HTML 混合的更多示例,请参阅同一目录中的各种 .xhtml 文件。一个特别引人注目的示例是这个,它动态创建数百个小型 SVG 文件作为内联元素,如下所示文本。

    对于你的问题:

    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 or text/xml, not text/html.

    phrogz$ curl --silent -I http://phrogz.net/svg/svg_in_xhtml5.xhtml | grep "Type"
    Content-Type: application/xhtml+xml
    

    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:

    Can SVG evaluate math expressions in element attributes(just to be sure)?

    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

    够运 2024-11-08 17:02:54

    使用 or 和 标签访问 SVG 元素有什么区别(就脚本而言)?也许有人可以提供简单的示例。

    尝试访问框架内容的脚本受同源策略的约束,并且如果其他窗口对象是从不同域加载的,则无法访问该窗口对象中的大多数属性。这也适用于框架内尝试访问其父窗口的脚本。通过window.postMessage仍然可以实现跨域通信。

    来源:https://developer.mozilla.org/en/HTML/Element/iframe #Scripting

    我们通过iframe_element.contentWindow方法访问iframe内容:

    <html>
      <body>
        <iframe id="SVG_frame" src="image.svg"></iframe>
      </body>
      <script>
        var SVG_frame = document.getElementById ( "SVG_frame" );
        var SVG_content = null;
        function getContent ()
        {
          SVG_content = SVG_frame.contentWindow;
          SVG_content ? alert ( "YAY!" ) : alert ( "BOO!" );
        }
          SVG_frame.onload = getContent;
      </script>
    </html>
    

    示例(查看源代码):https://jwatt.org/svg/demos/scripting-across-embed.html

    (两种方法均失败至少在 Chromium 中)

    示例(查看源代码):https://jwatt.org/svg/demos/scripting-across-object.html


    SVG 可以计算数学表达式吗
    元素属性(只是为了确定)?

    我在 SVG 规范 中没有找到任何有关它的文字。


    编辑

    就个人而言,我建议使用 + 数据URI方案和/或object_element.contentDocument。我已经在 Chromium 和 Firefox 中进行了测试。

    啊哈!


    EDIT2

    如果您有兴趣如何让标记矢量图形在没有插件的情况下在 Internet Explorer 中工作,那么 矢量标记语言就是这样。

    Whats the difference (in terms of scripting) in using an or and tag for accessing the SVG elements?, maybe someone can include simple examples.

    <iframe>:

    Scripts trying to access a frame's content are subject to the same-origin policy, and cannot access most of the properties in the other window object if it was loaded from a different domain. This also applies to a script inside a frame trying to access its parent window. Cross-domain communication can still be achieved with window.postMessage.

    Source: https://developer.mozilla.org/en/HTML/Element/iframe#Scripting

    We access iframe content by iframe_element.contentWindow method:

    <html>
      <body>
        <iframe id="SVG_frame" src="image.svg"></iframe>
      </body>
      <script>
        var SVG_frame = document.getElementById ( "SVG_frame" );
        var SVG_content = null;
        function getContent ()
        {
          SVG_content = SVG_frame.contentWindow;
          SVG_content ? alert ( "YAY!" ) : alert ( "BOO!" );
        }
          SVG_frame.onload = getContent;
      </script>
    </html>
    

    <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


    Can SVG evaluate math expressions in
    element attributes(just to be sure)?

    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/or object_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.

    疯到世界奔溃 2024-11-08 17:02:54

    嗯,这取决于你对动态的理解。在大多数情况下,是的,您可能需要脚本。如果将脚本放入 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().

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文