Home Web Front-end H5 Tutorial Html5 Canvas Preliminary Study Notes (6) -Transformation

Html5 Canvas Preliminary Study Notes (6) -Transformation

Feb 28, 2017 pm 03:38 PM

In html5, a variety of graphic transformations including translation, scaling and rotation can be achieved. There are two methods, namely the matrix method and the function method. The following describes how translation, scaling and rotation can be implemented using these two methods.

1. Panning:

 context.fillRect(50,50,50,50);
 context.translate(100,100);
 context.fillRect(50,50,50,50);
Copy after login

If there is no middle sentence, the effect you will see will be only one rectangle, because the first rectangle and the second rectangle overlap, now The effect is to pan, and the effect is as follows:



First of all, the methods introduced in this article are all state values, and That is to say, its scope will affect all the code below it, and you can also use save and restore to store and pop up state. The above introduction is to call the function to solve the translation problem. The matrix method is introduced below. We can use the #transform method of context to do it. The matrix change operation has six parameters. Some explanations like to interpret them as responsible for different operations. I prefer to introduce these six parameters as a whole, which means that these parameters are responsible for translation, scaling and rotation respectively. Different, first introduce the whole. Overall, the first four parameters are responsible for scaling and rotation, the last two parameters are translation, the first four parameters are 1, 4 is a group, 2, 3 is a group, 1 and 2 are the values ​​of x, 3 and 4 is the value of y, 5 and 6 are the translations of x, y respectively. If the above code uses a matrix, it should be written as follows:

context.fillRect(50,50,50,50);
context.transform(1,0,0,1,100,100);
//context.transform(0,1,1,0,100,100);
context.fillRect(50,50,50,50);
Copy after login

Here is the first The second sentence of code has the same meaning as the code commented out in the third sentence. The reason why it should be in the group of 1, 4 is the same as 2, 3 is written 1 in one of these groups because we want to ensure a rectangular shape It is not scaled. If it is 0, the size will be scaled to 0.

2. Scaling

 context.fillRect(50,50,50,50);
 context.translate(150,50);
 context.scale(0.5,0.5);
 context.fillRect(0,0,50,50);
Copy after login

Both scaling and rotation require translation. This is because there will be problems if we write the following code

context.fillRect(50,50,50,50);
 context.scale(0.5,0.5);
 context.fillRect(150,50,50,50);
Copy after login

This code It seems to be the same as the code above, but it is actually different, because when you call context.scale, all coordinates will be scaled to half of the original size, so the effect will be the same as the following Different

The following is a comparison between the first and second paragraphs of code:



It can be seen that the latter picture is misaligned.

The matrix method is introduced below:

context.fillRect(50,50,50,50);
context.transform(0,0.5,0.5,0,150,50);
//context.transform(0.5,0,0,0.5,150,50);
context.fillRect(0,0,50,50);
Copy after login

The same as above, the effect of commenting out the code is the same, the same, it also needs to be translated first, the same, the first parameter and the fourth parameter The first parameter group is the same as the second and third parameter group.

3. Rotation

context.fillRect(50,50,50,50);
 context.translate(150,50);
 context.rotate(Math.PI/4);
 context.fillRect(0,0,50,50);
Copy after login

The following code can achieve rotation. For the same reason, it also needs to be translated first. The angle of rotation uses the radian system. The effect is as follows:



下面介绍的是使用矩阵法:

context.fillRect(50,50,50,50);
context.transform(Math.cos(Math.PI/4),Math.sin(Math.PI/4),-Math.sin(Math.PI/4),Math.cos(Math.PI/4),150,50);
//context.transform(-Math.sin(Math.PI/4),Math.cos(Math.PI/4),Math.cos(Math.PI/4)
//,Math.sin(Math.PI/4),150,50);
context.fillRect(0,0,50,50);
Copy after login

两组参数分别为cos旋转角,sin旋转角,负的sin旋转角,cos旋转角,或者为负的sin旋转角,cos旋转角,cos旋转角,sin旋转角。

如有错误,希望大家多多指正

 以上就是Html5 Canvas初探学习笔记(6) -变换的内容,更多相关内容请关注PHP中文网(www.php.cn)!


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)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months 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)

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.

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.

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

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.

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.

See all articles