HTML5 Canvas中绘制椭圆的几种方法

1.canvas自带的绘制椭圆的方法

ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise)是后来添加的,

参数的意思:(起点x.起点y,半径x,半径y,旋转的角度,起始角,结果角,顺时针还是逆时针)

 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4     <meta charset="UTF-8">
 5     <title>椭圆</title>
 6 </head>
 7 <body>
 8 <canvas id="canvas" style="border:1px solid #aaa;display:block;margin:50px auto;">
 9     当前浏览器不支持Canvas,请更换浏览器后再试
10 </canvas>
11  
12 <script>
13     window.onload = function(){
14         var canvas = document.getElementById("canvas");
15         var  ctx=canvas.getContext('2d');
16         canvas.width = 800;
17         canvas.height = 800;
18         if(ctx.ellipse){
19             ctx.ellipse(400,400,300,200,0,0,Math.PI*2);
20             ctx.fillStyle="#058";
21             ctx.strokeStyle="#000";
22             ctx.fill();
23             ctx.stroke();
24         }else{
25             alert("no ellipse!");
26         } 
27     }
28 </script>
29 </body>
30 </html>

目前谷歌可以支持,其他浏览器还未存在ellipse();

下面这几种方法是我在网上看到的,在此也做个总结:

2.用参数方程绘制椭圆

//函数的参数x,y为椭圆中心;a,b分别为椭圆横半轴、
 //纵半轴长度,不可同时为0
//该方法的缺点是,当lineWidth较宽,椭圆较扁时
//椭圆内部长轴端较为尖锐,不平滑,效率较低

 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4     <meta charset="UTF-8">
 5     <title>椭圆</title>
 6 </head>
 7 <body>
 8 <canvas id="canvas" style="border:1px solid #aaa;display:block;margin:50px auto;">
 9     当前浏览器不支持Canvas,请更换浏览器后再试
10 </canvas>
11 <script>
12     var canvas = document.getElementById("canvas");
13     canvas.width = 600;
14     canvas.height = 600;
15     var context = canvas.getContext("2d");
16         context.lineWidth = 10;
17         context.strokeStyle="black";
18         ParamEllipse(context, 130, 80, 100, 20); //椭圆
19     function ParamEllipse(context, x, y, a, b){
20         //max是等于1除以长轴值a和b中的较大者
21         //i每次循环增加1/max,表示度数的增加
22         //这样可以使得每次循环所绘制的路径(弧线)接近1像素
23         var step = (a > b) ? 1 / a : 1 / b;
24         context.beginPath();
25         context.moveTo(x + a, y); //从椭圆的左端点开始绘制
26         for (var i = 0; i < 2 * Math.PI; i += step)
27         {
28         //参数方程为x = a * cos(i), y = b * sin(i),
29         //参数为i,表示度数(弧度)
30         context.lineTo(x + a * Math.cos(i), y + b * Math.sin(i));
31         }
32         context.closePath();
33         context.stroke();
34     };
35 </script>
36 </body>
37 </html>

 

3.均匀压缩法绘制椭圆

    //其方法是用arc方法绘制圆,结合scale进行
    //横轴或纵轴方向缩放(均匀压缩)
    //这种方法绘制的椭圆的边离长轴端越近越粗,长轴端点的线宽是正常值
    //边离短轴越近、椭圆越扁越细,甚至产生间断,这是scale导致的结果
    //这种缺点某些时候是优点,比如在表现环的立体效果(行星光环)时
    //对于参数a或b为0的情况,这种方法不适用

 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4     <meta charset="UTF-8">
 5     <title>椭圆</title>
 6 </head>
 7 <body>
 8 <canvas id="canvas" style="border:1px solid #aaa;display:block;margin:50px auto;">
 9     当前浏览器不支持Canvas,请更换浏览器后再试
10 </canvas>
11 <script>
12     var canvas = document.getElementById("canvas");
13     canvas.width = 600;
14     canvas.height = 600;
15     var context = canvas.getContext("2d");
16         context.lineWidth = 10;
17         context.strokeStyle="black";
18        EvenCompEllipse(context, 130, 200, 100, 20); //椭圆
19     function EvenCompEllipse(context, x, y, a, b){
20         context.save();
21         //选择a、b中的较大者作为arc方法的半径参数
22         var r = (a > b) ? a : b; 
23         var ratioX = a / r; //横轴缩放比率
24         var ratioY = b / r; //纵轴缩放比率
25         context.scale(ratioX, ratioY); //进行缩放(均匀压缩)
26         context.beginPath();
27         //从椭圆的左端点开始逆时针绘制
28         context.moveTo((x + a) / ratioX, y / ratioY);
29         context.arc(x / ratioX, y / ratioY, r, 0, 2 * Math.PI);
30         context.closePath();
31         context.stroke();
32         context.restore();
33     };
34 </script>
35 </body>
36 </html>

 纠正:上述方法也可以实现线条粗细一致的椭圆

只需要调整两句话的顺序(先恢复上下文状态,再描边)

context.stroke();
context.restore();

变为:
context.restore();
context.stroke();

4.使用三次贝塞尔曲线模拟椭圆1

//此方法也会产生当lineWidth较宽,椭圆较扁时,
//长轴端较尖锐,不平滑的现象

 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4     <meta charset="UTF-8">
 5     <title>椭圆</title>
 6 </head>
 7 <body>
 8 <canvas id="canvas" style="border:1px solid #aaa;display:block;margin:50px auto;">
 9     当前浏览器不支持Canvas,请更换浏览器后再试
10 </canvas>
11 <script>
12     var canvas = document.getElementById("canvas");
13     canvas.width = 600;
14     canvas.height = 600;
15     var context = canvas.getContext("2d");
16         context.lineWidth = 10;
17         context.strokeStyle="black";
18         BezierEllipse1(context, 470, 80, 100, 20); //椭圆
19     function BezierEllipse1(context, x, y, a, b){
20         //关键是bezierCurveTo中两个控制点的设置
21         //0.5和0.6是两个关键系数(在本函数中为试验而得)
22         var ox = 0.5 * a,
23         oy = 0.6 * b;
24         context.save();
25         context.translate(x, y);
26         context.beginPath();
27         //从椭圆纵轴下端开始逆时针方向绘制
28         context.moveTo(0, b); 
29         context.bezierCurveTo(ox, b, a, oy, a, 0);
30         context.bezierCurveTo(a, -oy, ox, -b, 0, -b);
31         context.bezierCurveTo(-ox, -b, -a, -oy, -a, 0);
32         context.bezierCurveTo(-a, oy, -ox, b, 0, b);
33         context.closePath();
34         context.stroke();
35         context.restore();
36     };
37 </script>
38 </body>
39 </html>

 

5.使用三次贝塞尔曲线模拟椭圆2

     //此方法也会产生当lineWidth较宽,椭圆较扁时
    //,长轴端较尖锐,不平滑的现象
    //这种方法比前一个贝塞尔方法精确度高,但效率稍差

 

 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4     <meta charset="UTF-8">
 5     <title>椭圆</title>
 6 </head>
 7 <body>
 8 <canvas id="canvas" style="border:1px solid #aaa;display:block;margin:50px auto;">
 9     当前浏览器不支持Canvas,请更换浏览器后再试
10 </canvas>
11 <script>
12     var canvas = document.getElementById("canvas");
13     canvas.width = 600;
14     canvas.height = 600;
15     var context = canvas.getContext("2d");
16         context.lineWidth = 10;
17         context.strokeStyle="black";
18         BezierEllipse2(context, 470, 200, 100, 20); //椭圆
19     function BezierEllipse2(ctx, x, y, a, b){
20         var k = .5522848,
21         ox = a * k, // 水平控制点偏移量
22         oy = b * k; // 垂直控制点偏移量</p> <p> ctx.beginPath();
23         //从椭圆的左端点开始顺时针绘制四条三次贝塞尔曲线
24         ctx.moveTo(x - a, y);
25         ctx.bezierCurveTo(x - a, y - oy, x - ox, y - b, x, y - b);
26         ctx.bezierCurveTo(x + ox, y - b, x + a, y - oy, x + a, y);
27         ctx.bezierCurveTo(x + a, y + oy, x + ox, y + b, x, y + b);
28         ctx.bezierCurveTo(x - ox, y + b, x - a, y + oy, x - a, y);
29         ctx.closePath();
30         ctx.stroke();
31     };
32 </script>
33 </body>
34 </html>

 

原文:https://blog.csdn.net/gao_xu_520/article/details/58588020

posted @ 2018-11-07 16:47  方帅  阅读(16233)  评论(3编辑  收藏  举报