Many basic elements have been introduced before, including structure-related combinations and reuse elements. Here we will briefly summarize the remaining relevant elements in the document structure of SVG, and then move forward to appreciate other features of SVG.
The elements of SVG documents can basically be divided into the following categories:
•Animation elements: animate, animateColor, animateMotion, animateTransform, set;
•Explanation elements: desc, metadata, title;
•Graphics Elements: circle, ellipse, line, path, polygon, polyline, rect;
• Structural elements: defs, g, svg, symbol, use;
• Gradient elements: linearGradient, radialGradient;
• Other elements :a,altGlyphDef,clipPath,color-profile,cursor,filter,font,font-face,foreignObject,image,marker,mask,pattern,script,style,switch,text,view, etc.
Among them, graphic elements, gradient elements, text, image elements and combinations have all been introduced. Here are several other elements related to structure.
View-svg element
You can place any other elements in any order within the svg element, including nested svg elements.
The commonly used attributes supported by the svg element are id, class, x, y, width, height, viewBox, preserveAspectRatio, and related attributes of fill and stroke.
The events supported by the svg element are also commonly used onload, onmouseover, onmousemove, onmousedown, onmouseup, onclick, onfocusin, onfocusout, onresize, onscroll, onunload, etc. Not much to say about the svg element. For a complete list of attributes and events, please refer to the official documentation later.
Explanatory elements-desc element and title element
Each container element (an element that can contain other container elements or graphic elements, such as: a, defs, glyph, g, Marker, mask, missing-glyph, pattern, svg, switch and symbol) and graphic elements can contain desc and title elements, both of which are auxiliary elements used to explain related situations; their contents are text . When the SVG document is rendered, these two elements will not be rendered into the graphic. The difference between these two elements is not too big. In some implementations, title appears as prompt information, so title is usually placed in the first position of the parent element.
Typical usage is as follows:
< ;svgxmlns="http://www.w3.org/2000/svg" version="1.1"width="4in"height="3in">
Companysalesbyregion< ;/title>
Thisisabarchartwhichshows
companysalesbyregion.
🎜>
Usually, the outermost svg element should be accompanied by a title description, so that the program is more readable.
Marker-marker element
Marker defines graphic elements (arrows and multi-point markers) attached to one or more vertices (path, line, polyline or polygon vertices) ). Arrows can be made by attaching a marker to the start or end point of a path, line, or polyline. Multipoint markers can attach a marker to all vertices of a path, line, polyline or polygon. The mark is defined by the marker element, and then just set the relevant attributes (marker, marker-start, marker-mid, and marker-end) in path, line, polyline or polygon. Take an example:
viewBox="0040002000"version="1.1"
xmlns="http://www.w3.org/2000/svg">
viewBox="001010"refX="0"refY="5"
markerUnits="strokeWidth"
markerWidth="4"markerHeight="3"
orient="auto">
Placinganarrowheadattheendofapath. ;/desc>
fill="none"stroke="black"stroke-width="100"
marker-end="url(#Triangle)"/> ;
Let’s take a detailed look at marker-related knowledge:
1. Marker is a container element that can store graphic elements, container elements, animations, gradient elements, etc. in any order.
2. The marker element can create a new view window: set the value of the viewBox.
3. Marker’s more important attributes:
markerUnits="strokeWidth|userSpaceOnUse"
This attribute defines the coordinate system used by the attributes markerWidth, markerHeight and marker content. This attribute has two optional values. The first value, strokeWidth, is the default value, which means that the units of the coordinate system used by the attributes markerWidth, markerHeight and the content of the marker are equal to the value set by the stroke-width of the graphic element that references the marker.
For example, in the above example, the width of the marker element is 400 and the height is 300. However, do not be confused. The coordinates used by the path in the mark element are the new user coordinate system set by the viewBox.
Another value of this attribute is userSpaceOnUse, which means that the attributes markerWidth, markerHeight and the content of the marker use the coordinate system of the graphic element that references the marker.
refX, refY: Define the position coordinates of the referenced point aligned with the marker. For example, in the above example, the referenced point is the end point, and it should be aligned to the (0,5) position of the marker. Note that refX and refY use the end-user coordinate system transformed by the viewBox.
markerWidth, markerHeight: The width and height of the marker window. This is easy to understand.
orient: defines the angle of marker rotation. You can specify an angle or assign auto directly.
auto means that the positive direction of the x-axis is rotated according to the following rules :
a. If the point where the marker is located only belongs to one path, the positive x-axis direction of the marker is the same as the direction of the path. See the example above.
b. If the point where the marker is located belongs to two different paths, then the positive x-axis direction of the marker is consistent with the angular bisector of the angle between the two paths.
4. Marker attributes of graphic elements
If a graphic element wants to reference a marker, it needs to use related attributes, mainly these three: marker-start (put the referenced marker at the starting point), marker- mid (place the referenced marker at all points except the starting point and end point), marker-end (place the referenced marker at the end point). The values of these three attributes may be none (meaning not to reference the marker), marker reference (referencing a certain marker), and inherit (no need to say more about this).
You can also see the usage of markers from the above example.
Script and style-script element and style element
In fact, basically all attributes (for all elements, not just text) can be associated with an element using CSS, And all CSS properties are available in SVG images. You can directly use the style attribute to style the element, or refer to the style sheet to style the element. Stylesheets should not be parsed for XML files (because they occasionally contain characters that can cause problems), so they need to be placed in an XMLCDATA section. The same goes for scripts, which need to be placed in the XMLCDATA section. Look at the following CSS example:
Text
.abbreviation{text-decoration:underline;}
]]>
< /defs>
Colorscanbespecified
bytheir
R
G
Bvalues text>
orbykeywordssuchas
lightsteelblue,
Look at the script example again:
Scriptingtheonclickevent
functionhideReveal(evt){
varimageTarget=evt.target;
vartheFill=imageTarget.getAttribute("fill");
if(theFill=='white')
imageTarget.setAttribute("fill","url(#notes)");
else
imageTarget.setAttribute("fill","white");
}
]]>
patternTransform="rotate(15)"
patternUnits="userSpaceOnUse">
stroke-width="3"stroke="black"/>
stroke-width="3"stroke="black"/>
fill="url(#notes)"stroke="black"stroke-width="5"/>
条件处理-switch元素
条件处理属性是能控制所在元素渲染与否的属性。基本上大多数的元素(特别是图形元素)都可以指定条件处理属性。条件处理属性有3个:requiredFeatures,requiredExtensions和systemLanguage。这些属性就是一组测试,都允许指定一个值列表(前面两个属性是空格隔开的,语言这个属性是使用逗号隔开的),默认值都为true。
SVG的switch元素提供了按指定条件渲染的能力。switch元素是一个容器元素,可以包含图形元素,解释性元素,动画元素,a,foreignObject,g,image,svg,switch,text,use等元素。switch元素会按顺序检查直接子元素的条件处理属性,然后渲染满足自身条件的的第一个子元素,其他的子元素都会被忽略。这些属性与display属性一样,只会影响直接使用这些属性的元素的渲染,不会影响引用的元素(比如use引用的元素)。简单的说,这3个属性会影响a,altGlyph,foreignObject,textPath,tref,tspan,animate,animateColor,animateMotion,animateTransform,set等元素,不会影响defs,cursor,mask,clipPath,pattern等元素(这些元素那么不是可渲染的,要么就是引用别的元素)。
注意:子元素的display和visibility属性值并不影响switch元素条件判断的结果。
条件处理属性的取值列表参看官方文档,这里就看一个小例子:
x="10"y="10"width="322"height="502"opacity="0.6"
fill="black"stroke="none"filter="url(#gblurshadow)"/>
fill="black"stroke="none"/>
这个例子的意思简单的说就是:使用的浏览器支持filter特性,那么就绘制上面的矩形(带filter属性),如果不支持filter特性,就绘制下面的矩形。
其实更多的时候,用的比较多的属性是systemLanguage,就是文本的多语言处理能力。例如:
de-HAHA
en-haha
实用参考:
脚本索引: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/