Home Web Front-end H5 Tutorial Introduction to SVG 2D in HTML5 3—Introduction to text, images and rendering text_html5 tutorial skills

Introduction to SVG 2D in HTML5 3—Introduction to text, images and rendering text_html5 tutorial skills

May 16, 2016 pm 03:50 PM
2d svg image text

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
< /text>

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 of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to clear desktop background recent image history in Windows 11 How to clear desktop background recent image history in Windows 11 Apr 14, 2023 pm 01:37 PM

&lt;p&gt;Windows 11 improves personalization in the system, allowing users to view a recent history of previously made desktop background changes. When you enter the personalization section in the Windows System Settings application, you can see various options, changing the background wallpaper is one of them. But now you can see the latest history of background wallpapers set on your system. If you don't like seeing this and want to clear or delete this recent history, continue reading this article, which will help you learn more about how to do it using Registry Editor. &lt;/p&gt;&lt;h2&gt;How to use registry editing

How to Download Windows Spotlight Wallpaper Image on PC How to Download Windows Spotlight Wallpaper Image on PC Aug 23, 2023 pm 02:06 PM

Windows are never one to neglect aesthetics. From the bucolic green fields of XP to the blue swirling design of Windows 11, default desktop wallpapers have been a source of user delight for years. With Windows Spotlight, you now have direct access to beautiful, awe-inspiring images for your lock screen and desktop wallpaper every day. Unfortunately, these images don't hang out. If you have fallen in love with one of the Windows spotlight images, then you will want to know how to download them so that you can keep them as your background for a while. Here's everything you need to know. What is WindowsSpotlight? Window Spotlight is an automatic wallpaper updater available from Personalization &gt in the Settings app

How to use image semantic segmentation technology in Python? How to use image semantic segmentation technology in Python? Jun 06, 2023 am 08:03 AM

With the continuous development of artificial intelligence technology, image semantic segmentation technology has become a popular research direction in the field of image analysis. In image semantic segmentation, we segment different areas in an image and classify each area to achieve a comprehensive understanding of the image. Python is a well-known programming language. Its powerful data analysis and data visualization capabilities make it the first choice in the field of artificial intelligence technology research. This article will introduce how to use image semantic segmentation technology in Python. 1. Prerequisite knowledge is deepening

Try new ringtones and text tones: Experience the latest sound alerts on iPhone in iOS 17 Try new ringtones and text tones: Experience the latest sound alerts on iPhone in iOS 17 Oct 12, 2023 pm 11:41 PM

In iOS 17, Apple has overhauled its entire selection of ringtones and text tones, offering more than 20 new sounds that can be used for calls, text messages, alarms, and more. Here's how to see them. Many new ringtones are longer and sound more modern than older ringtones. They include arpeggio, broken, canopy, cabin, chirp, dawn, departure, dolop, journey, kettle, mercury, galaxy, quad, radial, scavenger, seedling, shelter, sprinkle, steps, story time , tease, tilt, unfold and valley. Reflection remains the default ringtone option. There are also 10+ new text tones available for incoming text messages, voicemails, incoming mail alerts, reminder alerts, and more. To access new ringtones and text tones, first, make sure your iPhone

How to search for text across all tabs in Chrome and Edge How to search for text across all tabs in Chrome and Edge Feb 19, 2024 am 11:30 AM

This tutorial shows you how to find specific text or phrases on all open tabs in Chrome or Edge on Windows. Is there a way to do a text search on all open tabs in Chrome? Yes, you can use a free external web extension in Chrome to perform text searches on all open tabs without having to switch tabs manually. Some extensions like TabSearch and Ctrl-FPlus can help you achieve this easily. How to search text across all tabs in Google Chrome? Ctrl-FPlus is a free extension that makes it easy for users to search for a specific word, phrase or text across all tabs of their browser window. This expansion

Using large models to create a new paradigm for text summary training Using large models to create a new paradigm for text summary training Jun 10, 2023 am 09:43 AM

1. Text task This article mainly discusses the method of generative text summarization, and how to use contrastive learning and large models to implement the latest generative text summarization training paradigm. It mainly involves two articles, one is BRIO: Bringing Order to Abstractive Summarization (2022), which uses contrastive learning to introduce ranking tasks in the generative model; the other is OnLearning to Summarize with Large Language Models as References (2023), which further introduces large models to generate high-quality training data based on BRIO. 2. Generative text summarization training methods and

What to do if win7 system cannot open txt text What to do if win7 system cannot open txt text Jul 06, 2023 pm 04:45 PM

What should I do if win7 system cannot open txt text? When we need to edit text files on our computers, the easiest way is to use text tools. However, some users find that their computers cannot open txt text files. So how to solve this problem? Let’s take a look at the detailed tutorial to solve the problem of unable to open txt text in win7 system. Tutorial to solve the problem that win7 system cannot open txt text. 1. Right-click any txt file on the desktop. If there is no txt file, you can right-click to create a new text document, and then select properties, as shown below: 2. In the opened txt properties window , find the change button under the general options, as shown in the figure below: 3. In the pop-up open mode setting

iOS 17: How to use one-click cropping in photos iOS 17: How to use one-click cropping in photos Sep 20, 2023 pm 08:45 PM

With the iOS 17 Photos app, Apple makes it easier to crop photos to your specifications. Read on to learn how. Previously in iOS 16, cropping an image in the Photos app involved several steps: Tap the editing interface, select the crop tool, and then adjust the crop using a pinch-to-zoom gesture or dragging the corners of the crop tool. In iOS 17, Apple has thankfully simplified this process so that when you zoom in on any selected photo in your Photos library, a new Crop button automatically appears in the upper right corner of the screen. Clicking on it will bring up the full cropping interface with the zoom level of your choice, so you can crop to the part of the image you like, rotate the image, invert the image, or apply screen ratio, or use markers

See all articles