Home Web Front-end H5 Tutorial html5 Canvas drawing tutorial (9)—Drawing rectangles and circles in canvas_html5 tutorial skills

html5 Canvas drawing tutorial (9)—Drawing rectangles and circles in canvas_html5 tutorial skills

May 16, 2016 pm 03:50 PM
canvas round rectangle

This article talks about how to draw rectangles and circles in canvas. They are basic graphics. Of course, there are more basic graphics than them, but in canvas, you can only draw rectangles and circles without using other methods to simulate them.

Canvas draws rectangle
1, fillRect and strokeRect
fillRect can directly fill a rectangle, and the fill style is the style you currently set; the same The rationale for strokeRect is to directly stroke a rectangle
. Their parameters are consistent, in order (starting point x coordinate, starting point y, rectangular width, rectangular height). The starting point here, note, refers to the upper left corner of the rectangle.
We usually use them to do simple things, and they can only do simple things. Why? Because there is no "path" in the graphics they drew, it just came out directly.
For example, if you first fill a rectangle with fillRect, and then you want to stroke the rectangle, if you use stroke(), it will have no effect, because although there is a rectangle, there is no path.
If you desperately want to stroke this rectangle, you can use strokeRect() to stroke a rectangle at the same position - but they are actually independent, just overlapping positions.

Copy code
The code is as follows:

ctx.fillRect(200,100,50,40 );
ctx.strokeRect(200,100,50,40);

If we want a rectangle with both fill and stroke, then using fillRect and strokeRect at the same time will undoubtedly be cumbersome . So in this case we usually use the following method.

2, rect
The parameters of rect are no different from fillRect and strokeRect. The difference is that it only draws a path. As for the stroke or filling, you have to complete it yourself later. .

Copy code
The code is as follows:

ctx.rect(300,100,50,40 );
ctx.stroke()
ctx.fill();

What are the benefits of doing this? I mentioned in the previous article that filling or stroking consumes a lot of resources, so we often need to draw hundreds of paths at once (such as loops) and then stroke or fill them. At this time, use rect to draw the path and fill it at the end, which avoids the problem of fillRect and strokeRec having to fill or stroke each time.

3, lineTo
Of course you can also use 4 lineTo to draw a rectangle like my line drawing tutorial. But this is not necessary, see that article for details.

Canvas draws a circle
There is no eye in the sky. In fact, canvas does not have a real function that can directly draw a circle. What it draws is actually a 360-degree arc. , it looks like a circle.
We have talked about the function of canvas to draw arcs before, namely arc. We use it to draw a circle:

Copy code
The code is as follows:

ctx.arc(300 25,100 20,20,0,Math.PI*2);
ctx.stroke()
ctx. fill();

This arc is the same as rect, it also draws a path, and the filling or stroke needs to be completed later.
But it should be noted that the position judgment of a circle is different from that of a rectangle. We use the upper left corner of the rectangle as the starting point to determine its position, but we usually use the center of the circle to determine the position of the circle.
If you want to draw a set of horizontally and vertically centered rectangles and circles, then you must remember not to regard the starting point of the rectangle as the starting point of the circle - the starting point of the circle is the center of the circle!

Forget it, let me give you the current formula. For aligned circles and rectangles, the coordinates of the center of the circle = the coordinates of the rectangle and half the width and height of the rectangle.
That is, circle center x = rectangle x rectangle width/2, circle y = rectangle y rectangle height/2. This way they are absolutely aligned.
Although arc is not as easy to use as the method of directly drawing a circle - the method I envisioned to directly draw a circle only requires 3 parameters, namely the coordinates of the center of the circle and the radius - but arc can not only draw circles, but also semicircles and so on. , so the function is more powerful and you can make do with it.
Since there is a circle, there should be an ellipse, but there is not even a regular function for drawing a circle in canvas, let alone an ellipse. Therefore, drawing an ellipse must be simulated by other methods. This is more complicated, so I will leave it to later.
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 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)

How to make round pictures and text in ppt How to make round pictures and text in ppt Mar 26, 2024 am 10:23 AM

First, draw a circle in PPT, then insert a text box and enter text content. Finally, set the fill and outline of the text box to None to complete the production of circular pictures and text.

What is the area of ​​a circle within a rectangle inscribed in a semicircle? What is the area of ​​a circle within a rectangle inscribed in a semicircle? Sep 13, 2023 am 08:45 AM

A circle inscribed in a rectangle is tangent to the longer side of the rectangle, that is, its length is tangent to the circle. A rectangle inscribed in a semicircle touches two points on the arc of the semicircle. The width of the rectangle is equal to the diameter of the circle. If R is the radius of the semicircle. The length of the rectangle = √2R/2 The width of the rectangle = R/√2 The radius of the inscribed circle is r = b/2 = R/2√2 Using this formula we can calculate the rectangle inscribed in the semicircle The area of ​​a circle, area = (π*r2)=π*R/8 Example Demonstration #include<stdio.h>intmain(){&

How to make a round picture in ppt How to make a round picture in ppt Mar 25, 2024 pm 03:54 PM

How to make a circular picture in ppt: 1. Use the crop function; 2. Use the shape tool; 3. Use shortcut keys and control points to adjust.

What is the area of ​​the circular sector? What is the area of ​​the circular sector? Aug 30, 2023 am 08:33 AM

A circular sector, also known as a circular sector/sector of a circle, is a portion of a circle bounded by an arc between two radii. This area is bounded by two radii and an arc. To find the area inscribed, we need to find the angle between the two radii. The total area is equal to 360 degrees of angle. To find the area of ​​an angle, we multiply the area by θ/360. This gives the area of ​​the inscribed part. where θ is the angle (in degrees) between the two radii. The area of ​​the circular sector = π*r*r*(θ/360). For example, the area of ​​a circular sector with a radius of 5 and an angle of 60 degrees is 13.083. Area=(3.14*5*5)*(60/360)=13.03Example codeDemo#incl

How to merge a graphic after CAD rectangles are scattered How to merge a graphic after CAD rectangles are scattered Feb 28, 2024 pm 12:10 PM

When using CAD software, we often encounter situations where we need to recombine "scattered" rectangular objects into a single graphic. This need arises in many fields, such as space planning, mechanical design and architectural drawings. In order to meet this demand, we need to understand and master some key functions in CAD software. Next, the editor of this website will introduce you in detail how to complete this task in the CAD environment. Users who have doubts can come and follow this article to learn. Method for merging CAD rectangles into one graphic after breaking them up: 1. Open the CAD2023 software, create a rectangle, and then enter the X command and a space. As shown below: 2. Select the rectangular object and space it. You can break up the objects. 3. Select all open lines

What are the canvas arrow plug-ins? What are the canvas arrow plug-ins? Aug 21, 2023 pm 02:14 PM

The canvas arrow plug-ins include: 1. Fabric.js, which has a simple and easy-to-use API and can create custom arrow effects; 2. Konva.js, which provides the function of drawing arrows and can create various arrow styles; 3. Pixi.js , which provides rich graphics processing functions and can achieve various arrow effects; 4. Two.js, which can easily create and control arrow styles and animations; 5. Arrow.js, which can create various arrow effects; 6. Rough .js, you can create hand-drawn arrows, etc.

What are the details of the canvas clock? What are the details of the canvas clock? Aug 21, 2023 pm 05:07 PM

The details of the canvas clock include clock appearance, tick marks, digital clock, hour, minute and second hands, center point, animation effects, other styles, etc. Detailed introduction: 1. Clock appearance, you can use Canvas to draw a circular dial as the appearance of the clock, and you can set the size, color, border and other styles of the dial; 2. Scale lines, draw scale lines on the dial to represent hours or minutes. Position; 3. Digital clock, you can draw a digital clock on the dial to indicate the current hour and minute; 4. Hour hand, minute hand, second hand, etc.

What versions of html2canvas are there? What versions of html2canvas are there? Aug 22, 2023 pm 05:58 PM

The versions of html2canvas include html2canvas v0.x, html2canvas v1.x, etc. Detailed introduction: 1. html2canvas v0.x, which is an early version of html2canvas. The latest stable version is v0.5.0-alpha1. It is a mature version that has been widely used and verified in many projects; 2. html2canvas v1.x, this is a new version of html2canvas.

See all articles