views:

223

answers:

1

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);
        }
+2  A: 

Try using gridContext.globalCompositeOperation = 'destination-over'; when drawing the background.

KennyTM
Worked absolutely beautifully! I was messing with globalCompositeOperation earlier but looks like I only tried the "source-" variations. thanks!Notice anything else fishy about my code? I have to admit I don't fully understand the canvas drawing stack - or how save() and restore() are meant to be used. Or whether I should be using closePath(). Back to the docs!
MrMatt
I seem to have run into a new issue, but in the same vein. I'm trying to draw board pieces ontop of the board itself (of course!), but I'm running into the same "layering"/compositing issues.I added the following chunk:var stone = new Image();stone.src = "b.png";stone.onload = function(){ gridContext.drawImage(stone, xStart - 30, yStart - 30);};What gives?
MrMatt
@MrMatt Change the `globalCompositeOperation` back to `source-over` when drawing pieces.
KennyTM
Works... but now the grid lines have vanished.
MrMatt
@MrMatt: Can you pastie (http://pastie.org/) the code you're using now? (BTW, it seems easier to background-load all images at the beginning to avoid `globalCompositeOperation`.)
KennyTM
Thanks for taking the time to help me through this. I'm having a tough time wrapping my head around this one: http://pastie.org/835157
MrMatt
@MrMatt: See http://pastie.org/835165. The problem is `onload` will *not* execute at the same time you define it. So all your previous composite operations will not take effect when `onload` is executed. You can only change it during the `onload`.
KennyTM
I wish I could upvote you a thousand times.
MrMatt