


html5 Canvas drawing tutorial (1)—Basic knowledge of drawing_html5 tutorial skills
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:
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.
It is necessary to talk about the characteristics of this canvas. It has two native attributes, namely width and height. At the same time, because it is also an html element, it can also use css to define width and height. However, tens of millions of Please note: its own width and height are different from the width and height defined through css!
We use JS to change the width and height of the Canvas, like this:
canvas.width= 400
canvas.height = 300
But using JS to change the width and height of the Canvas by operating CSS is like this:
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 is their difference?
For example, on a canvas with a width of 1000 pixels, you draw a vertical line on the left side of the canvas, 100 pixels wide. 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 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 it is too casual, 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:
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 for canvas to get the pen 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? You can see this Painting is 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:
var con = cvs .getContext('2d');
var ctx = cvs.getContext('2d');
Hahahaha, it seems to have worked. I thought so before I tested it, 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.
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... can only be said to be a habit
Some basic common sense of drawing
First of all, you need to know, what kind of coordinate changes will draw what lines? For example, if the x coordinate becomes larger but the y coordinate remains unchanged, a horizontal line can be drawn; if the y coordinate changes but the x coordinate remains unchanged, a vertical line can be drawn.
Of course, there are also slashes, left slashes, right slashes, etc. If you can compare them with the pictures, most people can understand them at a glance; just drawing them with code is more depressing, and you can only rely on logical thinking. come out.
Don’t worry if you feel confused about lines now, you will naturally understand them as you learn.
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Dewu APP is currently a very popular brand shopping software, but most users do not know how to use the functions in Dewu APP. The most detailed usage tutorial guide is compiled below. Next is the Dewuduo that the editor brings to users. A summary of function usage tutorials. Interested users can come and take a look! Tutorial on how to use Dewu [2024-03-20] How to use Dewu installment purchase [2024-03-20] How to obtain Dewu coupons [2024-03-20] How to find Dewu manual customer service [2024-03-20] How to check the pickup code of Dewu [2024-03-20] Where to find Dewu purchase [2024-03-20] How to open Dewu VIP [2024-03-20] How to apply for return or exchange of Dewu

How to upgrade numpy version: Easy-to-follow tutorial, requires concrete code examples Introduction: NumPy is an important Python library used for scientific computing. It provides a powerful multidimensional array object and a series of related functions that can be used to perform efficient numerical operations. As new versions are released, newer features and bug fixes are constantly available to us. This article will describe how to upgrade your installed NumPy library to get the latest features and resolve known issues. Step 1: Check the current NumPy version at the beginning

After rain in summer, you can often see a beautiful and magical special weather scene - rainbow. This is also a rare scene that can be encountered in photography, and it is very photogenic. There are several conditions for a rainbow to appear: first, there are enough water droplets in the air, and second, the sun shines at a low angle. Therefore, it is easiest to see a rainbow in the afternoon after the rain has cleared up. However, the formation of a rainbow is greatly affected by weather, light and other conditions, so it generally only lasts for a short period of time, and the best viewing and shooting time is even shorter. So when you encounter a rainbow, how can you properly record it and photograph it with quality? 1. Look for rainbows. In addition to the conditions mentioned above, rainbows usually appear in the direction of sunlight, that is, if the sun shines from west to east, rainbows are more likely to appear in the east.

PhotoshopCS is the abbreviation of Photoshop Creative Suite. It is a software produced by Adobe and is widely used in graphic design and image processing. As a novice learning PS, let me explain to you today what software photoshopcs5 is and how to use photoshopcs5. 1. What software is photoshop cs5? Adobe Photoshop CS5 Extended is ideal for professionals in film, video and multimedia fields, graphic and web designers who use 3D and animation, and professionals in engineering and scientific fields. Render a 3D image and merge it into a 2D composite image. Edit videos easily

Testing a monitor when buying it is an essential part to avoid buying a damaged one. Today I will teach you how to use software to test the monitor. Method step 1. First, search and download the DisplayX software on this website, install it and open it, and you will see many detection methods provided to users. 2. The user clicks on the regular complete test. The first step is to test the brightness of the display. The user adjusts the display so that the boxes can be seen clearly. 3. Then click the mouse to enter the next link. If the monitor can distinguish each black and white area, it means the monitor is still good. 4. Click the left mouse button again, and you will see the grayscale test of the monitor. The smoother the color transition, the better the monitor. 5. In addition, in the displayx software we

1. First open WeChat. 2. Click [+] in the upper right corner. 3. Click the QR code to collect payment. 4. Click the three small dots in the upper right corner. 5. Click to close the voice reminder for payment arrival.

With the continuous development of smart phones, the functions of mobile phones have become more and more powerful, among which the function of taking long pictures has become one of the important functions used by many users in daily life. Long screenshots can help users save a long web page, conversation record or picture at one time for easy viewing and sharing. Among many mobile phone brands, Huawei mobile phones are also one of the brands highly respected by users, and their function of cropping long pictures is also highly praised. This article will introduce you to the correct method of taking long pictures on Huawei mobile phones, as well as some expert tips to help you make better use of Huawei mobile phones.

PHP Tutorial: How to Convert Int Type to String In PHP, converting integer data to string is a common operation. This tutorial will introduce how to use PHP's built-in functions to convert the int type to a string, while providing specific code examples. Use cast: In PHP, you can use cast to convert integer data into a string. This method is very simple. You only need to add (string) before the integer data to convert it into a string. Below is a simple sample code
