HTML Canvas - 圆圈周围的虚线描边

问题描述 投票:0回答:5

我确实知道在画布上渲染点划线没有原生支持,但我已经看到人们能够为此提供支持的聪明方法。

我想知道是否有任何方法可以翻译它以允许在形状(特别是圆形)周围渲染点状笔划?

html canvas rendering dotted-line
5个回答
16
投票

最简单的方法使用 context.setLineDash()

ctx.setLineDash([5, 5]);
ctx.beginPath();
ctx.arc(100, 60, 50, 0, Math.PI * 2);
ctx.closePath();
ctx.stroke();

9
投票

现场演示

calcPointsCirc
需要 4 个参数:中心 x/y、半径和虚线的长度。它返回点数组 x,y,ex,ey。您可以循环遍历这些点来绘制虚线圆。可能有更优雅的方法来做到这一点,但我想尝试一下。

function calcPointsCirc( cx,cy, rad, dashLength)
{
    var n = rad/dashLength,
        alpha = Math.PI * 2 / n,
        pointObj = {},
        points = [],
        i = -1;

    while( i < n )
    {
        var theta = alpha * i,
            theta2 = alpha * (i+1);

        points.push({x : (Math.cos(theta) * rad) + cx, y : (Math.sin(theta) * rad) + cy, ex : (Math.cos(theta2) * rad) + cx, ey : (Math.sin(theta2) * rad) + cy});
        i+=2;
    }              
    return points;            
} 


var canvas = document.getElementById('canvas'),
    ctx = canvas.getContext('2d');

canvas.width = canvas.height= 200;

var pointArray= calcPointsCirc(50,50,50, 1);
    ctx.strokeStyle = "rgb(255,0,0)";
    ctx.beginPath();

    for(p = 0; p < pointArray.length; p++){
        ctx.moveTo(pointArray[p].x, pointArray[p].y);
        ctx.lineTo(pointArray[p].ex, pointArray[p].ey);
        ctx.stroke();
    }

    ctx.closePath();

4
投票

如果所有其他方法都失败,您始终可以从 0 到 2*

pi
循环变量,跳过每个
step
项目并在所有其他
step
项目上绘制指向
sin(angle)*radius+centerx, cos(angle)*radius+centery
的项目。

就是这样,自制的虚线圆圈:)


2
投票

我的JavaScript路径库实现任意路径的虚线和点线绘制(可以由任意数量的直线或曲线段组成),包括椭圆。下载它并查看示例。


1
投票

我正在为我的游戏寻找一个虚线圆圈,在阅读了所有页面后,我用打字稿编写了一个课程,效果非常好。如果有人在打字稿中寻找虚线圆圈,就在这里;

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();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.