Home Web Front-end H5 Tutorial HTML5 canvas drawing tutorial case analysis_html5 tutorial skills

HTML5 canvas drawing tutorial case analysis_html5 tutorial skills

May 16, 2016 pm 03:50 PM
canvas html5

Although everyone calls Canvas the new label of HTML5, it seems that Canvas is a new knowledge of the HTML language, but in fact Canvas drawing is done through JavaScript. Therefore, if you want to learn Canvas drawing, you must have a Javascript foundation.
In addition, when it comes to drawing, there are always some image terms and knowledge points, so if you have experience in drawing or art, it will be easier to learn Canvas.
Canvas means canvas. The Canvas in Html5 is really very similar to the canvas in real life. So seeing it as a physical canvas can speed up understanding.

Canvas
To paint with canvas, first of all, you need a "canvas". If you don’t have a canvas in your bookshelf, you can buy a roll and put it in there. Of course, we don’t need to spend money to buy it on the web page, we can just write a canvas, similar to:

Copy code
The code is as follows:

Your browser does not support canvas

The text in the label is for browsers that do not support canvas, and those that support it will never be able to see it.

Note: It is necessary to mention the characteristics of this canvas. Like IMG, it has two native attributes, namely width and height. At the same time, because it is also an html element, so He can also use CSS to define width and height, but be sure to note: his own width and height are different from the width and height defined through CSS!
We use JS to change the native width and height of Canvas, as follows:

Copy the code
The code is as follows:

canvas.width= 400
canvas.height = 300

But using JS to change the width and height of the Canvas by operating CSS is like this:

Copy code
The code is as follows:

canvas.style.width = '400px'
canvas.style.height = '300px'

It can be seen that the grammatical difference is obvious. In fact the difference is more obvious.
What’s the difference between them?
For example, on a canvas with a width of 1000 pixels, you draw a vertical line on the left side of the canvas with a width of 100 pixels. At this time, you set the width of the canvas itself to 500, which is equivalent to clicking off the right half of the canvas, but the width of the vertical line is still 100 at this time.
But if you change the width of the canvas to 500 through CSS, it is equivalent to squeezing the canvas from 1000 to 500, so the width of the vertical line becomes 50.
(This is only a theoretical situation, in practice When setting the native width of the canvas, it will clear out the drawn content. )
The width and height of the canvas itself are the properties of the canvas itself, and the width and height given by css can be regarded as scaling. If you scale it too casually, the graphics on the canvas may become unrecognizable to you.
So here is a suggestion: Unless there are special circumstances, do not use css to define the width and height of the Canvas.
The canvas is there, now let’s take it out:

Copy the code
The code is as follows:

var cvs = document.getElementById('cvs');

Look, it’s exactly the same as getting other elements.

Brush
Now that you have the canvas, if you want to graffiti on it, of course you need a pen. The method of getting the pen from canvas is as follows:

Copy the code
The code is as follows:

var ctx = cvs.getContext('2d');

The getContext method is used to get the pen, but there is another parameter here: 2d. What does this mean? This can be regarded as the type of brush.
Since there is 2D, then there will be 3D? I guess there will be in the future, but not now. So let's use this 2d pen first.
! So can we put a few more pens in reserve? The answer is no.
I want to ask a question: How many pens do you use at the same time when drawing? I believe 99.9% of people can only use one. Although some martial arts masters such as Xiao Longnu can draw with two hands at the same time, this is very unrealistic for ordinary people, isn't it?
So now you can feel relieved, because the canvas tag of html5 only supports using one pen at the same time!
Some students who are more familiar with writing JS may want to play a trick: I can use the previous method of obtaining brushes to get a few more pens, isn’t that enough? !

For example:

Copy the code
The code is as follows:

var con = cvs.getContext('2d');
var ctx = cvs.getContext('2d');

Hahahaha, it seems to be successful, but not I thought so before the test, but in fact it was just an illusion!
Because I discovered that when I dipped one of the pens in red ink, the other pen was automatically dipped in red ink! Because the two pens are one! fuck.
If you need to draw different colors, the way is to keep dipping this only "pen" in new colors.
This is actually not an advantage, but a flaw, which you will realize in the future.

Coordinates
The 2d world is a plane. To determine a point on a plane, two values ​​are required, the x coordinate and the y coordinate. This is a very important basic concept, but since everyone has studied mathematics, I won’t go into details.
The origin of canvas is the upper left corner, the same as flash. But the annoying thing is that the origin in mathematics is the lower left corner. This... I can only say that you just get used to it

Others
One feature of canvas that is different from the real canvas is that it is transparent by default and has no background color. This is very important most of the time.
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

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