


html5 Canvas drawing tutorial (2)—Drawing straight lines and setting line styles such as color/endpoint/intersection_html5 tutorial skills
If you still don’t know what Canvas is, you can read the previous article . When learning to draw, lines are the most basic, and the connection of lines can form any graphics. The same is true in Canvas.
Before we start, let’s take out the canvas and brushes:
var ctx = cvs.getContext('2d'); // Brush
But there’s no use dangling around, we have to start drawing. Let’s draw the simplest one first: a straight line
The method of drawing a straight line is lineTo. Its parameters are the same as moveTo, which is a point.
ctx.lineTo(x,y) Of course, when you draw a line, the writing point also moves, so after lineTo, the writing point becomes its target point.
ctx.lineTo(200,100);
Students who have used PS will definitely know the two modes of graphics, one is fill and the other is stroke. Now that we have drawn a line, it is equivalent to drawing a path in PS. At this time, we can draw the edge of the path and the graphics will be displayed.
The canvas stroke method is stroke(). Now let us complete the code:
ctx.lineTo(200,100);
ctx.lineTo(200,100);
ctx.lineTo(200,200);
ctx.lineTo(100,200);
ctx.lineTo(100,100);
ctx.stroke();
——–Author’s complaint: One disadvantage of Canvas drawing is that it is basically based on guessing, which is very unintuitive.
Serious reminder: The canvas drawing process (i.e. filling and stroking) is very resource-consuming. If you want to save system resources and improve efficiency, it is best to draw all paths and then fill or stroke the graphics at once.
We can see from the above graphic that the default line thickness is 1px and the line color is black. Of course we can set them, but the strange thing is that setting the line width is lineWidth, but setting the line style is called strokeStyle. Why not lineStyle? I don't know either. :
ctx.strokeStyle = 'rgba(255,0,0,0.5)';
The above code sets the line width to 10px and the line color to translucent red.
Question: If the rectangular path I draw has a width and height of 100, and my side line width is 10px, what is the overall width and height of the rectangle with the sides drawn? Is it 100 10*2=120?
If the edge is completely drawn outside the path, it will be 120. But Canvas is not. All lines in Canvas have a "middle line", which is located in the absolute middle of the line. The strokes of the line extend to both sides from the center line. For example, if your line width is 1, then the center line is at 0.5; if the width is 5, then the center line is at 2.5. When the canvas graphics are stroked, the path is aligned with the center line of the line and then stroked. As shown in Figure 2:
So, when tracing, half of the line is on the outside and half is on the inside. That is, the overall width of the above rectangle is 100 (10/2)*2, which is equal to 110.
It is for this reason that the upper left It is natural for the corners to appear chipped. Because no one paints here.
But why aren’t the rest of the corners notched? Doesn't it look like there are gaps in all four corners of your picture?
That’s because I didn’t “lift” the brush when I was drawing the line. The brush was continuous, that is, there was no moveTo. If you don’t believe it, let’s moveTo now:
ctx. moveTo(100,100);
ctx.lineTo(200,100);
ctx.moveTo(200,100); //Pay attention here
ctx.lineTo(200,200);
ctx.lineTo(100,200);
ctx.lineTo(100,100);
ctx.lineWidth = 10;
ctx.strokeStyle = 'rgba(255,0,0,0.5)';
ctx.stroke();
We movedTo before drawing the second line, and the coordinates of moveTo did not change. It was still the same point, but after refreshing, the graph became like this [Picture 3]:
Got it? Because we lifted the pen.
Now let’s delete moveTo and stop worrying about it. Let’s think about how to fill in the missing corner in the upper left corner?
First of all, let me ask a question, is our path closed? Isn’t this nonsense? Haven’t we already circled the path back to the original point? Of course it’s closed!
Wrong! This only makes the last point of the path coincide with the starting point, but the path itself is not closed!
How to close the path in Canvas? Use closePath().
ctx.moveTo( 100,100);
ctx.lineTo(200,100);
ctx.lineTo(200,200);
ctx.lineTo(100,200);
ctx.lineTo(100,100);
ctx.closePath( );//Closed path
ctx.lineWidth = 10;
ctx.strokeStyle = 'rgba(255,0,0,0.5)';
ctx.stroke();
Refresh at this time and it will be a perfect square. Figure 4:
No matter how thick we make the lines - the thicker the lines, the more people like them, right? ————The four corners of this square are regular right angles and will not be rounded. What about the rounded corners? Please look at the square stroke in PS, Figure 5:
As you can see, the thicker the edge, the larger the arc of its corners.
If I want the edges in Canvas to be the same as those in PS, is there any way? Of course, it is the lineJoin attribute.
lineJoin, meaning the intersection of lines, has three attributes: miter (default, sharp corner), bevel (bevel), round (rounded corner), as shown in Figure 6:

There is no doubt that we can understand at once that our rectangle has sharp corners, so try to change it to rounded corners:
The graphic becomes like this, Figure 7:

A bit like PS, right?
In addition, from the previous picture, we know that the ends of the Canvas lines are flat. Can this be changed? After all, it’s flat and doesn’t look good.
is also possible, that is, the lineCap attribute, which defines the endpoint of the line. lineCap has 3 values: butt (flat, default), round (circle), square (square), as shown in Figure 8

You can find out by looking at the picture that actually the flat head and the square head are the same. The only difference is that the flat head does not stick out so much. Both the round head and the square head will stick out for a while. How long is this section? That's half the width of the line.
Have you thought of anything? Haha, for the previous closed path problem, if we set lineCap to a square head, the effect will be the same!
But for the sake of safety, we still have to close the path, remember!
I would also like to remind you: a closed path has no endpoints! Therefore, the endpoint style cannot be seen on a closed path.
Also: lineCap is somewhat similar to lineJoin, be careful not to confuse them.
If you have a sharp eye and are unlucky, you may find that sometimes the 1 pixel line is not 1 pixel wide, but seems wider and blurry. As shown in Figure 9:

Congratulations! You have encountered a bug that is not a bug. This is very special. I will talk about it in the next article.

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

How to write clean and efficient HTML5 code? The answer is to avoid common mistakes by semanticizing tags, structured code, performance optimization and avoiding common mistakes. 1. Use semantic tags such as, etc. to improve code readability and SEO effect. 2. Keep the code structured and readable, using appropriate indentation and comments. 3. Optimize performance by reducing unnecessary tags, using CDN and compressing code. 4. Avoid common mistakes, such as the tag not closed, and ensure the validity of the code.

H5 improves web user experience with multimedia support, offline storage and performance optimization. 1) Multimedia support: H5 and elements simplify development and improve user experience. 2) Offline storage: WebStorage and IndexedDB allow offline use to improve the experience. 3) Performance optimization: WebWorkers and elements optimize performance to reduce bandwidth consumption.

HTML5 code consists of tags, elements and attributes: 1. The tag defines the content type and is surrounded by angle brackets, such as. 2. Elements are composed of start tags, contents and end tags, such as contents. 3. Attributes define key-value pairs in the start tag, enhance functions, such as. These are the basic units for building web structure.

HTML5 is a key technology for building modern web pages, providing many new elements and features. 1. HTML5 introduces semantic elements such as, , etc., which enhances web page structure and SEO. 2. Support multimedia elements and embed media without plug-ins. 3. Forms enhance new input types and verification properties, simplifying the verification process. 4. Offer offline and local storage functions to improve web page performance and user experience.

Best practices for H5 code include: 1. Use correct DOCTYPE declarations and character encoding; 2. Use semantic tags; 3. Reduce HTTP requests; 4. Use asynchronous loading; 5. Optimize images. These practices can improve the efficiency, maintainability and user experience of web pages.

Web standards and technologies have evolved from HTML4, CSS2 and simple JavaScript to date and have undergone significant developments. 1) HTML5 introduces APIs such as Canvas and WebStorage, which enhances the complexity and interactivity of web applications. 2) CSS3 adds animation and transition functions to make the page more effective. 3) JavaScript improves development efficiency and code readability through modern syntax of Node.js and ES6, such as arrow functions and classes. These changes have promoted the development of performance optimization and best practices of web applications.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

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.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment