Table of Contents
Text filling
Line segment attribute
Line segment coverage
Line segment joining
Clip
Transparency
Pixel Merge
Home Web Front-end H5 Tutorial HTML5 Canvas text filling, line segment attributes, cropping, transparency and pixel merging methods

HTML5 Canvas text filling, line segment attributes, cropping, transparency and pixel merging methods

Feb 27, 2017 pm 03:38 PM
canvas html5 canvas


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>
Copy after login
var canvas = document.getElementById(&#39;myCanvas&#39;),
    ctx = canvas.getContext(&#39;2d&#39;);
Copy after login

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);
Copy after login

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);
Copy after login

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();
Copy after login

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();
Copy after login


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


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


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;
Copy after login

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);
Copy after login

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);
Copy after login

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();
Copy after login

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

Video Face Swap

Video Face Swap

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

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)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

See all articles