Adapting Cube Sizes on the Fly Using AngularJS

Trying to create a dynamic cube in Three.js that can be modified based on user input. The connection is established using angular.js:

This snippet of HTML code enables users to input and adjust the values:

<body>
<div id="mainContainer">
    <h2 id="mainTitle">3D house generator</h2>
    <form ng-app="3dHouseGenerator" ng-controller="validateInput" name="myForm" novalidate>
    <p>House length: </br>
        <input type="number" name="length" ng-model="length.value" min="0.001" required />
    </p>

    <p>House width: </br>
        <input type="number" name="width" ng-model="width.value" min="0.001" required />
    </p>

    <p>House walls height: </br>
        <input type="number" name="height" ng-model="height.value" min="0.001" required />
    </p>
</body>

The following code involves Three.js and my objective is to incorporate the input values for the cube. Here is what I aim to achieve:

var geometry = new THREE.BoxGeometry(length, width, height);
var material = new THREE.MeshBasicMaterial({color: 0xCC0000});
var cube = new THREE.Mesh(geometry, material);
scene.add(cube);

// dynamically change and update the dimensions
cube.geometry.dynamic = true;
cube.geometry.verticesNeedUpdate = true;
cube.geometry.normalsNeedUpdate = true;

If you have any ideas or potential solutions to resolve this matter, your assistance would be greatly appreciated!

Edit:

This is the complete code. To clarify further, this is the initial value.

var geometry = new THREE.BoxGeometry(1, 1, 1);

And this is what I am striving to achieve.

var geometry = new THREE.BoxGeometry(length.value, width.value, height.value);

Edit #2

Per msmolens' request, I have included the angularjs controller

(function(angular) {
angular.module('3dHouseGenerator', [])
    .controller('validateHouseInput', ['$scope', function($scope) {
        $scope.length = {value: 7};
        $scope.width = {value: 5};
        $scope.height = {value: 4};         
    }]);

})(window.angular);

Answer №1

The problem lies in the javascript call.

cube.scale.set(x,y,z) // Where x, y, and z are numeric values representing scale percentage.

cube.scale.set(1,2,1) to double the height of the cube.

Answer №2

To ensure the cube's scale reflects changes in user inputs, it is essential to update it accordingly. One efficient method is to utilize the ng-change directive on the <input> elements:

<input type="number" name="height" ng-model="height.value" ng-change="updateScale()" min="0.001" required />

Subsequently, you can define the updateScale() function within your controller:

$scope.updateScale = function() {
    cube.scale.set(length.value, width.value, height.value);
};

If you encounter a situation where access to the cube object is restricted within the controller, consider organizing your code differently. A recommended approach would involve encapsulating your three.js logic in a service, injecting this service into the controller, and modifying the cube's scale through a designated function within the service.

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

Order of Loading WebResources in ScriptControl

As I work on integrating a new control into a project that already contains ASP.Net controls, I am faced with the challenge of managing multiple .Net classes and JavaScript files. Each control consists of a .NET class (inheriting ScriptControl) along with ...

Displaying a 404 error page in a Vue.js and Vue Router single-page application when a resource is not

Implementing Vue and Vue Router in a single page application (SPA) has presented me with a challenge. In one of my view components, I query a repository for a specific resource. If the resource cannot be found, I'd like to display a 404 error page wit ...

How to efficiently stream and parse a CSV file within a web browser using node and webpack

In the past, I created a node project that was intended to operate in the browser. One of its main functionalities is parsing CSV files streamed from a server and processing them in chunks. Initially, I had to cater to IE11 compatibility through a strenuou ...

I need to inform users that this application is not accessible on devices with small screens

Showing this app on a small device is not supported, such as when the device width falls between 320px and 480px. ...

If someone rapidly clicks on an AngularJS app, the page fails to load

Just a heads up - I am pretty new to Angular. Most of the code is from a tutorial on Lynda that I'm experimenting with. I've noticed an issue when I try to implement a type of "pagination" feature to display various elements from a data.json file ...

Tips for enhancing the speed of ngRepeat when working with a large dataset in angular.js

I have a large dataset consisting of thousands of rows with about 10 fields each, totaling approximately 2MBs of data. My challenge is to effectively display this information in a web browser. The most obvious solution involves fetching the data, assigning ...

How to implement a cyclic item generation feature in React.js

My function is meant to draw multiple items in a cycle, but it is only drawing the first item when I attempt to draw 5. This is my function: export default function CinemaHole() { const items = []; for(let i = 0; i < 5; i++) { item ...

Ways to determine changes made to a table in MSSQL

What is the most efficient method to determine if a table or row in MSSQL (using Node.js) has been modified? I am looking to verify whether my database has been updated within the past 30 minutes. If no updates have been made in the last half hour, I pla ...

Implement a personalized click function for the delete icon in the mini cart

Is there a way to customize the click event for the remove button in the mini cart? function ajax_call(){ $.ajax({ url: ajax_url, type: 'post', data: {action : 'remove_from_cart','cart_item_key' : 10}, ...

Getting query parameter with hyphen in nextJS 13 - step by step tutorial

One of the challenges I am facing involves accessing a query parameter with a hyphen in its name within a route: /?previous-page=building-details Within my Page component: import EnergyEfficiency from "@/ui/energy-check/energy-efficiency" cons ...

Assassin eradicated from list of cast members

In my quest to select a killer from the Cast Members Array for the game, I want the function to pick one person as the killer and then remove them from the castMember player array. Once the killer is chosen, they should be removed from the survivors array ...

ReactJs - Organize your data with the sort method

In my React application, I have a table that fetches its data from an API. I need to implement sorting for one of the columns. The values in this column are usually strings of numbers but sometimes can be equal to "-" (Dash). Below is the sort function I ...

When defining a stripe in TypeScript using process.env.STRIPE_SECRET_KEY, an error of "string | undefined" is encountered

Every time I attempt to create a new stripe object, I encounter the error message "Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string& ...

Finding the average of a column in AngularJS

I am having difficulty with angularjs in calculating averages, here is the scenario The data in the table is fetched from the server and updates automatically (via ajax), I would like to calculate the average of a column in real time and display the resul ...

Unlocking the power of python with web2py by tapping into html form values

I'm working on a project using web2py and need to incorporate ajax/javascript with a form. Currently, when a user selects an option in the departure box, the arrival box appears. However, I'm unsure of how to filter the list of arrival options ba ...

Using setInterval to perform an AJAX call and update a textarea may not function properly on Internet Explorer 8

I have developed a webpage that serves as a real-time log for monitoring a `txt` file continuously updated on Unix. To achieve this, I am utilizing Ajax requests to fetch data from a PHP script which reads the file and populates the content into a `textare ...

Challenges associated with utilizing img src in JavaScript

I am facing a simple issue. I have carInfo data retrieved from a JSON file, but I am struggling to correctly parse the img source stored in the variable $imgsrc instead of treating it as a string called "$imgsrc". This data needs to be appended to my HTML ...

Troubleshooting a Problem with AppCheck Firebase reCaptcha Configuration

Currently integrating Firebase with my Next.js project. I've been attempting to configure AppCheck reCaptcha following the documentation and some recommendations, but I encounter an issue when running yarn build The build process fails with the foll ...

Imitate adjustment of orientation without the need to resize the browser window

Is there a way to simulate page portrait orientation on a PC browser without resizing the window? In my HTML code, I have a <div> element that displays content dynamically based on screen orientation. Is it possible to use JavaScript to trick this & ...

Common reasons why you may encounter the error "Uncaught TypeError: $(...).DataTable is not a function"

I recently started working with DataTable.js, and when I tried to integrate it into my ASP.NET Core MVC 5.0 project, I encountered an error: Uncaught TypeError: $(...).DataTable is not a function After doing some research on Google, I discovered that this ...