search
HomeWeb Front-endH5 TutorialIntroduction to SVG 2D in HTML5 9—Introduction and application of masks and mask elements_html5 tutorial skills

SVG supported masks

SVG supports a variety of mask effects. Using these features, we can create many cool effects. As for whether mask is called "mask" or "mask" in Chinese, we won't distinguish it. Here we call it mask.

Mask types supported by SVG:
1. Clipping path (clipping path)

The clipping path is a graphic composed of path, text or basic graphics. All graphics within the clipping path are visible, and all graphics outside the clipping path are invisible.
2. Mask/mask

A mask is a container that defines a set of shapes and uses them as a semi-transparent medium that can be used to combine foreground objects and background.
An important difference between the clipping path and other masks is that the clipping path is a 1-bit mask, which means that the object covered by the clipping path is either fully transparent (visible, located inside the clipping path), or fully opaque (Invisible, outside the clipping path). The mask can specify the transparency at different locations.

Clip path of the window - overflow and clip properties

The overflow attribute and clip attribute of the HTML element jointly set the clipping behavior of the content of the element. Similarly, in SVG, these two attributes can also be used.

overflow = visible | hidden | scroll | auto | inherit
The overflow attribute defines the behavior to be taken when the content of the element exceeds the element's border.

This attribute can be used for elements that can create new windows (svg, symbol, image, foreignObject), pattern and marker elements. The value meaning of this attribute is as follows:
visible: Display all content, even if the content is already outside the element's border, this is the default value.
hidden: Hide content beyond the clipping path. The clipping path is specified by the clip property.
scroll: Use the form of a scroll bar to present excess content.
auto: Use browser-defined behavior, this seems unreliable.
This attribute is basically the same as the attribute of the same name in CSS2, except that in SVG, there are some different processing processes:
1. The overflow attribute is for elements other than creating new windows (svg, symbol, image, foreignObject) , elements other than pattern and marker elements have no effect.
2. The clipping path corresponds to the window. When a new window is created, a new clipping path is created. The default clipping path is the viewport boundary.
clip = | auto | inherit
clip attribute is used to set the clipping path of the current window.

This attribute can be used for elements that can create new windows (svg, symbol, image, foreignObject), pattern and marker elements. This property has the same parameters as the CSS2 property of the same name. auto means that the clipping path is consistent with the window border. When using graphics as parameters (setting the top, right, bottom and left values ​​​​of the cropping rectangle), you can use user coordinate values ​​​​(that is, coordinates without units). For example:

Copy code
The code is as follows:

P { clip: rect(5px , 10px, 10px, 5px); }

Note here that by default (both overflow and clip have default values), the clipping path is consistent with the border of the window. When viewBox and preserveAspectRatio are set, it is usually necessary to map the four sides of the clipping path to the four sides of the viewBox, so as to ensure that some display effects are still the same (of course, if they are all default values, there is no need to set them).

The clipping path of the object - clipPath element
The clipping path is defined using the clipPath element and then referenced using the clip-path attribute.
clipPath can contain path elements, text elements, basic graphic elements (circle, etc.) and use elements. If it is a use element, it must directly reference path, text or basic graphic elements, and cannot reference other elements.
Note that the clipping path is just a one-bit mask layer, the path is the union of all the elements it contains. Objects in this collection can be displayed, objects not in this range will not be displayed. The algorithm that determines whether the specific decision point is within the range is specified by the "clip-rule" attribute.

For graphic objects, the clipping path is equal to the union of the clipping path set by its own clip-path and the clipping paths of all outer elements (including the clipping paths set by clip-path and overflow). Note a few points:
1. The clipPath element itself does not inherit the clipping path defined by clipPath from the outer node.
2. The clipPath element itself can set the clip-path attribute. The effect is the intersection of two paths.
3. The child elements of the clipPath element can set the clip-path attribute: the effect is the union of the two paths.
4. An empty clipping path will cut off all content within the element.
Let’s take a look at several important attributes:
clipPathUnits = "userSpaceOnUse(Default value) | objectBoundingBox"
This attribute defines the coordinates used by the clipPath element System, these two values ​​​​we are all familiar with, are the user coordinate system of the element that refers to the current clipping path and the bounding box scale value.
The clipPath element is never rendered directly, but is referenced through clip-path, so setting the display attribute of the clipPath element has no effect.
clip-path = "clipping path name)> | none inherit"
Needless to say, this attribute is used to reference the clipping path , it should be noted here that all container elements, basic graphic elements and clipPath elements can use this attribute.
clip-rule = "nonzero(Default value) | evenodd | inherit"
This attribute is used to determine which points belong to the points inside the clipping path. For simple closed shapes, this is easy to determine, but for complex shapes with holes inside, there is a difference. The value of this attribute has the same meaning as the value of fill-rule:
nonzero: The algorithm used for this value is: emit a line in any direction from the point to be determined, and then calculate the direction of the intersection of the graph and the line segment. ; The calculation result starts from 0, and every time a line segment at an intersection is from left to right, add 1; every time a line segment at an intersection is from right to left, decrease 1; after calculating all intersections, If the result of this calculation is not equal to 0, then the point is within the graph and needs to be filled; if the value is equal to 0, then the point is outside the graph and does not need to be filled. Look at the example below:


evenodd: The algorithm used for this value is: launch a line in any direction from the point to be determined, and then calculate the number of intersections between the graph and the line segment. If the number is an odd number, change the point within the graph. Filling is required; if the number is an even number, the points are outside the graphic and do not need to be filled. Look at the example below:


The clip-rule attribute can only be used on internal graphic elements of the clipPath element. For example, the following settings work:

Copy the code
The code is as follows:








It will not work if the element is not in the clipPath. For example, the following settings will not work:

Copy the code
The code is as follows:








Finally look at a small example of the clipping path:

Copy the code
The code is as follows:










Only the 10*10 area in the upper left corner of the rectangle is visible.

Mask - mask element
In SVG, you can specify any graphic element or g element as a mask for the rendered object to combine the rendered objects. into the background.
The mask is defined with the mask element. When using the mask, you only need to reference the mask in the mask attribute of the object.
The mask element can contain any graphic elements and container elements (such as g).
In fact, everyone knows the effect of the mask. It basically calculates a final transparency based on the color and transparency of each point in the mask, and then when rendering the object, mask this with different transparency on the object. The mask layer reflects the blocking effect of the mask. For rendering objects, only the parts within the mask will be rendered according to the transparency of the points on the mask, and the parts not within the mask will not be displayed. Look at the example below:

Copy the code
The code is as follows:

xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http:/ /www.w3.org/1999/xlink">

x1="0" y1="0" x2 ="800" y2="0">



x="0" y ="0" width="800" height="300">


font-family="Verdana" font-size="100" text-anchor="middle" >
Masked text





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
Is H5 a Shorthand for HTML5? Exploring the DetailsIs H5 a Shorthand for HTML5? Exploring the DetailsApr 14, 2025 am 12:05 AM

H5 is not just the abbreviation of HTML5, it represents a wider modern web development technology ecosystem: 1. H5 includes HTML5, CSS3, JavaScript and related APIs and technologies; 2. It provides a richer, interactive and smooth user experience, and can run seamlessly on multiple devices; 3. Using the H5 technology stack, you can create responsive web pages and complex interactive functions.

H5 and HTML5: Commonly Used Terms in Web DevelopmentH5 and HTML5: Commonly Used Terms in Web DevelopmentApr 13, 2025 am 12:01 AM

H5 and HTML5 refer to the same thing, namely HTML5. HTML5 is the fifth version of HTML, bringing new features such as semantic tags, multimedia support, canvas and graphics, offline storage and local storage, improving the expressiveness and interactivity of web pages.

What Does H5 Refer To? Exploring the ContextWhat Does H5 Refer To? Exploring the ContextApr 12, 2025 am 12:03 AM

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

H5: Tools, Frameworks, and Best PracticesH5: Tools, Frameworks, and Best PracticesApr 11, 2025 am 12:11 AM

The tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.

The Legacy of HTML5: Understanding H5 in the PresentThe Legacy of HTML5: Understanding H5 in the PresentApr 10, 2025 am 09:28 AM

HTML5hassignificantlytransformedwebdevelopmentbyintroducingsemanticelements,enhancingmultimediasupport,andimprovingperformance.1)ItmadewebsitesmoreaccessibleandSEO-friendlywithsemanticelementslike,,and.2)HTML5introducednativeandtags,eliminatingthenee

H5 Code: Accessibility and Semantic HTMLH5 Code: Accessibility and Semantic HTMLApr 09, 2025 am 12:05 AM

H5 improves web page accessibility and SEO effects through semantic elements and ARIA attributes. 1. Use, etc. to organize the content structure and improve SEO. 2. ARIA attributes such as aria-label enhance accessibility, and assistive technology users can use web pages smoothly.

Is h5 same as HTML5?Is h5 same as HTML5?Apr 08, 2025 am 12:16 AM

"h5" and "HTML5" are the same in most cases, but they may have different meanings in certain specific scenarios. 1. "HTML5" is a W3C-defined standard that contains new tags and APIs. 2. "h5" is usually the abbreviation of HTML5, but in mobile development, it may refer to a framework based on HTML5. Understanding these differences helps to use these terms accurately in your project.

What is the function of H5?What is the function of H5?Apr 07, 2025 am 12:10 AM

H5, or HTML5, is the fifth version of HTML. It provides developers with a stronger tool set, making it easier to create complex web applications. The core functions of H5 include: 1) elements that allow drawing graphics and animations on web pages; 2) semantic tags such as, etc. to make the web page structure clear and conducive to SEO optimization; 3) new APIs such as GeolocationAPI support location-based services; 4) Cross-browser compatibility needs to be ensured through compatibility testing and Polyfill library.

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

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

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.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment