繁体   English   中英

在jsPDF中创建虚线或虚线

[英]Create a dashed or dotted line in jsPDF

我需要在使用jsPDF创建的PDF中绘制一条虚线( https://mrrio.github.io/jsPDF/doc/symbols/jsPDF.html

创建一个简单的行:

doc.line(20, 25, 60, 25);

http://jsfiddle.net/S3XRp/78/

如何创建虚线或虚线?

我有同样的问题,并且这样做:

/**
 * Draws a dotted line on a jsPDF doc between two points.
 * Note that the segment length is adjusted a little so
 * that we end the line with a drawn segment and don't
 * overflow.
 */
function dottedLine(doc, xFrom, yFrom, xTo, yTo, segmentLength)
{
    // Calculate line length (c)
    var a = Math.abs(xTo - xFrom);
    var b = Math.abs(yTo - yFrom);
    var c = Math.sqrt(Math.pow(a,2) + Math.pow(b,2));

    // Make sure we have an odd number of line segments (drawn or blank)
    // to fit it nicely
    var fractions = c / segmentLength;
    var adjustedSegmentLength = (Math.floor(fractions) % 2 === 0) ? (c / Math.ceil(fractions)) : (c / Math.floor(fractions));

    // Calculate x, y deltas per segment
    var deltaX = adjustedSegmentLength * (a / c);
    var deltaY = adjustedSegmentLength * (b / c);

    var curX = xFrom, curY = yFrom;
    while (curX <= xTo && curY <= yTo)
    {
        doc.line(curX, curY, curX + deltaX, curY + deltaY);
        curX += 2*deltaX;
        curY += 2*deltaY;
    }
}

更高版本的jsPDF具有内置函数:

setLineDash [文档]

例如,下面绘制一条虚线,画出10mm的线条,然后沿着从左到右的方向重复10mm的空间。 我假设你正在绘制一个具有所有默认设置(即A4,mm单位等)的页面:

doc.setLineDash([10, 10], 0);
doc.line(20, 25, 60, 25);

在此输入图像描述

下面将绘制7毫米的线,3毫米的空间,1毫米的线,3毫米的空间然后重复,然而,它将启动10毫米的图案,因此要绘制的仪表板的第一部分是1毫米部分:

doc.setLineDash([7, 3, 1, 3], 10);
doc.line(20, 25, 60, 25);

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM