gpt4 book ai didi

javascript - 为什么我的动画 SVG 动画停止改变颜色?

转载 作者:行者123 更新时间:2023-12-02 20:59:03 25 4
gpt4 key购买 nike

我的网站上有一些 SVG 动画。一个矩形变得越来越大,越来越小,迅速变成随机颜色,同时“买我们的垃圾”字样从其中跳出来。

由于某种原因,它停止改变颜色。我不记得更改或删除任何内容。为什么停了?我可以做什么来修复它?

function rand(max){
return Math.floor(Math.random() * (max+1));
}

function init() {
let refreshRate = Math.random() *1000 + 50; // 10 times per second
setInterval(function(){document.getElementById("RectElement").setAttribute("fill","rgb("+rand(255)+", "+rand(255)+", "+rand(255)+")");},refreshRate);
}

window.document.onload = init;
<?xml version="1.0" standalone="no"?>
<svg width="8cm" height="3cm" viewBox="0 0 800 300" xmlns="http://www.w3.org/2000/svg">
<desc>Example anim01 - demonstrate animation elements</desc>
<rect x="1" y="1" width="900" height="400"
fill="none" stroke="none" stroke-width="2" />
<!-- The following illustrates the use of the 'animate' element
to animate a rectangles x, y, and width attributes so that
the rectangle grows to ultimately fill the viewport. -->
<rect id="RectElement" x="300" y="100" width="300" height="100"
fill="rgb(0,255,0)" >
<animate attributeName="fill" begin="0" dur="1.3"
repeatCount="indefinite"/>
<animate attributeName="x" begin="0s" dur="1.3s"
fill="freeze" from="300" to="0" repeatCount="indefinite"/>
<animate attributeName="y" begin="0s" dur="1.3s"
fill="freeze" from="100" to="0" repeatCount="indefinite"/>
<animate attributeName="width" begin="0s" dur="1.3s"
fill="freeze" from="300" to="800" repeatCount="indefinite"/>
<animate attributeName="height" begin="0s" dur="1.3s"
fill="freeze" from="100" to="300" repeatCount="indefinite"/>


</rect>
<!-- Set up a new user coordinate system so that
the text string's origin is at (0,0), allowing
rotation and scale relative to the new origin -->
<g transform="translate(100,100)" >
<!-- The following illustrates the use of the 'set', 'animateMotion',
'animate' and 'animateTransform' elements. The 'text' element
below starts off hidden (i.e., invisible). At 3 seconds, it:
* becomes visible
* continuously moves diagonally across the viewport
* changes color from blue to dark red
* rotates from -30 to zero degrees
* scales by a factor of three. -->
<text id="TextElement" x="0" y="0"
font-family="Verdana" font-size="35.27" visibility="hidden" >
Buy our crap!
<set attributeName="visibility" to="visible"
begin="0s" dur="1.3s" fill="freeze" repeatCount="indefinite"/>
<animateMotion path="M 0 0 L 100 100"
begin="0s" dur="1.3s" fill="freeze" repeatCount="indefinite"/>

<animateTransform attributeName="transform"
type="rotate" from="-30" to="0"
begin="0s" dur="1.3s" fill="freeze" repeatCount="indefinite"/>
<animateTransform attributeName="transform"
type="scale" from="0.5" to="2" additive="sum"
begin="0s" dur="1.3s" fill="freeze" repeatCount="indefinite"/>
</text>
</g>
</svg>

矩形没有改变颜色。我对此还很陌生,所以请耐心等待,并在评论中提出您认为可以改善此问题的任何建议。

最佳答案

没有window.document.onload。将 window.document.onload = init; 更改为 window.onload = init;

GlobalEventHandlers.onload

Window: load event

关于javascript - 为什么我的动画 SVG 动画停止改变颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61398849/

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