# Canvas画椭圆的方法

er74 9年前

 oGC . arc ( 400 , 300 , 100 , 0 , 2 * Math . PI , false ) ;

 circle ( oGC , 400 , 300 , 100 ) ; function circle ( context , x , y , a ) { // x,y是坐标;a是半径 var r = 1 / a ; // ①注意：此处r可以写死，不过不同情况下写死的值不同 context . beginPath ( ) ; context . moveTo ( x + a , y ) ; for ( var i = 0 ; i < 2 * Math . PI ; i += r ) { context . lineTo ( x + a * Math . cos ( i ) , y + a * Math . sin ( i ) ) ; } context . closePath ( ) ; context . fill ( ) ; }

r和2*Math.PI配合就是圆的精细程度，在半径为100的时候，r取1/10就可以了，通用的话可以写死，写成r = 1 / a；这样无论半径取大或者小，圆都会很精细，但是性能会有很大影响

 function EllipseOne ( context , x , y , a , b ) { var step = ( a > b ) ? 1 / a : 1 / b ; context . beginPath ( ) ; context . moveTo ( x + a , y ) ; for ( var i = 0 ; i < 2 * Math . PI ; i += step ) { context . lineTo ( x + a * Math . cos ( i ) , y + b * Math . sin ( i ) ) ; } context . closePath ( ) ; context . fill ( ) ; }

 function EllipseTwo ( context , x , y , a , b ) { context . save ( ) ; var r = ( a > b ) ? a : b ; var ratioX = a / r ; var ratioY = b / r ; context . scale ( ratioX , ratioY ) ; context . beginPath ( ) ; context . arc ( x / ratioX , y / ratioY , r , 0 , 2 * Math . PI , false ) ; context . closePath ( ) ; context . restore ( ) ; context . fill ( ) ; }

 function EllipseThree ( context , x , y , a , b ) { var ox = 0.5 * a , oy = 0.6 * b ; context . save ( ) ; context . translate ( x , y ) ; context . beginPath ( ) ; context . moveTo ( 0 , b ) ; context . bezierCurveTo ( ox , b , a , oy , a , 0 ) ; context . bezierCurveTo ( a , - oy , ox , - b , 0 , - b ) ; context . bezierCurveTo ( - ox , - b , - a , - oy , - a , 0 ) ; context . bezierCurveTo ( - a , oy , - ox , b , 0 , b ) ; context . closePath ( ) ; context . fill ( ) ; context . restore ( ) ; } function EllipseFour ( context , x , y , a , b ) { var k = 0.5522848 , ox = k * a , oy = k * b ; context . translate ( x , y ) ; context . beginPath ( ) ; context . moveTo ( - a , 0 ) ; context . bezierCurveTo ( - a , oy , - ox , - b , 0 , - b ) ; context . bezierCurveTo ( ox , - b , a , - oy , a , 0 ) ; context . bezierCurveTo ( a , oy , ox , b , 0 , b ) ; context . bezierCurveTo ( - ox , b , - a , oy , - a , 0 ) ; context . closePath ( ) ; context . fill ( ) ; }

 function EllipseFive ( context , x , y , a , b ) { var data = context . getImageData ( 0 , 0 , 800 , 600 ) ; var imageData = data . data ; var tx = 0 ; var ty = b ; var d = b * b + a * a * ( - b + 0.25 ) ; var mx = a * a / Math . sqrt ( a * a + b * b ) ; while ( tx <= mx ) { if ( d < 0 ) { d += b * b * ( 2 * tx + 3 ) ; } else { ty -- ; d += b * b * ( 2 * tx + 3 ) + 2 * a * a * ( 1 - ty ) ; } tx ++ ; setPix ( x + tx , y + ty ) ; setPix ( x + tx , y - ty ) ; setPix ( x - tx , y + ty ) ; setPix ( x - tx , y - ty ) ; } d = b * b * ( tx + 0.5 ) * ( tx + 0.5 ) + a * a * ( ty - 1 ) * ( ty - 1 ) - a * a * b * b ; while ( ty > 0 ) { if ( d < 0 ) { tx ++ ; d += b * b * ( 2 * tx + 2 ) + a * a * ( - 2 * ty + 3 ) ; } else { d += a * a * ( - 2 * ty + 3 ) ; } ty -- ; setPix ( x + tx , y + ty ) ; setPix ( x - tx , y + ty ) ; setPix ( x + tx , y - ty ) ; setPix ( x - tx , y - ty ) ; } context . putImageData ( data , 0 , 0 ) ; function setPix ( x , y ) { console . log ( x , y ) ; var index = getStartIndex ( x , y ) ; for ( var i = 0 ; i < 4 ; i ++ ) { if ( i == 3 ) { imageData [ index + i ] = 255 ; } else { imageData [ index + i ] = 128 ; } } } function getStartIndex ( x , y ) { return y * 800 * 4 + x * 4 ; } }