javascript - 从已经包含动画的文件渲染和动画化 SVG

标签 javascript css reactjs svg less

我正在开发一个 reactjs 网络应用程序。我有一个已经包含动画的 svg 微调器文件。我想知道是否有一种方法可以在 rectjs 上渲染它(包括动画)。我已经在渲染 svg 图像,但是当我使用与此相同的 clase 时,没有渲染任何东西。我认为这是因为动画。

有办法从 svg 中“提取”动画并将其转换为关键帧吗?

这是 svg 文件:

<svg width='48px' height='48px' xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" class="uil-default">
  <rect x="0" y="0" width="100" height="100" fill="none" class="bk"></rect>
  <rect  x='47' y='40' width='6' height='20' rx='3' ry='3' fill='#000000' transform='rotate(0 50 50) translate(0 -30)'>
    <animate attributeName='opacity' from='1' to='0' dur='1s' begin='-1s' repeatCount='indefinite'/>
  </rect>
  <rect  x='47' y='40' width='6' height='20' rx='3' ry='3' fill='#000000' transform='rotate(25.714285714285715 50 50) translate(0 -30)'>
    <animate attributeName='opacity' from='1' to='0' dur='1s' begin='-0.9285714285714286s' repeatCount='indefinite'/>
  </rect>
  <rect  x='47' y='40' width='6' height='20' rx='3' ry='3' fill='#000000' transform='rotate(51.42857142857143 50 50) translate(0 -30)'>
    <animate attributeName='opacity' from='1' to='0' dur='1s' begin='-0.8571428571428571s' repeatCount='indefinite'/>
  </rect>
  <rect  x='47' y='40' width='6' height='20' rx='3' ry='3' fill='#000000' transform='rotate(77.14285714285714 50 50) translate(0 -30)'>
    <animate attributeName='opacity' from='1' to='0' dur='1s' begin='-0.7857142857142857s' repeatCount='indefinite'/>
  </rect>
  <rect  x='47' y='40' width='6' height='20' rx='3' ry='3' fill='#000000' transform='rotate(102.85714285714286 50 50) translate(0 -30)'>
    <animate attributeName='opacity' from='1' to='0' dur='1s' begin='-0.7142857142857143s' repeatCount='indefinite'/>
  </rect>
  <rect  x='47' y='40' width='6' height='20' rx='3' ry='3' fill='#000000' transform='rotate(128.57142857142858 50 50) translate(0 -30)'>
    <animate attributeName='opacity' from='1' to='0' dur='1s' begin='-0.6428571428571429s' repeatCount='indefinite'/>
  </rect>
  <rect  x='47' y='40' width='6' height='20' rx='3' ry='3' fill='#000000' transform='rotate(154.28571428571428 50 50) translate(0 -30)'>
    <animate attributeName='opacity' from='1' to='0' dur='1s' begin='-0.5714285714285714s' repeatCount='indefinite'/>
  </rect>
  <rect  x='47' y='40' width='6' height='20' rx='3' ry='3' fill='#000000' transform='rotate(180 50 50) translate(0 -30)'>
    <animate attributeName='opacity' from='1' to='0' dur='1s' begin='-0.5s' repeatCount='indefinite'/>
  </rect>
  <rect  x='47' y='40' width='6' height='20' rx='3' ry='3' fill='#000000' transform='rotate(205.71428571428572 50 50) translate(0 -30)'>
    <animate attributeName='opacity' from='1' to='0' dur='1s' begin='-0.42857142857142855s' repeatCount='indefinite'/>
  </rect>
  <rect  x='47' y='40' width='6' height='20' rx='3' ry='3' fill='#000000' transform='rotate(231.42857142857142 50 50) translate(0 -30)'>
    <animate attributeName='opacity' from='1' to='0' dur='1s' begin='-0.35714285714285715s' repeatCount='indefinite'/>
  </rect>
  <rect  x='47' y='40' width='6' height='20' rx='3' ry='3' fill='#000000' transform='rotate(257.14285714285717 50 50) translate(0 -30)'>
    <animate attributeName='opacity' from='1' to='0' dur='1s' begin='-0.2857142857142857s' repeatCount='indefinite'/>
  </rect>
  <rect  x='47' y='40' width='6' height='20' rx='3' ry='3' fill='#000000' transform='rotate(282.85714285714283 50 50) translate(0 -30)'>
    <animate attributeName='opacity' from='1' to='0' dur='1s' begin='-0.21428571428571427s' repeatCount='indefinite'/>
  </rect>
  <rect  x='47' y='40' width='6' height='20' rx='3' ry='3' fill='#000000' transform='rotate(308.57142857142856 50 50) translate(0 -30)'>
    <animate attributeName='opacity' from='1' to='0' dur='1s' begin='-0.14285714285714285s' repeatCount='indefinite'/>
  </rect>
  <rect  x='47' y='40' width='6' height='20' rx='3' ry='3' fill='#000000' transform='rotate(334.2857142857143 50 50) translate(0 -30)'>
    <animate attributeName='opacity' from='1' to='0' dur='1s' begin='-0.07142857142857142s' repeatCount='indefinite'/>
  </rect>
</svg>

非常感谢!

最佳答案

我不确定我是否理解“提取”的意思,但请注意 React 支持 SVG:

定义:

const Spinner = () => (
  <svg width='48px' height='48px' xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" className="uil-default">
    <rect x="0" y="0" width="100" height="100" fill="none" className ="bk" />
    <rect  x='47' y='40' width='6' height='20' rx='3' ry='3' fill='#000000' transform='rotate(0 50 50) translate(0 -30)'>
      <animate attributeName='opacity' from='1' to='0' dur='1s' begin='-1s' repeatCount='indefinite'/>
    </rect>
  ...
  </svg>);

然后在你的 React 应用中像这样使用:

<Spinner />

关于javascript - 从已经包含动画的文件渲染和动画化 SVG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54818320/

相关文章:

javascript - 将参数从屏幕传递到 Header 组件 react 导航

javascript - 从日期中删除 1 周并保留格式

javascript - Chart.js 自定义图表

javascript - 显示 :none (hidden) element (chrome & opera) 上的鼠标悬停事件

html - 网站 "too short"页脚下方有空白怎么办?

javascript - 语义用户界面( react ): Responsive vertical menu by changing to horizontal on mobile devices

javascript - 将本地 JSON 加载到 AngularJS 中的 Jasmine/Karma 单元测试中

css - 有什么方法可以在 block 中的多行堆栈的当前行中选择最后一个元素?

reactjs - 如何处理 React-admin 中的服务器错误?

javascript - 状态给出不正确的结果