How can I combine an ordinary image and a CSS2D image in THREE.js CSS2DRenderer and save them together as a single .jpg file?

After implementing the following code...

(1) I successfully saved regular rendered THREE.js scenes as .jpg files;

(2) Additionally, I managed to utilize CSS2DRenderer to display CSS2D labels on top of the canvas;

(3) Now, my goal is to save the image with CSS2DRenderer-rendered overlay as a separate .jpg file;

However, I encountered an error:

TypeError: labelRenderer.domElement.toDataURL is not a function
.

I'm starting to think that exporting from CSS2D might not be feasible?

(4) Ideally, I aim to save the combined onscreen image (ordinary and CSS2D overlay) as a single .jpg file.

Here's the code snippet:

// GLOBALS

// INITIATION

    var MyContainer = document.getElementById( "for_3JS" );
    MyContainer.innerHTML = "";

    renderer = new THREE.WebGLRenderer( {antialias:true , preserveDrawingBuffer: true} );   
    renderer.setPixelRatio( window.devicePixelRatio );
    renderer.setSize( wwww, hhhh );

    MyContainer.appendChild( renderer.domElement ); 

    var labelRenderer = new CSS2DRenderer();
    labelRenderer.setSize( wwww, hhhh );     
    labelRenderer.domElement.style.position = 'absolute';
    labelRenderer.domElement.style.top = 0;
    
    MyContainer.appendChild( labelRenderer.domElement ); 

// ANIMATION & RENDERING

    renderer.render     ( scene, camera );
    labelRenderer.render( scene, camera );

// ONCLICK
            
    var strMime = "image/jpg";
    
    //... THIS WORKS OK (but NO LABELS):- 
    var imgDataA = renderer.domElement.toDataURL( strMime );            
    
    //... THIS FAILS  with:- TypeError: labelRenderer.domElement.toDataURL is not a function.           
    var imgDataB = labelRenderer.domElement.toDataURL( strMime );   
    
    //... save as file (imgDataA only at present).
    var fileURL = imgDataA;//imgDataB
            //...from http://muaz-khan.blogspot.co.uk/2012/10/save-files-on-disk-using-javascript-or.html
    var Fsave = document.createElement('a'); //... creates an html <a> anchor object.                       
            
    document.body.appendChild( Fsave ); //... for Firefox, not needed in Opera (?)
    Fsave.style     = "display: none";
    Fsave.href      = fileURL;
    Fsave.target    = '_blank';
    Fsave.download  = filename || 'unknown';
    Fsave.click();
    document.body.removeChild( Fsave );

Answer №1

(2) Utilizing the CSS2DRenderer has allowed me to seamlessly incorporate CSS2D labels onto the canvas;

This statement needs clarification as the use of CSS2DRenderer or CSS3DRenderer does not involve rendering "onto the canvas". Instead, HTML elements are placed on top of the canvas with the appropriate 2D or 3D transformations.

As a result, combining toDataURL() to save the output of both WebGLRenderer and CSS2DRenderer into an image is not feasible.

While inspecting .domElement in the debugger, I did not come across toDataURL even for renderer.domElement.

The toDataURL method is indeed available when using WebGLRenderer, as demonstrated here: https://jsfiddle.net/dxmrvzw6/.

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

Instead of returning a single array of data from a form as expected, Jquery is returning two arrays

Having a bit of trouble with my code involving radio buttons and arrays using Jquery. I'm trying to record the selected values into one array, but it's creating separate arrays for each fieldset. Here's what I have: <script> $(doc ...

Encountered an Unpredictable SyntaxError: Is this a Cross-Domain Problem?

I have been attempting to connect with the Indian Railway API using Ajax in order to retrieve data in JSON format. Below is the code I am using: <!DOCTYPE> <html> <head> <meta charset="UTF-8"> <script src="https://ajax.googleap ...

Utilize formData to upload an image to a database with ajax

I'm struggling with the process of uploading an image from an HTML form. The goal is for the form to send the image name along with other data to a PHP page. Instead of using the serialized function, I opted for formData as it has the capability to h ...

What is the best way to determine the count of elements in an array that have the active property set to true?

Can anyone help me figure out the most efficient way to solve this problem? (Filter, ng-repeat, or another method?) <div>Number of active items: {{product.length}} </div> //total number of items <div>Number of inactive items: {{product.l ...

Displaying the data from a database on a browser using node.js, MySQL, and Jade

Currently, I am delving into Node.js, Express.js, and Jade while utilizing a MySQL database. As a newcomer to node.js, I decided to start with something simple: presenting data from the database in a table on the browser. Unfortunately, my attempts have no ...

javascript Unable to execute function in Internet Explorer 11

I'm experiencing an issue where this script works in Google Chrome but not in IE 11. Can anyone explain why?: <script type="text/javascript"> window.onload = function () { var ammount = document.getElementById('ammount'); var price = ...

Adding custom styles to an unidentified child element in React using Material-UI

When the function below is executed in a loop, I need to include styles for the icon which will be passed as an argument to the function. The icon element will be an unspecified React Material-UI Icon component. const renderStyledCard = (lightMode, headi ...

What is the best way to determine the moving average of an Object value within an array?

In my dataset, I have an array called 'scores' that contains 4 objects. export const scores = [ { day: '1', Barcelona: 1, Real: 3, Valencia: 0}, { day: '2', Barcelona: 4, Real: 6, Valencia: 3}, { day: '3', Bar ...

The CSS layout is not positioning elements correctly

Here is a preview of how I want my final design to appear: I am facing difficulty in properly aligning the two columns using my CSS code. Whenever I apply the float: left; property, the columns end up stacking on top of each other. /*------ Code st ...

Can the Angular link function activate the change event?

I am facing an issue with my Angular directive that includes a link function. Inside this function, I am initializing a jQuery plugin which can be seen in action here: https://plnkr.co/edit/58nOhypt6FRdI4At5jwu. The problem arises when every time the dire ...

When attempting to access http://localhost:3000/highLightTitle.png using Next.js, a 404 error (Not Found) was encountered in the content

Despite not having any mention of GET http://localhost:3000/highLightTitle.png in my Next.js project code, I am encountering an error related to this issue. The error can be viewed here, and specifically at line 199 in content.js which can be seen here. T ...

Triggering a state in Reactjs from a different component: A step-by-step guide

Hey there, I could really use some assistance with this situation. Currently, I have a progress bar component and another component where I utilize the progress bar. Additionally, there is a third component that is meant to initiate the progress bar. Let ...

Retrieving the text of a selected option by its value

I need to create a menu that returns text based on a given value. For example, if the value is 0, I want it to return “zero”, and if it's 1, then “one”, and so on. I don't need to know which option is selected or auto-select anything, I j ...

Change the flyout menu to activate once clicked instead of when the mouse hovers over

Want to add a cool flyout menu action that triggers on click instead of mouseover? The current code triggers the flyouts on mouseover, but I need them to open only when clicked. Specifically, I'd like to change the functionality so that you click on a ...

Loading scripts dynamically with async/await in JavaScript

I may be committing a typical beginner error. Aim I have a script named loader.js, where I intend to provide a collection of JavaScript files that control the shape, size, and position of components. The structure of the file is as follows: const loadSc ...

The sticky position is malfunctioning even when there is no parent element with the overflow hidden property

// observer for feature section let featuresSection = document.querySelector('#featuresSection'); let callbackFeature = (items) => { items.forEach((item) => { if (item.isIntersecting) { item.target.classList.add("in ...

What is the purpose of utilizing these three JavaScript function parameters?

I've been researching JavaScript functions and arguments, but I haven't found anything that fully explains a function like the one below. For reference, you can check out the original tutorial. In createPuppy, there are three arguments: req, res ...

I'm encountering an issue with numbers that contain comma decimal separators

My current script is designed to calculate the amount of water saved by a shower column. However, I have encountered issues with its functionality in Firefox and Edge when switching between using "," and "." for values interchangeably. I have attempted va ...

Im testing the creation of a global style using styled-components

Is there a way to create snapshot tests for styled-components with createGlobalStyle? The testing environment includes jest v22.4.4, styled-components v4.1.2, react v16.7, jest-styled-components v5.0.1, and react-test-renderer v16.6.3 Currently, the outp ...

Issue: In an Angular electron app, a ReferenceError is thrown indicating that 'cv' is

I have been working on a face detection app using OpenCv.js within an Angular electron application. To implement this, I decided to utilize the ng-open-cv module from npm modules. However, when attempting to inject the NgOpenCVService into the constructor ...