EaselJS网格绘图奇怪地偏移/缩放

时间:2013-09-18 16:57:48

标签: javascript canvas easeljs createjs

这段代码应该绘制一个网格(它用于迷宫,所以我只需绘制一部分):

function drawMaze() {
for (var r = 0; r < Rows.length; r++) {
    var row = Rows[r];
    var sr = new createjs.Shape();
    sr.x = 0;
    sr.y = (r + 1) * offY;
    var gr = sr.graphics.beginStroke("black");
    gr.moveTo(0, (r + 1) * offY);
    for (var i = 0; i < row.length; i++) {
        if (row[i] === true) {
            console.log("r=" + r + ",i=" + i + ",x=" + (i + 1) * offX + ",y=" + (r + 1) * offY);
            gr.lineTo((i + 1) * offX, (r + 1) * offY);
        } else {
            gr.moveTo((i + 1) * offX, (r + 1) * offY);
        }
    }
    stage.addChild(sr);
}
for (var c = 0; c < Cols.length; c++) {
    var col = Cols[c];
    var sc = new createjs.Shape();
    sc.x = (c + 1) * offX;
    sc.y = 0;
    var gc = sc.graphics.beginStroke("black");
    gc.moveTo((c + 1) * offX, 0);
    for (var i = 0; i < col.length; i++) {
        if (col[i] === true) {
            console.log("c=" + c + ",i=" + i + ",x=" + (c + 1) * offX + ",y=" + (i + 1) * offY);
            gc.lineTo((c + 1) * offX, (i + 1) * offY);
        } else {
            gc.moveTo((c + 1) * offX, (i + 1) * offY);
        }
    }
    stage.addChild(sc);
}
var sd = new createjs.Shape();
sd.graphics.s("black").mt(0, 0).lt(offX * Cols.length, offY * Rows.length).f("red").r(175,175,175,175);
stage.addChild(sd);
stage.update();
}

对角线/红色方块仅用于可视化。 这样,行/列之间的间距加倍,因此只有一半的线形成网格。以下是完整代码:http://jsfiddle.net/vyLRT/9/

1 个答案:

答案 0 :(得分:0)

我刚刚发现了什么问题!我只是简单地移动了两次。

所以而不是

sr.x = 0;
sr.y = (r + 1) * offY;

sc.x = (c + 1) * offX;
sc.y = 0;

所有值只需设置为0。