Placing HTML text on top of a three.js renderer

I have been attempting to overlay HTML text onto my three.js sketch, but adjusting the z-index does not seem to be working. While I know that I could utilize innerHTML, the issue arises when I need to also use the onclick attribute, which is not the main focus of my question here.

In the provided example, I am using a CSS3D renderer and my goal is to display the HTML content to the side. However, when adjusting padding values, the entire body shifts down instead of just moving the text.

Is there a fix for this?

UPDATE:

 // WebGL renderer2
 renderer2= new THREE.WebGLRenderer({ antialias: true });
renderer2.setClearColor(0xffffff, 1.0)
 renderer2.setSize(window.innerWidth, window.innerHeight);
//renderer2.domElement.style.position = 'relative';
 renderer2.domElement.style.zIndex = '-1';
 document.body.appendChild(renderer2.domElement);

After some investigation, I decided to add another renderer that would load CSS3D and WEBGL simultaneously on the same page. However, when I set both positions to relative, they end up stacked one below the other. Ideally, I would like them to stack on top of each other with the HTML text displayed to the side.

    <!DOCTYPE html>
<html>
<head>

    <script src="http://threejs.org/build/three.min.js"></script>
    <script src="http://threejs.org/examples/js/renderers/CSS3DRenderer.js"></script>
    <style>
        body {

            margin: 0;
            overflow: hidden;
            background-color: white;

        }

        .text{

            z-index: 99;
            padding-top: 5%;

        }

        .large {
            font-size: xx-large;
        }
    </style>


</head>
<body>
    <div class="text">
        <p>Lorem Ipsum</p>
    </div>

<script>

    var string = '<div>' +
            '<h1>A test CSS3D example.</h1>' +
            '<span class="large">this is an input example</span>' +
            '<textarea> Try adding text here</textarea>' +
            '</div>';

    // global variables
    var renderer;
    var scene;
    var camera;

    function init() {


        scene = new THREE.Scene();
        camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
        renderer = new THREE.CSS3DRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);

        renderer.domElement.style.position = 'absolute';
        renderer.domElement.style.zIndex= '1';

        camera.position.x = 500;
        camera.position.y = 500;
        camera.position.z = 500;
        camera.lookAt(scene.position);


        document.body.appendChild(renderer.domElement);
        var cssElement = createCSS3DObject(string);
        cssElement.position.set(100, 100, 100);
        scene.add(cssElement);

        render();
    }

    function createCSS3DObject(s) {

        var wrapper = document.createElement('div');
        wrapper.innerHTML = s;
        var div = wrapper.firstChild;


        div.style.width = '370px';
        div.style.height = '300px';
        div.style.transform = 'rotate(70deg)';
        div.style.opacity = 0.7;
        div.style.background = new THREE.Color(Math.random() * 0xffffff).getStyle();


        var object = new THREE.CSS3DObject(div);
        return object;
    }

    function render() {

        renderer.render(scene, camera);
         requestAnimationFrame(render);
    }

    window.onload = init;

</script>
</body>

</html>

Answer №1

Your element with the class text currently has a z-index set, but in order for it to take effect, you must position it either absolute or relative. You can learn more about z-index from this resource: https://developer.mozilla.org/en-US/docs/Web/CSS/z-index

If you can share a jsfiddle or a codepen link, I'll be able to provide you with a more specific solution.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Submitting forms with Ajax without reloading the page can cause errors in Internet Explorer

Simply put: Upon clicking the login button in Firefox, Chrome, or Safari, the form is submitted correctly, running a validation via ajax and opening a new page. However, in Internet Explorer (version less than 9), the submission attempts to post to the c ...

What could be causing issues with mysqli connection to the database?

Here is the content of my connect.php file: $mysql_hostname = "localhost"; $mysql_username = "root"; $mysql_password = "xxxxxxxx"; $mysql_database = "marrybrown_clean"; $bd = mysqli_connect ($mysql_hostname, $mysql_username, $mysql_password) or die (&apos ...

Using HTML nested lists in Wordpress sites

Working on nested, ordered lists using ol types and encountering an issue where the code appears correctly in Wordpress preview but displays incorrectly in Firefox and IE. The lists are showing as numbered instead of the specified types in the code provide ...

Having trouble retrieving an object property in HTML or TypeScript within an Angular framework?

export class ComponentOne { array_all_items: Array<{ page_details: any }> = []; array_page_details: Array<{ identifier: number, title: string }> = []; initial_item: Array<{ identifier: number, title: string }> = [ { ...

Calculate averages of an array and show them when the page loads using Vue

In my coding project, there is an array named products that follows this specific structure: { "_id": "150", "name": "Milk", "description": "Skimmed", "price": "10", "ratings": [ ...

Include token in src tag requests Angular version 8

In the process of developing a website, I have encountered a challenge. I am creating a platform where users can access another website I am currently working on after they log in. Once authorized, users receive a JWT token which is sent in the header with ...

Achieving consistent margins across various browsers for UL elements

UL element margins are the same, but Firefox and IE are displaying differently. FULL CODE: <html> <head> <style> body { background-color: Red; } ul ...

Is there a way to extract information from a <span> element with restricted access?

I'm currently utilizing bs4 and urllib2 to extract data from a website. Check out the webpage here. The goal is to retrieve the remaining digits of the telephone number 3610...... but beforehand, it's necessary to click on a button to reveal th ...

Harness the power of a NUXT component on a different website

Currently, I have a fully functional NUXT application that consists of numerous pages and components operating in `universal` mode. My challenge now is to render one of these components on a separate static HTML website. Exporting a component from a stand ...

What is the proper method for triggering an animation using an IF statement with A-Frame animation mixer?

I am currently exploring the capabilities of the animation mixer in A-Frame and trying to trigger a specific action when a particular animation is playing (like Animation 'B' out of A, B, C). Although I'm not well-versed in Javascript, I ha ...

Customize the X and Y position of the CSS background with specific degrees

Looking for help customizing the background position in a PSD image to adjust the slope at the top, right-left side, and bottom. Here is my PSD: https://i.stack.imgur.com/Jr4h7.png Below is some of my CSS code: html{ background:#aea99f; } body{ backgroun ...

Comparison between Login popups and Login pagesIt's time

Curious about the most effective approach for handling multiple login forms? Would you rather: A) Keep each form on a separate web page. B) Utilize modal popups with update panels. Weighing SEO considerations as well. Cheers! ...

What steps should I take to incorporate Google AdSense advertisements onto my website?

After developing a website using HTML and PHP, I attempted to incorporate Adsense ads. However, upon inserting the ad code between the body tags, the ads failed to display on the site. How can I resolve this issue? ...

Add hyphens to separate the words in AngularJS if there is a break in the string

Within a div of set width, a string is being bound to it. This string could be short or long. I would like for the string to break with a hyphen inserted on each line except for the last one. For example: If the string is "misconception" and it breaks at ...

I have been having trouble getting the entire background to display in a specific color with Styled Components

It seems like a simple issue, but I just can't get the background to cover the entire screen. Even though I have provided the code I used, only a quarter of the top of the screen appears black when wrapping something in the component. Here is the cod ...

What steps should I take with my Android PWA manifest and service workers?

I created a web application that I want to prompt Android users to download when they visit. To build my service workers and manifest, I utilized PWA Builder which can be found at Now that I have the manifest file ready, should I simply upload it to the r ...

The issue with Thymeleaf recursive function not functioning as expected

I am attempting to create a recursive list using Thymeleaf. I have a simple Java object that represents a node with a description and an array list of child nodes. However, my current HTML/Thymeleaf structure is not properly iterating through the nested le ...

Is the AngularJS email validation triggering too soon?

My challenge involves validating an email field in angularjs using the following code: <input type="email" ng-model="email" class="form-control" name="email" ng-required="true" placeholder="Email Address"> <span ng-show="loginForm.email.$touched ...

Tips for properly aligning an image within an HTML iframe

I've been working on positioning an image inside an iframe, but so far my research hasn't yielded the results I need. Here's what my code looks like: The HTML <div class="modal-body description"> <div class="modal ...

Strategies for managing large numbers within WebGL GLSL shader programs

What is the best approach to handle large numbers like the one provided in GLSL? I have a shader that requires Date.now() as a uniform, which is defined as: The Date.now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 ...