Home > Web Front-end > H5 Tutorial > HTML5 canvas basic drawing: drawing rectangle

HTML5 canvas basic drawing: drawing rectangle

巴扎黑
Release: 2017-05-21 14:40:53
Original
1901 people have browsed it

is a new tag in HTML5, used for drawing graphics. This article mainly introduces the basic drawing method of HTML5 canvas to draw rectangles in detail. Interested friends can For reference

is just a container for drawing graphics. In addition to attributes such as id, class, style, etc., it also has height and width attributes. There are three main steps for drawing on the > element:

1. Get the DOM object corresponding to the element, which is a Canvas object;
2. Call the getContext( of the Canvas object ) method to get a CanvasRenderingContext2D object;
3. Call the CanvasRenderingContext2D object for drawing.

Draw rectangles rect(), fillRect() and strokeRect()

•context.rect(x, y, width, height): only define the path of the rectangle ;
•context.fillRect( x , y , width , height ): directly draw a filled rectangle;
•context.strokeRect( x , y , width , height ): directly draw a rectangular border;


JavaScript CodeCopy content to clipboard

##here Two points need to be explained: The first point is the order in which stroke() and fill() are drawn. If fill() is drawn later, then when the stroke border is larger, half of the border drawn by stroke() will be obviously covered. ;Second point: When setting the fillStyle or strokeStyle attribute, you can set it through the "rgba(255,0,0,0.2)" setting method. The last parameter of this setting is transparency.

There is another one related to rectangular drawing: clearing the rectangular area: context.clearRect(x,y,width,height).

The received parameters are: clear the starting position of the rectangle and the width and length of the rectangle.

In the above code, add at the end of drawing the graphics:


context.clearRect(100,60,600,100);

The following effect can be obtained:

The above is the detailed content of HTML5 canvas basic drawing: drawing rectangle. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source: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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template