上古卷轴5裹尸布代码:HTML5画布方格图案(checkered patern)

我正在尝试创建一个复古的像素化背景画。“像素”的 x 和 y 位置是奇数和偶数相应。这似乎适用于 4 的分辨率 (res 变量),然后% 运算符似乎不起作用。

function drawPixelatedBackground()
{
    var res = 5;
    for (var x=0; x<settings.width/res;x++ )
    { 
        for (var y=0;y<settings.height/res;y++)
        {
            if ( (x%2==0) && (y%2==0) )
            {
                nx = x * (settings.width/res);
                ny = y * (settings.width/res);
                ctx.fillStyle= settings.colors.Fill;
                ctx.fillRect(nx,ny, nx+  (settings.width/res),ny+   (settings.height/res) );
            }
        }
    }
}
5

你的逻辑有点问题。我将在解释我的。

http://jsfiddle.net/2eee9moq/2/
function drawCheckeredBackground(can, nRow, nCol) {
    var ctx = can.getContext("2d");
    var w = can.width;
    var h = can.height;
    nRow = nRow || 8;    // default number of rows
    nCol = nCol || 8;    // default number of columns
    w /= nCol;            // width of a block
    h /= nRow;            // height of a block
    for (var i = 0; i < nRow; ++i) {
        for (var j = 0, col = nCol / 2; j < col; ++j) {
            ctx.rect(2 * j * w + (i % 2 ? 0 : w), i * h, w, h);
        }
    }
    ctx.fill();
}
var canvas = document.getElementById("canvas");
drawCheckeredBackground(canvas);
<canvas id="canvas" width="300" height="300"></canvas>

嵌套的for循环在一行中绘制块。

2 * j * w + (i % 2 ? 0 : w)是每隔一行移动每个块的 x 坐标。

3

循环通过行和列像这样:

for (let column = 0; column < board.columns; column++) {
  for (let row = 0; row < board.rows; row++) {
  }
}

如果以下任一条件为真,则通过绘制矩形来创建方格图案:

行是偶数,列是奇数

行是奇数,列是偶数

在代码中:

if (row % 2 === 0 && column % 2 === 1 || row % 2 === 1 && column % 2 === 0) {
  canvas.context.rect(
    column * column_width,
    row * row_height,
    column_width,
    row_height
  );
}
const canvas = {
  element: document.querySelector("canvas"),
  context: document.querySelector("canvas").getContext('2d')
}
const board = {
  rows: 15,
  columns: 17,
  colors: {
    light: '#a3cf53',
    dark: '#abd55a'
  }
}
canvas.context.beginPath();
canvas.context.fillStyle = board.colors.dark;
canvas.context.rect(0, 0, canvas.element.width, canvas.element.height);
canvas.context.fill();
canvas.context.beginPath();
canvas.context.fillStyle = board.colors.light;
for (let column = 0; column < board.columns; column++) {
  for (let row = 0; row < board.rows; row++) {
    if (row % 2 === 0 && column % 2 === 1 || row % 2 === 1 && column % 2 === 0) {
      canvas.context.rect(
        column * canvas.element.width / board.columns,
        row * canvas.element.height / board.rows,
        canvas.element.width / board.columns,
        canvas.element.height / board.rows
      );
    }
  }
}
canvas.context.fill();
body {
  margin: 0;
}
<canvas width="595" height="525"></canvas>
0

为了改变正方形的颜色,我们可以使用以下事实:(1,1),(2,2),(3,3),...处的对角正方形将具有相同的颜色,例如白色,而(1,2),(2,3),(3,4),...将具有相同的颜色,例如黑色。我们在这里注意到一个模式,即在(col, row)中,如果1

代码逻辑:

if ((i % 2 == 0 && j % 2 == 0) || (i % 2 != 0 && j % 2 != 0)){
    square is white/color1
}
else{
    square is black/color2
}                   
     
const board = document.getElementById("board")
const ctx = board.getContext("2d")
let cols = 8 
let rows = 8
let squareSize = 50
function drawCheckeredBoard(ctx, squareSize, rows, cols) {
    let whiteSquareColor = "#ffe6cc"
    let blackSquareColor = "#cc6600"
    for (let j = 0; j < rows; j++)
        for (let i = 0; i < cols; i++) {
            if ((i % 2 == 0 && j % 2 == 0) || (i % 2 != 0 && j % 2 != 0)) 
                ctx.fillStyle = whiteSquareColor
            else ctx.fillStyle = blackSquareColor
            ctx.fillRect(i * squareSize, j * squareSize, squareSize, squareSize)
        }
}
drawCheckeredBoard(ctx, squareSize, rows, cols)
<canvas id="board" width="400" height="400">

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(90)
E Blocks:HDFS中的文件数与块数(no blocks)
上一篇
什么是cfg桩:.cfg文件和.xml文件有什么区别
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(38条)