search
HomeWeb Front-endH5 TutorialHTML5 Canvas text filling, line segment attributes, cropping, transparency and pixel merging methods


Many properties in CSS3 can be compared to some properties of our canvas
Many properties of the "brush" environment object in canvas can be compared to properties in CSS3
We are not only You can only draw graphics, and you can also add text to the canvas

Text filling

Similarly obtain the element object and environment object first

<canvas id="myCanvas" width=500 height=500></canvas>
var canvas = document.getElementById(&#39;myCanvas&#39;),
    ctx = canvas.getContext(&#39;2d&#39;);

font is used to set the font attribute
fillText sets the entity text and position
strokeText sets the hollow text and position

ctx.fillStyle = &#39;red&#39;;
ctx.font = &#39;50px sans-serif&#39;;
ctx.fillText(&#39;hello world!&#39;, 100, 100);

font can refer to the font attribute of css
Default value '10px sans-serif'


There is also a method to measure text width, just know it
measureText()

console.log(ctx.measureText(&#39;hello world!&#39;).width);

Line segment attribute

Line segment coverage

lineCap( ) is used to set the line segment coverage attribute
There are three values, butt/square/round

ctx.lineCap = &#39;butt&#39;; //默认ctx.lineWidth = 50;
ctx.moveTo(100, 100);
ctx.lineTo(400, 100);
ctx.stroke();

ctx.beginPath();
ctx.lineCap = &#39;square&#39;;
ctx.lineWidth = 50;
ctx.moveTo(100, 200);
ctx.lineTo(400, 200);
ctx.stroke();

ctx.beginPath();
ctx.lineCap = &#39;round&#39;;
ctx.lineWidth = 50;
ctx.moveTo(100, 300);   
ctx.lineTo(400, 300);
ctx.stroke();

The gray lines in the picture were added by me
so that you can see the three worthy differences

Line segment joining

lineJoin() defines the behavior of line segment joining
There are also three values, miter/round/bevel

ctx.lineWidth = 40;
ctx.lineJoin = &#39;miter&#39;; //默认ctx.moveTo(100, 100);
ctx.lineTo(400, 400);
ctx.lineTo(100, 400);
ctx.closePath();
ctx.stroke();


ctx.lineWidth = 40;
ctx.lineJoin = &#39;round&#39;; //改ctx.moveTo(100, 100);
ctx.lineTo(400, 400);
ctx.lineTo(100, 400);
ctx.closePath();
ctx.stroke();


ctx.lineWidth = 40;
ctx.lineJoin = &#39;bevel&#39;; //改ctx.moveTo(100, 100);
ctx.lineTo(400, 400);
ctx.lineTo(100, 400);
ctx.closePath();
ctx.stroke();


When we use the default miter
When two When the angle of the line segment is very small
the "point" will become larger and larger

When it reaches a certain degree of "point", the default value will become bever

We can set up to break through this limit, use miterLimit
In this way, changing the length of the default value will be set to limit*lineWidth/2
Just understand it

ctx.miterLimit = 30;

Clip

The clip attribute indicates that the area outside the current path will no longer be drawn
It is equivalent to cutting the current area from the canvas

ctx.arc(250, 250, 100, 0, Math.PI*2, 0);ctx.clip();ctx.fillRect(0, 0, 500, 500);

Here I cut the canvas into a circle
So that when filling the rectangle, it can only be filled into this "circular canvas"

Transparency

Use globalAlpha You can set global transparency
This is very simple and I won’t explain it much

ctx.globalAlpha = 0.4;ctx.fillRect(100, 100, 300, 300);

Pixel Merge

globalCompositeOperation is used to set
New graphics pixels and The merging method of old graphics pixels
It has 11 values
There are 3 common ones, source-over (default)/destination-over/copy
souce-over is to overwrite the graphics drawn first to the graphics drawn later The above
destination-over is to draw the graphics first and then the top of the graphics
copy is to display only the graphics drawn later (the graphics drawn first disappear)
Other values ​​are theoretically like this (different browser implementation levels Or in different ways)

ctx.fillStyle = &#39;blue&#39;;
ctx.fillRect(100, 100, 200, 200);
ctx.globalCompositeOperation = &#39;source-over&#39;;
ctx.fillStyle = &#39;red&#39;;
ctx.arc(300, 300, 100, 0 ,Math.PI*2, 0);
ctx.fill();

Below I give the 11 values ​​​​that I tested on the latest version of chrome for your reference

source-over:

destination-over:

copy:

##lighter:


xor:


source-atop:


destination-atop:


source-in:


destination-in:


source-out:

destination-out:


## The above is the HTML5 Canvas text Regarding filling, line segment attributes, cropping, transparency and pixel merging methods, please pay attention to the PHP Chinese website (www.php.cn) for more related content!



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
H5: New Features and Capabilities for Web DevelopmentH5: New Features and Capabilities for Web DevelopmentApr 29, 2025 am 12:07 AM

H5 brings a number of new functions and capabilities, greatly improving the interactivity and development efficiency of web pages. 1. Semantic tags such as enhance SEO. 2. Multimedia support simplifies audio and video playback through and tags. 3. Canvas drawing provides dynamic graphics drawing tools. 4. Local storage simplifies data storage through localStorage and sessionStorage. 5. The geolocation API facilitates the development of location-based services.

H5: Key Improvements in HTML5H5: Key Improvements in HTML5Apr 28, 2025 am 12:26 AM

HTML5 brings five key improvements: 1. Semantic tags improve code clarity and SEO effects; 2. Multimedia support simplifies video and audio embedding; 3. Form enhancement simplifies verification; 4. Offline and local storage improves user experience; 5. Canvas and graphics functions enhance the visualization of web pages.

HTML5: The Standard and its Impact on Web DevelopmentHTML5: The Standard and its Impact on Web DevelopmentApr 27, 2025 am 12:12 AM

The core features of HTML5 include semantic tags, multimedia support, offline storage and local storage, and form enhancement. 1. Semantic tags such as, etc. to improve code readability and SEO effect. 2. Simplify multimedia embedding with labels. 3. Offline storage and local storage such as ApplicationCache and LocalStorage support network-free operation and data storage. 4. Form enhancement introduces new input types and verification properties to simplify processing and verification.

H5 Code Examples: Practical Applications and TutorialsH5 Code Examples: Practical Applications and TutorialsApr 25, 2025 am 12:10 AM

H5 provides a variety of new features and functions, greatly enhancing the capabilities of front-end development. 1. Multimedia support: embed media through and elements, no plug-ins are required. 2. Canvas: Use elements to dynamically render 2D graphics and animations. 3. Local storage: implement persistent data storage through localStorage and sessionStorage to improve user experience.

The Connection Between H5 and HTML5: Similarities and DifferencesThe Connection Between H5 and HTML5: Similarities and DifferencesApr 24, 2025 am 12:01 AM

H5 and HTML5 are different concepts: HTML5 is a version of HTML, containing new elements and APIs; H5 is a mobile application development framework based on HTML5. HTML5 parses and renders code through the browser, while H5 applications need to run containers and interact with native code through JavaScript.

The Building Blocks of H5 Code: Key Elements and Their PurposeThe Building Blocks of H5 Code: Key Elements and Their PurposeApr 23, 2025 am 12:09 AM

Key elements of HTML5 include,,,,,, etc., which are used to build modern web pages. 1. Define the head content, 2. Used to navigate the link, 3. Represent the content of independent articles, 4. Organize the page content, 5. Display the sidebar content, 6. Define the footer, these elements enhance the structure and functionality of the web page.

HTML5 and H5: Understanding the Common UsageHTML5 and H5: Understanding the Common UsageApr 22, 2025 am 12:01 AM

There is no difference between HTML5 and H5, which is the abbreviation of HTML5. 1.HTML5 is the fifth version of HTML, which enhances the multimedia and interactive functions of web pages. 2.H5 is often used to refer to HTML5-based mobile web pages or applications, and is suitable for various mobile devices.

HTML5: The Building Blocks of the Modern Web (H5)HTML5: The Building Blocks of the Modern Web (H5)Apr 21, 2025 am 12:05 AM

HTML5 is the latest version of the Hypertext Markup Language, standardized by W3C. HTML5 introduces new semantic tags, multimedia support and form enhancements, improving web structure, user experience and SEO effects. HTML5 introduces new semantic tags, such as, ,, etc., to make the web page structure clearer and the SEO effect better. HTML5 supports multimedia elements and no third-party plug-ins are required, improving user experience and loading speed. HTML5 enhances form functions and introduces new input types such as, etc., which improves user experience and form verification efficiency.

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

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

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.