📅  最后修改于: 2023-12-03 15:05:24.398000             🧑  作者: Mango
SVG (Scalable Vector Graphics) is a markup language used to create vector images. SVG images can be animated using the animate
element, which allows for the creation of smooth and dynamic motion.
To use animate
, you must first define the SVG element you want to animate. Once the element has been defined, you can add the animate
element as a child element. Here is an example of using animate
to change the x-coordinate of a rectangle:
<svg>
<rect x="0" y="0" width="100" height="100" fill="red">
<animate attributeName="x" from="0" to="200" dur="1s" fill="freeze" />
</rect>
</svg>
In this example, the animate
element changes the x
attribute of the rect
element from 0
to 200
over a duration of 1s
. The fill
attribute is set to freeze
, which ensures that the final value of x
is retained after the animation is complete.
The animate
element can animate a variety of attribute types, including numbers, colors, and transforms. Some of the most commonly used attribute types include:
cx
, cy
, r
(for circles and ellipses)x
, y
, width
, height
(for rectangles)points
(for polygons and polylines)d
(for path)Here is an example of using animate
to change the color of a circle:
<svg>
<circle cx="50" cy="50" r="25" fill="red">
<animate attributeName="fill" to="blue" dur="1s" fill="freeze" />
</circle>
</svg>
In this example, the animate
element changes the fill
attribute of the circle
element from red
to blue
over a duration of 1s
.
In addition to the attributeName
, from
, to
, and dur
attributes used in the examples above, there are several other attributes that can be used with the animate
element:
begin
- Specifies when the animation should start. Can be a time value or an event trigger.repeatCount
- Specifies how many times the animation should repeat. Use "indefinite"
for infinite repetitions.calcMode
- Specifies the calculation mode for the animation. Can be "linear"
, "paced"
, "discrete"
, or "spline"
.keyTimes
- Specifies the timing for each keyframe, as a comma-separated list of values between 0
and 1
.keySplines
- Specifies the easing function for each keyframe, as a comma-separated list of Bezier control points.The animate
element is a powerful tool for creating dynamic and engaging SVG animations. By combining animate
with other SVG elements and attributes, you can create complex animations that respond to user interactions and events.