Issues arising from using THREE.js canvasTexture

Describing my current issue.

I have generated a Terrain geometry using a DEM. I am now downloading OSM Tiles to use as textures for this geometry.

Every time a new Tile is downloaded, the callback function processMapImage is triggered:

function processMapImage( img, y, x)
{
myImages[y][x] = img;
myTilesToLoad--;
    
if( myTilesToLoad == 0)
    {
    var tilesy = myImages.length;
    var tilesx = myImages[0].length;
    canvas = document.createElement('canvas');
    canvas.width  = 256*tilesx;
    canvas.height = 256*tilesy;
    ctx = canvas.getContext('2d');
    for( y = 0; y < tilesy; y++)
        {
        for( x = 0; x < tilesx; x++)
            ctx.drawImage( myImages[y][x], 256*x, 256*y, 256, 256);
        }
    tex = new THREE.CanvasTexture( canvas);
    mat = new THREE.MeshPhongMaterial( { map: tex});
    var mesh = new THREE.Mesh( myTerrainGeometry, mat);
    mesh.rotateX( -Math.PI/2);
    scene.add( mesh);
    }
}

While waiting for all Tiles to load, they are stored in myImages array. Once all tiles are loaded, a canvas is created where all tiles are drawn onto it. A texture and material are created, along with the terrain geometry, to finally create a mesh that is added to the scene.

However, the model is only displaying a black surface for my terrain.

Things I have tested so far:

If I use a simple colored Phong texture, everything works fine. This suggests that lighting, geometry, and camera are functioning correctly. When I view my canvas in a browser, all tiles appear in correct order. So the canvas itself seems fine. When I draw different shapes (lines, rectangles) on my canvas, they display correctly as textures on my geometry. So the texturing process appears to be working. It's just the Images that are not displaying properly.

Any suggestions on what could be causing this issue?

Answer №1

After some trial and error, I was finally able to resolve the issue I was facing.

I discovered that the key to enabling CORS on my images was to include the following line of code:

            img.crossOrigin = "anonymous";

This simple addition made all the difference in allowing cross-origin requests for my images upon creation.

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

Retrieve an array from a URL using d3.js

I have a specific set of data available: { "_id" : ObjectId("kdahfa"), "minTime" : ISODate("2016-04-02T00:00:00.000+0000"), "maxTime" : ISODate("2016-04-02T00:11:00.000+0000"), "Time" : 660.0, "Name" : "Sam" } { "_id" : ObjectId("aabhk"), "minTime" : I ...

AngularJS 500 server error

In my current project, I am developing a straightforward angularjs - J2EE application that fetches data from a mysql server and then displays it on an HTML page. The angular function is triggered on form submission as shown below: <div id="register_for ...

What is the best way to convert an HTML table into an array of objects?

In my Angular Protractor end-to-end (e2e) tests, I need to perform assertions on an HTML fragment similar to the following: <table> <thead> <tr> <th>Name</th> <th>Age</th> ...

Submit button on Ajax form causes automatic page refresh

After adding a simple Ajax form to my WordPress website, I encountered an issue where the validation works fine, but instead of sending the email, the form refreshes after submitting. Here is the code snippet: <script type="text/javascript"> jQ ...

An error occurred while trying to access the property 'send' which is undefined

I am currently facing an issue while attempting to send data to the router from an external script. The error message that I receive is TypeError: Cannot read property 'send' of undefined Below is the code snippet: app.js var Cake = require(&a ...

How can I ensure security measures are in place to avoid XSS attacks on user-generated HTML content?

Currently, I am in the process of developing a web application that will allow users to upload entire web pages onto my platform. My initial thought was to utilize HTML Purifier from http://htmlpurifier.org/, but I am hesitant because this tool alters the ...

Utilizing jQuery's $(window).width function for handling browser window width

Currently, my website utilizes $(window).width() in a JavaScript function. For my Selenium testing, I need to verify whether the function was executed and a div contains the value of $(window).width() * 0.8. Is there a feasible way to accomplish this tas ...

Allowing multiple requests to be executed simultaneously in Express.js without causing any blocking issues

I am facing an issue with my Express.js website while handling post requests. The server hangs when a request triggers a query that takes several minutes to execute and respond from the database. Below is the code structure I use for database queries: Se ...

Flag is to be set while moving through the input fields

I am currently working on a form with multiple text fields and a text area. I have implemented a loop to go through each of these fields and check if there is a value present. If the field is empty, I set a flag to pass=false. On the other hand, if a value ...

Protractor: The top tool for testing AngularJS applications

Protractor is a comprehensive testing framework designed specifically for Angular applications, utilizing WebDriverJS as its foundation. As someone who is just beginning to explore web testing, I am curious about the benefits of choosing Protractor over u ...

What sets Joomla file inclusion apart from the rest?

Can someone please explain the difference between including a JavaScript script file in the following two ways? I created this within a system plugin in Joomla and included the JavaScript file inside the "onAfterInitialise" function. 1) <script type ...

customize Form Modal Bootstrap with AJAX Chained Dropdown for pre-selected options

I am facing an issue with a chained dropdown on a bootstrap modal, Here is the code snippet: function changeDetail(id) { var id = id; $('#edit').prop('hidden', true); $('#modal_form').prop('hidden', fa ...

limitations on passing url parameters in react.js

Currently, I am facing an issue with passing URL parameters in my React.js web app. For illustration purposes, here is a snippet of the routes: import React from "react"; import {BrowserRouter, Route, Switch, Redirect} from 'react-router-dom'; ...

Unusual body padding found in the mobile design

When visiting on a mobile device and inspecting the elements in Google Chrome, try disabling the style rule overflow-x: hidden from the body element and then resizing the window. You may notice a white vertical stripe (padding) appearing on the right side ...

Swap out the HTML tags with JavaScript code

I've been looking everywhere, but I couldn't find the answer. Here is my issue / question: I have a page from CKEDITOR with: var oldText = CKEDITOR.instances.message.getData(); So far, so good. The string looks something like this: <table ...

Ensuring a value is fully defined before passing it as a prop in a component

Is there a way to handle passing down state as a prop in a React component that is being fetched from an API using useEffect and axios? The state is initially set to "null", and I am encountering issues when trying to pass it down as a prop before it is ...

When working with Function components in NextJS, it's important to note that they cannot be assigned refs directly. If you're trying to access a ref within a Function component, you

I'm having an issue with wrapping a card component using the Link component from 'next/link'. Instead of redirecting me to the desired link when I click the card, I receive a warning that says 'Function components cannot be given refs. ...

Use a combination of the reduce and map functions in JavaScript to transform a one-dimensional array into a multi-dimensional array

Below is an array containing mySQL results: [ {"eventId":"84","shootId":"72","clubEventId":"253","clubId":"7"}, {"eventId":"84","sh ...

Why is the body the last element in Angular modules arrays?

I have a question regarding architectural practices. When defining an Angular module, one common approach is to do it like this: angular.module('app', []) .controller('Ctrl', ['$scope', function Ctrl($scope) { //body.. ...

Ways to show a div element within an input field?

I want to incorporate tags similar to stackoverflow on my website. Users will be able to create tags for filtering results, searching, showcasing expertise, and more. I have managed to create tags, but I am struggling to display them inside an input box l ...