英文:
Why does layering html text div over a webgl shader background by specifying higher z-index still result in text obstructed by the shader?
问题
我打算通过使用JavaScript中的three.js库动态创建glsl着色器,从而扩展WordPress主题,将这些着色器嵌入到其他HTML元素中,包括作为文本内容的背景可能。
我有部分工作代码用于将着色器用作文本背景,除了应该显示在着色器前面的文本被着色器的白色像素部分部分阻挡之外,一切正常,尽管它的z-index比文本低。我在项目中陷入困境,任何可接受的有效答案将不胜感激。
问题的视觉效果:
请注意,着色器的某些部分(白色像素)挡住了放在着色器前面的黑色文本
文档检查器显示div id "text-container" 应该在z-index上置于画布前面,据我了解:
检查器控制台
最后可能不重要的,但以防万一包括的JavaScript代码,该代码将着色器渲染器放置到页面上,并为着色器及其父文本容器设置z-index。
// 设置渲染器大小并将其添加到容器中
iReturn.renderer = new THREE.WebGLRenderer();
iReturn.renderer.setSize(
shadowEl.getBoundingClientRect().width,
shadowEl.getBoundingClientRect().height
);
// 将渲染器的dom元素定位为absolute,以像素完美地覆盖其父容器
iReturn.renderer.domElement.style.position = "absolute";
iReturn.renderer.domElement.style.top = 0;
iReturn.renderer.domElement.style.left = 0;
// 使用z-index将其定位在父元素内容的后面
iReturn.renderer.domElement.style.zIndex = "1";
// 阴影元素应该具有比其子着色器渲染器更高的z-index,即"1" z-index
shadowEl.style.zIndex = "2";
// 透明背景,只显示文本
shadowEl.style.backgroundColor = "transparent";
shadowEl.style.position = "relative";
shadowEl.appendChild(iReturn.renderer.domElement);
英文:
I intend to extend a wordpress theme by dynamically creating glsl shaders using the three.js library for JavaScript, and using those shaders embedded into other html elements including as backgrounds for text content possibly.
I have partially working code for using shader as text background, except the text which should be displayed in front of the shader is actually partially blocked by the white pixels of the shader program, even though it has a lower z-index than the the text. I am stuck on this for a project, any working accepted answer shall be appreciated
visual of the problem:
notice that parts of shader (white pixels) block the black text placed in front of shader
document inspector showing div id "text-container" should be in front of canvas per zindex as I understand it):
inspector console
lastly not important probably, but including just in case is the javascript code which places the shader renderer onto the page, and also sets zindex for shader and it's parent text container.
// set the renderer size and add it to the container
iReturn.renderer = new THREE.WebGLRenderer();
iReturn.renderer.setSize(
shadowEl.getBoundingClientRect().width,
shadowEl.getBoundingClientRect().height
);
//set renderer dom element positioning to absolute, position pixel perfect over its parent container
iReturn.renderer.domElement.style.position = "absolute";
iReturn.renderer.domElement.style.top = 0;
iReturn.renderer.domElement.style.left = 0;
//position behind parent's contents using zindex
iReturn.renderer.domElement.style.zIndex = "1";
//shadow element should have higher zindex than it's child shader renderer which is "1" zindex
shadowEl.style.zIndex = "2";
//transparent background, only text displays
shadowEl.style.backgroundColor = "transparent";
shadowEl.style.position = "relative";
shadowEl.appendChild(iReturn.renderer.domElement);
答案1
得分: 1
已解决。通过将着色器画布放置在与 div 相同的父容器中,以便 z-index 作用域相关。
英文:
Resolved. by placing shader canvas in the same parent container as the div, so zindex scope would be relevent.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论