clearing circular regions from HTML5 Canvas

后端 未结 6 1060
耶瑟儿~
耶瑟儿~ 2020-11-30 01:37

It appears the only way to clear a region from a canvas is to use the clearRect() command - I need to clear a circle (I am masking out areas from a filled canvas, point ligh

6条回答
  •  旧时难觅i
    2020-11-30 02:33

    Where x = left position, y = right position, r = radius, and ctx = your canvas:

    function clearCircle( x , y , r ){
        for( var i = 0 ; i < Math.round( Math.PI * r ) ; i++ ){
            var angle = ( i / Math.round( Math.PI * r )) * 360;
            ctx.clearRect( x , y , Math.sin( angle * ( Math.PI / 180 )) * r , Math.cos( angle * ( Math.PI / 180 )) * r );
        }
    }
    

提交回复
热议问题