Introduction and programming demonstration of SVG-based web page drawing API

Source: Internet
Author: User
Tags image filter

I. What is SVG?
SVG is a 2D graphic description language released by W3C in 1999. It is a markup language based solely on XML format.
The full name is extensible vector graphics, which is very different from traditional Raster graphics (JPG, PNG, GIF, etc.).
No. SVG is a 2D graphics development platform. It consists of two parts: XML-based data description and
The other part is a programmable API. Its key features support graphics, text, gradient fill, paint style, and graphics.
Special filter effects, such as Gaussian blur, will be demonstrated in code later. Also supports various mouse events and DOM
API. Almost all mainstream browsers support the reality and drawing of SVG graphics formats, and IE9 + and above have also started.
Supports SVG, and plug-ins are required in earlier versions of IE.
For more information about SVG visit here: http://www.w3.org/Graphics/SVG/About.html

Ii. svg api programming demonstration in JavaScript
Create and obtain SVG objects
Copy codeThe Code is as follows:
// Create svg object
Var mySvg = document. createElementNS ("http://www.w3.org/2000/svg", "svg ");
MySvg. setAttribute ("version", "1.2"); // IE9 + support SVG 1.1 version
MySvg. setAttribute ("baseProfile", "tiny ");
Container. appendChild (mySvg );

Create a rectangular image in SVG:
Copy codeThe Code is as follows:
Var c1 = document. createElementNS ("http://www.w3.org/2000/svg", "rect ");
C1.setAttribute ("x", "20 ");
C1.setAttribute ("y", "20 ");
C1.setAttribute ("width", "150 ");
C1.setAttribute ("height", "150 ");
C1.setAttribute ("fill", "rgb (0,0, 255 )");
C1.setAttribute ("stroke", "rgb (0, 0, 0 )");
C1.setAttribute ("stroke-width", "4 ");
MySvg. appendChild (c1 );

To draw text in SVG:
Copy codeThe Code is as follows:
// SVG draw text
Var stext = document. createElementNS ("http://www.w3.org/2000/svg", "text ");
Stext. setAttribute ("x", "700 ");
Stext. setAttribute ("y", "100 ");
Stext. setAttribute ("font-size", "18px ");
Stext. setAttribute ("fill", "# FF0000 ");
Var textString = document. createTextNode ("Hello SVG ");
Stext. appendChild (textString );
MySvg. appendChild (stext );

Processing of mouse click events and MouseUp events on SVG objects:
Copy codeThe Code is as follows:
// Mouse event handling
C1.addEventListener ("click", changeColor, false );
C2.addEventListener ("mouseup", changeColor, false );

Gaussian Blur using SVG image filter:
Copy codeThe Code is as follows:
<Div id = "blur-image-demo">
<Div id = "left" style = "width: 20%; "> </div>
<Div id = "right" style = "width: 80%;">
<Svg xmlns = "http://www.w3.org/2000/svg" version = "1.1">
<Defs>
<Filter id = "f1" x = "0" y = "0">
<FeGaussianBlur in = "SourceGraphic" stdDeviation = "5"/>
</Filter>
</Defs>
<Image x = "0" y = "0" width = "325" height = "471" xlink: href = "woniu.png" filter = "url (# f1)"/>
</Svg>
</Div>
</Div>

Running effect:
 
Source code, which can be copied and run directly
JavaScript Section
Copy codeThe Code is as follows:
Window. onload = function (){
// Get DIV
Var container = document. getElementById ("svgContainer ");
// Create svg object
Var mySvg = document. createElementNS ("http://www.w3.org/2000/svg", "svg ");
MySvg. setAttribute ("version", "1.2"); // IE9 + support SVG 1.1 version
MySvg. setAttribute ("baseProfile", "tiny ");
Container. appendChild (mySvg );

// Create svg shape-rectangle
Var c1 = document. createElementNS ("http://www.w3.org/2000/svg", "rect ");
C1.setAttribute ("x", "20 ");
C1.setAttribute ("y", "20 ");
C1.setAttribute ("width", "150 ");
C1.setAttribute ("height", "150 ");
C1.setAttribute ("fill", "rgb (0,0, 255 )");
C1.setAttribute ("stroke", "rgb (0, 0, 0 )");
C1.setAttribute ("stroke-width", "4 ");
MySvg. appendChild (c1 );

// Create svg shape-circle
Var c2 = document. createElementNS ("The http://www.w3.org/2000/svg", "circle ");
C2.setAttribute ("cx", "250 ");
C2.setAttribute ("cy", "100 ");
C2.setAttribute ("r", "60 ");
C2.setAttribute ("fill", "#996699 ");
C2.setAttribute ("stroke", "# AA99FF ");
C2.setAttribute ("stroke-width", "7 ");
MySvg. appendChild (c2 );

// Create svg shape-ellipse
Var c3 = document. createElementNS ("http://www.w3.org/2000/svg", "ellipse ");
C3.setAttribute ("cx", "450 ");
C3.setproperty ("cy", "100 ");
C3.setAttribute ("rx", "100 ");
C3.setAttribute ("ry", "50 ");
C3.setAttribute ("fill", "# FF0000 ");
C3.setAttribute ("stroke", "purple ");
C3.setAttribute ("stroke-width", "3 ");
MySvg. appendChild (c3 );

// Create svg shape-draw lines
For (var I = 0; I <10; I ++)
{
Var sline = document. createElementNS ("http://www.w3.org/2000/svg", "line ");
Var x1 = 580 + I * 10;
Console. log (x1 );

Sline. setAttribute ("x1", x1.toString ());
Sline. setAttribute ("y1", "10 ");
Sline. setAttribute ("x2", x1.toString ());
Sline. setAttribute ("y2", "180 ");
Sline. setAttribute ("stroke", "rgb (0,255, 0 )");
Sline. setAttribute ("stroke-width", "2 ");
MySvg. appendChild (sline );
}

// SVG draw text
Var stext = document. createElementNS ("http://www.w3.org/2000/svg", "text ");
Stext. setAttribute ("x", "700 ");
Stext. setAttribute ("y", "100 ");
Stext. setAttribute ("font-size", "18px ");
Stext. setAttribute ("fill", "# FF0000 ");
Var textString = document. createTextNode ("Hello SVG ");
Stext. appendChild (textString );
MySvg. appendChild (stext );

// Mouse event handling
C1.addEventListener ("click", changeColor, false );
C2.addEventListener ("mouseup", changeColor, false );
};
Function changeColor (evt ){
Var target = evt.tar get;
Target. setAttributeNS (null, "fill", "green ");
}

HTML section:
Copy codeThe Code is as follows:
<Html>
<Head>
<Title> Gloomyfish SVG Demo </title>
<Style>
# SvgContainer {
Width: 800px;
Height: 200px;
Background-color: # EEEEEE;
}
# Left {float: left ;}
# Right {float: right ;}
</Style>
</Head>
<Body>
<Div id = "svgContainer"> </div>
<Div id = "blur-image-demo">
<Div id = "left" style = "width: 20%; "> </div>
<Div id = "right" style = "width: 80%;">
<Svg xmlns = "http://www.w3.org/2000/svg" version = "1.1">
<Defs>
<Filter id = "f1" x = "0" y = "0">
<FeGaussianBlur in = "SourceGraphic" stdDeviation = "5"/>
</Filter>
</Defs>
<Image x = "0" y = "0" width = "325" height = "471" xlink: href = "woniu.png" filter = "url (# f1)"/>
</Svg>
</Div>
</Div>
</Body>
</Html>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.