Home > Articles

Basic SVG Elements and Shapes

This sample chapter examines six of the SVG elements that provide basic graphic shapes we can use, and reuse, as visual components.
This chapter is from the book

This chapter is from the book

In This Chapter

  • The line Element

  • The rect Element

  • The circle Element

  • The ellipse Element

  • The polyline Element

  • The polygon Element

  • Color in SVG

  • Gradients in SVG

  • SVG Patterns

  • SVG DOM Interfaces for the SVG Basic Shapes

  • Other SVG DOM Interfaces

SVG provides several elements that describe commonly used graphic shapes.

In this chapter, we will examine six of the SVG elements that provide basic graphic shapes we can use, and reuse, as visual components. In particular, we will examine in detail the following elements and many of their attributes that SVG provides for a developer to modify the visual appearance produced by an SVG rendering engine:

  • The line element, which describes a straight line

  • The rect element, which describes a rectangle or square

  • The circle element, which describes a circle

  • The ellipse element, which describes an ellipse

  • The polyline element, which describes a shape created by a number of straight lines

  • The polygon element, which describes a closed multisided shape with straight edges

Each basic graphic shape will be described in this chapter. We will describe and demonstrate the ways in which each shape can be used and explore the techniques to use the permitted attributes to alter the visual appearance of each shape. In addition, we will also describe the interfaces in the Document Object Model that relate to each shape.

Not all SVG shapes can be described using the graphic shapes discussed in this chapter. The path element can express any arbitrary shape and is described in Chapter 6, "Paths in SVG." In this chapter, the focus is on static graphic shapes. The techniques to animate SVG graphic shapes are described in Chapter 11, "SVG Animation Elements." In addition, each of the SVG basic graphic shapes may be used in clipping paths, which are described in Chapter 9, "Clipping, Masking, Compositing."

In addition to the basic graphic shapes, the syntax for use of gradients and patterns in SVG will be described. SVG DOM interfaces relevant to the SVG basic graphic shapes, gradients, and patterns will also be described.

The line Element

The line element describes a single straight line. To draw a straight line in a coordinate system, you need to know the coordinates of each end of the line. In SVG, the coordinates of one end of the line are defined by the x1 and y1 attributes. The coordinates of the other end of the line are defined by x2 and y2 attributes.

Listing 3.1 shows four straight lines, each being created using a line element.

Listing 3.1 FourLines.svg—Creating Straight Lines Using the line Element

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg width="400px" height="250px">
<line x1="50px" y1="100px" x2="350px" y2="100px" stroke="black"/>
<line x1="20px" y1="50px" x2="20px" y2="350px" stroke="black"/>
<line x1="0px" y1="0px" x2="400px" y2="250px" stroke="black"/>
<line x1="300px" y1="30px" x2="200px" y2="50px" stroke="black"/>
</svg>

Figure 3.1 shows the onscreen appearance of Listing 3.1. Recall that when the value of the x attribute and the value of the y attribute equal 0, the top-left corner of the viewport is being referred to. The viewport is the area (of the potentially infinite SVG canvas) which is rendered onscreen.

Figure 3.1 Four lines created using the SVG line element.

The onscreen appearance of these lines is dull. No style information is present on the line elements in Listing 3.1. Listing 3.2 shows some possible ways in which we can style line elements.

Listing 3.2 FourLinesStyled.svg—Applying Style Information to line Elements

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg width="400px" height="250px">
<line x1="50px" y1="100px" x2="350px" y2="100px"
 style="stroke:red; stroke-width:5" />
<line x1="20px" y1="50px" x2="20px" y2="350px"
 stroke="green" stroke-width="2" />
<line x1="0px" y1="0px" x2="400px" y2="250px"
 style="stroke:blue; fill:none; stroke-dasharray:9,9;"/>
<line x1="300px" y1="30px" x2="200px" y2="50px"
 style="stroke:blue; fill:none; stroke-dasharray:3,3;"/>
</svg>

The appearance produced by Listing 3.2 is shown in Figure 3.2.

Figure 3.2 Styling applied to the four lines.

You may wonder why it is necessary to specify fill:none; for the third and fourth line elements in Listing 3.2. If you don't do so, as in Listing 3.3, a faint gray line will appear onscreen in Adobe SVG Viewer 3.

Listing 3.3 TwoLinesStyled.svg—The Need for Specifying No Fill on Dashed line Elements

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg width="400px" height="250px">
<line x1="0px" y1="0px" x2="400px" y2="250px"
 style="stroke:blue; stroke-width:5; fill:none; stroke-dasharray:9,9;"/>
<line x1="0px" y1="20px" x2="400px" y2="270px"
 style="stroke:blue; stroke-width:5; stroke-dasharray:9,9;"/>
</svg>

Figure 3.3 shows the two lines zoomed in the Adobe SVG Viewer.

Figure 3.3 The effect of the presence and absence of fill:none on a dashed line element.

We don't need to specify fill:none on a solid line—the color of the stroke will completely cover the faint gray default line produced in the Adobe Viewer.

You can specify opacity for the line element either by adding an opacity attribute or specifying an opacity property within a style attribute. The latter technique is used in Listing 3.4.

Listing 3.4 ThreeSimpleLines.svg—Opacity of Three Lines

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg width="300" height="300">
<line x1="20" y1="20" x2="220" y2="20"
 style="stroke:#FF0000; stroke-width:4; opacity:0.1"/>
<line x1="20" y1="120" x2="220" y2="120"
 style="stroke:#FF0000; stroke-width:4; opacity:0.4"/>
<line x1="20" y1="220" x2="220" y2="220"
 style="stroke:#FF0000; stroke-width:4; opacity:1"/>
</svg>

Figure 3.4 shows the visual effect, slightly zoomed, of three levels of opacity on otherwise identical lines.

Figure 3.4 Three lines of different opacity values.

In Figure 3.4, you will notice that the topmost of the three lines is fairly faint, due to its opacity being only 0.1. The bottom line is fully opaque, having an opacity of 1, and the middle line is semitransparent, having an opacity of 0.4.

We can use line elements to create the axes of a graph, as well as the scale marks for each axis. Listing 3.5 shows the use of line elements to create the skeleton of a graph. The comments within the code indicate the purpose of each line element.

Listing 3.5 GraphAxes.svg—A Skeleton for a Graph Created with line Elements

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg width="600" height="400">
<!-- The vertical axis -->
<line x1="40" y1="360" x2="40" y2="40"
 style="stroke:#000000; stroke-width:0.5;"/>

<!-- The scale marks on the vertical axis -->
<line x1="40" y1="110" x2="48" y2="110"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="40" y1="160" x2="48" y2="160"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="40" y1="210" x2="48" y2="210"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="40" y1="260" x2="48" y2="260"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="40" y1="310" x2="48" y2="310"
 style="stroke:#000000; stroke-width:0.5;"/>

<!-- The horizontal axis -->
<line x1="40" y1="360" x2="550" y2="360"
 style="stroke:#000000; stroke-width:0.5;"/>

<!-- The scale marks on the horizontal axis -->
<line x1="80" y1="360" x2="80" y2="352"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="120" y1="360" x2="120" y2="352"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="160" y1="360" x2="160" y2="352"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="200" y1="360" x2="200" y2="352"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="240" y1="360" x2="240" y2="352"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="280" y1="360" x2="280" y2="352"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="320" y1="360" x2="320" y2="352"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="360" y1="360" x2="360" y2="352"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="400" y1="360" x2="400" y2="352"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="440" y1="360" x2="440" y2="352"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="480" y1="360" x2="480" y2="352"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="520" y1="360" x2="520" y2="352"
 style="stroke:#000000; stroke-width:0.5;"/>
</svg>

As you can see in Figure 3.5, using only the line element, we can create the basis of a graph in SVG. Clearly, to create a more respectable graph, we will need to learn how to control the display of text in SVG, which is described in Chapter 8, "Laying Out Text in SVG."

Figure 3.5 Creating the framework for a graph using line elements.

It is straightforward to add further line elements to produce a bar chart, which might indicate monthly sales for a particular product or division of a corporation. This procedure is shown in Listing 3.6.

Listing 3.6 GraphAxes02.svg—Adding Bars to the Graph

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg width="600" height="400">
<!-- The vertical axis -->
<line x1="40" y1="360" x2="40" y2="40"
 style="stroke:#000000; stroke-width:0.5;"/>

<!-- The scale marks on the vertical axis -->
<line x1="40" y1="110" x2="48" y2="110"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="40" y1="160" x2="48" y2="160"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="40" y1="210" x2="48" y2="210"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="40" y1="260" x2="48" y2="260"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="40" y1="310" x2="48" y2="310"
 style="stroke:#000000; stroke-width:0.5;"/>

<!-- The horizontal axis -->
<line x1="40" y1="360" x2="550" y2="360"
 style="stroke:#000000; stroke-width:0.5;"/>

<!-- The scale marks on the horizontal axis -->
<line x1="80" y1="360" x2="80" y2="352"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="120" y1="360" x2="120" y2="352"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="160" y1="360" x2="160" y2="352"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="200" y1="360" x2="200" y2="352"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="240" y1="360" x2="240" y2="352"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="280" y1="360" x2="280" y2="352"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="320" y1="360" x2="320" y2="352"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="360" y1="360" x2="360" y2="352"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="400" y1="360" x2="400" y2="352"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="440" y1="360" x2="440" y2="352"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="480" y1="360" x2="480" y2="352"
 style="stroke:#000000; stroke-width:0.5;"/>
<line x1="520" y1="360" x2="520" y2="352"
 style="stroke:#000000; stroke-width:0.5;"/>

<!-- The lines representing monthly sales -->
<line x1="83" y1="360" x2="83" y2="250"
 style="stroke:#FF0000; stroke-width:4;"/>
<line x1="123" y1="360" x2="123" y2="240"
 style="stroke:#FF0000; stroke-width:4;"/>
<line x1="163" y1="360" x2="163" y2="230"
 style="stroke:#FF0000; stroke-width:4;"/>
<line x1="203" y1="360" x2="203" y2="240"
 style="stroke:#FF0000; stroke-width:4;"/>
<line x1="243" y1="360" x2="243" y2="210"
 style="stroke:#FF0000; stroke-width:4;"/>
<line x1="283" y1="360" x2="283" y2="220"
 style="stroke:#FF0000; stroke-width:4;"/>
<line x1="323" y1="360" x2="323" y2="230"
 style="stroke:#FF0000; stroke-width:4;"/>
<line x1="363" y1="360" x2="363" y2="220"
 style="stroke:#FF0000; stroke-width:4;"/>
<line x1="403" y1="360" x2="403" y2="240"
 style="stroke:#FF0000; stroke-width:4;"/>
<line x1="443" y1="360" x2="443" y2="190"
 style="stroke:#FF0000; stroke-width:4;"/>
<line x1="483" y1="360" x2="483" y2="210"
 style="stroke:#FF0000; stroke-width:4;"/>
<line x1="523" y1="360" x2="523" y2="200"
 style="stroke:#FF0000; stroke-width:4;"/>
</svg>

Figure 3.6 shows the bars added to the skeleton of the graph.

Figure 3.6 Creating bars of a bar graph using line elements.

The line element, in common with many other SVG elements, can be animated using declarative SVG animations. SVG declarative animations are discussed in Chapter 11.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020