【问题标题】:How do I get these canvases to align properly?如何让这些画布正确对齐?
【发布时间】:2021-05-01 10:22:27
【问题描述】:

我试图将三幅画布相互叠加显示,但它们只是彼此并排显示。我对css的经验很少。我需要添加什么?

<body>
<div id="canvas_container">
<canvas id="Background" width="330" height="330"></canvas>
<canvas id="mycanvas" width="330" height="330"></canvas>
<canvas id="Overlay" width="330" height="330"></canvas>
</div>

<style>
.canvas_container {
  position: relative;
}

.container > canvas {
  position: absolute;
  top: 0;
  left: 0;
}
</style>

<div id="codeinput">
<br>
The "X" goes here:<input type="text" id="shapeX">

<br>
The "Y" goes here:<input type="text" id="shapeY">

<br>
Choose the color: <input type="text" id="shapeColor" value="green">


<script>
unrelated javascript
</script>

<br>
<button onclick=makeSquare();>Draw shape</button>
</div>
</body>

【问题讨论】:

    标签: html css canvas html5-canvas


    【解决方案1】:

    你的 CSS 没问题,但你设置了错误的选择器。您有id = canvas_container,但为canvas_container 类和container 类设置样式

    #canvas_container {
      position: relative;
      width: 330px;
      height: 330px;
    }
    
    #canvas_container > canvas {
      position: absolute;
      top: 0;
      left: 0;
      border: 1px solid red;
    }
    <div id="canvas_container">
      <canvas id="Background" width="330" height="330"></canvas>
      <canvas id="mycanvas" width="330" height="330"></canvas>
      <canvas id="Overlay" width="330" height="330"></canvas>
    </div>
    
    <div id="codeinput">
      <br>
      The "X" goes here:<input type="text" id="shapeX">
      <br>
      The "Y" goes here:<input type="text" id="shapeY">
      <br>
      Choose the color: <input type="text" id="shapeColor" value="green">
      <br>
      <button onclick=makeSquare();>Draw shape</button>
    </div>

    【讨论】:

    • 谢谢,现在如何让文本显示在画布下方?
    • @blobfishsandwich 只是设置容器的宽度和高度。它不是按内容大小计算的,因为内容有position: absolute。我用这个修复编辑我的答案
    • @blobfishsandwich 并且您需要为所有画布设置不同的z-index 以控制它们按Z轴排序
    • 谢谢!你帮了我很多忙。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 2014-08-08
    • 2018-08-03
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多