Encountering a CORS policy error in Three.js when attempting to run locally

I'm just starting out on this website and I'm eager to dive into learning javascript.

My initial attempt at following this example from resulted in an error without any animation or background showing up. Instead, all I see is a photo displaying the error I encountered.

To troubleshoot, I downloaded the three.js files to my computer and placed them in the /js directory as illustrated in the image.

While researching solutions, I came across posts like Allow anything through CORS Policy and Cors policy not allowing upload, but the technical details are a bit overwhelming for me as a beginner.

Considering my limited understanding and language barrier, I would greatly appreciate a simplified explanation. Thank you.

The provided code snippet:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>three.js webgl - cloth simulation</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
        <style>
            body {
                font-family: Monospace;
                background-color: #000;
                color: #000;
                margin: 0px;
                overflow: hidden;
            }
            #info {
                position: absolute;
                padding: 10px;
                width: 100%;
                text-align: center;
            }
            a {
                text-decoration: underline;
                cursor: pointer;
            }
        </style>
    </head>

    <body>
        <div id="info">Simple Cloth Simulation<br/>
            Verlet integration with relaxed constraints<br/>
            <a onclick="wind = !wind;">Wind</a> |
            <a onclick="sphere.visible = !sphere.visible;">Ball</a> |
            <a onclick="togglePins();">Pins</a>
        </div>

        <script src="../build/three.js"></script>

        <script src="js/Detector.js"></script>
        <script src="js/controls/OrbitControls.js"></script>
        <script src="js/libs/stats.min.js"></script>

        <script src="js/Cloth.js"></script>

        <script>
            // JavaScript code omitted for brevity
        </script>
    </body>
</html>

EDIT 1: @prisoner849 I attempted the second method suggested, but unfortunately, it did not resolve the issue. I'm uncertain if I implemented it correctly - would providing a public Google Drive link be sufficient, or does it need to be hosted on a specific server? I also experimented with a Discord web link, but the problem persists even after trying different methods. I made changes to the path lines as follows:

var clothTexture = loader.load('https://cdn.discordapp.com/attachments/402574308532027422/475765536735756302/circuit_pattern.png');

and

var groundTexture = loader.load('https://drive.google.com/open?id=1VdGnJBALOWDsp9GWbskwa6rnqF_gghP9' );

Answer №1

Just a couple of quick tips for working with Three.js. First, make sure you have a web server up and running to properly display your Three.js projects. If you have NPM installed, you can easily set this up by running a few simple commands:

npm install http-server -g

cd /path/to/your/project

http-server

If you don't already have Node and NPM installed, you'll need to get those set up first. Here are the links to download Node and NPM:

https://nodejs.org/en/download/

https://www.npmjs.com/get-npm

Secondly, if you're trying to link to an external source in your Three.js project, you'll need to make sure to set the cross-origin attribute to "anonymous". Here's a simple example to help you achieve this:

var loader = new THREE.TextureLoader();
loader.setCrossOrigin("anonymous");
var texture = loader.load('external-image.jpg');

Answer №2

I found that adding the flag --allow-file-access-from-files to the chrome shortcut also resolved the issue for me. Grateful for the help from everyone.

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

Checking for the Existence of a Database Table in HTML5 Local Storage

Upon each visit to my page, I automatically generate a few local database tables if they do not already exist. Subsequently, I retrieve records from the Actual Database and insert them into these newly created local tables. I am wondering if there is a me ...

What is the best way to reset Owl Carousel following an ajax request?

I am attempting to reinitialize the owl carousel following a successful ajax call. The ajax call will update the data while retaining the same view. I am encountering an issue where the carousel structure in the view does not reinitialize, and I am unsure ...

In JavaScript, I would like to be able to input an end date and have the program calculate and display the number of days remaining until the deadline

I'm working on a school project that involves JavaScript code. I'm struggling with converting a date prompt from string to number format. As a beginner in JavaScript, I could really use some guidance. <script> enddate = prompt('Wh ...

Enhancing website security using Content Security Policy in conjunction with Google Closure

Implementing CSP for my web application is a top priority. Here's the policy I have in mind: "default-src 'self' gap: cdvfile;" I rely on google closure for my javascript needs. However, it seems that without javascript optimization, my ...

Utilize event delegation to retrieve the data attribute

Working great for me, able to access data-club-id: <!-- example --> <a class="clubCode" href="" data-club-id= "1234">join with the code</a> $("a.clubCode").on("click",function(e){ e.preventDefault(); var clubId = $(this) ...

Using JQuery validate to extract the interest rate from a regular expression

I am looking for a regular expression that can extract the interest rate. I need it to accept values such as: Examples: 0 0.4 0.44 4 44 4.00 44.00 4.2 4.22 44.22 Minimum value allowed is 0 and maximum is 99.99 The regular expression should be ab ...

D3 - Rounded edge chart width

Currently facing an issue with the chart where the data value is small, resulting in an 'ear' effect. Can anyone help me with this problem? Below is the code I am currently using: const rx = 30; const ry = 30; svg ...

The Alertify dialog vanished without being able to confirm

While working on some code, I encountered a specific issue: alertify.dialog("confirm").set( { 'labels': { ok: 'Personal', cancel: 'Share' }, 'message': 'Select target:', ...

What is the best way to filter an array based on the property of its inner array?

I am grappling with the following array of objects: let a = [ { b: [1, 11, 12], c: "a1" }, { b: [2, 56, 1], c: "a2" }, { b: [1, 2, 3], c: "a3" } ] In search of an operation that is b ...

Expanding Bootstrap 5 navbar-nav to occupy entire screen space in collapsed state

Hello everyone, I hope you are doing well! I am currently working on a website project using Bootstrap 5 and have encountered an issue with the navbar toggler. While the toggler is functioning properly, I am having trouble making it fill the full width o ...

Exploring the Power of JQuery and Iterating with For-

I'm currently facing a small issue with my code. I need to retrieve information about each module when I display the hidden table row. The information is fetched from the page {modules/$moduleid}. While I understand how to utilize AJAX, my challenge l ...

Establishing headers in hapi.js

Seeking guidance on setting up custom variables for individual routes in hapi.js. How can I configure these variables to be accessible on 'onPreHandler'? Additionally, looking for a way to include headers right before calling reply.continue. Any ...

When working with props.data in MDBNav, learn how to set the active TAB or LINK

Utilizing ReactJS, I am incorporating MDBNav from MDBreact. https://i.sstatic.net/IQtEe.png This snippet demonstrates the container where props.data is defined: import React from 'react' import MiTabs from "../componentes/MiTabs"; class VpnLi ...

Transform HTML into a Vue component dynamically during runtime

Currently, I am in the process of learning Vue and working on a simple wiki page project. The raw article data is stored in a JSON file written using custom markup. For example, the following markup: The ''sun'' is {r=black black} shoul ...

Suspend features for moving between pages in history

In the application, we have two main pages - Home.vue and Statistics.vue. The Home.vue page renders the TableFields.vue component. On the Home page, there are fields with numbers that have an initial value of "3" set upon page load. An interval running a c ...

Find the nth element of an array using Javascript's map function

Recently, I took on the challenge of learning Javascript independently and came across a rather complex task. I have an array with 18 labels and another 1D array containing all the values. Each label index corresponds to every nth element in the array. F ...

Editing a video directly within the browser prior to uploading it

For my new video upload platform, I am looking to give users the ability to trim their videos directly in their browser before uploading to the cloud storage. Can anyone suggest a Javascript solution for this feature? ...

I encountered an ERR_CONNECTION_REFUSED issue after deploying my Node.js application with Socket.io to Heroku

Upon running my live node.js Heroku app, I encountered the following error message in the web console: polling-xhr.js:264 GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=M2MDZUw net::ERR_CONNECTION_REFUSED Interestingly, when testin ...

How do I make a component in React delete itself from a page when its internal state changes?

For instance: {!loading ? (data.map((v, i) => { return <Card key={i} title={v.name} image={v.pictures.sizes[4].link}} /> }) These cards are displayed as a series of components on the main screen. Each card ...

The Cordova Network Information Plugin is experiencing some functionality issues

I successfully developed a Mobile application using Cordova, Onsen UI, and Vue.js. While addressing network connectivity issues, I incorporated the cordova plugin cordova plugin add cordova-plugin-network-information For determining the type of connectio ...