Guide on animating the expansion of a line using Three.js

What I have:

var pointA = new THREE.Vector3(camera_RC_Holder.position.x, camera_RC_Holder.position.y, camera_RC_Holder.position.z);

var direction = camera_RC.position.clone();
direction.applyMatrix4( camera_RC.matrixWorld );
direction.normalize();

var distance = 700;

var pointB = new THREE.Vector3();
pointB.addVectors ( pointA, direction.multiplyScalar( -distance ) );

var geometry = new THREE.Geometry();
geometry.vertices.push( pointA );
geometry.vertices.push( pointB );

var material = new THREE.LineBasicMaterial( { color : 0xff0000 } );
var line = new THREE.Line( geometry, material );

scene_Main.add( line );

https://i.sstatic.net/ZzAOy.png

What I want:

What I'm trying to achieve is a gradual visualization of a ray starting from the camera and moving through the view volume. Instead of instantly creating a line between point A and point B, I would like to animate the line pixel by pixel until it reaches its destination at point B.

Question:

How can I implement the animation to draw the lines pixel by pixel as demonstrated in the code snippet below??

var w = 200;
var h = 150;
var x;

function setup(){

  createCanvas(w,h);
  x=0;
  y=0;
}

function draw(){
  
  
  if(x>w){
    x = 0;
  }
  background(250);
  line(0,50,x,50); //x1,y1,x2,y2
  x++;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.3/p5.js"></script>

Answer №1

The question's title and explanation are not clear to me, I may have misunderstood it. Let me attempt to address the title question and will revise my response once more information is provided.

(I prefer avoiding multiple questions whenever possible.)

Regarding the title question:

When you mention adding something to the scene, are you referring to drawing it or something else?

To create a raycast related to your mouse click event, use the following code snippet. You must integrate this into your scene to visualize it. Afterwards, you can manipulate your camera to observe its appearance:

var material = new THREE.LineBasicMaterial({
    color: 0x0000ff
});
var geometry = new THREE.Geometry();

geometry.vertices.push(new THREE.Vector3(raycaster.ray.origin.x, raycaster.ray.origin.y, raycaster.ray.origin.z));
geometry.vertices.push(new THREE.Vector3(raycaster.ray.origin.x + (raycaster.ray.direction.x * 100000), raycaster.ray.origin.y + (raycaster.ray.direction.y * 100000), raycaster.ray.origin.z + (raycaster.ray.direction.z * 100000)));
var line = new THREE.Line(geometry, material);

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

What are some methods for utilizing jQuery or Javascript to dynamically modify CSS styles depending on the current URL?

I'm working on integrating a separate navigation area file with my content using PHP includes. I have this idea where I want the links in the navigation area to change color depending on the active page (current URL). Essentially, I need jQuery or Jav ...

Master the art of linking multiple axios calls together in a single promise

I've been delving into Axios and promises lately, trying to grasp the concept. It seems like I'm on the right track, but there are definitely some mistakes in my approach. My javascript method is supposed to return a promise, within which I have ...

What is the best way to initiate an ajax asynchronous call following the getCurrentPosition callback?

I used the browser's Navigator getCurrentPosition to obtain my current location. Within the callback function of getCurrentPosition, I am attempting to retrieve the address based on latitude and longitude coordinates. The only thing I am able to fetch ...

Exploring the power of V-for with checkboxes in VueJS

One issue I am facing is related to triggering my save method only when a checkbox is selected or true in Vue JS. I have no problem listing values and saving them to the database using axios, Vue JS, and Spring Boot. However, every time I click the checkbo ...

Next.js throwing an error: TypeError - attempting to read property 'map' of undefined

I am facing an issue with my Next Js project. I have implemented the backend logic to display data, but when I attempt to show all the data using the useEffect hook and the map function, I encounter the following error: TypeError: Cannot read property &apo ...

Issue with Angular 2 page refresh is not triggering blur event

In our Angular 2 component, updates (HTTP PUT) are triggered when a user enters data and either tabs to the next field or hits enter. The event handling for this is set up using both blur and key.enter. Blur works efficiently as it is fired when the user ...

Setting the content-type for static assets in NuxtJS

I'm trying to use the Nuxt built-in server to serve the static file /.well-known/apple-app-site-association with a content-type of application/json. Unfortunately, because the file does not have a .json extension, it is returning as application/octet- ...

Using AJAX to upload jQuery file - sharing PHP variables

After setting up the blueimp jQuery file upload, I've encountered a problem that's stumping me. My goal is to retrieve a PHP variable that's posted when the file is uploaded and execute some PHP code if it exists. I have made modifications ...

Perform an additional HTTP request and utilize the response within the existing Observable

In my Angular 2 project, I am utilizing the http component to make API calls. The specific REST API I want to access returns a maximum of 100 Elements in one request. If more items exist, a hasMore flag is included in the response. To retrieve additional ...

Issues encountered when updating MySql Database from WordPress with Error 500, whereas the code functions properly when used outside of WordPress environment

Question: How can I update a MySQL database from within a JavaScript function on a WordPress WooCommerce page using Ajax? I have a working code snippet for updating the database, but when I integrate it into my WordPress page, I encounter a 500 error. To ...

Optimizing time complexity while mapping an array of objects in Javascript/Node.js

Imagine having an array containing unique book IDs like [1,42,34,56,21,8,88,32], and also having an array of book objects as shown below: [ { BookId: 34, BookName: Foo1 }, { BookId: 56, BookName: Bar1 }, { BookId: 1, BookName: Foo2 }, ..... ] ...

Creating Helper Functions for Modifying Arrays in Javascript Without Creating a New Reference: How Can I Extend the Array Class?

This library is designed for managing user lists using socket.io. I've implemented custom methods in an Array extension class. However, I've encountered an issue with the removeUser method. While logging indicates that the user is successfully ...

Swap out the setInterval function with a different algorithm embedded within the designated function

I have created a script that transitions images with a transparency effect in the same location. However, I am not a fan of using setInterval to call it. Is there a way to achieve the same result but perhaps with a loop inside a function that introduces a ...

What steps should I take to convert this function into an async await function?

Utilizing: MongoDB, Express, Mongoose, NodeJs. My current task involves seeding a 'months' collection using an array of months. The process is as follows: const monthData = [{ month: 'January', shifts: [] }, { ...

CodeIgniter tutorial on dynamically populating the second dropdown based on the selection of the first dropdown menu

I have been working on a task to create two dependent drop-down lists (penalty_type based on vehicle_type). Is there anyone willing to help me with the implementation? Here is my code: This is the Controller code snippet: public function index() { ...

Parsing DOM data from an HTTP request in Node.js

Looking to extract data attributes from an SVG element on github.com/profile_name using a node.js HTTP request, and then parsing it into JSON format. <rect class="day" width="10" height="10" x="-36" y="10" fill="#ebedf0" data-count="0" data-date="2017- ...

JavaScript error: Resource could not be loaded

When I have a js function called by an onclick event in a radio button, it doesn't work if the function is placed in the same ascx file where the radio button is defined. To resolve this issue, I moved the function to the ascx that includes the ascx w ...

Guide on sorting an array within a specific range and extracting a sample on each side of the outcome

I need a simple solution for the following scenario: let rangeOfInterest = [25 , 44]; let input = [10, 20, 30, 40, 50, 60]; I want to extract values that fall between 25 and 44 (inclusive) from the given input. The range may be within or outside the inpu ...

Implementing JavaScript to assign a symbol to several <span> elements with identical ids

I have a looping span element on my page that is generated based on the number of records in a database table. The appearance of the span can vary, displaying either one or multiple instances. Each span has the following structure: <span class="add-on" ...

What is the best method for integrating UL / LI into JSON to HTML conversion?

Currently, I am working on converting a JSON string into HTML format. If you want to take a look at the code, here is the jsfiddle link: http://jsfiddle.net/2VwKb/4/ The specific modifications I need to make involve adding a li element around the model da ...