


Introduction to SVG 2D in HTML5 11—User interactivity (animation) introduction and application_html5 tutorial skills
SVG has good user interactivity, such as:
1. SVG can respond to most DOM2 events.
2. SVG can well capture the movement of the user's mouse through cursor.
3. Users can easily achieve zoom and other effects by setting the value of the zoomAndPan attribute of the svg element.
4. Users can easily combine animations with events to complete some complex effects.
By attaching events to SVG elements, we can use scripting language to easily complete some interactive tasks. SVG supports most DOM2 events, such as onfocusin, onfocusou, onclick, onmousedown, onmouseup, onmousemove, onmouseout, onload, onresize, onscroll and other events. In addition to these, SVG also provides unique animation-related events, such as: onroom, onbegin, onend, onrepeat, etc.
Everyone is familiar with the incident, so I won’t go into details.
Animation method
SVG uses text to define graphics. This document structure is very suitable for creating animations. To change the position, size and color of graphics, you only need to adjust the corresponding properties. In fact, SVG has properties specially designed for various event processing, and many of them are even specially tailored for animation. In SVG, there are several ways to implement animation:
1. Use SVG animation elements. This will be highlighted below.
2. Use scripts. Using DOM operations to start and control animations is already a mature technology. There is a small example below.
3. SMIL (Synchronized Multimedia Integration Language). If you are interested, please refer to: http://www.w3.org/TR/2008/REC-SMIL3-20081201/.
The following example contains several of the most basic animations in SVG:
xmlns="http://www.w3.org /2000/svg" version="1.1">
animate and animateTransform are used to perform other types of animation-->
It's alive!
begin="3s" dur="6s" fill="freeze" />
begin="3s" dur="6s" fill="freeze" />
begin="3s" dur="6s" fill="freeze" />
g>
Put this code into the body of the html document and run it to see the effect of the animation.
Public attributes of animated elements
Category 1: Specifying target elements and attributes
xlink:href
This should be familiar , pointing to the element on which the animation is performed. This element must be defined in the current SVG document fragment. If this attribute is not specified, the animation will be applied to its parent element.
attributeName = "
This attribute specifies the attribute to which animation is applied. If the attribute has a namespace (don't forget, SVG is essentially an XML document), this namespace must also be added. For example, in the following example, different aliases are given to xlink. Here, animate uses namespace when specifying attributes:
xmlns:xlink="http ://www.w3.org/1999/xlink">
attributeType = "CSS | XML | auto (default value)"
This attribute specifies the namespace of the attribute value. This The meanings of several values are as follows:
CSS: Represents that the attribute specified by attributeName is a CSS attribute.
XML: The attribute specified by attributeName is an attribute under the XML default namespace (note that svg documents are essentially xml documents).
auto: means first searching for the attribute specified by attributeName in the CSS attribute. If not found, the attribute will be searched for in the default XML namespace.
Category 2: Attributes that control animation time
The following attributes are all animation time attributes; they control the timeline of animation execution, including how to start and end the animation, and whether to repeat the animation. Whether to save the end state of the animation, etc.
begin = "begin-value-list"
This attribute defines the start time of the animation. Can be a series of time values separated by semicolons. It can also be some other value that triggers the animation to start. Such as events, shortcut keys, etc.
dur = Clock-value | "media" | "indefinite"
defines the duration of the animation. Can be set to a value displayed in clock format. It can also be set to the following two values:
media: Specifies that the animation time is the duration of the internal multimedia element.
indefinite: Specify the animation time to be infinite.
The clock format refers to the following legal value formats:
:30:03= 2 hours, 30 minutes and 3 seconds
:00:10.25 = 50 hours, 10 seconds and 250 milliseconds
:33 = 2 minutes and 33 seconds
:10.5 = 10.5 seconds = 10 seconds and 500 milliseconds
.2h= 3.2 hours = 3 hours and 12 minutes
min = 45 minutes
s = 30 seconds
ms = 5 milliseconds
. 467= 12 seconds and 467 milliseconds
.5s = 500 milliseconds
:00.005 = 5 milliseconds
end = "end-value-list"
defines the end time of the animation. Can be a series of values separated by semicolons.
min = Clock-value | "media"
max = Clock-value | "media"
Sets the maximum and minimum value of the animation duration.
restart = "always" | "whenNotActive" | "never"
Set whether the animation can be restarted at any time. always means the animation can start at any time. whenNotActive means it can only be restarted when it is not playing, for example, the previous playback has ended. never means the animation cannot be restarted.
repeatCount = numeric value | "indefinite"
Set the number of times the animation repeats. Indefinite represents infinite repetition.
repeatDur = Clock-value | "indefinite"
Set the total animation time of repetition. Indefinite represents infinite repetition.
fill = "freeze" | "remove(default value)"
Sets the state of the element after the animation ends. Freeze means that the element stays in the final state of the animation after the animation ends. remove means that after the animation ends, the element returns to the state before the animation. This is the default value.
Category 3: Attributes that define animation values
These attributes define the values of the attributes being animated. In fact, it defines some algorithms for key frames and interpolation.
calcMode = "discrete | linear (default value) | paced | spline"
defines the animation interpolation method: discrete: discrete, no interpolation; linear: linear interpolation; paced: step interpolation; spline: sample Strip interpolation. The default is linear (linear interpolation), but if the attribute does not support linear interpolation, discrete interpolation will be used.
values = "
- "
defines a semicolon-separated list of values for animation keyframes. Support vector value.
keyTimes = "
- "
Defines a semicolon-separated list of animation keyframe times. This corresponds to values one-to-one. This value is affected by the interpolation algorithm. If it is linear and spline interpolation, the first value of keyTimes must be 0 and the last value must be 1. For discrete non-interpolation methods, the first value of keyTimes must be 0. For the step interpolation method, it is obvious that keyTimes is not needed. And if the animation's duration is set to infinite, keyTimes are ignored.
keySplines = "
- "
This attribute defines the control points during spline interpolation (Bessel interpolation). Obviously, it only works when the interpolation mode is selected as spline. The values in this list range from 0 to 1.
from = "
to = "
by = "
Define the start value, end value and step value of the animation attribute . Note here: if values have already been specified, any from/to/by values will be ignored.
Category 4: Properties that control whether the animation is incremental
Sometimes, it is very useful if the relevant value is not set to an absolute value, but to an incremental value, using additive Properties can serve this purpose.
additive = "replace(default value) | sum"
This attribute controls whether the animation is incremental. sum means that the animation will be larger on related attribute values or other low-priority animations. replace is the default value, which means that the animation will overwrite related attribute values or other low-priority animations. Look at a small example:
< ;/rect>
This example demonstrates the animation effect of increasing rectangular width.
Sometimes, it is also very useful if the repeated animation results are superimposed. This can be achieved by using the accumulate attribute.
accumulate = "none (default value) | sum"
This attribute controls whether the animation effect is cumulative. None is the default value, which means repeated animations are not accumulated. sum means that repeated animation effects are cumulative. For single execution animations, this property has no meaning. Let’s look at a small example:
This example demonstrates that the length of the rectangle increases with each iteration.
Summary of animation elements
SVG provides the following animation elements:
1. animate element
This is the most basic animation element and can be directly used for related animation elements. Properties provide values at different points in time.
2. set element
This is the abbreviation of animate element and supports all attribute types, which is especially convenient when animating non-numeric attributes (such as visibility). The set element is non-incremental and the related attributes have no effect on it. The animation end value type specified by to must conform to the value type of the attribute.
3. animateMotion element
Lu Jin animation element. Most of the attributes of this element are the same as above, with only the following few differences:
calcMode = "discrete | linear | paced | spline"
The default value of this attribute is different. The default in this element is paced. .
path = "
The path along which the animation element moves, the format is consistent with the format of the value of the d attribute of the path element.
keyPoints = "
The value of this attribute is a series of floating point values separated by semicolons, and the value range of each value is 0~1. These values represent the distance moved at the corresponding time point specified by the keyTimes attribute. The specific distance here is determined by the browser itself.
rotate =
This attribute specifies the angle of rotation when the element moves. The default value is 0, the number represents the angle of rotation, and auto means rotating in the direction of the road. Object. Auto-reverse means turning in the opposite direction to the moving direction.
In addition, the values of from, by, to, and values of the animateMotion element are composed of coordinate pairs; the x value and the y value are separated by commas or spaces. Each coordinate pair is separated by a semicolon. For example, from="33,15" means that the starting point x coordinate is 33 and the y coordinate is 15.
There are two ways to specify the motion path: one is to directly give the path attribute. Assignment, one is to use mpath element as the child element of animateMotionde. If both methods are used, the priority of using mpath element is higher than values, from, by, to. 🎜>Look at a small example:
"http://www.w3. org/Graphics/SVG/1.1/DTD/svg11.dtd">
xmlns="http:// www.w3.org/2000/svg" version="1.1"
xmlns:xlink="http://www.w3.org/1999/xlink" >
< circle cx="400" cy="250" r="17.64" fill="blue"/>
< mpath xlink:href="#path1"/>
4. animateColor element color animation element. This is an outdated element. Basically all functions can be replaced by animate, so don’t use it anymore.
5. animateTransform element Transform animation element. Take a look at some special attributes:
type = "translate | scale | rotate | skewX | skewY"
This attribute specifies the type of transformation, translate is the default value.
The values of from, by and to are corresponding parameters of the transformation. This is consistent with the transformation mentioned above. values is a series of values separated by semicolons.
Elements and attributes that support animation effects
Basically all graphic elements (path, rect, ellipse, text, image...), container elements (svg, g, defs, use, switch, clipPath, mask. ..) all support animation. Basically most properties support animation effects. Please refer to the official documentation for detailed instructions.
Use DOM to implement animation SVG animation can also be completed using scripts. The details of DOM will be introduced later. Here is a simple example:
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
xmlns="http://www.w3.org/2000/svg"
onload="StartAnimation(evt)" version="1.1">
实用参考:
脚本索引:http://msdn.microsoft.com/zh-cn/library/ff971910(v=vs.85).aspx
开发中心:https://developer.mozilla.org/en/SVG
热门参考:http://www.chinasvg.com/
官方文档:http://www.w3.org/TR/SVG11/
SVG动画技术:http://msdn.microsoft.com/zh-cn/library/gg589525(v=vs.85).aspx

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



When Microsoft launched Windows 11, it brought a lot of changes. One of the changes is an increase in the number of user interface animations. Some users want to change the way things appear, and they have to find a way to do it. Having animations makes it feel better and more user-friendly. Animation uses visual effects to make the computer look more attractive and responsive. Some of them include sliding menus after a few seconds or minutes. There are many animations on your computer that can affect PC performance, slow it down, and interfere with your work. In this case you have to turn off animation. This article will introduce several ways that users can improve the speed of their animations on PC. You can apply the changes using Registry Editor or a custom file you run. How to improve animations in Windows 11

CSS animation: How to achieve the flash effect of elements, specific code examples are needed. In web design, animation effects can sometimes bring a good user experience to the page. The glitter effect is a common animation effect that can make elements more eye-catching. The following will introduce how to use CSS to achieve the flash effect of elements. 1. Basic implementation of flash First, we need to use the animation property of CSS to achieve the flash effect. The value of the animation attribute needs to specify the animation name, animation execution time, and animation delay time
![Animation not working in PowerPoint [Fixed]](https://img.php.cn/upload/article/000/887/227/170831232982910.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
Are you trying to create a presentation but can't add animation? If animations are not working in PowerPoint on your Windows PC, then this article will help you. This is a common problem that many people complain about. For example, animations may stop working during presentations in Microsoft Teams or during screen recordings. In this guide, we will explore various troubleshooting techniques to help you fix animations not working in PowerPoint on Windows. Why aren't my PowerPoint animations working? We have noticed that some possible reasons that may cause the animation in PowerPoint not working issue on Windows are as follows: Due to personal

How to use SVG to achieve image mosaic effect without using Javascript? The following article will give you a detailed understanding, I hope it will be helpful to you!

We often use ppt in our daily work, so are you familiar with every operating function in ppt? For example: How to set animation effects in ppt, how to set switching effects, and what is the effect duration of each animation? Can each slide play automatically, enter and then exit the ppt animation, etc. In this issue, I will first share with you the specific steps of entering and then exiting the ppt animation. It is below. Friends, come and take a look. Look! 1. First, we open ppt on the computer, click outside the text box to select the text box (as shown in the red circle in the figure below). 2. Then, click [Animation] in the menu bar and select the [Erase] effect (as shown in the red circle in the figure). 3. Next, click [

How to use Vue to implement typewriter animation special effects Typewriter animation is a common and eye-catching special effect that is often used in website titles, slogans and other text displays. In Vue, we can achieve typewriter animation effects by using Vue custom instructions. This article will introduce in detail how to use Vue to achieve this special effect and provide specific code examples. Step 1: Create a Vue project First, we need to create a Vue project. You can use VueCLI to quickly create a new Vue project, or manually

This website reported on January 26 that the domestic 3D animated film "Er Lang Shen: The Deep Sea Dragon" released a set of latest stills and officially announced that it will be released on July 13. It is understood that "Er Lang Shen: The Deep Sea Dragon" is produced by Mihuxing (Beijing) Animation Co., Ltd., Horgos Zhonghe Qiancheng Film Co., Ltd., Zhejiang Hengdian Film Co., Ltd., Zhejiang Gongying Film Co., Ltd., Chengdu The animated film produced by Tianhuo Technology Co., Ltd. and Huawen Image (Beijing) Film Co., Ltd. and directed by Wang Jun was originally scheduled to be released in mainland China on July 22, 2022. Synopsis of the plot of this site: After the Battle of the Conferred Gods, Jiang Ziya took the "Conferred Gods List" to divide the gods, and then the Conferred Gods List was sealed by the Heavenly Court under the deep sea of Kyushu Secret Realm. In fact, in addition to conferring divine positions, there are also many powerful evil spirits sealed in the Conferred Gods List.

Microsoft Windows 11 includes many new features and functions. The user interface has been updated and the company has also introduced some new effects. By default, animation effects are applied to controls and other objects. Should I disable these animations? Although Windows 11 features visually appealing animations and fade effects, they can cause your computer to feel sluggish to some users as they add a bit of lag to certain tasks. It's easy to turn off animations for a more responsive user experience. After we see what other changes have been made to the operating system, we'll walk you through how to turn animation effects on or off in Windows 11. We also have an article on how to
