Canvas drawImage - visible edges of tiles in firefox/opera/ie (not chrome)

前端 未结 4 1039
自闭症患者
自闭症患者 2020-12-11 19:53

I\'m drawing a game map into canvas. The ground is made of tiles - simple 64x64 png images.

When I draw it in Chrome, it looks ok (left), but when I draw it in Firef

4条回答
  •  离开以前
    2020-12-11 20:58

    The simplest solution (and I'd argue most effective) is to use tiles that have a 1 pixel overlap (are either 1x1 or 2x2 larger) when drawing the background tiles of your game.

    Nothing fancy, just draw slightly more than you would normally. This avoids complications and performance considerations of bringing extra transformations into the mix.

    For example:

    var img = new Image();
    img.onload = function () {
        for (var x = 0.3; x < 200; x += 15) {
            for (var y = 0.3; y < 200; y += 15) {
                ctx.drawImage(img, 0, 0, 15, 15, x, y, 15, 15);
                // If we merely use 16x16 tiles instead,
                // this will never happen:
                //ctx.drawImage(img, 0, 0, 16, 16, x, y, 16, 16);
            }
        }
    }
    img.src = "http://upload.wikimedia.org/wikipedia/en/thumb/0/06/Neptune.jpg/100px-Neptune.jpg";
    

    Before: http://jsfiddle.net/d9MSV

    And after: http://jsfiddle.net/d9MSV/1/


    Note as the asker pointed out, the extra pixel needs to account for scaling, so a more correct solution is his modification: http://jsfiddle.net/d9MSV/3/

提交回复
热议问题