<画布>围棋/围棋/围棋棋盘

发布于 2024-08-22 15:44:46 字数 1513 浏览 6 评论 0原文

我正在尝试使用新标签,并且已经遇到了第一个障碍。我想我可以通过实现经典棋盘游戏围棋/围棋/围棋的一个版本来开始实践。

我使用 moveTo() 和 lineTo() 绘制了 xy 网格,并且使用 fillRect() 绘制了木质背景,当然它需要位于 XY 网格“下方”。

然而,这就是我的问题。 fillRect() 背景绘制在网格顶部 - 从而遮挡网格。

我该如何扭转这个局面?这是我正在处理的内容:

        var boardSize = 19;                 
        var gridSpacing = 25;
        var gridSize = boardSize * gridSpacing;

        var xStart = (window.innerWidth / 2) - (gridSize / 2) + 0.5;
        var yStart = (window.innerHeight / 2) - (gridSize / 2) + 0.5;
        var xEnd = xStart + gridSize;
        var yEnd = yStart + gridSize;


        var gridContext = canvas.getContext("2d");

        gridContext.beginPath();

        // Draw the board x lines
        for (var x = xStart; x <= xEnd; x += gridSpacing)
        {
            gridContext.moveTo(x, yStart);
            gridContext.lineTo(x, yEnd);
        }

        // Draw the board y lines
        for (var y = yStart; y <= yEnd; y += gridSpacing)
        {
            gridContext.moveTo(xStart, y);
            gridContext.lineTo(xEnd, y);
        }

        gridContext.strokeStyle = "#000000";
        gridContext.stroke();

        // Create new image object to use as pattern
        var img = new Image();
        img.src = 'bg_wood.jpg';
        img.onload = function()
        {
            var boardBG = gridContext.createPattern(img, 'repeat');
            gridContext.fillStyle = boardBG;
            gridContext.fillRect(xStart, yStart, gridSize, gridSize);
        }

I'm experimenting a bit with the new tag and I've already hit my first roadbump. I figured I'd start getting my feet wet by implementing a version of the classic board game Go/Baduk/Weiqi.

I've drawn the xy grid using moveTo() and lineTo(), and I've drawn a wood background using fillRect() that needs to be "under" that XY grid of course.

However, therein lies my problem. The fillRect() background is drawn on top of the grid - thus obscuring the grid.

How do I reverse this? Here's what I'm working with:

        var boardSize = 19;                 
        var gridSpacing = 25;
        var gridSize = boardSize * gridSpacing;

        var xStart = (window.innerWidth / 2) - (gridSize / 2) + 0.5;
        var yStart = (window.innerHeight / 2) - (gridSize / 2) + 0.5;
        var xEnd = xStart + gridSize;
        var yEnd = yStart + gridSize;


        var gridContext = canvas.getContext("2d");

        gridContext.beginPath();

        // Draw the board x lines
        for (var x = xStart; x <= xEnd; x += gridSpacing)
        {
            gridContext.moveTo(x, yStart);
            gridContext.lineTo(x, yEnd);
        }

        // Draw the board y lines
        for (var y = yStart; y <= yEnd; y += gridSpacing)
        {
            gridContext.moveTo(xStart, y);
            gridContext.lineTo(xEnd, y);
        }

        gridContext.strokeStyle = "#000000";
        gridContext.stroke();

        // Create new image object to use as pattern
        var img = new Image();
        img.src = 'bg_wood.jpg';
        img.onload = function()
        {
            var boardBG = gridContext.createPattern(img, 'repeat');
            gridContext.fillStyle = boardBG;
            gridContext.fillRect(xStart, yStart, gridSize, gridSize);
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文