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

JavaScript placeholder-type expression

Hey there, I am a C++ developer who is venturing into the world of JavaScript. While exploring JavaScript syntax, I stumbled upon something that resembles templates in C++. For example, while going through RxJs tutorials, I came across this line of code: ...

Error message: "jQuery Ajax CORS request returning undefined value"

I am delving into the world of cross-domain Ajax requests for the first time by interacting with an API. Utilizing jQuery, I aim to extract specific elements from the response and display them on the webpage. Here is the code snippet for the request: $.a ...

Apply jQuery styling to new select box on page in order to maintain consistent styling throughout

I've encountered an issue with my jQuery select box styling. It appears to work perfectly on the initial page load, but when new content containing a select box is dynamically loaded onto the page, the styling doesn't get applied to it. Can anyo ...

Curved Edges Ensure Non-Interactive Elements

element, there is a query about utilizing a color wheel from a repository on GitHub. The goal is to disable any actions when clicking outside of the canvas border radius in this color wheel. Various steps need to be taken to prevent unwanted outcomes: - S ...

What are the steps to making a textfield editable with JavaScript and HTML?

My textfield is currently populated with values from the database and is functioning perfectly. I would like to implement a checkbox feature that, when clicked, will clear the textfield of the database values, allowing the user to input new values. If t ...

Tips for conducting performance analysis in React 16

In the React documentation, it is mentioned that react-addons-perf does not function with React 16 and suggests using Chrome's built-in tools for equivalent functionality. However, in my experience, I have not found this to be true. For example, let& ...

geolocation data not being updated in isolate scope binding

I'm currently facing an issue with using isolated scope in a directive. Surprisingly, everything seems to be working perfectly fine. I have set the '=' scope on two properties and successfully bind them in my template. However, when I call m ...

Can loading HTML and js through ajax be considered secure?

I am currently utilizing the Jquery load function $('#result').load('test.php'); in order to dynamically load a page within another page when clicking on a tab. The page being loaded contains javascript, php, and a form. Upon inspecting ...

What are some ways to implement the 'charCodeAt' method within a for loop effectively?

Currently, I am working on developing a hangman game in JavaScript to enhance my coding skills. One task that I am facing is extracting the keyCode of a character at a particular position within an array. To achieve this, I have been using the 'charCo ...

Guide on syncing Bootstrap 4 sliders using a single set of controls

Is it possible to synchronize a bootstrap 4 carousel with controls to a carousel without any controls? 1) When the control arrows are clicked, both carousels will slide simultaneously. 2) Hovering over either carousel will pause both carousels from slidi ...

How to Retrieve a Specific Line with jQuery

Could jQuery be used to retrieve the offset of the first letter in the 5th visual line of a block's content? I am referring to the visual line determined by the browser, not the line seen in the source code. ...

Electron Developer: "An issue has occurred within the primary process - Callback function missing"

My Electron application runs smoothly when launched from the command line with npm start. However, I am now looking to distribute the application as user-friendly installers for Mac/Windows/Linux. To achieve this, I am utilizing Electron-Builder for packag ...

Is it possible to analyze an API call and determine the frequency of a specific field?

Code: var textArray = new Array(); var allText = results.data._contained.text; for (var i = 0; i < allText.length; i++) { var text1 = allText[i]; var textHtml = "<div id='text_item'>"; textHtml += "& ...

Utilizing a TypeScript definition file (.d.ts) for typings in JavaScript code does not provide alerts for errors regarding primitive types

In my JavaScript component, I have a simple exporting statement: ./component/index.js : export const t = 'string value'; This component also has a TypeScript definition file: ./component/index.d.ts : export const t: number; A very basic Typ ...

What is the maximum number of rows that Handsontable can handle at once?

Issue encountered in queued task: Security check failed - Too many TRs. Please specify table height to enable scrollbars. at WalkontableTable._doDraw (client/libs/handsontable-0.10.5/jquery.handsontable.full.js?37b46fd989b9a974c3501865b51effd7adec37e4:1285 ...

Why is the button missing from the codepen?

I attempted to recreate the code from this Codepen link: https://codepen.io/jakaric/pen/mjJQvg However, unlike what you see here (in the run result), the liquid "Pretty little button" is not showing up in my local files. On Codepen, there is no library me ...

`Nginx with a touch of material design`

Currently, I'm in the process of implementing material design UI on a functioning web application. The application utilizes Nginx, PHP, and PostgreSQL. While I have proficiency in PHP and PostgreSQL to ensure the functionality (code composed in notepa ...

Exploring the use of functions in the setup method of Vue 3

I'm currently working on a simple app and utilizing mock-json-server to mimic http requests. Within my project, I have defined a function designed to retrieve the necessary information: import { ref } from 'vue' const getScores = () => ...

Excessive whitespace appearing on the right and bottom edges of the Angular application

I'm fairly new to web development, so this may be a silly question, but I really could use some help. I've created my first Angular app and noticed that there is a lot of white space on the right side of the landing page, as well as at the bottom ...

Is the NodeJS rmdir function experiencing issues specific to LINUX?

Currently, I have a NodeJS script section that runs on my Windows 10 machine with the latest Node version and another version on my CentOS8 Linux webserver. The code executes as expected on Windows but throws an error when running on Linux at this specific ...