HTML5 SVG2D entry 3-Introduction to text, image, and rendering texts _ html5 tutorial skills-

Source: Internet
Author: User
One of the powerful capabilities of SVG is that it can control the text to a level that is impossible for standard HTML pages, without the need to seek help from images or other plug-ins, even though SVG's text rendering is so powerful, however, there is still a disadvantage: SVG cannot execute automatic line breaks. Next we will introduce how to render text in SVG. If you are interested, you may find it helpful. Rendering text in SVG

One of the powerful capabilities of SVG is that it can control the text to a level that is not possible for standard HTML pages, without the need to turn to images or other plug-ins. Any operations (such as painting or filter) that can be performed on the shape or path can be performed on the text. Although SVG text rendering is so powerful, there is still a disadvantage: SVG cannot execute automatic line breaks. If the text is longer than the allowed space, simply cut it off. In most cases, creating multiple lines of text requires multiple text elements.
In addition, you can use the tspan element to divide the text element into several parts, allowing each part to have its own style.

In addition, in the text element, the processing of spaces is similar to HTML: line breaks and carriage return are converted into spaces, while multiple spaces are compressed into a single space.

Text-text element directly displayed in the image
You can use the text element to directly display text. The example is as follows:

The Code is as follows:





SVG


As shown in the preceding example, you can set the following attributes for the text element:

X and y are the coordinates of text positions. Text-anchor is the direction of text display, that is, the position (x, y) is in the text position. This attribute has three values: start, middle, and end. Start indicates that the coordinates (x, y) of the text are at the beginning of the text, and the text is displayed to the right from this point. Middle indicates that (x, y) is located in the middle of the text, and the text is displayed in the left and right directions. In fact, the text is displayed in the center. End indicates that (x, y) points are at the end of the text, and the text is displayed to the left one by one.

In addition to these attributes, the following attributes can be specified either in CSS or directly in attributes:

Fill and stroke: fill and stroke colors. The specific usage is summarized later. Font-family, font-style, font-weight, font-variant, font-stretch, font-size, font-size-adjust, kerning, letter-spacing, word-spacing and text-decoration.

Text interval-tspan Element
This element is a powerful complement to the text element. It is used to render text within a range. It can only appear in the text element or the child element of the tspan element. A typical usage is to emphasize displaying part of the text. For example:

The Code is as follows:



This is bold and red


The tspan element has the following attributes: x and y are used to set the absolute coordinate value of the contained text. This value will overwrite the default text position. These attributes can contain a series of numbers, which are applied to each corresponding single character. The character with no corresponding settings will keep up with the previous character. For example:

The Code is as follows:


Hello World!
This is bold and red


Dx and dy are used to set the offset between the included text and the default text location. These attributes can also contain a series of numbers, each of which will be applied to the corresponding characters. The character with no corresponding settings will keep up with the previous character. You can replace x in the above example with dx to see the effect. Rotate is used to set the Rotation Angle of the font. This property page can contain a series of numbers applied to each character. If no character is set, the last number is used.

The Code is as follows:


Hello World!
This is bold and red


TextLength: This is the most confusing attribute. It is said that after the settings are completed, the length of the rendered text may vary with the value. But I did not try it out.

Text reference-tref Element
This element allows you to reference the defined text and efficiently copy it to the current position. It is usually used with xlink: href to specify the target element. Because it is copied, the original text is not modified when the current text is modified using css. Example:

The Code is as follows:


This is an example text.




Text path-textPath Element
This is interesting, and the effect is cool. It can make a lot of artistic effects. This element obtains the specified path from its xlink: href attribute and alignment the text to this path. For example:

The Code is as follows:




This text follows a curve.


Rendering image elements in SVG
The image Element in SVG can directly display grating images, which is easy to use. See the following example:

The Code is as follows:






Note the following points::
1. If the x or y coordinates are not set, the default value is 0.

2. If no width or height is set, the default value is 0.

3. If the width or height is explicitly set to 0, rendering of this image is prohibited.

4. The image format supports png, jpeg, jpg, svg, and so on. Therefore, svg supports nested svg.

5. Like other elements, an image is a common element of svg. Therefore, it supports all the effects such as cropping, masks, filters, and rotating.

Practical reference:
Script Index: http://msdn.microsoft.com/zh-cn/library/ff971910 (v = vs.85). aspx
Development Center: https://developer.mozilla.org/en/SVG
Hot reference: http://www.chinasvg.com/
Official documents: http://www.w3.org/TR/SVG11/

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.