Loading textures in ThreeJS can cause a momentary display of black when using Firefox

I am currently working on implementing an undo/redo feature for a 3D paint tool. The process involves storing the texture in an array after each draw using the following code:

        var image3 = mesh.material.map.image;
        var testCanvas = image3.getContext('2d').canvas;
        var canvasData = testCanvas.toDataURL("image/jpeg");
        undoArray[undoArrayCursor] = canvasData;

To restore the texture, I have developed the following code snippet:

    var canvasimg = mesh.material.map.image;
    var img = new Image();
    img.src = srcimg;
    var tmpcanvas = document.createElement('canvas');
    tmpcanvas.width = canvasimg.width;
    tmpcanvas.height = canvasimg.height;
    var tmpctx = tmpcanvas.getContext('2d');
    tmpctx.drawImage(img,0,0);
    var pMap = new THREE.Texture( tmpcanvas );
    pMap.flipY = true;
    pMap.needsUpdate = true;
    pMaterial = new THREE.MeshLambertMaterial( { map:pMap } );
    mesh.material = pMaterial;

Although this implementation works well on Chrome and IE, it is encountering issues on Firefox. I am experiencing some latency where undo/redo clicks randomly display full black or correct textures. After a period of time (around 15-20 seconds), all textures are eventually displayed correctly as I cycle through undo/redo actions. It seems like Firefox may be taking longer to load the textures. Is there something that I might have overlooked in my code?

Answer №1

The image loading process is being restricted.

const newImage = new Image();
newImage.onload = function(){

  const temporaryCanvas = document.createElement('canvas');
  temporaryCanvas.width = canvasimg.width;
  temporaryCanvas.height = canvasimg.height;
  const tempCtx = temporaryCanvas.getContext('2d');
  tempCtx.drawImage(this,0,0); // please note the use of "this" instead of img
  const textureMap = new THREE.Texture( temporaryCanvas );
  textureMap.flipY = true;
  textureMap.needsUpdate = true;
  const lambertMaterial = new THREE.MeshLambertMaterial( { map:textureMap } );
  mesh.material = lambertMaterial;    

};

newImage.src = srcimg;

Adjustments for variable scope may be necessary.

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

React higher order component (HOC) DOM attributes are causing the error message 'Unknown event handler property' to be triggered

I recently created a Higher Order Component (HOC) using recompose, but I'm encountering a React Warning every time the props are passed down. Warning: Unknown event handler property `onSaveChanges`. It will be ignored. All my properties with a speci ...

Something is seriously wrong with the datetime in fullcalendar JavaScript

I've been diving into a tutorial for creating a calendar scheduler in asp.net MVC5 from this link. One issue I'm facing is the datetime being passed and stored as the min value in the database (1/1/0001 12:00:00 AM), almost like it's null b ...

Sorry, the variable you are trying to access has not been

I encountered an unexpected error: TypeError: slides[i] is undefined. It's puzzling because shouldn't the slides variable be accessible? <script> $('document').ready(function() { $.ajax({ type: "POST", ...

Utilizing Object Assign to reset the state

Here lies the declaration of my current state: export default new Vuex.Store({ state: { items: [ ], user: { isAuthenticated: false, info: { createdAt: '', email: '', firstName: '&a ...

Is it possible to create an app apk using jQuery Mobile that pulls its information directly from a website?

Currently in the process of developing a blog app using jQuery Mobile. Once completed, my plan is to host it online as a mobile website under a specific domain. However, I also want users to have the option to download it as an app from the app stores. A ...

Combining data search for an element within an array

In my attempt to populate the contact field by fetching data from the collection companies.contacts, I am using the following code snippet. // COMPANY MODEL const objectContact = { name: { type: String, required: true }, email: { type: String, requir ...

Error! Unable to Inject ComponentFactoryResolver

Recently, I attempted to utilize ComponentFactoryResolver in order to generate dynamic Angular components. Below is the code snippet where I am injecting ComponentFactoryResolver. import { Component, ComponentFactoryResolver, OnInit, ViewChild } from "@an ...

Retrieve an array from a JSON object by accessing the corresponding key/value pair using the utility library underscore

I have a dataset in JSON format (as shown below) and I am attempting to use the _.where method to extract specific values from within the dataset. JSON File "data": [{ "singles_ranking": [116], "matches_lost": ["90"], "singles_high_rank": [79 ...

The jQuery Select2 Plugin for Dynamic Dropdowns with Ajax Integration

Utilizing the Select2 plugin with Ajax to connect to my employee database has been quite helpful. It allows setting up a meeting and selecting all the employees you wish to invite. Here is an example of the code: $("#requiredAttendees").select2({ ...

Internet Explorer is unable to utilize the "return false" statement on a radio button

Here is a snippet of JavaScript code I use to prevent clicking on certain radio buttons. This solution works perfectly in Chrome. However, when testing in IE8, the radio button still cannot be checked and the default value disappears when another radio b ...

Node.js error: Headers cannot be set once they have been sent

I'm struggling with sending form data through express.js and encountering the following error: After establishing communication, it's not possible to modify headers. This is the current state of my code: app.post('/api/users/profile/:us ...

End the sessions of all users sharing the identical JWT token

Currently, I am utilizing React and Nodejs while incorporating JWT for user authentication. An issue has arisen where multiple users are simultaneously logged in with the same email address such as "[email protected]". Our objective now is ...

What is the method to calculate the total length of all children within a JSON array?

In the dictionary provided below, I am looking to determine the total length of all notifications (4). [ { "name": "3 Bedroom Fixer Upper", "city": "Santa Rosa", "id": 1, "state": "CA", "date_added": "2/3/14", ...

Transform your sidebar menu into a collapsible dropdown using Bootstrap

My goal is to transform the left sidebar, which currently has a vertical menu built with "nav nav-pills nav-stacked", into a suitable dropdown menu that starts off as closed/collapsed when the screen size is XS (mobile). Here is the starting code snippet: ...

Mastering basic operations with Vue in Laravel

Recently, I delved into learning vue.js after having experience with angular 2+. The transition seemed smooth after watching tutorials. However, when I tried to create a simple app where a user can post a story with comments, I found myself struggling wit ...

Exploring the methods to access GuildMember roles along with their respective details

const Discord = require('discord.js'); const bot3 = new Discord.Client(); const token3 = 'I am not disclosing my bot's token'; const mark2 = '*info personal' bot3.on('message', msg =>{ let args2 = msg.co ...

Implement the Vue.js @click directive in a location outside of an HTML element

I've set up a link like this: <a href="#" @click="modal=true">Open modal</a> Here's the data setup: export default { data () { return { modal: false } } } Is there a way to trigger the cli ...

When attempting to trigger an error, the unit test will fail to pass

I am relatively new to Mocha, Chai, and Unit Testing. I'm currently attempting to create a basic test to verify the presence of Authorization headers in the request that passes through my middleware function. Despite trying various approaches such as ...

I'm baffled by the unexpected result I'm getting when using 'foreach' in node.js

Recently delving into node.js, I've encountered a puzzling issue. I'm perplexed as to why my output appears as: ciao data data instead of: data data ciao Below is the code causing this unexpected output: fs.readdir("sender", (err, fil ...

javascript Something went wrong

Trying to integrate bower with Django framework, I have completed the following steps: - Installed bower using sudo. - Installed packages along with their versions: npm: 3.5.2-0ubuntu, nodejs: 4.2.6~dfsg-1, bower: 1.8.2 Below is the code snippet: <!DO ...