Javascript 将画布放置在重叠画布下方

Javascript 将画布放置在重叠画布下方,javascript,css,canvas,position,Javascript,Css,Canvas,Position,我有两张画布故意重叠,画一个球落在另一张画布上。我想把第三块画布放在页面上两块重叠的画布下面,没有任何重叠。当保存重叠画布的div元素具有相对位置时,它不会阻止其他元素重叠它。据我所知,该分区必须相对定位,以便其内部的画布可以绝对定位并重叠 这是我的HTML: <div id="box" style="position: relative;"></div> <div id="countdiv"></div> 下面是第三个画布的JavaScript

我有两张画布故意重叠,画一个球落在另一张画布上。我想把第三块画布放在页面上两块重叠的画布下面,没有任何重叠。当保存重叠画布的div元素具有相对位置时,它不会阻止其他元素重叠它。据我所知,该分区必须相对定位,以便其内部的画布可以绝对定位并重叠

这是我的HTML:

<div id="box" style="position: relative;"></div>
<div id="countdiv"></div>
下面是第三个画布的JavaScript:

countCanvas = document.createElement("canvas");
countCanvas.width = window.innerWidth - (window.innerWidth * .06);
countCanvas.height = 100;
countCanvas.style = "border: 1px solid #808080;";
document.getElementById("box").appendChild(countCanvas);
countctx = countCanvas.getContext("2d");
ballCanvas.height = boxCanvas.height;
ballCanvas.style = "border: 1px solid #808080; position: absolute; left: 0; top: 0; z-index: 1";
document.getElementById("countdiv").appendChild(ballCanvas);

单击drawbox()然后单击“显示命中数”可以看到问题。谢谢你的帮助!如果我能提供更多的信息,请告诉我。

我必须承认,我不太确定你在问什么

我想你应该在顶部有两个重叠的画布,然后在底部有一个单独的画布

您希望使用javascript而不是CSS来进行样式设置

下面是代码和小提琴:


这正是我要问的,谢谢!我很感激你的回答,这正是我想要的!我不知道您可以使用CSS设置属性的高度和宽度,以避免重叠。再次感谢你。
countCanvas = document.createElement("canvas");
countCanvas.width = window.innerWidth - (window.innerWidth * .06);
countCanvas.height = 100;
countCanvas.style = "border: 1px solid #808080;";
document.getElementById("box").appendChild(countCanvas);
countctx = countCanvas.getContext("2d");
ballCanvas.height = boxCanvas.height;
ballCanvas.style = "border: 1px solid #808080; position: absolute; left: 0; top: 0; z-index: 1";
document.getElementById("countdiv").appendChild(ballCanvas);
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>

<style>
#box{
  border:1px solid blue;
}
.subcanvs{
  border: 1px solid green;
}
#countCanvas{
  border: 1px solid gold;    
}

</style>

<script>
$(function(){

    var w=300;
    var h=200;

    var boxDiv=document.getElementById("box");
    boxDiv.style.width=w+"px";
    boxDiv.style.height=h+"px";
    boxDiv.style.position="relative";

    var box=document.getElementById("boxCanvas");
    var boxCtx=box.getContext("2d");
    box.width=w;
    box.height=h;
    box.style.position="absolute";
    box.style.left=0;
    box.style.top=0;

    var ball=document.getElementById("ballCanvas");
    var ballCtx=ball.getContext("2d");
    ball.width=w;
    ball.height=h;
    ball.style.position="absolute";
    ball.style.left=0;
    ball.style.top=0;

    var counter=document.getElementById("countCanvas");
    var countCtx=counter.getContext("2d");
    counter.width=w;
    counter.height=100;

    test(boxCtx,20,30,"red");
    test(ballCtx,100,30,"green");
    test(countCtx,30,30,"blue");

    function test(ctx,x,y,color){
        ctx.beginPath();
        ctx.fillStyle=color;
        ctx.rect(x,y,50,50);
        ctx.fill();
    }

});   // end $(function(){});
</script>
</head>

<body>
    <div id="box">
      <canvas id="boxCanvas" class="subcanvs"></canvas>
      <canvas id="ballCanvas" class="subcanvs"></canvas>
    </div>
    <canvas id="countCanvas"></canvas>
</body>
</html>
// this sets the canvas drawing area to 100px wide
myCanvas.width=100;

// this styles the canvas element to be 100px wide on the page
// if this isn't == myCanvas.width then the image will be horizontally distorted
myCanvas.style.width="100px";