Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

placing a div over a canvas in html5

I am trying to place a div element over a canvas (it is for a game) on the center of my page. It is about a startscreen that I want to show over the canvas, before starting the game(this startscreen has options like "Play game", "Settings" etc). The problem is that I cannnot get this done, I have searched a lot on Google, I have seen a lot of examples, but none of them seems to be working for me. Here is my code:

<div id="gamecontainer">
            <canvas id="myCanvas" width="800" height="600">
                Sorry, <strong>CANVAS</strong> is not supported by your browser. Get a more recent one to play my game!
            </canvas>

            <div id="gamestartscreen" class="gamelayer">
                <img src="images/icons/play.png" alt="Play Game" onclick="alert('ceve');"><br>
                <img src="images/icons/settings.png" alt="Settings">
            </div>
</div>

here is the css:

#gamecontainer {
    width: 800px;
    height: 600px;
    background-color: beige;
    border: 1px solid black;
    position: relative;
    margin: 0 auto;
}

.gamelayer {
    width: 800px;
    height: 600px;
    position: absolute;
    display: none;
    z-index: 0;
}


/* Screen for the main menu (Like Play, Settings, etc.) */
#gamestartscreen {
    position: relative;
    margin: 0 auto;
}

#gamestartscreen img {
    margin: 10px;
    cursor: pointer;
}

Could someone please tell me where I am doing it wrong?

like image 849
André Hincu Avatar asked Apr 07 '13 10:04

André Hincu


1 Answers

The problem was that the canvas layer was in its native static positioning, while the gamestart div was in relative positioning with no positive z-index value. Essentially, you jut have to set the canvas element to position: absolute or even position: relative, while having a >1 z-index for the startstreen div. This JSFiddle should make it all clear. Cheers!

like image 120
Radu Chelariu Avatar answered Oct 07 '22 13:10

Radu Chelariu