简体   繁体   English

从数组在画布上绘制正方形

[英]Draw squares on a canvas from an array

I have a problem about how to draw squares on a canvas, using the coordinates in a nested array. 我有一个关于如何使用嵌套数组中的坐标在画布上绘制正方形的问题。 The idea is to highlight some squares so the player knows he can move on these ones. 想法是突出显示一些正方形,以便玩家知道他可以在这些正方形上移动。

The function drawings these squares is called after, so the entire canvas is not drawn again. 之后将调用绘制这些正方形的函数,因此不会再次绘制整个画布。

I know how to display an image on a precise square, but don't understand how I can iterate through a nested array so it is usable by the canvas to draw again some squares in red. 我知道如何在精确的正方形上显示图像,但不了解如何遍历嵌套数组,因此画布可用于再次绘制红色正方形。

How to transform "array coordinates" into values usable by the canvas drawing methods. 如何将“数组坐标”转换为画布绘制方法可用的值。

Or does the problem comes from a difference between availableSquare and chartBoard? 还是问题来自于availableSquare和chartBoard之间的差异?

Since I didn't find any topic on this, I hope my example is detailed enough. 由于我没有找到关于此的任何主题,所以我希望我的示例足够详细。

 function Game(width, height) { this.width = width; this.height = height; } const game = new Game(10, 10) const chartBoard = []; const availableSquares = [ [6, 6], [6, 7], [6, 8] ] // The nested arrays with all the possible position function createBoard(width, height) { for (let i = 0; i < width; i++) { const row = []; chartBoard.push(row); for (let j = 0; j < height; j++) { const col = {}; row.push(col); } } return chartBoard; }; createBoard(game.width, game.height); console.log(chartBoard) // Display the array on the canvas const ctx = $('#board').get(0).getContext('2d'); function drawBoard(width, height) { for (let i = 0; i < width; i++) { for (let j = 0; j < height; j++) { ctx.strokeStyle = 'rgba(0, 0, 0, 0.7)'; ctx.beginPath(); ctx.strokeRect(j * 64, i * 64, 64, 64); ctx.closePath(); } } }; drawBoard(game.width, game.height); // Function to highlight the squares from the array availableSquares // function showAvailableMovement(array) { // for (let i = 0; i < array.length; i++) { // for (let j = 0; j < array[i].length; j++) { // // ctx.strokeStyle = 'red'; // ctx.beginPath(); // ctx.strokeRect(j * 64, i * 64, 64, 64); // ctx.closePath(); // } // } // }; // showAvailableMovement(availableSquares); 
 body { background-color: black; } #board { background-color: white; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <canvas id="board" width="640" height="640"></canvas> 

You're actually really close, you're just confusing your x and y values: 您实际上真的很接近,只是混淆了x和y值:

// Function to highlight the squares from the array availableSquares
function showAvailableMovement(array) {
    for (let i = 0; i < array.length; i++) {
        for (let j = 0; j < array[i].length; j++) {

            let x = array[i][0];
            let y = array[i][1];

            ctx.strokeStyle = 'red';
            ctx.beginPath();
            ctx.strokeRect(x * 64, y * 64, 64, 64);
            ctx.closePath();
        }
    }
};
showAvailableMovement(availableSquares);

Since you have just array of coordinates, you don't need to run nested for loops to access values. 由于您只有一组坐标,因此不需要运行嵌套的for循环即可访问值。 You can do it like this: 您可以这样做:

function showAvailableMovement(array) {
    for (let i = 0; i < array.length; i++) {
        let x = array[i][0],
            y = array[i][1];
        ctx.strokeStyle = 'red';
        ctx.beginPath();
        ctx.strokeRect(x * 64, y * 64, 64, 64);
        ctx.closePath();
        }
    };

In your method you were actually running for twice, so the values were by iterations: 在您的方法中,您实际上运行了两次,因此这些值是通过迭代进行的:

  1. i = 0, j = 0 i = 0,j = 0
  2. i = 0, j = 1 i = 0,j = 1
  3. ... and so on ... 等等

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

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