SVG basic usage

1, Introduction

SVG It is a program language that uses XML to describe two-dimensional graphics and drawings. It became a W3C recommendation on January 14, 2003;
1. Is a scalable Vector graph shape
2. Is to use xml Format defines vector based graphics for the network
3. Enlarge or change the size of the picture without loss of quality
4. svg files must be used svg suffix to save

Creation of SVG graphics (in two ways)

html Create directly in:
	<svg width="" height="" version="1.1" 
		// Insert the desired drawing
External creation(This type of document needs to be described in the document): 
	<?xml version="1.0" standalone="no">
	<!DOCtype svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
	<svg width="100%" height="100%" version="1.1"
		// Insert the desired picture

svg files can be embedded in HTML through the following tags (external imported. svg files):

	<embed src="rect.svg" width="300" height="100" type="image/svg+xml"
		pluginspage="" />
	<object data="rect.svg" width="300" height="100" 	
	codebase="" />

	<iframe src="rect.svg" wodth="300" height="100"></iframe>

II. Create SVG graphics

The created elements will not take effect until they are placed in the svg element
1, Rectangle (< rect > label)

<svg width="100%" height="100%" version="1.1"
	<rect width="30" height="40" x="20" y="20"
    style="fill:rgb(0,0,255); stroke-width:1; stroke:rgb(0,0,0)" />
	rect Properties:
		x: Defines the left position of the rectangle
		y: Defines the position of the top of the rectangle
		width: Defines the width of the rectangle
		height: Defines the height of the rectangle
		style: Style rectangle

***style contains (svg shape common):
Fill: defines the fill color of the rectangle
Stroke width: defines the width of the rectangular border
stroke: defines the color of the rectangular border
Fill opcity: defines the fill color transparency
Stroke opacity: defines the transparency of the border color
opcity: defines the overall transparency of the drawing

2, Circle (< circle > label)

<circle cx="50" cy="50" r="20" stroke="black" stroke-width="2" 
	cx and cy Property defines the shape of the circle x and y coordinate
	stroke: Define black round background
	stroke-width: Defines the width of the circle border
	fill: Define border color

3, Ellipse (< ellipse > label)

<ellipse cx="200" ct="150px"
style="fill:rgb(200, 100, 50); stroke:rgb(0,0,100); stroke-width:2;">

	cx: Polka Dot x coordinate
	cy: Polka Dot y coordinate
	rx: Horizontal radius
	ry: Vertical radius

IV. line (< line > label)

<line x1="0" y1="0" x2="300" y2="300" style="stroke: rgb(99,99,99); stroke-width:2" />

x1, y1 Where the line begins; x2, y2 Where the line ends

V. polygon (< polygon > label)

<polygon points="220,100 300,210 170,250" style="fill: #ccc; stroke: #000; stroke-width:1" />

Notes: points Attribute defines the of each corner of the polygon x and y coordinate

Vi. broken line (< polyline > label)

<polyline points="0,0 0,20 20,20 20,40 40,40 40,60" 
style="fill:white; stroke:red; stroke-width: 2" />

Notes: points Property defines the of each corner x and y coordinate

7, Path (< Path > label)

<path d="M250 150 L150 350 L350 350 Z" />

Path data:
	M = moveto(Start tracing points)
	L = lineto(Draw the line where you want to go)
	H = horizontal lineto(Where to draw the line horizontally)
	V = vertical lineto(Where to draw a vertical line)
	C = curveto(Corner position of bending curve, including multiple coordinates)
	S = smooth curveto(Position of curve angle of smooth curve, including multiple coordinates)
	Q = quadratic Belzier curve(secondary Belzier curve)
	T = smooth quadratic Belzier curveto(Smooth quadratic Belzier curve)
	A = elliptical Arc(Elliptic arc)
	Z = closepath(Close path)

3, Gradual change

SVG gradient must be defined in the label. Gradient types include: linear gradient and radioactive gradient
: it can define special elements such as gradients

Linear gradient (< LinearGradient > label):
1. Horizontal gradient: Y1 = Y2, X1= x2
2. Vertical gradient: X1 = X2, Y1= y2
3. Angular gradient: X1= x2, y1 != y2

// definition
	<linearGradient id="orange_red" x1="0%" y1="0%" x2="100%" y2="0%">
		<stop offset="0%"
		style="stop-color:rgb(255,255,0); stop-opacity: 1" />
		<stop offset="100%"
		style="stop-color:rgb(255,0,0); stop-opacity: 1" />
// application
<ellipse cx="200" cy="190" rx="85" ry="50" style="fill: url(#orange_red)" /> 


1. The fill:url(#orange_red) attribute links the ellipse element to this gradient
2. The x1,x2,y1,y2 attributes of the label define the start and end positions of the gradient
3. The label defines the components of the gradient color, and the offset attribute defines the start and end positions of the gradient

Radioactive gradient (< RadialGradient > label):

// definition
	<radialGradient id="grey_blue" cx="50%" cy="50%" r="50%"
	fx="50%" fy="50%">
		<stop offset="0%"
			style="stop-color: rgb(200,200,200); stop-opacity:0">
		<stop offset="100%"
			style="stop-color: rgb(0,0,255); stop-opacity: 1">
<ellipse cx="230" cy="200" rx="110" ry="100" style="fill:url(#grey_blue)"/>

1. The cx,cy attributes define the outer circle
2. The fx,fy attributes define the inner circle


The elements beginning with fe are all filters, which are Gaussian blur as follows:
Gaussian blur (< fegaussian Blur > label)

// definition
	<filter id="Gaussian_Blur">
		<feGaussianBlur in="SourceGraphic" stdDeviation="3" />
// application
<ellipse cx="200" cy="150" rx="70" ry="40"
	style="fill: #ff00; stroke: #0000; stroke-width: 2;
	filter: url(#Gaussian_Blur)"

1. The filter:url attribute is used to link elements to filters
2. stdDeviation: the attribute is used to define the degree of fuzziness
3. in = "SourceGraphic" definition has the effect of creating the whole image

Common elements in containers

It refers to some tag elements often used in svg:

defs: It is used to store the images to be used in the future fill:url(id)Reference
g: For other SVG Elements are grouped into containers, and setting attributes and transformations on their elements will affect g All elements grouped
mask: Used to define a alpha Channel to synthesize the current object into the background
pattern: Defines a fill object that can be repeated and tiled within its host element to fill its element
marker: Used in a given<path>,<line>,<polyline>or<polygon>Drawing an arrow or multipoint marker on an element
symbol:Define a template element,adopt use Attribute reference


Tags: xml

Posted by phppucci on Tue, 17 May 2022 03:19:30 +0300