Animating SVG with SMIL

Animating SVG with SMIL

本文关键字:SMIL with SVG Animating      更新时间:2023-09-26

我正在尝试使用SVG创建视频元素的UI。我正在寻找一个简单的解决方案来动画控制栏,我想从窗口的底部上升,当鼠标在里面(像YouTube)。

我想这样做:

<svg xmlns="http://www.w3.org/2000/svg" height="100%" width="100%" viewBox="0 0 640 360" preserveAspectRatio="none" version="1.1">
  <g id="control_bar" transform="translate(0,360)">
    <animateTransform attributeName="transform" attributeType="XML" type="translate" begin="window.mouseover" from="0,360" to="0,328" dur="350ms" fill="freeze"/>
    <animateTransform attributeName="transform" attributeType="XML" type="translate" begin="window.mouseout" from="0,328" to="0,360" dur="350ms" fill="freeze"/>
    <rect x="0" y="0" width="640" height="32" fill="grey"/>
  </g>
</svg>

不幸的是,窗口。鼠标悬停不做任何事情。相反,我创建了一个透明的矩形来覆盖整个窗口,并赋予它id="screen",并使用begin="screen。鼠标悬停"等。当鼠标在窗口时,控制栏就像我想要的那样动画,不幸的是,屏幕阻止了它下面的所有元素获得自己的鼠标事件。

我正在寻找最简单的方法来实现这一点,最好只使用标记(SMIL),因为我想避免大量的JavaScript或库。

谢谢!

>>> EDIT<& lt; & lt; 澄清什么是我后:

我想为HTML5

我当前的解决方案:

我在HTML文档中放置了一个

<video width="640" src="myvideo.webm"/>

,然后我使用以下javascript (jquery):

$(function(){
  $("video").each(function(){
    var old_video = $(this);
    var width = old_video.attr("width")
    var height = Math.floor(width / (16 / 9))
    var video = $("<object/>")
    video.addClass("video")
    video.css({
      width: width,
      height: height,
    })
    var src = old_video.attr("src")     
    video.attr("data", "video.xhtml#" + src)        
    old_video.replaceWith(video)
  })
})

将视频元素替换为,其数据uri设置为:video.xhtml#myvideo.webm

video.xhtml的内容如下:

<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
  <script src="jquery.js"/>
  <script>
    $(function(){
      $(window).hover(function(){
        $("#in")[0].beginElement()
      }, function(){
        $("#out")[0].beginElement()
      })
      var video = document.createElement("video")
      video.setAttribute("src", location.hash.substr(1))
      $("div#video").replaceWith(video)
    })
  </script>
  <style>
    svg {
      position: absolute;
      top: 0;   left: 0;
    }
    video {
      position: absolute;
      top: 0;   left: 0;
      width: 100%;
      height: 100%;
      background: black;
    }
  </style>
</head>
<body>
  <div id="video"/>
  <svg xmlns="http://www.w3.org/2000/svg" height="100%" width="100%" viewBox="0 0 640 360" preserveAspectRatio="none" version="1.1">
    <g id="bar" transform="translate(0,360)">
      <animateTransform id="in" attributeName="transform" attributeType="XML" type="translate" begin="indefinite" from="0,360" to="0,328" dur="50ms" fill="freeze"/>
      <animateTransform id="out" attributeName="transform" attributeType="XML" type="translate" begin="indefinite" from="0,328" to="0,360" dur="350ms" fill="freeze"/>
      <rect x="0" y="0" width="640" height="32" fill="grey"/>
      <rect onclick="$('video')[0].play()" x="0" y="0" width="64" height="32" fill="blue">
        <set id="btn" attributeName="fill" to="red" begin="mousedown" end="mouseup;mouseout" dur="1s" fill="remove" />
      </rect>
    </g>
  </svg>
</body>
</html>

该文档从散列中获取视频uri,并在SVG UI后面注入视频元素。由于它是XHTML文档(而不是SVG),我现在能够使用jquery来处理鼠标事件,这不是理想的,但似乎可以工作。唯一的问题是,我得到一个JavaScript错误:a. comparedocumentposition不是一个函数源文件:jquery.js Line: 17在Firefox。

这个方法有意义吗?有没有更好的办法?我也更喜欢只使用SMIL而不是JavaScript/jQuery的动画解决方案。

谢谢!

开始属性中的"window"部分只是一个id。svg所做的是在具有该id的元素上注册一个事件侦听器,它甚至不必在svg中,它可以是同一文档中的HTML元素。下面是一个例子:

<html xmlns="http://www.w3.org/1999/xhtml">
<body>
  <div id="window">hover here</div>
  <svg xmlns="http://www.w3.org/2000/svg" height="640" width="480" viewBox="0 0 640 360" preserveAspectRatio="none" version="1.1">
    <g id="control_bar" transform="translate(0,360)">
      <animateTransform attributeName="transform" attributeType="XML" type="translate" begin="window.mouseover" from="0,360" to="0,328" dur="350ms" fill="freeze"/>
      <animateTransform attributeName="transform" attributeType="XML" type="translate" begin="window.mouseout" from="0,328" to="0,360" dur="350ms" fill="freeze"/>
      <rect x="0" y="0" width="640" height="32" fill="grey"/>
    </g>
  </svg>
</body>
</html>

似乎在Chrome, Firefox和Opera中工作得很好。

您是否尝试给<svg>元素本身一个id属性?例如

<svg xmlns="http://www.w3.org/2000/svg" id="screen" height="100%" width="100%" viewBox="0 0 640 360" preserveAspectRatio="none" version="1.1" >
  <g id="control_bar" transform="translate(0,360)">
    <animateTransform attributeName="transform" attributeType="XML" type="translate" begin="screen.mouseover" from="0,360" to="0,328" dur="350ms" fill="freeze"/>
    <animateTransform attributeName="transform" attributeType="XML" type="translate" begin="screen.mouseout" from="0,328" to="0,360" dur="350ms" fill="freeze"/>
    <rect x="0" y="0" width="640" height="32" fill="grey"/>
  </g>
</svg>