Html5Canvas drawing tutorial (7)-quadraticCurveTo Method for drawing curves in canvas _ html5 tutorial tips-

Source: Internet
Author: User
Today we will talk about the quadraticCurveTo method to draw a curve. quadratic refers to the quadratic equation in mathematics. Next we will introduce the use of the quadraticCurveTo method in detail, 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> <ptml lang = "en-US"> <pead> <meta charset = "UTF-8"> <title> Canvas Learning -- quadraticCurveTo draw curve </title> <style type = "text/css"> canvas {box-shadow: 0 0 10px rgba (0.2, 0 )} </style> </pead> <body> <canvas id = "cvs" width = "800" height = "600"> canvas </canvas> </body> <script type = "text/javascript"> var cvs = document. getElementById ('cvs '); // canvas var ctx = cvs. getContext ('2d '); // paint brush // ctx. closePath (); var x1 = 350, y1 = 250, x2 = 440, y2 = 550, x = 400, y = 500; var fan = 1, fan1 = 1; function draw () {ctx. clearRect (1, 100,100,600,600); ctx. beginPath (); ctx. strokeStyle = "#000"; ctx. moveTo (300,300); ctx. quadraticCurveTo (x1, y1, x, y); ctx. stroke ();/* ctx. beginPath (); ctx. strokeStyle = "rgba (0,255,)" ctx. moveTo (300,300); ctx. bezierCurveTo (x1, y1, x2, y2, x, y); ctx. stroke (); */ctx. beginPath (); ctx. strokeStyle = "rgba (255,0, 0, 0.5)"; ctx. moveTo (300,300); ctx. lineTo (x1, y1); // ctx. lineTo (x2, y2); ctx. lineTo (x, y); ctx. moveTo (300,300); ctx. lineTo (x, y);/* ctx. moveTo (x2, y2); ctx. lineTo (x, y); */ctx. stroke (); if (x1> 600) {fan =-1;} else if (x1 <200) {fan = 1;} if (y1> 700) {fan1 =-1;} else if (y1 <200) {fan1 = 1;} x1 + = fan; y1 + = fan1; setTimeout (draw, 22 );} draw (); // ctx. strokeRect (100,100, 50, 50); script </ptml>

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.