编程语言
首页 > 编程语言> > javascript-如何在PaperJS中绘制一个简单的2D网格(非交互式)?

javascript-如何在PaperJS中绘制一个简单的2D网格(非交互式)?

作者:互联网

我有一个使用PaperJS制作的交互式应用程序模型,但是它仍然缺少一个小功能.我需要绘制一个2D网格(您知道…在表面上无休止地重复的均匀网格线),当在屏幕上拖动内容时,它将用作用户交互的指导(但是网格本身可以是完全静态的).

我只是不知道如何在PaperJS中实现它.它不能只是背景图像,因为它会以不同的比例显示,我也希望它能非常快地呈现,因为它总是可见的.

我要绘制的网格类型是2D网格居中网格,如该图的示例(a)所示:

任何启发都是值得欢迎的.

解决方法:

如果您只需要行:

var drawGridLines = function(num_rectangles_wide, num_rectangles_tall, boundingRect) {
    var width_per_rectangle = boundingRect.width / num_rectangles_wide;
    var height_per_rectangle = boundingRect.height / num_rectangles_tall;
    for (var i = 0; i <= num_rectangles_wide; i++) {
        var xPos = boundingRect.left + i * width_per_rectangle;
        var topPoint = new paper.Point(xPos, boundingRect.top);
        var bottomPoint = new paper.Point(xPos, boundingRect.bottom);
        var aLine = new paper.Path.Line(topPoint, bottomPoint);
        aLine.strokeColor = 'black';
    }
    for (var i = 0; i <= num_rectangles_tall; i++) {
        var yPos = boundingRect.top + i * height_per_rectangle;
        var leftPoint = new paper.Point(boundingRect.left, yPos);
        var rightPoint = new paper.Point(boundingRect.right, yPos);
        var aLine = new paper.Path.Line(leftPoint, rightPoint);
        aLine.strokeColor = 'black';
    }
}

drawGridLines(4, 4, paper.view.bounds);

如果您希望每个矩形都是单独的矩形的HitTest路径:

var drawGridRects = function(num_rectangles_wide, num_rectangles_tall, boundingRect) {
    var width_per_rectangle = boundingRect.width / num_rectangles_wide;
    var height_per_rectangle = boundingRect.height / num_rectangles_tall;
    for (var i = 0; i < num_rectangles_wide; i++) {
        for (var j = 0; j < num_rectangles_tall; j++) {
            var aRect = new paper.Path.Rectangle(boundingRect.left + i * width_per_rectangle, boundingRect.top + j * height_per_rectangle, width_per_rectangle, height_per_rectangle);
            aRect.strokeColor = 'white';
            aRect.fillColor = 'black';
        }
    }
}

drawGridRects(4, 4, paper.view.bounds);

标签:html5-canvas,javascript
来源: https://codeday.me/bug/20191202/2086379.html