Html5 Canvas painting tutorial (7)-quadraticCurveTo Method for drawing curves in canvas

Source: Internet
Author: User

Comments: Today we will talk about the quadraticCurveTo method to draw a curve. quadratic refers to the quadratic equation in mathematics. Next we will detail the use of the quadraticCurveTo method, if you are interested, you can learn more about how to draw curves in canvas. Today we will talk about quadraticCurveTo.
To be honest, this method is a little scary and can be used by function names. In other words, I think it is necessary to shorten the function name.
Quadratic refers to the quadratic equation in mathematics. The parameters of ctx. quadraticCurveTo are as follows:

The Code is as follows:
Ctx. quadraticCurveTo (x1, y1, x, y );

Where are the coordinates of x, y, and x1 and y1, respectively, the coordinates of the curve control points? What? Where do you want to start? The start point is determined by moveTo before this.
The reason why I put the coordinates of the control point with sequence number 1 is that the function of drawing a curve later has two control points, that is, x2 and y2, so I should take a needle here.
The start point determined by moveTo and the end point determined by quadraticCurveTo can be connected into a straight line. Because quadraticCurveTo has only one control point, this control point is either on the left or right of the straight line, quadraticCurveTo can only draw an arc, or cannot draw an S shape.

For ease of understanding, I still use the method of drawing guides in the previous article. The initial code is as follows:

The Code is as follows:
Var x1 = 350,
Y1 = 250,
* = 400,
Y = 500;
Ctx. beginPath ();
Ctx. strokeStyle = "#000 ";
Ctx. moveTo (300,300); // start point
Ctx. quadraticCurveTo (x1, y1, x, y); // card Curve
Ctx. stroke ();
Ctx. beginPath ();
Ctx. strokeStyle = "rgba (255,0, 0, 0.5 )";
Ctx. moveTo (300,300 );
Ctx. lineTo (x1, y1); // the line between the current row and the next row draws the control point.
Ctx. lineTo (x, y );
Ctx. moveTo (300,300); // start and end points of the curve
Ctx. lineTo (x, y );
Ctx. stroke ();

Here I drew two guides. One is the connection line between the start point and the end point, and the other is the Auxiliary Line from the start point to the control point and then to the end point (actually two guides ), the intersection of these two lines is the coordinate of the control point of quadraticCurveTo.


QuadraticCurveTo can only draw an arc curve, but this arc can be very irregular, compared to arc and arcTo, it is also an improvement.
In addition, quadraticCurveTo will not reverse like arcTo.
Of course, if you pull the control point very far, the graphics may become invisible to you. Let's give it a try:
"Y1 = 950;
"I just increased y1, and the curve is out of the canvas range.


However, the curve range drawn by quadraticCurveTo can never reach or exceed the coordinates of the control point. We only need to "control" the control point so that we don't have to worry about it.
I wrote a simple and movable Example page to show how quadraticCurveTo draws a curve:


<! Doctype html> 

Tip: you can modify some code before running
The code is ugly.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.