Encountering a Firefox glitch while running a three.js script

While attempting to create a 3D animation using Three.js controls, I encountered an error in Firefox: EDIT

SyntaxError: import declarations may only appear at top level of a module

Below is the simplified version of my code:

<!DOCTYPE html>
    <head>
        <title>Three.js Test</title>

        <style>
            body { margin: 0; }
            canvas { width: 100%; height: 100%;};
        </style>

    </head>

    <body>
                <script src="js/three.js"></script>
        <script src="js/OrbitControls.js"></script>

        <script type="module">
                var scene = new THREE.Scene();
                var camera = new THREE.PerspectiveCamera(75,window.innerWidth / window.innerHeight, 0.1, 1000);


                var renderer = new THREE.WebGLRenderer();
                renderer.setSize(window.innerWidth, window.innerHeight);
                document.body.appendChild(renderer.domElement);

                //keep the scene in center of the page
                window.addEventListener('resize', function() {

                    var width = window.innerWidth;
                    var height = window.innerHeight;
                    renderer.setSize(width,height);

                    //prevent distortion
                    camera.aspect = width / height;
                    camera.updateProjectionMatrix();
                });

                controls = new THREE.OrbitControls(camera, renderer.domElement);

                // create the shape
                var geometry = new THREE.BoxGeometry( 1, 1, 1 );
                var material = new THREE.MeshBasicMaterial( { color: 0xFF0080 } );
                var cube = new THREE.Mesh( geometry, material );
                scene.add( cube );

                camera.position.z = 5;

                function animate()
                {
                    requestAnimationFrame( animate );

                    /*cube.rotation.x += 0.01;
                    cube.rotation.y += 0.01;
                    cube.rotation.z += 0.01;*/

                    renderer.render( scene, camera );
                }
                animate();
        </script>
    </body>
</html>

I am unsure about the source of the error and how to resolve it.

I have now switched to running my code on a wamp server.

Your assistance is greatly appreciated!

Answer №1

import * as THREE from './jsthree.module.js';

There seems to be a mistake in the file path. It should actually be ./js/three.module.js.

Regardless, it is recommended to execute your code on a local web server to prevent any security vulnerabilities. For more detailed information on this topic, refer to the following guide:

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

Combining package.json commands for launching both an Express server and a Vue app

I recently developed an application using Vue.js and express.js. As of now, I find myself having to open two separate terminal windows in order to run npm run serve in one and npm start in the other. My ultimate goal is to streamline this process and have ...

Creating an extension of a Vue.js component from a third-party library

I'm currently using the ElDatepicker component from element-ui and I am looking to customize its template and event handler method. I have attempted to achieve this within a single file component: import Vue from 'vue'; import ElDatePicker ...

"Incorporating JavaScript into Website, Navigating Browser Discrepan

Upon designing a simple webpage that requires minimal JavaScript functionality, I encountered varying behaviors across different browsers depending on the placement of the <script> tag. In Chrome (65) and Firefox (59), the position of the <script ...

Tips for locating a file using javascript

My application scans a folder and displays all folders and HTML files inside it in a dropdown menu. It also shows any HTML files inside an iframe. There is one file named "highlighted.html" that should not appear in the dropdown menu, but if it exists in t ...

What causes the HTML element's X position value to double when its X position is updated after the drag release event in Angular's CDK drag-drop feature?

I am facing a challenge with an HTML element that has dual roles: Automatically moving to the positive x-level whenever an Obsarbalve emits a new value. Moving manually to both positive and negative x-levels by dragging and dropping it. The manual drag a ...

JQuery Keyup event is not functioning as expected

For my blog navigation, I have set up an event where pressing the 'J' key takes me to the previous post and the 'K' key takes me to the next post. However, I am facing an issue where the event works initially but stops working after the ...

Collaborating on user authorization within a MEAN.JS framework

Recently, I decided to dive into the world of web application development by using MEAN.js full stack solution. Using the generators within MEAN.js, I was able to effortlessly create multiple CRUD models along with all the necessary express routes. In ad ...

Select2 in Bootstrap does not trigger the select event when pasting until a word has been typed

One issue I encountered while using bootstrap select2 v4.* on my site (Yii 2.x) is that the event select2:select does not fire when text is pasted with ctrl+v until some text is manually typed. Once some text is entered, the event works as expected. What ...

What is the best way to update my component when the selected value is changed?

Currently, I am facing an issue with my map component. It plots polyline data based on the option selected from the select menu component. The problem arises when I change the value in the select menu and click the play button for the animation. Instead of ...

After successfully authenticating, you may introduce a new React component

Currently, I am working on a page that will only display information once a user has logged into their account. I have successfully implemented an authentication system using NodeJS, and now my goal is to restrict access to specific components or pages bas ...

convert json formatting to a string

Can anyone assist me in resolving this issue? I am trying to achieve the following format: true: 2,3,4 false: 5 I am using sequelize, I am struggling with outputting JSON correctly Please see my code snippet below. However, it is not functioning as ex ...

CSS rotation causing issues with absolute positioning

In the example below, I have demonstrated the current behavior I am experiencing and the desired outcome. // Rotated div rotated.style.left = "50px"; rotated.style.top = "100px"; // Original "untouched" div original.style.left = "50px"; original.style.t ...

Vue.js framework is experiencing an issue where button click events are not firing as expected

I am working with two files here. One is an HTML file that contains the button definition, and the other is a JavaScript file that implements the button event using the Vue.js framework. The concept behind this example is that when the button is clicked, a ...

Steps to add an SVG to a DIV when clicked

This script is fully functional and produces the desired results. var play = function(){ $( this ).click(function() { console.log( "Test" ); }); } I am looking to replace the console.log( "Test" ); with an SVG that will display in the cli ...

I am having trouble getting Highcarts to load the JSON data

I am currently in the process of utilizing Highcharts to generate a chart. The JSON output I have is in standard format and was generated using PHP's json_encode() function. Below is the JSON output: [["2015-07-13 16:41:05","3"],["2015-08-15 16:41:05 ...

Implementing non-radial texture mapping on a circular shape in WebGL with the help of Three.js

Currently, I am experimenting with creating image deformation effects using textures on 2D geometries in ThreeJS. My goal is to apply a texture image onto a hollow circle (specifically, a ring created with the THREE.RingGeometry function) in order to achie ...

What's causing my variables to be returned as null in the alerts?

Why am I receiving null values when alerting my variables? I'm attempting to pass a string stored in a variable from an external JavaScript file back to the main page using alerts. Initially, I suspected that the JavaScript was not fetching data cor ...

Having trouble passing data from JavaScript to PHP with AJAX

I attempted to use AJAX to send data from an array (emp) and other string variables like 'gtotal' in my code, but the data did not transfer to the PHP file. Despite no errors being shown, when I removed the isset() function it displayed an error ...

Executing action in PHP script simultaneously with Ajax

My JavaScript skills are a bit rusty, and I need help adding a "delete" feature to some code that displays rows of data. The code includes PHP, jQuery, and other JS elements. Here's the snippet: <?php require_once '../../app/Mage.php'; M ...

Display a toasted notification following a refresh

After reloading the page, I want to display a toast notification confirming that the file has been uploaded. Here is my current code: _fileUploads.delete = function(reload_on_return) { var filtered = root.fileUploads().filter(_ => _._id() == _fileUpl ...