search
HomeWeb Front-endH5 TutorialIntroduction to SVG 2D in HTML5 3—Introduction to text, images and rendering text_html5 tutorial skills

Rendering text in SVG

One of the powerful capabilities of SVG is that it can control text to a degree not possible with standard HTML pages, without resorting to images or other plug-ins. Any operation that can be performed on a shape or path (such as painting or filtering) can be performed on text. Although SVG's text rendering is so powerful, there is still one shortcoming: SVG cannot perform automatic word wrapping. If the text is longer than the allowed space, simply cut it off. In most cases, creating multiline text requires multiple text elements.
In addition, you can use the tspan element to divide a text element into several parts, allowing each part to have its own style.

Also, in the text element, spaces are treated similarly to HTML: line feeds and carriage returns become spaces, and multiple spaces are compressed into a single space.

Text displayed directly in the image - text element
To display text directly, you can use the text element. Examples are as follows:

Copy code
The code is as follows:




SVG


As shown in the example above, text Elements can set the following attributes: x,y are the text position coordinates. text-anchor is the direction of text display, which is actually the position (x, y) of the text. This attribute has three values: start, middle and end. start means that the text position coordinates (x, y) are at the beginning of the text, and the text is displayed one by one to the right starting from this point. Middle means that (x, y) is located in the middle of the text, and the text is displayed in both directions, which is actually centered. end means that the (x, y) point is at the end of the text, and the text is displayed one by one to the left.

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

fill, stroke: fill and stroke colors, the specific usage will be summarized later. Related attributes of font: font-family, font-style, font-weight, font-variant, font-stretch, font-size, font-size-adjust, kerning, letter-spacing, word-spacing and text-decoration.

Text range - tspan element
This element is a powerful supplement to the text element; it is used to render text within a range; it can only appear in text elements or tspan elements in child elements. A typical usage is to highlight part of the text. For example:

Copy code
The code is as follows:


This is bold and red


The tspan element has the following attributes. Settings: x, y are used to set the absolute coordinates of the included text. This value will override the default text position. These properties can contain a sequence of numbers that are applied to each corresponding single character. Characters without corresponding settings will immediately follow the previous character. For example:

Copy code
The code is as follows:

Hello World!
This is bold and red


dx,dy is used to set the offset of the included text relative to the default text position. These properties can also contain a series of numbers, each of which will be applied to the corresponding character. Characters without corresponding settings will immediately follow 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 that apply to each character. Characters without a corresponding setting will use the last number set.

Copy code
The code is as follows:

Hello World!
This is bold and red


textLength: This is the most puzzling attribute. It is said that after setting, if the rendering finds that the length of the text is inconsistent with this value, this length will prevail. But I didn't try it out.

Text reference - tref element
This element allows to reference defined text and efficiently copy it to the current location, usually in conjunction with xlink:href to specify the destination element. Because it is copied, when you use css to modify the current text, the original text will not be modified. Look at the example:

Copy the code
The code is as follows:

This is an example text.




Text Path - textPath element
This is more interesting, the effect is also cool, and can create many artistic effects; this element obtains the specified path from its xlink:href attribute And align the text to this path, see the example:

Copy the code
The code is as follows:



This text follows a curve.


Rendering pictures in SVG - image element
SVG The image element in can directly support the display of raster images and is very simple to use. Look at the example below:

Copy the code
The code is as follows:





A few points to note here:
1. If the x or y coordinate is not set, the default is 0.

2. If width or height is not set, the default is also 0.

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

4. The image format supports png, jpeg, jpg, svg, etc., so svg supports nested svg.

5.image, like other elements, is a regular element of svg, so it supports all cropping, masking, filters, rotation and other effects.

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

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is the H5 tag in HTML?What is the H5 tag in HTML?May 09, 2025 am 12:11 AM

The H5 tag in HTML is a fifth-level title that is used to tag smaller titles or sub-titles. 1) The H5 tag helps refine content hierarchy and improve readability and SEO. 2) Combined with CSS, you can customize the style to enhance the visual effect. 3) Use H5 tags reasonably to avoid abuse and ensure the logical content structure.

H5 Code: A Beginner's Guide to Web StructureH5 Code: A Beginner's Guide to Web StructureMay 08, 2025 am 12:15 AM

The methods of building a website in HTML5 include: 1. Use semantic tags to define the web page structure, such as, , etc.; 2. Embed multimedia content, use and tags; 3. Apply advanced functions such as form verification and local storage. Through these steps, you can create a modern web page with clear structure and rich features.

H5 Code Structure: Organizing Content for ReadabilityH5 Code Structure: Organizing Content for ReadabilityMay 07, 2025 am 12:06 AM

A reasonable H5 code structure allows the page to stand out among a lot of content. 1) Use semantic labels such as, etc. to organize content to make the structure clear. 2) Control the rendering effect of pages on different devices through CSS layout such as Flexbox or Grid. 3) Implement responsive design to ensure that the page adapts to different screen sizes.

H5 vs. Older HTML Versions: A ComparisonH5 vs. Older HTML Versions: A ComparisonMay 06, 2025 am 12:09 AM

The main differences between HTML5 (H5) and older versions of HTML include: 1) H5 introduces semantic tags, 2) supports multimedia content, and 3) provides offline storage functions. H5 enhances the functionality and expressiveness of web pages through new tags and APIs, such as and tags, improving user experience and SEO effects, but need to pay attention to compatibility issues.

H5 vs. HTML5: Clarifying the Terminology and RelationshipH5 vs. HTML5: Clarifying the Terminology and RelationshipMay 05, 2025 am 12:02 AM

The difference between H5 and HTML5 is: 1) HTML5 is a web page standard that defines structure and content; 2) H5 is a mobile web application based on HTML5, suitable for rapid development and marketing.

HTML5 Features: The Core of H5HTML5 Features: The Core of H5May 04, 2025 am 12:05 AM

The core features of HTML5 include semantic tags, multimedia support, form enhancement, offline storage and local storage. 1. Semantic tags such as, improve code readability and SEO effect. 2. Multimedia support simplifies the process of embedding media content through and tags. 3. Form Enhancement introduces new input types and verification properties, simplifying form development. 4. Offline storage and local storage improve web page performance and user experience through ApplicationCache and localStorage.

H5: Exploring the Latest Version of HTMLH5: Exploring the Latest Version of HTMLMay 03, 2025 am 12:14 AM

HTML5isamajorrevisionoftheHTMLstandardthatrevolutionizeswebdevelopmentbyintroducingnewsemanticelementsandcapabilities.1)ItenhancescodereadabilityandSEOwithelementslike,,,and.2)HTML5enablesricher,interactiveexperienceswithoutplugins,allowingdirectembe

Beyond Basics: Advanced Techniques in H5 CodeBeyond Basics: Advanced Techniques in H5 CodeMay 02, 2025 am 12:03 AM

Advanced tips for H5 include: 1. Use complex graphics to draw, 2. Use WebWorkers to improve performance, 3. Enhance user experience through WebStorage, 4. Implement responsive design, 5. Use WebRTC to achieve real-time communication, 6. Perform performance optimization and best practices. These tips help developers build more dynamic, interactive and efficient web applications.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),