Use canvas to draw the besell curve and canvas to draw the besell Curve

Source: Internet
Author: User

Use canvas to draw the besell curve and canvas to draw the besell Curve
This article mainly introduces how to use canvas to draw the bésel curve, including the fansel curve twice and three times. It is very detailed and recommended to a small partner.

1. Quadratic besell Curve

QuadraticCurveTo (cpx, cpy, x, y) // cpx, cpy indicates the coordinate of the control point, and x, y indicates the coordinate of the end point;

The mathematical formula is as follows:

The path of the bizz curve is defined by function B of the given point P0, P1, and P2 (T) Tracking:

Code example:


The Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Title> canvas line </title>
<Meta name = "Keywords" content = "">
<Meta name = "author" content = "@ my_programmer">
<Style type = "text/css">
Body, h1 {margin: 0 ;}
Canvas {margin: 20px ;}
</Style>
</Head>
<Body onload = "draw ()">
<H1> quadratic besell curve <Canvas id = "canvas" width = 200 height = 200 style = "border: 1px solid # ccc;"> </canvas>
<Script>
Function draw (){
Var canvas = document. getElementById ('canvas ');
Var context = canvas. getContext ('2d ');
// Draw the start point, control point, and end point
Context. beginPath ();
Context. moveTo (20,170 );
Context. lineTo (130,40 );
Context. lineTo (180,150 );
Context. stroke (); </p> <p> // draws the besell curve twice.
Context. beginPath ();
Context. moveTo (20,170 );
Context. quadraticCurveTo (180,150 );
Context. strokeStyle = "red ";
Context. stroke ();
}
</Script>
</Body>
</Html>

Code effect:

2. Cubic besell Curve

BezierCurveTo (cp1x, cp1y, cp2x, cp2y, x, y) // cp1x, cp1y indicates the coordinates of the first control point, cp2x, cp2y indicates the coordinates of the second control point, x, y indicates the coordinates of the end point;

The mathematical formula is as follows:

P0, P1, P2, and P3 point on a plane or in a three-dimensional space, a cubic Beitz curve is defined. The curve starts from P0 to P1 and starts from P2 to P3. It generally does not pass through P1 or P2; these two points only provide direction information there. The spacing between P0 and P1 determines the "length" of the curve toward P2 before it is switched to P3 ".

Code example:


The Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Title> canvas line </title>
<Meta name = "Keywords" content = "">
<Meta name = "Description" content = "">
<Style type = "text/css">
Body, h1 {margin: 0 ;}
Canvas {margin: 20px ;}
</Style>
</Head>
<Body onload = "draw ()">
<H1> cubic besell curve <Canvas id = "canvas" width = 200 height = 200 style = "border: 1px solid # ccc;"> </canvas>
<Script>
Function draw (){
Var canvas = document. getElementById ('canvas ');
Var context = canvas. getContext ('2d ');
// Draw the start point, control point, and end point
Context. beginPath ();
Context. moveTo (25,175 );
Context. lineTo (60, 80 );
Context. lineTo (150,30 );
Context. lineTo (170,150 );
Context. stroke (); </p> <p> // draw the besell curve three times.
Context. beginPath ();
Context. moveTo (25,175 );
Context. bezierCurveTo (60, 80, 170,150 );
Context. strokeStyle = "red ";
Context. stroke ();
}
</Script>
</Body>
</Html>

Code:

Is it cool... HTML5 + canvas is really a fun and addictive.

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.