gpt4 book ai didi

javascript - 如何从具有相同元素 ID 的 SVG 序列创建动画

转载 作者:行者123 更新时间:2023-11-30 19:38:46 25 4
gpt4 key购买 nike

我正在尝试从一系列 SVG 文件创建一些 2D 动画。

所有 SVG 文件都具有相同数量的元素,并且所有对应的元素都具有相同的元素 ID。我想创建动画,其中每个 svg 都被视为关键帧,并且通过在关键帧之间插入具有相同 id 的元素来生成动画。

我研究过使用 snap.svg,但我不确定如何跨不同的 svg 文件链接对象。

最佳答案

我会这样做:

  1. 我准备了一个 svg 根元素,在里面,一个在另一个之上的几个 svg 元素,每个元素都有它的 id

  2. 我在 css 中添加:除非 svg 元素是目标 display: none;

svg > svg:not(:target) {
display: none;
}
  1. 假设您的 svg 文件是:https://domain/.../rects.svg,如果您想定位其中一个 svg 元素,您可以使用 svg 的 id像这样:https://domain/.../rects.svg#svg_id

这是根 svg 的样子:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100px" height="100px" viewBox="-50 -50 100 100">
<defs>
<rect id="theRect" x="-30" y="-30" width="60" height="60" fill="none" stroke="red"></rect>
</defs>
<style type="text/css">
<![CDATA[
svg > svg:not(:target) {
display: none;
}
]]>
</style>


<svg viewBox="-50 -50 100 100" x="-50" y="-50" id="_0">
<use xlink:href="#theRect" transform="rotate(0)"></use>
</svg>
<svg viewBox="-50 -50 100 100" x="-50" y="-50" id="_1">
<use xlink:href="#theRect" transform="rotate(6)"></use>
</svg>
<svg viewBox="-50 -50 100 100" x="-50" y="-50" id="_2">
<use xlink:href="#theRect" transform="rotate(12)"></use>
</svg>
<svg viewBox="-50 -50 100 100" x="-50" y="-50" id="_3">
<use xlink:href="#theRect" transform="rotate(18)"></use>
</svg>
<svg viewBox="-50 -50 100 100" x="-50" y="-50" id="_4">
<use xlink:href="#theRect" transform="rotate(24)"></use>
</svg>
<svg viewBox="-50 -50 100 100" x="-50" y="-50" id="_5">
<use xlink:href="#theRect" transform="rotate(30)"></use>
</svg>
<svg viewBox="-50 -50 100 100" x="-50" y="-50" id="_6">
<use xlink:href="#theRect" transform="rotate(36)"></use>
</svg>
<svg viewBox="-50 -50 100 100" x="-50" y="-50" id="_7">
<use xlink:href="#theRect" transform="rotate(42)"></use>
</svg>
<svg viewBox="-50 -50 100 100" x="-50" y="-50" id="_8">
<use xlink:href="#theRect" transform="rotate(48)"></use>
</svg>
<svg viewBox="-50 -50 100 100" x="-50" y="-50" id="_9">
<use xlink:href="#theRect" transform="rotate(54)"></use>
</svg>
<svg viewBox="-50 -50 100 100" x="-50" y="-50" id="_10">
<use xlink:href="#theRect" transform="rotate(60)"></use>
</svg>
<svg viewBox="-50 -50 100 100" x="-50" y="-50" id="_11">
<use xlink:href="#theRect" transform="rotate(66)"></use>
</svg>
<svg viewBox="-50 -50 100 100" x="-50" y="-50" id="_12">
<use xlink:href="#theRect" transform="rotate(72)"></use>
</svg>
<svg viewBox="-50 -50 100 100" x="-50" y="-50" id="_13">
<use xlink:href="#theRect" transform="rotate(78)"></use>
</svg>
<svg viewBox="-50 -50 100 100" x="-50" y="-50" id="_14">
<use xlink:href="#theRect" transform="rotate(84)"></use>
</svg>
</svg>

这就是我制作动画的方式:

let i = 0; 
function Frame(){
let n = i % 15;
requestAnimationFrame(Frame);
theImg.setAttribute("src", `https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/rects.svg#_${n}`);
i++
}

Frame()
img{border:10px solid #d9d9d9}
<img id="theImg" width="100" height="100" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/rects.svg#_0" />

粗略地说,由于所有这些 svg 元素都在同一个文件中,您将无法多次使用相同的 id。您将需要唯一的 id

关于javascript - 如何从具有相同元素 ID 的 SVG 序列创建动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55644704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com