HTML Canvas - Dotted stroke around circle

前端 未结 5 1856
陌清茗
陌清茗 2021-01-02 17:06

I do know there is no native support for doing dotted stroke lines rendered on a canvas, but I have seen the clever ways people have been able to generate support for this.<

5条回答
  •  借酒劲吻你
    2021-01-02 17:59

    I was looking for a dashed-circle for my game and after reading all of the pages I have written a class in typescript it works very well. If anybody looks for the dashed-circle in typescript, it is here;

    export class DashedCircle
    {
        centerX: number;
        centerY: number;
        radius: number;
        color: string;
        dashSize: number;
        ctx: CanvasRenderingContext2D;
    
        constructor(ctx:CanvasRenderingContext2D, centerX: number, centerY: number, radius: number, color: string, dashSize: number)
        {
            this.ctx = ctx;
            this.centerX = centerX;
            this.centerY = centerY;
            this.radius = radius;
            this.color = color;
            this.dashSize = dashSize;
        }
    
        CalculateCirclePoints()
        {
            var n = this.radius / this.dashSize;
            var alpha = Math.PI * 2 / n;
            var pointObj = {};
            var points = [];
            var i = -1;
    
            while (i < n)
            {
                var theta = alpha * i;
                var theta2 = alpha * (i + 1);
                points.push({
                    x: (Math.cos(theta) * this.radius) + this.centerX,
                    y: (Math.sin(theta) * this.radius) + this.centerY,
                    ex: (Math.cos(theta2) * this.radius) + this.centerX,
                    ey: (Math.sin(theta2) * this.radius) + this.centerY });
                    i += 2;
            }
    
            return points;
        }
    
        Draw()
        {
            var points = this.CalculateCirclePoints();
            this.ctx.strokeStyle = this.color;
            this.ctx.beginPath();
            for (var p = 0; p < points.length; p++)
            {
                this.ctx.moveTo(points[p].x, points[p].y);
                this.ctx.lineTo(points[p].ex, points[p].ey);
                this.ctx.stroke();
            }
            this.ctx.closePath();
        }
    }
    

提交回复
热议问题