Home  >  Article  >  Web Front-end  >  How to make cool dynamic icons with SVG? (code example)

How to make cool dynamic icons with SVG? (code example)

青灯夜游
青灯夜游Original
2018-09-11 16:18:214182browse

This chapter will introduce to you how to use SVG to create cool dynamic icons in HTML5? (code example). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Basic graphic elements

svg has some predefined shape elements: rectangle , circle , ellipse, straight line, polyline, polygon, path and text.




    
    

    
    

    
    

    
    

    
    Try SVG

2. Style and effect

The style of the svg element can be written as the attribute of the tag, or it can be written in style. Here are some main style attributes:

stroke: stroke color
stroke-width: stroke width
stroke-opacity: Transparency of stroke
fill: fill color, i.e. background
fill-opacity: transparency of fill color
Transform: graphics transformation, similar to css3 transform

svg also supports many filter effects, such as gradient, shadow, blur, image mixing, etc. You don’t need to know that much. For example, if you want to draw a few colored circles, just use circle and fill.

Note: transform defaults to the upper left corner of svg as the base point, not the center of the circle or other center. The upper left corner is the origin of the svg coordinate system. To understand transform and coordinate system, you can refer to here.

3. Auxiliary elements

svg has several auxiliary elements:

The element is usually used to group related graphic elements for unified operations, such as rotating, scaling or adding related styles.
Realizes the reuse of existing SVG graphics. You can reuse a single SVG graphic element or a group element defined by .
Internally defined elements will not be displayed directly. You do not need to define the style in advance, but define it when instantiating it using .
can create its own window, which has the grouping function of and the initial invisible feature of .

For the transform base point problem mentioned above, you can reset the base point by nesting the tag and offsetting the position of . Draw several horizontally arranged circles as follows, and set different zoom sizes


    
        
    
    
        
    
    
        
    
    
        
    

4. Realization of animation

Animation effect of svg It is implemented based on the animation tag element:

 To achieve the transition effect of a single attribute;
​​Realize transform animation effect;
​​Achieve path animation effects.

The writing method of svg is very flexible. The style can be written as a tag attribute or in style. The animation tag can be specified through xlink or written inside the animation element. The following demonstrates the xlink writing method of animateTransform:


    
    
        attributeName="transform"  
        type="scale"  
        begin="0s"    
        dur="3s"      
        from="1"      
        to="2"        
        repeatCount="indefinite"   
    />

The animation in the above example is the transition from A to B. To form a smooth cycle, at least three key points must be defined. animateTransform supports more flexible attribute settings:

values: values ​​of multiple key points, replacing from and to, such as values="0;1;0"

keyTimes: followed by values Correspondingly, the time points of each point

calcMode: animation speed mode. discrete | linear | paced | spline

keySplines: Set the Bezier control points of motion, valid when calcMode is spline

##5. Code examples

circle draws a circle, fill coloring, wrap and position it with the g tag, transform sets the initial deformation, and animateTransform sets the animation. Now look at the code, I believe you will no longer be confused:


    
        
            
        
    
    
        
            
        
    
    
        
            
        
    
    
        
            
        
    

Rendering:

How to make cool dynamic icons with SVG? (code example)

You can also use js to control the properties of svg, control The animation process of svg is made into an icon button that can respond to clicks and other events.


The above is the detailed content of How to make cool dynamic icons with SVG? (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn