简体   繁体   English

如何在 React 中将内容放在画布上

[英]How can I put content over my canvas in React

This is my code for my canvas and my Navbar and it looks like this:这是我的画布和导航栏的代码,它看起来像这样:

<DashboardNavbar style={{ zIndex: 2 }} />
  <canvas id="canvas" style={{ background: "#232323", width: "100%", zIndex: 1 }}
></canvas>

The problem is I want to achieve something like this:问题是我想实现这样的目标:


<canvas id="canvas" style={{ background: "#232323", width: "100%", zIndex: 1 }}>
<DashboardNavbar style={{ zIndex: 2 }} />
</canvas>

I want that I can display my canvas behaves like the background of the website so I can display content over it in this case it is my navbar but I plan to display more stuff below the navbar and the canvas as my background.我希望我的画布可以像网站的背景一样显示,这样我就可以在它上面显示内容,在这种情况下它是我的导航栏,但我计划在导航栏和画布下方显示更多内容作为我的背景。

What I have tried:我尝试过的:

  • Using zIndexes to put my canvas one below my navbar but this did not work使用 zIndexes 将我的画布放在导航栏下方,但这不起作用
  • Tried using position absolute and relative on canvas but did not work either尝试在画布上使用绝对位置和相对位置,但也不起作用

You can use a "layout div" as a background for all the elements you will put over the canvas, just after the canvas, and both the canvas and the layout div should have position absolute and need to be inside another div:您可以使用“布局 div”作为将放在画布上的所有元素的背景,就在画布之后,并且画布和布局 div 都应该具有绝对位置,并且需要位于另一个 div 内:

JS JS

const DashboardNavbar = () => (
  <div className="navbar">Hello</div>
)
const HelloWorld = () => (
  <div className="main">
      <canvas id="canvas" style={{ background: "#232323", width: "100%", height:100}} />
      <div className="layout">
         <DashboardNavbar />  
      </div>
  </div>
)
ReactDOM.render(<HelloWorld />, document.getElementById('app'))

CSS CSS

.layout{
  position:absolute;
  width:100%;
  height:100px;
}
#canvas{
  position: absolute;
}
.navbar{
  color: white;
  background-color: blue;
  padding:2px;
  margin:20px;
}

HTML HTML

<div id="app"></div>

Codepen working example Codepen 工作示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM